Advertisement
Guest User

Untitled

a guest
Jan 10th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.87 KB | None | 0 0
  1.  
  2. #include <ESP8266WiFi.h>
  3.  
  4. const char* ssid = "MOTO";
  5. const char* password = "nina";
  6. WiFiServer server(8087);
  7.  
  8. #define LED1 D3
  9. #define LED2 D4
  10. #define LED3 D5
  11.  
  12.  
  13. void GetSwitchState(WiFiClient);
  14. void SetLEDStates(WiFiClient, String);
  15.  
  16.  
  17. void setup() {
  18. Serial.begin(115200);
  19. delay(10);
  20.  
  21.  
  22. pinMode(LED1, OUTPUT);
  23. pinMode(LED2, OUTPUT);
  24. pinMode(LED3, OUTPUT);
  25.  
  26. Serial.println();
  27. Serial.println();
  28. Serial.println();
  29. Serial.print("Connecting to ");
  30. Serial.println(ssid);
  31.  
  32. WiFi.begin(ssid, password);
  33.  
  34. while (WiFi.status() != WL_CONNECTED) {
  35. delay(500);
  36. Serial.print(".");
  37. }
  38. Serial.println("");
  39. Serial.println("WiFi connected");
  40.  
  41. // Start the server
  42. server.begin();
  43. Serial.println("Server started");
  44.  
  45. // Print the IP address
  46. Serial.println(WiFi.localIP());
  47. }
  48.  
  49. String HTTP_req; // stores the HTTP request
  50. void loop()
  51. {
  52. WiFiClient client = server.available();
  53.  
  54. if (client)
  55. { // if you get a client,
  56. Serial.print("new client! "); // print a message out the serial port
  57. String currentLine = ""; // make a String to hold incoming data from the client
  58. boolean newConnection = true; // flag for new connections
  59. unsigned long connectionActiveTimer; // will hold the connection start time
  60.  
  61. boolean currentLineIsBlank = true;
  62. while (client.connected())
  63. { // loop while the client's connected
  64. if (newConnection)
  65. { // it's a new connection, so
  66. connectionActiveTimer = millis(); // log when the connection started
  67. newConnection = false; // not a new connection anymore
  68. }
  69. if (!newConnection && connectionActiveTimer + 1000 < millis())
  70. {
  71. // if this while loop is still active 1000ms after a web client connected, something is wrong
  72. break; // leave the while loop, something bad happened
  73. }
  74.  
  75.  
  76. if (client.available())
  77. { // if there's bytes to read from the client,
  78. char c = client.read(); // read a byte, then
  79. // This lockup is because the recv function is blocking.
  80. Serial.print(c);
  81. HTTP_req += c; // save the HTTP request 1 char at a time
  82. // last line of client request is blank and ends with \n
  83. // respond to client only after last line received
  84. if (c == '\n' && currentLineIsBlank)
  85. {
  86. // send a standard http response header
  87. client.println("HTTP/1.1 200 OK");
  88. client.println("Content-Type: text/html");
  89. client.println("Connection: keep-alive");
  90. client.println();
  91.  
  92. // AJAX request for switch state
  93. if (HTTP_req.indexOf("ajax_switch") > -1)
  94. {
  95. // read switch state and send appropriate paragraph text
  96. GetSwitchState(client);
  97. SetLEDStates(client, HTTP_req);
  98.  
  99. }
  100. else
  101. { // HTTP request for web page
  102. // send web page - contains JavaScript with AJAX calls
  103. client.println("<!DOCTYPE html>");
  104. client.println("<html>");
  105. client.println("<head>");
  106. client.println("<title>Moises Esp8266</title>");
  107. client.println("<script>");
  108. //------------------------------------------------
  109. client.println("var led1,led2,led3;");
  110. client.println("function GetSwitchState() {");
  111. client.println("nocache = led1 + led2 + led3 + \"&nocache=\"\+ Math.random() * 1000000;");
  112. //-----------------------------------------------
  113. client.println("var request = new XMLHttpRequest();");
  114. client.println("request.onreadystatechange = function() {");
  115. client.println("if (this.readyState == 4) {");
  116. client.println("if (this.status == 200) {");
  117. client.println("if (this.responseText != null) {");
  118. //--------------------------------------------------------------------------------------------------------------------------------
  119. client.println("if (this.responseText.indexOf(\"LED1ON\") > -1){document.getElementById(\"LED1\").style.fill = \"yellow\";}");
  120. client.println("else {document.getElementById(\"LED1\").style.fill = \"black\";}");
  121. client.println("if (this.responseText.indexOf(\"LED2ON\") > -1){document.getElementById(\"LED2\").style.fill = \"yellow\";}");
  122. client.println("else {document.getElementById(\"LED2\").style.fill = \"black\";}");
  123. //------------------------------------------------------------------------------------------------------------------------------------
  124. client.println("if (this.responseText.indexOf(\"LED3ON\") > -1){document.getElementById(\"LED3\").style.fill = \"yellow\";}");
  125. client.println("else {document.getElementById(\"LED3\").style.fill = \"black\";}");
  126. //--------------------------------------------------------------------------------------------------------------------------------
  127. client.println("if (this.responseText.indexOf(\"S1:ON\") > -1){document.getElementById(\"SW1\").style.fill = \"red\";document.getElementById(\"text1\")\.innerHTML =\"SW1:ON\";}");
  128. client.println("else {document.getElementById(\"SW1\").style.fill = \"white\"; document.getElementById(\"text1\")\.innerHTML =\"SW1:OFF\";}");
  129. client.println("}}}}");
  130. client.println("request.open(\"GET\", \"ajax_switch\" + nocache, true);");
  131. client.println("request.send(null);");
  132. //----------------------------------------------------
  133. client.println("led1=\"\";led2=\"\";led3=\"\";");
  134. //----------------------------------------------------
  135. client.println("setTimeout('GetSwitchState()', 1000);");
  136. client.println("}");
  137. client.println("function SetLEDStates(num){");
  138. client.println("switch(num){");
  139. client.println("case 1: led1=\"&LED1Change\";break;");
  140. client.println("case 2: led2=\"&LED2Change\";break;");
  141. //------------------------------------------------------
  142. client.println("case 3: led3=\"&LED3Change\";break;");
  143. //------------------------------------------------------
  144. client.println("case 0: break;");
  145. client.println("}}");
  146. client.println("</script>");
  147. client.println("</head>");
  148. client.println("<body onload=\"GetSwitchState();SetLEDStates(0);\"><center>");
  149. client.println("<h1>MOISES ESP8266 Web Server </h1>");
  150. client.println("<div id=\"text1\"></div>");
  151.  
  152. client.println("<div id=\"text2\"></div>");
  153. client.println("<p><button type=\"L1\" onclick=\"SetLEDStates(1);\">LED1</button>&nbsp;");
  154. client.println("<button type=\"L2\" onclick=\"SetLEDStates(2);\">LED2</button>&nbsp;");
  155. client.println("<button type=\"L3\" onclick=\"SetLEDStates(3);\">LED3</button>&nbsp;</p>");
  156. client.println("<p><center>");
  157. //-----------------------------------------------------------------------------------------------
  158.  
  159. //-----------------------------------------------------------------------------------------------
  160. client.println("<svg width=\"50\" height=\"50\"><circle id=\"LED1\" cx=\"20\" cy=\"20\" r=\"15\"stroke=\"green\" stroke-width=\"4\" fill=\"black\"/></svg>");
  161. client.println("<svg width=\"50\" height=\"50\"><circle id=\"LED2\" cx=\"20\" cy=\"20\" r=\"15\"stroke=\"green\" stroke-width=\"4\" fill=\"black\"/></svg>");
  162. //-----------------------------------------------------------------------------------------------------------------------------------------------------------
  163. client.println("<svg width=\"50\" height=\"50\"><circle id=\"LED3\" cx=\"20\" cy=\"20\" r=\"15\"stroke=\"green\" stroke-width=\"4\" fill=\"black\"/></svg>");
  164. //-----------------------------------------------------------------------------------------------------------------------------------------------------------
  165.  
  166. client.println("</center></p>");
  167. client.println("</center></body>");
  168. client.println("</html>");
  169. }
  170. // display received HTTP request on serial port
  171. Serial.print(HTTP_req);
  172. HTTP_req = ""; // finished with request, empty string
  173. break;
  174. }
  175. // every line of text received from the client ends with \r\n
  176. if (c == '\n')
  177. {
  178. // last character on line of received text
  179. // starting new line with next character read
  180. currentLineIsBlank = true;
  181. }
  182. else if (c != '\r')
  183. {
  184. // a text character was received from client
  185. currentLineIsBlank = false;
  186. }
  187. }
  188. }
  189. // close the connection:
  190. client.stop();
  191. Serial.println("client disonnected");
  192. }
  193. }
  194.  
  195. // send the state of the switch to the web browser
  196. void GetSwitchState(WiFiClient cl)
  197. {
  198.  
  199.  
  200. }
  201.  
  202. void SetLEDStates(WiFiClient cl, String HTTP_req)
  203. {
  204. bool x1,x2,x3;
  205. if (HTTP_req.indexOf("LED1Change") > -1)
  206. x1=true;
  207. else
  208. x1=false;
  209.  
  210. if (HTTP_req.indexOf("LED2Change") > -1)
  211. x2=true;
  212. else
  213. x2=false;
  214.  
  215.  
  216. if (HTTP_req.indexOf("LED3Change") > -1)
  217. x3=true;
  218. else
  219. x3=false;
  220.  
  221.  
  222. if (digitalRead(LED1))
  223. {
  224. if (x1)
  225. {
  226. digitalWrite(LED1, LOW);
  227. cl.println("LED1OFF");
  228. }
  229. else
  230. {
  231. digitalWrite(LED1, HIGH);
  232. cl.println("LED1ON");
  233. }
  234. }
  235. else
  236. {
  237.  
  238. if (x1)
  239. {
  240. digitalWrite(LED1, HIGH);
  241. cl.println("LED1ON");
  242. }
  243. else
  244. {
  245. digitalWrite(LED1, LOW);
  246. cl.println("LED1OFF");
  247. }
  248. }
  249.  
  250.  
  251. if (digitalRead(LED2))
  252. {
  253. if (x2)
  254. {
  255. digitalWrite(LED2, LOW);
  256. cl.println("LED2OFF");
  257. }
  258. else
  259. {
  260. digitalWrite(LED2, HIGH);
  261. cl.println("LED2ON");
  262. }
  263. }
  264. else
  265. {
  266. if (x2)
  267. {
  268. digitalWrite(LED2, HIGH);
  269. cl.println("LED2ON");
  270. }
  271. else
  272. {
  273. digitalWrite(LED2, LOW);
  274. cl.println("LED2OFF");
  275. }
  276. }
  277.  
  278.  
  279. if (digitalRead(LED3))
  280. {
  281. if (x3)
  282. {
  283. digitalWrite(LED3, LOW);
  284. cl.println("LED3OFF");
  285. }
  286. else
  287. {
  288. digitalWrite(LED3, HIGH);
  289. cl.println("LED3ON");
  290. }
  291. }
  292. else
  293. {
  294. if (x3)
  295. {
  296. digitalWrite(LED3, HIGH);
  297. cl.println("LED3ON");
  298. }
  299. else
  300. {
  301. digitalWrite(LED3, LOW);
  302. cl.println("LED3OFF");
  303. }
  304. }
  305.  
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement