Freezer sensor on Wemos D1
This commit is contained in:
@@ -0,0 +1,116 @@
|
|||||||
|
/*
|
||||||
|
* Freezer Reed Contact Door Sensor
|
||||||
|
* by Tilman Liero
|
||||||
|
* www.tilman.de
|
||||||
|
*
|
||||||
|
* on = door open
|
||||||
|
* off = door closed
|
||||||
|
*
|
||||||
|
* Sources:
|
||||||
|
* WEMOS D1 WiFi setup: https://averagemaker.com/2018/04/how-to-set-up-wifi-on-a-wemos.html
|
||||||
|
* ESP8266 web server: https://randomnerdtutorials.com/esp8266-web-server/
|
||||||
|
* Digital Input Pull-Up Resistor: https://docs.arduino.cc/tutorials/generic/digital-input-pullup/
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
//#include <ESP8266HTTPClient.h>
|
||||||
|
//#include <WiFiClient.h>
|
||||||
|
|
||||||
|
const char* ssid = "LeWe";
|
||||||
|
const char* password = "5275274365464843";
|
||||||
|
|
||||||
|
WiFiServer server(8081);
|
||||||
|
String header;
|
||||||
|
|
||||||
|
|
||||||
|
void setup()
|
||||||
|
{
|
||||||
|
pinMode(LED_BUILTIN, OUTPUT);
|
||||||
|
|
||||||
|
Serial.println("Reed switch on D2");
|
||||||
|
pinMode(D2, INPUT_PULLUP);
|
||||||
|
|
||||||
|
// start serial output
|
||||||
|
Serial.begin(115200);
|
||||||
|
Serial.println();
|
||||||
|
|
||||||
|
Serial.print("Connecting to ");
|
||||||
|
Serial.println(ssid);
|
||||||
|
WiFi.hostname("Freezer-Sensor");
|
||||||
|
WiFi.begin(ssid, password);
|
||||||
|
|
||||||
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
|
delay(500);
|
||||||
|
Serial.print(".");
|
||||||
|
}
|
||||||
|
Serial.println("");
|
||||||
|
Serial.print("WiFi connected, IP address: ");
|
||||||
|
Serial.println(WiFi.localIP());
|
||||||
|
WiFi.setAutoReconnect(true);
|
||||||
|
WiFi.persistent(true);
|
||||||
|
|
||||||
|
server.begin();
|
||||||
|
delay(500);
|
||||||
|
|
||||||
|
Serial.println("Setup completed");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
|
||||||
|
if (digitalRead(D2) == HIGH) {
|
||||||
|
digitalWrite(LED_BUILTIN, LOW); // D1 Mini: turns the LED *on*
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
digitalWrite(LED_BUILTIN, HIGH); // D1 Mini: turns the LED *off*
|
||||||
|
}
|
||||||
|
|
||||||
|
WiFiClient client = server.available(); // Listen for incoming clients
|
||||||
|
|
||||||
|
if (client) {
|
||||||
|
String currentLine = ""; // make a String to hold incoming data from the client
|
||||||
|
while (client.connected()) { // loop while the client's connected
|
||||||
|
if (client.available()) { // if there's bytes to read from the client,
|
||||||
|
char c = client.read(); // read a byte, then
|
||||||
|
//Serial.write(c); // print it out the serial monitor
|
||||||
|
header += c;
|
||||||
|
if (c == '\n') { // if the byte is a newline character
|
||||||
|
// if the current line is blank, you got two newline characters in a row.
|
||||||
|
// that's the end of the client HTTP request, so send a response:
|
||||||
|
if (currentLine.length() == 0) {
|
||||||
|
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
|
||||||
|
// and a content-type so the client knows what's coming, then a blank line:
|
||||||
|
client.println("HTTP/1.1 200 OK");
|
||||||
|
client.println("Content-type: text/html; charset=utf-8");
|
||||||
|
client.println("Connection: close");
|
||||||
|
client.println();
|
||||||
|
|
||||||
|
// Display the HTML web page
|
||||||
|
if (digitalRead(D2) == HIGH) {
|
||||||
|
client.println("{ \"open\": true }");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
client.println("{ \"open\": false }");
|
||||||
|
}
|
||||||
|
|
||||||
|
// The HTTP response ends with another blank line
|
||||||
|
client.println();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else { // if you got a newline, then clear currentLine
|
||||||
|
currentLine = "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (c != '\r') { // if you got anything else but a carriage return character,
|
||||||
|
currentLine += c; // add it to the end of the currentLine
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Clear the header variable
|
||||||
|
header = "";
|
||||||
|
// Close the connection
|
||||||
|
client.stop();
|
||||||
|
Serial.println("Client disconnected");
|
||||||
|
Serial.println("");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user