Fetch value from website and control relay?
I got a quite unusual question today from a client. They provide a mission-critical service where it's important that people are notified when a certain event happens. Currently they have an API that simply writes out TRUE or FALSE, when accessed by a web browser, depending on the state of their alert.
They want to take this even further, so they want to have a PC which would fetch the value from their existing API on a continuous basis, and depending on the value, toggle a relay, which would turn on/off an alarm 开发者_如何学编程or warning light in their office.
I have no idea of how one could build such a thing, as I work only on web applications. It doesn't sound like a project requiring too many braincells, but certainly a challenge for me. What languages do I need to learn to accomplish something like this?
Since the software event will trigger a physical action (turning on a light) it will require you to interface with some sort of electronic device.
A good start would be Arduino. It's open-source hardware and let's you interface with the board using a variant of C++.
This project consists of two parts:
Connect the internet to the PC. For example using Perl.
use LWP::Simple 'get'; my $url = 'http://...'; my $value = get($url); if ($value =~ m/TRUE/) { switch_relay(1); } elsif ($value =~ m/FALSE/) { switch_relay(0); }
Connect the PC to some hardware,
switch_relay
in the example. There are a few ways to do this.- You could use X10. But I haven't used it, so I can be more helpful with that.
- There is the Velleman K8055, a USB interface board, which can be connected with USB. I used this with Perl and it worked really well.
- Or some other USB to hardware device, like Arduino, would work as well.
- Even simpler: If you use an USB lamp, you can just power off the USB power of that device.
X10 + curl + any language that lets you communicate over a serial port:
http://www.smarthome.com/1140/X10-Activehome-CM11A-Computer-Interface-CM11A/p.aspx
http://www.smarthome.com/manuals/protocol.txt
http://curl.haxx.se/
Perhaps even just a batch file to tie it all together, including http://www.computerhope.com/cttyhlp.htm + 'echo' for serial communication
精彩评论