Open lucasgrocha opened 4 years ago
#include <ESP8266WiFi.h> #include <WiFiClient.h> #include <ESP8266WebServer.h> #include <ESP8266mDNS.h> #include <ArduinoJson.h> #ifndef STASSID #define STASSID "**********" #define STAPSK "**********" #endif #define termResistor A0 const char* ssid = STASSID; const char* password = STAPSK; ESP8266WebServer server(5000); void handleRoot() { server.send(200, "text/plain", "hello from esp8266!"); } void handleNotFound() { String message = "File Not Found\n\n"; message += "URI: "; message += server.uri(); message += "\nMethod: "; message += (server.method() == HTTP_GET) ? "GET" : "POST"; message += "\nArguments: "; message += server.args(); message += "\n"; for (uint8_t i = 0; i < server.args(); i++) { message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; } server.send(404, "text/plain", message); } void setup(void) { pinMode(termResistor, INPUT); Serial.begin(115200); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); Serial.println(""); // Wait for connection while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); if (MDNS.begin("esp8266")) { Serial.println("MDNS responder started"); } server.on("/", handleRoot); server.on("/data", []() { String webPage; // Allocate JsonBuffer // Use arduinojson.org/assistant to compute the capacity. StaticJsonBuffer<500> jsonBuffer; // Create the root object JsonObject& root = jsonBuffer.createObject(); root["light"] = lightPercentage(); //Put Sensor value root.printTo(webPage); //Store JSON in String variable server.send(200, "application/json", webPage); }); server.onNotFound(handleNotFound); server.begin(); Serial.println("HTTP server started"); } int lightPercentage() { return map(analogRead(A0), 0, 1024, 0, 100); } void loop(void) { server.handleClient(); MDNS.update(); }
current request url -> http://lucasiotnode.ddns.net:5000/data
NodeMCU ESP8266
Request