Guest User

Untitled

a guest
Dec 23rd, 2017
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.91 KB | None | 0 0
  1. // Created by edwin martin on the 03/09/2017 last modified 21/12/2017
  2. // All right reserved, copyright 23/12/2017
  3. // This code is not to be used commercially with out permission.
  4. // contact husqvarna1990@yahoo.com
  5.  
  6. #include <WiFiEsp.h>
  7. #include "SoftwareSerial.h"
  8.  
  9. SoftwareSerial Serial1(6, 7); // RX, TX
  10.  
  11. char ssid[] = "ESP8266_AP"; // your Access point SSID (name)
  12. char pass[] = "Password1"; // your Access point password
  13. char Bssid[60];
  14. char Bpass[60];
  15. int status = WL_IDLE_STATUS; // the Wifi radio's status
  16. int reqCount = 0; // number of requests received
  17. byte buffer[100];
  18. String output = "";
  19. String output2 = "";
  20. boolean loops = true;
  21. float temp, hum;
  22. int fan1, fan2;
  23. WiFiEspServer server(80);
  24.  
  25. void setup() {
  26. Serial.begin(115200); // initialize serial for debugging
  27. Serial1.begin(9600); // initialize serial for ESP module
  28. WiFi.init(&Serial1); // initialize ESP module
  29. // check for the presence of the shield
  30. if (WiFi.status() == WL_NO_SHIELD) {
  31. Serial.println(F("WiFi shield not present"));
  32. while (true); // don't continue
  33. }
  34. Serial.print(F("Attempting to start AP "));
  35. Serial.println(ssid);
  36. IPAddress localIp(192, 168, 4, 1);
  37. // uncomment these two lines if you want to set the IP address of the AP
  38. WiFi.configAP(localIp);
  39. status = WiFi.beginAP(ssid);// start access point
  40. Serial.println(F("Access point started"));
  41. printWifiStatus();
  42. // start the web server on port 80
  43. server.begin();
  44. Serial.println(F("Server started"));
  45. while (loops) {
  46. WiFiEspClient client = server.available(); // listen for incoming clients
  47. if (client) {
  48. int ctr = 0;
  49. buffer[100] = "";
  50. //Serial.println("Client connected");
  51. // an http request ends with a blank line
  52. boolean currentLineIsBlank = true;
  53. while (client.connected()) {
  54. while (client.available()) {
  55. char c = client.read();
  56. // if you've gotten to the end of the line (received a newline
  57. // character) and the line is blank, the http request has ended,
  58. // so you can send a reply
  59. if (c == 'n' && currentLineIsBlank) {
  60. // Here is where the POST data is.
  61. Serial.println(F("Post data: "));
  62. while (client.available()) {
  63. buffer[ctr] = client.read();
  64. ctr++;
  65. }
  66. int datactr = 0;
  67. int andctr = 0;
  68. for (int x = 0; x < ctr; x++) {
  69. if (buffer[x] == '=') {
  70. if (datactr < 1) {
  71. Serial.print(F("User: "));
  72. }
  73. datactr++;
  74. x++;
  75. }
  76. if (buffer[x] == '&') {
  77. andctr++;
  78. x++;
  79. Serial.print(F("nPassword: "));
  80. }
  81. if (datactr == 1 && andctr == 0) {
  82. Serial.write(buffer[x]);
  83. output += char(buffer[x]);
  84. }
  85. if (datactr == 2 && andctr == 1) {
  86. Serial.write(buffer[x]);
  87. output2 += char(buffer[x]);
  88. }
  89. }
  90. output.toCharArray(Bssid, 50);
  91. Serial.print(F("nSSID: "));
  92. Serial.println(Bssid);
  93. output2.toCharArray(Bpass, 50);
  94. Serial.print(F("nPASS: "));
  95. Serial.println(Bpass);
  96. Serial.println();
  97. //Serial.println("Sending response");
  98. // send a standard http response header
  99. client.println(F("HTTP/1.0 200 OK"));
  100. client.println(F("Content-Type: text/html"));
  101. client.println(F("Connection: keep-alive"));
  102. client.println();
  103. //form added to send data from browser and view received data in serial monitor
  104. client.println();
  105. client.println(F("<!DOCTYPE html>"));
  106. client.println(F("<html lang="en"><head><style>body{background-color:#eef;}"));
  107. client.println(F("h1{text-align:center;font-family:sans-serif;font-size:72px;}"));
  108. client.println(F("p{text-align:center;font-family:sans-serif;font-size:22px;padding-top:12px; margin-bottom:0px;}"));
  109. client.println(F("#dl{width:500px;height:260px;margin:70px auto;background-color:#d0d0e1;display: table;}"));
  110. client.println(F("</style></head><body><h1>Configure WiFi</h1>"));
  111. client.println(F("<div id="dl"><p>Please enter your wifi SSID and Password</p>"));
  112. client.println(F("<form action="" method="POST"><p>"));
  113. client.println(F("<label for="username">SSID:</label>"));
  114. client.println(F("<input type="text" name="uname"><br><br>"));
  115. client.println(F("<label for="password">Password:</label>"));
  116. client.println(F("<input type="password" name="pwd"><br><br>"));
  117. client.println(F("<input type="submit" value="Submit"></p></form>"));
  118. if (output != "") {
  119. client.println(F("<p>SSID and password changed.</p>"));
  120. loops = false;
  121. }
  122. client.println(F("</div></body>"));
  123. client.println(F("</html>"));
  124. client.println();
  125. client.stop();
  126. } else if (c == 'n') {
  127. // you're starting a new line
  128. currentLineIsBlank = true;
  129. } else if (c != 'r') {
  130. // you've gotten a character on the current line
  131. currentLineIsBlank = false;
  132. }
  133. }
  134. }
  135. Serial.println(F("Disconnected"));
  136. if (!loops) {
  137. int status = WL_IDLE_STATUS;
  138. network();
  139. }
  140. }
  141. }
  142. }
  143.  
  144. void network() {
  145. Serial1.println(F("AT+RST"));
  146. delay(5000);
  147. WiFi.init(&Serial1); // initialize ESP module
  148. // check for the presence of the shield
  149. if (WiFi.status() == WL_NO_SHIELD) {
  150. Serial.println(F("WiFi shield not present"));
  151. while (true); // don't continue
  152. }
  153. // attempt to connect to WiFi network
  154. while ( status != WL_CONNECTED) {
  155. Serial.print(F("Attempting: "));
  156. Serial.println(Bssid);
  157. // Connect to WPA/WPA2 network
  158. status = WiFi.begin(Bssid, Bpass);
  159. }
  160. Serial.println(F("You're connected"));
  161. printWifiStatus();
  162. server.begin();
  163. }
  164.  
  165. void loop() {
  166. temp = random(1, 30);
  167. hum = random(30, 99);
  168. fan1 = random(10, 99);
  169. fan2 = random(10, 99);
  170. WiFiEspClient client = server.available();
  171. if (client) {
  172. Serial.println(F("New client"));
  173. boolean currentLineIsBlank = true;
  174. while (client.connected()) {
  175. if (client.available()) {
  176. char c = client.read();
  177. Serial.write(c);
  178. if (c == 'n' && currentLineIsBlank) {
  179. Serial.println(F("Sending response"));
  180. client.print(F(
  181. "HTTP/1.1 200 OKrn"
  182. "Content-Type: text/htmlrn"
  183. "Connection: closern"
  184. "Refresh: 120rn"
  185. "rn"));
  186. client.print(F("<!DOCTYPE HTML>rn"));
  187. client.print(F("<html><head>rn"));
  188. client.print(F("<link rel='stylesheet' href='http://www.southamptonstudentrooms.com/ESP.css'>"));
  189. client.print(F("</head><body><h1>Realtime values V1.0</h1><div id="di">"));
  190. client.print(F("<p>These values will refresh every two minutes</p>"));
  191. client.print(F("<div class="div1"><h2>Temp<br>R/H<br>Fan 1<br>Fan 2</h2></div>"));
  192. client.print("<div class="div2"><h2>"+String(temp)+"&#x2103<br>"+String(hum)+"%<br>"+String(fan1)+"%<br>"+String(fan2)+"%</h2>");
  193. client.print(F("</div></div></body>"));
  194. client.print(F("</html>rn"));
  195. break;
  196. }
  197. if (c == 'n') {
  198. // you're starting a new line
  199. currentLineIsBlank = true;
  200. } else if (c != 'r') {
  201. // you've gotten a character on the current line
  202. currentLineIsBlank = false;
  203. }
  204. }
  205. }
  206. delay(10);
  207. client.stop();
  208. Serial.println(F("Client disconnected"));
  209. }
  210. }
  211.  
  212. void printWifiStatus() {
  213. // print your WiFi shield's IP address
  214. IPAddress ip = WiFi.localIP();
  215. Serial.print(F("IP Address: "));
  216. Serial.println(ip);
  217. Serial.println();
  218. }
Add Comment
Please, Sign In to add comment