Guest User

ESP Sketch

a guest
Apr 13th, 2020
595
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //The purpose of this sketch is to:
  2. //#1 - connect the module to the local Wi-Fi
  3. //#2 - setup a webserver that can take an input command that can be sent to the Mega
  4. //#3 - receive serial info from the Mega, and upload it to an external API (e.g. temp sensor data from the Mega)
  5.  
  6. //test with POSTMAN or Insomnia: POST to ESP's IP Address (e.g. 192.168.1.55/serialInput), body tab set to: x-www-form-urlencoded ; vars: key=Input, value=0 or 1
  7.  
  8.  
  9. #include <ESPWiFi.h>
  10. #include <ESP8266WiFi.h>
  11. #include <WiFiClient.h>
  12. #include <ESPHTTPClient.h>
  13. #include <ESP8266WebServer.h>
  14. #include <ESP8266mDNS.h>
  15.  
  16. #ifndef STASSID
  17. #define STASSID "YOUR-SSID"
  18. #define STAPSK  "YOUR-WIFI-PWD"
  19. #endif
  20.  
  21. const char *ssid = "YOUR-SSID";
  22. const char *password = "YOUR-WIFI-PWD";
  23. ESP8266WebServer server(80);
  24.  
  25. String testValFromMega = "not set";
  26. String readString;
  27.  
  28. //External API Setup
  29. // -where the sensor data from the Mega is going to get sent to
  30. WifiClient client;
  31. String apiHost = "http://SOME-API-ENDPOINT-THAT-COULD-RECEIVE-DATA.com";
  32. int apiPort = 80;
  33.  
  34. //a dummy "home page" for the esp's web server
  35. void handleRoot() {
  36.   server.send(200, "text/html", "<h3>SmartThermostat</h3><p>Functional Menu</p><ol><li>test 1</li><li>test 2</li></ol><p>From Mega: " + testValFromMega + "</p>");
  37. }
  38.  
  39. //here we are creating an HTTP response for when a client tries to send input to the ESP
  40. void handleSerialInput() {
  41.   if (! server.hasArg("input") || server.arg("input") == NULL) {
  42.     server.send(400, "text/plain", "400: Invalid Request");
  43.     return;
  44.   } else {
  45.  
  46.     //this line is the magic that sends a command to the Arduino Mega
  47.     Serial.println(server.arg("input"));
  48.    
  49.     //send the HTTP response
  50.     server.send(200, "text/plain", "Serial input successfully written to Serial3 of Arduino!  Sent: " + server.arg("input"));
  51.     return;
  52.   }
  53. }
  54.  
  55.  
  56. void setup() {
  57.  
  58.   //start serial communication, start it fast
  59.   Serial.begin(115200);
  60.   Serial.println();
  61.  
  62.   //connect to wifi
  63.   Serial.printf("Connecting to WiFi: %s\n", ssid);
  64.   //WiFi.config(staticIP, gateway, subnet);
  65.   WiFi.begin(ssid, password);
  66.   Serial.println("");
  67.  
  68.   // Wait for connection
  69.   while (WiFi.status() != WL_CONNECTED) {
  70.     delay(500);
  71.     Serial.print(".");
  72.   }
  73.   Serial.println();
  74.   Serial.print("Connected, IP Address: ");
  75.   Serial.println(WiFi.localIP());
  76.  
  77.   if (MDNS.begin("esp8266")) {
  78.     Serial.println("MDNS responder started");
  79.   }
  80.  
  81.   //register the web server's root and serialInput (via web) events
  82.   server.on("/", handleRoot);
  83.   server.on("/serialInput", HTTP_POST, handleSerialInput);
  84.  
  85.   Serial.end();
  86.  
  87.   //slow the serial communication down to 9600
  88.   Serial.begin(9600);
  89.   server.begin();
  90. }
  91.  
  92. void loop() {
  93.  
  94.   server.handleClient();
  95.  
  96.   //if there's an INCOMING serial message from the MEGA, read the incoming bytes into a string
  97.   char incomingByte;
  98.   while (Serial.available() > 0)
  99.   {
  100.     delay(10);
  101.     incomingByte = Serial.read();
  102.     readString += incomingByte;
  103.   }
  104.  
  105.   //if we have an INCOMING serial message from the MEGA, check what the message says, and act on it
  106.   //in this example, we're looking for the Mega to send some sensor data to the ESP.  The ESP is then supposed to upload that sensor data to an external API (to log it into a database)
  107.   if (readString != "")
  108.   {
  109.     testValFromMega = readString;
  110.  
  111.     if (readString.indexOf("SensorReport") > 0)
  112.     {
  113.       uploadSensorEvent(readString);
  114.     }
  115.  
  116.     //clear the message so we don't act on it more than once
  117.     readString = "";
  118.   }
  119.  
  120.  
  121. }
  122.  
  123. //connect to an external API and share the sensor data.
  124. //note - this example is uploading to my private web api, so the code below will likely not work as is for your specific needs. Instead of taking this
  125. //       specific action, you could upload the data to any of the free IoT APIs out there, or simply run an LED or something.
  126. void uploadSensorEvent(String json)
  127. {
  128.   if (!client.connect(apiHost, apiPort))
  129.   {
  130.     Serial.println("uploadSensorEvent HTTP Client connection failed");
  131.     return;
  132.   }
  133.  
  134.   client.println("POST /api/Values?actionType=SensorData HTTP/1.1");
  135.   client.println("Host: " + WiFi.localIP()); // or generate from your server variable to not hardwire
  136.   client.println("User-Agent: Arduino/MegaESP");
  137.   client.println("Connection: close");
  138.   client.print("Content-Length: ");
  139.   client.println(strlen(json));// number of bytes in the payload
  140.   client.println();// important need an empty line here
  141.   client.println(json);// the payload
  142. }
RAW Paste Data