Last active
January 12, 2020 09:33
-
-
Save dgellow/2d20ac3da603a1afc2493d43cde5b4a2 to your computer and use it in GitHub Desktop.
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> | |
#include <ESP8266mDNS.h> | |
const char* ssid = "<REDACTED>"; | |
const char* password = "<REDACTED>"; | |
auto port = 80; | |
ESP8266WebServer server(port); | |
void handleIndex() { | |
String message = "{"; | |
message += "'ssid': "; | |
message += ssid; | |
message += ", 'ip': "; | |
message += WiFi.localIP().toString(); | |
message += ", 'netMask': "; | |
message += WiFi.subnetMask().toString(); | |
message += ", 'gateway': "; | |
message += WiFi.gatewayIP().toString(); | |
message += ", 'mac': "; | |
message += WiFi.macAddress(); | |
message += ", 'dns': "; | |
message += WiFi.dnsIP().toString(); | |
message += ", 'hostName': "; | |
message += WiFi.hostname(); | |
message += "}"; | |
server.send(200, "application/json", message); | |
} | |
void handleMetrics() { | |
server.send(200, "application/json", "{'temperature': 10.2, 'humidity': 40.3, 'brightness': 1.4}"); | |
} | |
void handleNotFound() { | |
server.send(404, "text/plain", "404 not found"); | |
} | |
void setup() { | |
Serial.begin(115200); | |
delay(1000); | |
Serial.println("Started"); | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
Serial.print("Netmask: "); | |
Serial.println(WiFi.subnetMask()); | |
Serial.print("Gateway: "); | |
Serial.println(WiFi.gatewayIP()); | |
if (MDNS.begin("esp8266")) { | |
Serial.println("MDNS responder started"); | |
} | |
server.on("/", handleIndex); | |
server.on("/metrics", handleMetrics); | |
server.onNotFound(handleNotFound); | |
server.begin(); | |
Serial.print("HTTP server started at "); | |
Serial.print(WiFi.localIP()); | |
Serial.print(":"); | |
Serial.print(port); | |
Serial.println(""); | |
} | |
void loop() { | |
server.handleClient(); | |
MDNS.update(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment