Last active
March 25, 2017 12:00
-
-
Save digitaldias/3f3f617fb6273276b712fa990ecebf58 to your computer and use it in GitHub Desktop.
Short demo of how to instantly turn your ESP8266 module into a webserver for polling values from an IoT Gateway
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <WiFiClient.h> | |
#include <ESP8266WebServer.h> | |
const char* DeviceId = "<<your sensor id>>"; | |
const char* WIFI_SSID = "<<your wifi SSID>>"; | |
const char* WIFI_SECRET = "<<your wifi password>>"; | |
ESP8266WebServer webServer(80); | |
void setup() | |
{ | |
Serial.begin(9600); | |
InitWifi(); | |
InitWebServer(); | |
} | |
void loop() { | |
webServer.handleClient(); | |
} | |
void InitWebServer() | |
{ | |
webServer.on("/", []() | |
{ | |
Serial.println("GET request received for root URL"); | |
webServer.send(200, "text/plain", "Hello world"); | |
}); | |
webServer.on("/temperature", []() | |
{ | |
// Get sensor telemetry, package it as JSON and send it. This gist only displays a mock | |
webServer.send(200, "application/json", "{\"Temperature\" : 25.7 }"); | |
}); | |
webServer.begin(); | |
} | |
void InitWifi() | |
{ | |
Serial.println("Connecting to wifi"); | |
WiFi.begin(WIFI_SSID, WIFI_SECRET); | |
while (!WiFi.isConnected()) | |
{ | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.print("Connected to wifi on: "); | |
Serial.println(WiFi.localIP()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment