Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.30 KB | None | 0 0
  1.  
  2. #include <ESP8266WiFi.h>
  3. #include <Arduino.h>
  4. #include <ESPAsyncTCP.h>
  5. #include <ESPAsyncWebServer.h>
  6. #include <EEPROM.h>
  7. #include <DNSServer.h>
  8. #include <WiFiClient.h>
  9. #include <ESP8266HTTPClient.h>
  10.  
  11.  
  12. const IPAddress ap_ip(192,168,4,1);
  13. const IPAddress ap_subnet(255,255,255,0);
  14. const char* ap_ssid = "Board_010";
  15. const char* ap_password = "123456789";
  16. const char* board_id = "Board_010";
  17. String host = "3.18.101.41";
  18. boolean isSettingsMode;
  19. DNSServer dnsServer;
  20.  
  21. AsyncWebServer server(80);
  22.  
  23. // Replaces placeholder for future development
  24. String processor(const String& var){
  25. return "1";
  26. }
  27.  
  28. const char index_html[] PROGMEM = R"rawliteral(
  29. <!DOCTYPE HTML><html>
  30. <head>
  31. <meta name="viewport" content="width=device-width, initial-scale=1">
  32. <style>
  33. html {
  34. font-family: Arial;
  35. display: inline-block;
  36. margin: 0px auto;
  37. text-align: center;
  38. }
  39. h2 { font-size: 3.0rem; }
  40. p { font-size: 3.0rem; }
  41. .units { font-size: 1.2rem; }
  42. .dht-labels{
  43. font-size: 1.0rem;
  44. vertical-align:middle;
  45. padding-bottom: 15px;
  46. }
  47. </style>
  48. </head>
  49. <body>
  50. <h2>Pillbox Wifi Settings</h2>
  51. <form method=post action="/connect">
  52. <p>
  53. <span class="dht-labels">Wi-Fi Username:</span>
  54. <input type=text name=ssid>
  55. </p>
  56. <p>
  57. <span class="dht-labels">Wi-Fi Password:</span>
  58. <input type=text name=password>
  59. </p>
  60. <p>
  61. <input type=submit>
  62. </form>
  63. </body>
  64. </html>)rawliteral";
  65.  
  66.  
  67. void writeToStorage(String ssid, String pass)
  68. {
  69. for (int i = 0; i < 96; ++i) {
  70. EEPROM.write(i, 0);
  71. }
  72.  
  73. Serial.println("Writing SSID:");
  74. Serial.println(ssid);
  75. Serial.println("Writing Password:");
  76. Serial.println(pass);
  77.  
  78. for (int i = 0; i < ssid.length(); ++i) {
  79. EEPROM.write(i, ssid[i]);
  80. }
  81.  
  82. for (int i = 0; i < pass.length(); ++i) {
  83. EEPROM.write(32 + i, pass[i]);
  84. }
  85. EEPROM.commit();
  86. ESP.restart();
  87. }
  88.  
  89. boolean readFromStorageAndConnect() {
  90. Serial.println("Reading from storage and attempting connection.");
  91. String ssid_name = "";
  92. String ssid_pass = "";
  93.  
  94. if(EEPROM.read(0) != 0) {
  95. for (int i = 0; i < 32; ++i) {
  96. ssid_name += char(EEPROM.read(i));
  97. }
  98.  
  99. for(int i = 32; i < 96; ++i) {
  100. ssid_pass += char(EEPROM.read(i));
  101. }
  102.  
  103. WiFi.begin(ssid_name.c_str(), ssid_pass.c_str());
  104. Serial.println("Attempting Connection:");
  105. Serial.print("ssid name: ");
  106. Serial.println(ssid_name);
  107. Serial.print("ssid password: ");
  108. Serial.println(ssid_pass);
  109. int counter = 0;
  110. while (counter < 25) {
  111. if (WiFi.status() == WL_CONNECTED) {
  112. Serial.println("Connected to Wi-Fi");
  113. digitalWrite(0, LOW);
  114. return true;
  115. }
  116. delay(500);
  117. Serial.print(".");
  118. counter++;
  119. }
  120. Serial.println("Connection Failed.");
  121. return false;
  122. }
  123. else {
  124. Serial.println("Wi-Fi details were not found. entering setup mode.");
  125. return false;
  126. }
  127.  
  128. }
  129.  
  130.  
  131. void setup() {
  132. Serial.begin(115200);
  133. EEPROM.begin(512);
  134. pinMode(0, OUTPUT);
  135. pinMode(14, INPUT_PULLUP);
  136. digitalWrite(0, HIGH);
  137. delay(100);
  138. if(readFromStorageAndConnect()) {
  139. isSettingsMode = false;
  140. return;
  141. }
  142. isSettingsMode = true;
  143. WiFi.mode(WIFI_STA);
  144. WiFi.disconnect();
  145. delay(100);
  146. WiFi.mode(WIFI_AP);
  147. WiFi.softAPConfig(ap_ip, ap_ip, ap_subnet);
  148. WiFi.softAP(ap_ssid, ap_password);
  149. dnsServer.start(53, "*", ap_ip);
  150.  
  151. // Route for root / web page
  152. server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
  153. request->send_P(200, "text/html", index_html, processor);
  154. });
  155. server.on("/connect", HTTP_POST, [](AsyncWebServerRequest *request){
  156. Serial.println("form submitted.");
  157. if(request->getParam("ssid", true)->value() != "" && request->getParam("password", true)->value() != "") {
  158. request->send(200, "text/plain", "Logging in...");
  159.  
  160. writeToStorage(String(request->getParam("ssid", true)->value()), String(request->getParam("password", true)->value()));
  161.  
  162. return;
  163. }
  164. Serial.println("form illegal. going to login page.");
  165. request->send_P(400, "text/html", index_html, processor);
  166.  
  167. });
  168.  
  169. // Start server
  170. server.begin();
  171. }
  172.  
  173. void loop() {
  174. if(isSettingsMode) {
  175. dnsServer.processNextRequest();
  176. }
  177. else {
  178. int value = digitalRead(14);
  179. Serial.print("Pin 14 status:");
  180. Serial.println(value);
  181. if (value == 0) {
  182. Serial.println("POSTING to client");
  183. WiFiClient client;
  184. const int httpPort = 80;
  185. if (!client.connect(host, httpPort)) {
  186. Serial.println("HTTP connection to server failed.");
  187. return;
  188. }
  189. Serial.println("client connection success");
  190. String postData = "board_id=" + String(board_id);
  191. String url = "/api/autopublish";
  192. String address = host + ":" + String(httpPort) + url;
  193. Serial.print("sending POST request to address: ");
  194. Serial.println(address);
  195. HTTPClient http;
  196. http.begin("http://3.18.101.41:80/api/autopublish");
  197. http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  198. auto httpCode = http.POST(postData);
  199. Serial.println(httpCode);
  200. String payload = http.getString();
  201. Serial.println(payload);
  202. http.end();
  203. delay(10000);
  204. }
  205. delay(500);
  206.  
  207. }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement