Skip to content

Instantly share code, notes, and snippets.

@digitaldias
Last active March 25, 2017 12:00
Show Gist options
  • Save digitaldias/3f3f617fb6273276b712fa990ecebf58 to your computer and use it in GitHub Desktop.
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
#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