|
@@ -0,0 +1,53 @@
|
|
|
|
|
+#include <ESP8266WiFi.h>
|
|
|
|
|
+#include <ESP8266mDNS.h>
|
|
|
|
|
+#include <ESP8266WebServer.h>
|
|
|
|
|
+#include <OneWire.h>
|
|
|
|
|
+#include <DallasTemperature.h>
|
|
|
|
|
+
|
|
|
|
|
+#define ONE_WIRE_BUS 2
|
|
|
|
|
+
|
|
|
|
|
+const char* type = "temperature";
|
|
|
|
|
+const char* location = "xxx";
|
|
|
|
|
+
|
|
|
|
|
+const char* ssid = "xxx"; // Your ssid
|
|
|
|
|
+const char* password = "xxx"; // Your Password
|
|
|
|
|
+
|
|
|
|
|
+OneWire oneWire(ONE_WIRE_BUS);
|
|
|
|
|
+DallasTemperature sensors(&oneWire);
|
|
|
|
|
+ESP8266WebServer server(80);
|
|
|
|
|
+
|
|
|
|
|
+void setup() {
|
|
|
|
|
+ // Connect to WiFi network
|
|
|
|
|
+
|
|
|
|
|
+ WiFi.begin(ssid, password);
|
|
|
|
|
+
|
|
|
|
|
+ while (WiFi.status() != WL_CONNECTED) {
|
|
|
|
|
+ delay(500);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ MDNS.begin((String)"sensor-" + type + "-" + location);
|
|
|
|
|
+
|
|
|
|
|
+ sensors.begin();
|
|
|
|
|
+
|
|
|
|
|
+ // Start the server
|
|
|
|
|
+
|
|
|
|
|
+ server.on("/temperature/celcius", [](){
|
|
|
|
|
+ server.send(200, "application/json", getCelcius());
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ server.begin();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+String getCelcius() {
|
|
|
|
|
+ //get the temperature in celcius
|
|
|
|
|
+
|
|
|
|
|
+ sensors.requestTemperatures();
|
|
|
|
|
+
|
|
|
|
|
+ float temp = sensors.getTempCByIndex(0);
|
|
|
|
|
+
|
|
|
|
|
+ return "{\"temperature\":" + String(temp) + ", \"location\": \"" + location + "\", \"type\":\"" + type + "\"}";
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void loop(){
|
|
|
|
|
+ server.handleClient();
|
|
|
|
|
+}
|