REV: 10/6/2013 AGENT =========================== http.onrequest(function(req, resp){ if (req.method == "POST"){ local body = http.urldecode(req.body); server.log("Incoming Switch: " + body.data); // Received as two bytes: 0000 0000 // Reset All: 00 (0000 0000) // Switch 1: 01 (0000 0001) // Switch 2: 02 (0000 0010) // Switch 3: 04 (0000 0100) // Switch 4: 08 (0000 1000) // Switch 5: 10 (0001 0000) // Switch 6: 20 (0010 0000) // Switch 7: 40 (0100 0000) // Switch 8: 80 (1000 0000) device.send("data", body.data); } resp.send(200, "OK"); }); DEVICE =========================== // UART Communication to TI SN74LV8153 Serial to Parallel Chip // RECEIVING WHICH SWITCH IS PRESSED ... // There are a couple ways of doing this ... // You can keep track of which switches are on or off within // your online script (PHP, Perl, javascripting) and send a // single value that gets delivered to the TI chip. That // value will include the current positions of all your switches. // or ... // You can send a value indicating which single switch was pressed. // I'm doing it this way. Each time a switch is clicked, it // sends the value corresponding to a bit. That value is then // converted to an integer. Doing it this way, it's possible // to NOT know which position the switch is in. This method // should be used for momentary pushbuttons on a web page. I am // using JQuery slide switches, so switching from on to off or // off to on sends the same single bit. I always start out by // resetting the graphic switches (to off), and resetting the // imp to 00 (zero). A global 'mask' variable is used to keep // track of the current position of the switches. When a new // bit is received, it masks that bit (XOR bitwise) with the 'mask' // and that creates the new value for sending to the TI chip. function writeSerial(value) { // Do an Exclusive OR (XOR) to change only the bit that was received (toggle it). local newvalue=value ^ mask; if(value==00){ // A zero was received, so do a reset (turn off all outputs) newvalue=00; } hardware.pin9.write(0); local outputs = newvalue; local address = 0x00; // write to address 0 chip // Send serial data to TI chip hardware.uart12.write(format("%c%c", 0x01 | (address <<1) | (outputs & 0x0f) <<4, 0x01 | (address <<1) | (outputs & 0xf0))); server.log(format("WiFi Signal: %d dBm", imp.rssi())); // Imp RSSI signal strength // below -87 zero (still connected, but barely) // -87 .. -82 1 // -82 .. -77 2 // -77 .. -72 3 // -72 .. -67 4 // above -67 5 hardware.pin9.write(1); mask=newvalue; } // Convert hex string to an integer function hexToInteger(hex){ local result = 0; local shift = hex.len() * 4; // For each digit.. for(local d=0; d= 0x61) digit = hex[d] - 0x57; else if(hex[d] >= 0x41) digit = hex[d] - 0x37; else digit = hex[d] - 0x30; // Accumulate digit shift -= 4; result += digit << shift; } return result; } // pin9 is an LED blip indicator whenever data is sent to TI chip hardware.pin9.configure(DIGITAL_OUT); hardware.uart12.configure(19200, 8, PARITY_NONE, 1, NO_CTSRTS); // The global 'mask' variable keeps track of the current state of the switches. // I'm doing this on the imp. Alternatively, you could keep track of the switches // within your website script and send the proper positions of all 8 switches at once. mask <- 00; agent.on("data", function(value) { value=hexToInteger(value); writeSerial(value); }); writeSerial(00); // Register with the server imp.configure("TI_SN74LV8153", [], []);