Freezer sensor on Wemos D1

This commit is contained in:
Tilman
2024-12-08 09:09:20 +01:00
parent 980234e1ff
commit d0747aedd4
2 changed files with 116 additions and 0 deletions
+116
View File
@@ -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("");
}
}
+48
View File
@@ -0,0 +1,48 @@
# in rc.local:
# su pi -c 'python3 /home/pi/freezer.py >> /home/pi/freezer.log 2>&1 &'
import RPi.GPIO as GPIO
import urllib.request
import logging
import logging.handlers as handlers
import time
import cherrypy # https://docs.cherrypy.org/en/latest/tutorials.html#tutorial-1-a-basic-web-application
import requests, json
cherrypy.config.update({
'server.socket_host' : '192.168.178.111',
'server.socket_port' : 8081,
})
logger = logging.getLogger('Rotating Log')
logger.setLevel(logging.INFO)
logHandler = handlers.RotatingFileHandler('/home/pi/freezer.log', maxBytes=1000000, backupCount=2)
logHandler.setLevel(logging.INFO)
logHandler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s - %(message)s', '%Y-%m-%d %H:%M:%S'))
logger.addHandler(logHandler)
PIN_REED_SENSOR = 21
GPIO.setmode(GPIO.BCM)
GPIO.setup(PIN_REED_SENSOR, GPIO.IN, GPIO.PUD_UP)
def door_change(channel):
logger.info('open: ' + str(GPIO.input(PIN_REED_SENSOR) == 1).lower())
class FreezerService(object):
@cherrypy.expose
def index(self):
return '{ "open": ' + str(GPIO.input(PIN_REED_SENSOR) == 1).lower() + ' }'
GPIO.add_event_detect(PIN_REED_SENSOR, GPIO.BOTH, callback=door_change, bouncetime=250)
logger.info('Initializing')
cherrypy.quickstart(FreezerService())
logger.info('Shutdown')
logger.info('Reset GPIO configuration and close')
GPIO.cleanup()