Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //The purpose of this sketch is to:
- //#1 - connect the module to the local Wi-Fi
- //#2 - setup a webserver that can take an input command that can be sent to the Mega
- //#3 - receive serial info from the Mega, and upload it to an external API (e.g. temp sensor data from the Mega)
- //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
- #include <ESPWiFi.h>
- #include <ESP8266WiFi.h>
- #include <WiFiClient.h>
- #include <ESPHTTPClient.h>
- #include <ESP8266WebServer.h>
- #include <ESP8266mDNS.h>
- #ifndef STASSID
- #define STASSID "YOUR-SSID"
- #define STAPSK "YOUR-WIFI-PWD"
- #endif
- const char *ssid = "YOUR-SSID";
- const char *password = "YOUR-WIFI-PWD";
- ESP8266WebServer server(80);
- String testValFromMega = "not set";
- String readString;
- //External API Setup
- // -where the sensor data from the Mega is going to get sent to
- WifiClient client;
- String apiHost = "http://SOME-API-ENDPOINT-THAT-COULD-RECEIVE-DATA.com";
- int apiPort = 80;
- //a dummy "home page" for the esp's web server
- void handleRoot() {
- 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>");
- }
- //here we are creating an HTTP response for when a client tries to send input to the ESP
- void handleSerialInput() {
- if (! server.hasArg("input") || server.arg("input") == NULL) {
- server.send(400, "text/plain", "400: Invalid Request");
- return;
- } else {
- //this line is the magic that sends a command to the Arduino Mega
- Serial.println(server.arg("input"));
- //send the HTTP response
- server.send(200, "text/plain", "Serial input successfully written to Serial3 of Arduino! Sent: " + server.arg("input"));
- return;
- }
- }
- void setup() {
- //start serial communication, start it fast
- Serial.begin(115200);
- Serial.println();
- //connect to wifi
- Serial.printf("Connecting to WiFi: %s\n", ssid);
- //WiFi.config(staticIP, gateway, subnet);
- WiFi.begin(ssid, password);
- Serial.println("");
- // Wait for connection
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println();
- Serial.print("Connected, IP Address: ");
- Serial.println(WiFi.localIP());
- if (MDNS.begin("esp8266")) {
- Serial.println("MDNS responder started");
- }
- //register the web server's root and serialInput (via web) events
- server.on("/", handleRoot);
- server.on("/serialInput", HTTP_POST, handleSerialInput);
- Serial.end();
- //slow the serial communication down to 9600
- Serial.begin(9600);
- server.begin();
- }
- void loop() {
- server.handleClient();
- //if there's an INCOMING serial message from the MEGA, read the incoming bytes into a string
- char incomingByte;
- while (Serial.available() > 0)
- {
- delay(10);
- incomingByte = Serial.read();
- readString += incomingByte;
- }
- //if we have an INCOMING serial message from the MEGA, check what the message says, and act on it
- //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)
- if (readString != "")
- {
- testValFromMega = readString;
- if (readString.indexOf("SensorReport") > 0)
- {
- uploadSensorEvent(readString);
- }
- //clear the message so we don't act on it more than once
- readString = "";
- }
- }
- //connect to an external API and share the sensor data.
- //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
- // specific action, you could upload the data to any of the free IoT APIs out there, or simply run an LED or something.
- void uploadSensorEvent(String json)
- {
- if (!client.connect(apiHost, apiPort))
- {
- Serial.println("uploadSensorEvent HTTP Client connection failed");
- return;
- }
- client.println("POST /api/Values?actionType=SensorData HTTP/1.1");
- client.println("Host: " + WiFi.localIP()); // or generate from your server variable to not hardwire
- client.println("User-Agent: Arduino/MegaESP");
- client.println("Connection: close");
- client.print("Content-Length: ");
- client.println(strlen(json));// number of bytes in the payload
- client.println();// important need an empty line here
- client.println(json);// the payload
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement