Advertisement
Guest User

Untitled

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