-->
Page 1 of 6

Simple Pin On/Off Toggle Sketch

PostPosted: Wed Jan 07, 2015 1:24 am
by maptor
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

Re: Simple Pin On/Off Toggle Sketch

PostPosted: Wed Jan 07, 2015 10:53 am
by Kalix
Thanks a lot!

Going to try/adopt/modify your Sketch for use in one of my project.
To lazy to do all the coding myself.

Re: Simple Pin On/Off Toggle Sketch

PostPosted: Wed Jan 07, 2015 2:28 pm
by Dir
Maptor thank you for a good sketch!
But you have a problem with serveReply function.
it am change it a little. Look at the comments.

P.S. Sorry for my english.
Code: Select allvoid serveReply(int ch_id,String received){

  String reply = "{"+received+"}\r\n";
  dbg.println("Sending back a resopnse");
  esp.println("AT+CIPSEND="+String(ch_id)+","+String(reply.length())); // change 18 to reply length
  waitForEsp(2000,">"); //THIS ONE VERY IMPORTANT! AT+CIPSEND return just > and wait for a data.
 
  esp.print(reply); // It's PRINT not a printLN. Because printLN add 2 bytes to reply.
  waitForEsp(2000);  //THIS ONE VERY IMPORTANT! After receiving data CIPSEND return SEND OK. So we must wait for OK
  dbg.println("closing connection");
 
 esp.println("AT+CIPCLOSE="+String(ch_id));
 waitForEsp(2000);
}

Re: Simple Pin On/Off Toggle Sketch

PostPosted: Wed Jan 07, 2015 5:19 pm
by maptor
Dir wrote:Maptor thank you for a good sketch!
But you have a problem with serveReply function.
it am change it a little. Look at the comments.


Thanks for the improvements! I'll test them out and update the source just as soon as I can... packing for a short trip tomorrow and that takes priority. unfortunately. :(