I am working on using the Electric Imp I got from Adafruit to make a WiFi enabled Emoticon avatar. Basically it will receive code from a website and display the icon for that code on a small 8×8 LED matrix accompanied by a notification sound. This will also provide feedback through a button on the avatar to acknowledge the emoticon, and a motion detector to tell when someone is near the avatar.
So far I have gotten the Imp to receive data from a webpage and turn LEDs on or off from three independent I/O pins. The LEDs are red, green, and blue so they make different colors depending on which ones are on.
Here is the code I am using to do this.
Squirrel code:
// Register with the server // April with brightness-controlled LED hardware.pin1.configure(PWM_OUT, 1.0/500.0, 1.0); hardware.pin2.configure(PWM_OUT, 1.0/500.0, 1.0); hardware.pin5.configure(PWM_OUT, 1.0/500.0, 1.0); class TableIn extends InputPort { //name = "LED Brightness" //type = "number" function set(v) { // since pin 9 is configured for PWM, we can set the duty cycle with pin.write() // write a floating point number between 0.0 and 1.0, where 1.0 = 100% duty cycle hardware.pin1.write(v.red); hardware.pin2.write(v.green); hardware.pin5.write(v.blue); server.log("v.red: "+v.red+" v.green: "+v.green+" v.blue: "+v.blue); } } // Variable to represent LED state ledState <- 0; // blink function called every 100ms function blink() { // Change state ledState = ledState?0:1; // Reflect state to the pin hardware.pin8.write(ledState); // Schedule the next state change imp.wakeup(0.1, blink); } // Configure pin 9 as an open drain output with internal pull up hardware.pin8.configure(DIGITAL_OUT_OD_PULLUP); server.log("Brightness Controller Started"); imp.configure("April Brightness Controller", [ TableIn() ], []); //blink(); server.sleepfor(30);
HTML Form:
Note: You will need to replace the “https://api.electricimp.com/v1/…use.your.own.address…” with the URL from the HTTP IN node in the Imp Planner.
<html><head><script> function sendForm(form) { // Construct the JSON string in form.value // x is a string ('cos of the quotes) // y is an integer ('cos of no quotes) form.value.value = "{ "red": " + form.red.value + ", "green": " + form.green.value + ", "blue": " + form.blue.value + " }" form.submit(); } </script></head> <body> <table width="200" border="1"> <tr> <td>Off</td> <td><form action="https://api.electricimp.com/v1/...use.your.own.address..." method="post" target="stat"> <input type="hidden" name="value"> <input type="hidden" name="red" value=1> <input type="hidden" name="green" value=1> <input type="hidden" name="blue" value=1> <input type="button" value="ON" onclick="sendForm(this.form);" /> </form></td> </tr> <tr> <td>Red</td> <td><form action="https://api.electricimp.com/v1/...use.your.own.address..." method="post" target="stat"> <input type="hidden" name="value"> <input type="hidden" name="red" value=0> <input type="hidden" name="green" value=1> <input type="hidden" name="blue" value=1> <input type="button" value="ON" onclick="sendForm(this.form);" /> </form></td> </tr> <tr> <td>Orange</td> <td><form action="https://api.electricimp.com/v1/...use.your.own.address..." method="post" target="stat"> <input type="hidden" name="value"> <input type="hidden" name="red" value=0> <input type="hidden" name="green" value=0> <input type="hidden" name="blue" value=1> <input type="button" value="ON" onclick="sendForm(this.form);" /> </form></td> </tr> </tr> <tr> <td>green</td> <td><form action="https://api.electricimp.com/v1/...use.your.own.address..." method="post" target="stat"> <input type="hidden" name="value"> <input type="hidden" name="red" value=1> <input type="hidden" name="green" value=0> <input type="hidden" name="blue" value=1> <input type="button" value="ON" onclick="sendForm(this.form);" /> </form></td> </tr> </tr> <tr> <td>light blue</td> <td><form action="https://api.electricimp.com/v1/...use.your.own.address..." method="post" target="stat"> <input type="hidden" name="value"> <input type="hidden" name="red" value=1> <input type="hidden" name="green" value=0> <input type="hidden" name="blue" value=0> <input type="button" value="ON" onclick="sendForm(this.form);" /> </form></td> </tr> <tr> <td>blue</td> <td><form action="https://api.electricimp.com/v1/...use.your.own.address..." method="post" target="stat"> <input type="hidden" name="value"> <input type="hidden" name="red" value=1> <input type="hidden" name="green" value=1> <input type="hidden" name="blue" value=0> <input type="button" value="ON" onclick="sendForm(this.form);" /> </form></td> </tr> </tr> <tr> <td>perple</td> <td><form action="https://api.electricimp.com/v1/...use.your.own.address..." method="post" target="stat"> <input type="hidden" name="value"> <input type="hidden" name="red" value=0> <input type="hidden" name="green" value=1> <input type="hidden" name="blue" value=0> <input type="button" value="ON" onclick="sendForm(this.form);" /> </form></td> </tr> </table> <iframe src="" name="stat"> </body> </html>
This code is based on the example found here.