ESP-01 Home Made Remote MultiPlug

ESP-01 Home Made Remote MultiPlug

This page is to Setup and Config ESP-01 Home Made Remote MultiPlug

Hardware and Software

Hardware

ESP8266 ESP-01, Relay 220V, Home Multi-plug, 5V Adapter.

Software

Arduino IDE

Sanki Notes

https://sites.google.com/a/imediabank.com/sanki-os-developer/esp-01-home-made-remote-multiplug/esp002.jpg?attredirects=0
https://sites.google.com/a/imediabank.com/sanki-os-developer/esp-01-home-made-remote-multiplug/esp003.jpg?attredirects=0
https://sites.google.com/a/imediabank.com/sanki-os-developer/esp-01-home-made-remote-multiplug/esp004.jpg?attredirects=0
https://sites.google.com/a/imediabank.com/sanki-os-developer/esp-01-home-made-remote-multiplug/esp005.jpg?attredirects=0

Program Code

is ..........

          1. /*

          2. * SANKI 2016 03 31

          3. *

          4. * This sketch demonstrates how to set up a simple HTTP-like server.

          5. * The server will set a GPIO pin depending on the request

          6. * http://server_ip/gpio0 will set the GPIO0 high/low,

          7. * http://server_ip/gpio2 will set the GPIO2 high/low

          8. * server_ip is the IP address of the ESP8266 module, will be

          9. * printed to Serial when the module is connected.

          10. *

          11. * Before RUN :

          12. * Change SSID and Password

          13. * Change port number

          14. *

          15. */

          16. #include <ESP8266WiFi.h>

          17. #include <WiFiClient.h>

          18. #include <ESP8266WebServer.h>

          19. #include <ESP8266mDNS.h>

          20. #include <SoftwareSerial.h>

          21. const char* ssid = "<Your SSID>";

          22. const char* password = "<SSID Password>";

          23. ESP8266WebServer server(1102);

          24. const int led = 0;

          25. String sMessage = "";

          26. void handleNotFound(){

          27. digitalWrite(LED_BUILTIN , 1);

          28. String message = "File Not Found\n\n";

          29. message += "URI: ";

          30. message += server.uri();

          31. message += "\nMethod: ";

          32. message += (server.method() == HTTP_GET)?"GET":"POST";

          33. message += "\nArguments: ";

          34. message += server.args();

          35. message += "\n";

          36. for (uint8_t i=0; i<server.args(); i++){

          37. message += " " + server.argName(i) + ": " + server.arg(i) + "\n";

          38. }

          39. server.send(404, "text/plain", message);

          40. digitalWrite(LED_BUILTIN , 0);

          41. }

          42. void handleMessage(int gpio0, int gpio2){

          43. digitalWrite(LED_BUILTIN , 1);

          44. // Prepare the response

          45. //String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html";

          46. String s = "\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n";

          47. //s += "<body style=\"background-color:lightgrey;\">"; // Color Only

          48. s += "<head>";

          49. s += "<style>";

          50. // ------------------------------ Button

          51. s += ".button {";

          52. //s += " background-color: #4CAF50; /* Green */";

          53. s += " border: none;";

          54. s += " color: white;";

          55. s += " padding: 15px 32px;";

          56. s += " text-align: center;";

          57. s += " text-decoration: none;";

          58. s += " display: inline-block;";

          59. s += " font-size: 16px;";

          60. s += " margin: 4px 2px;";

          61. s += " cursor: pointer;";

          62. s += " -webkit-transition-duration: 0.4s; /* Safari */";

          63. s += " transition-duration: 0.4s;";

          64. s += "}";

          65. s += "";

          66. s += ".button1 {";

          67. s += " background-color: #4CAF50; font-size: 24px box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);";

          68. s += "}";

          69. s += "";

          70. s += ".button2 {";

          71. s += " background-color: #f44336; border=radius:12px font-size: 24px box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);";

          72. s += "}";

          73. s += ".reseton {";

          74. s += " background-color: #4CAF50; border-radius: 12px;";

          75. s += "}";

          76. s += ".resetoff {";

          77. s += " background-color: #f44336; border-radius: 12px;";

          78. s += "}";

          79. s += " #wrap{";

          80. s += "width:1000px;";

          81. s += "margin:0 auto;";

          82. s += "}";

          83. s += "body {";

          84. s += " background-image: url(\"http://www.imediabank.com/_/rsrc/1406774830204/home/sankidevice.png\");";

          85. s += " background-repeat: no-repeat;";

          86. s += " background-position: center top;";

          87. s += " background-color: lightgrey";

          88. s += " margin-right: 20px;";

          89. s += " margin-left: 20px;";

          90. s += " margin-top: 180px;";

          91. s += "}";

          92. s += "h1 {";

          93. s += " background-color: green;";

          94. s += "}";

          95. s += "";

          96. s += "div {";

          97. s += " background-color: lightblue;";

          98. s += "}";

          99. s += "";

          100. s += "p {";

          101. s += " background-color: yellow;";

          102. s += " font-size: 48px;";

          103. s += "}";

          104. s += " p.ridge {border-style: ridge;}";

          105. s += "</style>";

          106. s += "</head>";

          107. s += "<body align=center>";

          108. s += "";

          109. // s += " <div class=\"wrap\">";

          110. /*

          111. s += "<h1>GPIO2 is now ";

          112. s += (val)?"high":"low";

          113. s += "</h1>";

          114. */

          115. s += "<p>ESP-01 Relay </p>";

          116. s += "" + sMessage + "";

          117. if (gpio0 == 0)

          118. s += "<p class=\"ridge\">Relay 0 (gpio 0) <a href=\"\\gpio0\"><button class=\"button button2\">OFF</button></a></p>";

          119. else

          120. s += "<p class=\"ridge\">Relay 0 (gpio 0) <a href=\"\\gpio0\"><button class=\"button button1\">ON</button></a></p>";

          121. if (gpio2 == 0)

          122. s += "<p class=\"ridge\">Relay 0 (gpio 2) <a href=\"\\gpio2\"><button class=\"button button2\">OFF</button></a></p>";

          123. else

          124. s += "<p class=\"ridge\">Relay 0 (gpio 2) <a href=\"\\gpio2\"><button class=\"button button1\">ON</button></a></p>";

          125. s += "<p class=\"ridge\">Reset : <a href=\"\\reseton\"><button class=\"button reseton\">All ON</button></a><a href=\"\\resetoff\"><button class=\"button resetoff\">All OFF</button></a></p>";

          126. //s += "</div>";

          127. //s += m + "\n";

          128. s += "</body></html>\n";

          129. server.send(200, "text/html", s);

          130. digitalWrite(LED_BUILTIN , 0);

          131. }

          132. void handleRoot() {

          133. digitalWrite(LED_BUILTIN , 1);

          134. //server.send(200, "text/plain", "hello from esp8266! ---- \nTest Port /gpio");

          135. handleMessage(digitalRead(0), digitalRead(2));

          136. digitalWrite(LED_BUILTIN , 0);

          137. }

          138. void handleReset(int onoff) {

          139. digitalWrite(LED_BUILTIN , 1);

          140. //server.send(200, "text/plain", "hello from esp8266! ---- \nTest Port /gpio");

          141. if (onoff == 0) {

          142. digitalWrite(0, 0);

          143. digitalWrite(2, 0);

          144. handleMessage(0 ,0);

          145. }

          146. if (onoff == 1) {

          147. digitalWrite(0, 1);

          148. digitalWrite(2, 1);

          149. handleMessage(1, 1);

          150. }

          151. digitalWrite(LED_BUILTIN , 0);

          152. }

          153. String ipToString(IPAddress ip){

          154. String s="";

          155. for (int i=0; i<4; i++)

          156. s += i ? "." + String(ip[i]) : String(ip[i]);

          157. return s;

          158. } //- See more at: http://www.esp8266.com/viewtopic.php?p=25466#sthash.TxIeuo7o.dpuf

          159. void setup(void){

          160. pinMode(LED_BUILTIN, OUTPUT);

          161. digitalWrite(LED_BUILTIN, 0);

          162. Serial.begin(115200);

          163. WiFi.begin(ssid, password);

          164. Serial.println("");

          165. // Wait for connection

          166. while (WiFi.status() != WL_CONNECTED) {

          167. delay(500);

          168. Serial.print(".");

          169. }

          170. server.begin();

          171. Serial.println("HTTP server started");

          172. Serial.println("");

          173. Serial.print("Connected to ");

          174. Serial.println(ssid);

          175. Serial.print("IP address: ");

          176. Serial.println(WiFi.localIP());

          177. if (MDNS.begin("esp8266")) {

          178. Serial.println("MDNS responder started");

          179. }

          180. uint8_t MAC_array[6];

          181. char MAC_char[18];

          182. WiFi.macAddress(MAC_array);

          183. sprintf(MAC_char, "%02x:%02x:%02x:%02x:%02x:%02x", MAC_array[0],MAC_array[1],MAC_array[2],MAC_array[3],MAC_array[4],MAC_array[5]);

          184. Serial.print("MAC Address : ");

          185. Serial.println(MAC_char);

          186. sMessage += "[SSID : " + String(ssid) + "] [Server IP : " + ipToString(WiFi.localIP()) + "] [MAC : " + MAC_char + "]";

          187. // prepare GPIO2

          188. pinMode(2, OUTPUT);

          189. digitalWrite(2, 0);

          190. pinMode(0, OUTPUT);

          191. digitalWrite(0, 0);

          192. server.on("/", handleRoot);

          193. server.on("/reseton", [](){

          194. //server.send(200, "text/plain", "this works as well");

          195. handleReset(1);

          196. });

          197. server.on("/resetoff", [](){

          198. //server.send(200, "text/plain", "this works as well");

          199. handleReset(0);

          200. });

          201. server.on("/gpio2", [](){

          202. String m = "GPIO 2 : ";

          203. int val = digitalRead(2);

          204. if (val == 0) {

          205. val = 1;

          206. m += "ON";

          207. } else {

          208. val = 0;

          209. m += "OFF";

          210. }

          211. digitalWrite(2, val);

          212. //server.send(200, "text/plain", m);

          213. handleMessage(digitalRead(0), val);

          214. });

          215. server.on("/gpio0", [](){

          216. String m = "GPIO 0 : ";

          217. int val = digitalRead(0);

          218. if (val == 0) {

          219. val = 1;

          220. digitalWrite(0, 1);

          221. m += "ON";

          222. } else {

          223. val = 0;

          224. digitalWrite(0, 0);

          225. m += "OFF";

          226. }

          227. //digitalWrite(led, val);

          228. //server.send(200, "text/plain", m);

          229. handleMessage(val, digitalRead(2));

          230. });

          231. /*

          232. server.on("/gpio2/0", [](){

          233. server.send(200, "text/plain", "Turn OFF GPIO 2");

          234. digitalWrite(2, 0);

          235. });

          236. */

          237. server.onNotFound(handleNotFound);

          238. }

          239. void loop(void){

          240. server.handleClient();

          241. }