Simple Pin On/Off Toggle Sketch
Posted: Wed Jan 07, 2015 1:24 am
Not owning one, I decided to try and figure out what good an ESP module is to an Arduino without a Raspberry Pi. After a lot of frustration and a lot of cursing, I finally managed to put together an Arduino sketch that toggles pins 2-10 HIGH or LOW. Connected to LEDs or a relay board, this allows you to turn lights or even entire power circuits on and off remotely. It parses HTTP requests to extract a command string consisting of 1s and 0s (1 = HIGH/on, 0 = LOW/off) and then updates the corresponding pins accordingly.
I've got an Arduino UNO, 4-channel relay board and the ESP8266 module tucked into an electrical box with four power plugs coming out of it. I can (wirelessly) control each plug's power status from any device connected to my home wireless network, the same network the ESP is connected to. The only thing I need to know is the local IP address assigned to the ESP module. (So for permanent installations, it's probably better to assign it a static IP address.) Then I send an HTTP request to http://esp_ip_address/?command=0100E and wait for the board to react. (Yes, the "E" is necessary.) Every so often a request fails, but this is pretty rare. There is a return message, a JSON object containing the board's current pin power settings, so you can easily just wait for this to be returned in order to know that your command was successfully processed.
Because I left pins 0 and 1 unused, the IDE's Serial Monitor function can be used to watch and make sure the ESP connects to the network successfully. It'll also print out the IP address it is assigned. There are a few improvements I want to make, mostly related to latency, but it does work as-is and I've successfully tested it with everything running for multiple days. Thought I'd share it to get some suggestions from everybody here.
You'll need to modify the sketch with the SSID of your wireless network and its password, of course. I think you might also be able to connect directly to the network hosted by the ESP... haven't tested that yet. If you've gotten this far you have probably already figured out how to build this into something cool.
Anyway, here you go:
https://github.com/ckuzma/ESP8266-Arduino-Sketches/blob/master/light_server.ino
I've got an Arduino UNO, 4-channel relay board and the ESP8266 module tucked into an electrical box with four power plugs coming out of it. I can (wirelessly) control each plug's power status from any device connected to my home wireless network, the same network the ESP is connected to. The only thing I need to know is the local IP address assigned to the ESP module. (So for permanent installations, it's probably better to assign it a static IP address.) Then I send an HTTP request to http://esp_ip_address/?command=0100E and wait for the board to react. (Yes, the "E" is necessary.) Every so often a request fails, but this is pretty rare. There is a return message, a JSON object containing the board's current pin power settings, so you can easily just wait for this to be returned in order to know that your command was successfully processed.
Because I left pins 0 and 1 unused, the IDE's Serial Monitor function can be used to watch and make sure the ESP connects to the network successfully. It'll also print out the IP address it is assigned. There are a few improvements I want to make, mostly related to latency, but it does work as-is and I've successfully tested it with everything running for multiple days. Thought I'd share it to get some suggestions from everybody here.
You'll need to modify the sketch with the SSID of your wireless network and its password, of course. I think you might also be able to connect directly to the network hosted by the ESP... haven't tested that yet. If you've gotten this far you have probably already figured out how to build this into something cool.
Anyway, here you go:
https://github.com/ckuzma/ESP8266-Arduino-Sketches/blob/master/light_server.ino