From 33ab43eddf97d5cade5261a5b049894378567ea5 Mon Sep 17 00:00:00 2001 From: matt Date: Mon, 2 Oct 2023 18:58:50 +0200 Subject: [PATCH] Door sensor WIP --- .../esp32-door-sensor-mqtt.ino | 102 +++++++++++++----- esp32-light-switch/esp32-light-switch.ino | 62 +++++++++-- 2 files changed, 132 insertions(+), 32 deletions(-) diff --git a/esp32-door-sensor-mqtt/esp32-door-sensor-mqtt.ino b/esp32-door-sensor-mqtt/esp32-door-sensor-mqtt.ino index 743dc4b..655582d 100644 --- a/esp32-door-sensor-mqtt/esp32-door-sensor-mqtt.ino +++ b/esp32-door-sensor-mqtt/esp32-door-sensor-mqtt.ino @@ -6,13 +6,17 @@ #define LOG_LOCAL_LEVEL ESP_LOG_DEBUG #define DOOR_SENSOR_PIN 19 #define PIR_SENSOR_PIN 13 +#define ONBOARD_LED_PIN 2 #include #include #include #include #include +#include #include +#include +#include IPAddress serverIp; const char* ssid = "Unknown Network"; @@ -30,7 +34,7 @@ PubSubClient mqttClient(espClient); long lastMsg = 0; char msg[50]; int doorState = LOW; -int pirState = LOW; +int pirState = LOW; int doorPrevState = LOW; int pirPrevState = LOW; @@ -49,12 +53,14 @@ void IRAM_ATTR pir_ISR() { { pirState = digitalRead(PIR_SENSOR_PIN); if (pirState != pirPrevState) { + Serial.print("PIR state: "); + Serial.println(pirState); + digitalWrite(ONBOARD_LED_PIN, pirState); pubPirState = true; pirPrevState = pirState; } - delay(250); } - + last_motion_time = motion_time; } @@ -69,14 +75,9 @@ void IRAM_ATTR door_ISR() { pubDoorState = true; doorPrevState = doorState; } - delay(250); } - - last_button_time = button_time; -} -void IRAM_ATTR window_ISR() { - mqttClient.publish("esp32/temperature", "The window is closed."); + last_button_time = button_time; } /* Returns a semi-unique id for the device. The id is based @@ -131,17 +132,13 @@ void FindServer() Serial.println("Server IP: " + serverIp.toString()); } -void setup() { - Serial.begin(115200); +void setupWifi() { + delay(10); + // We start by connecting to a WiFi network + Serial.println(); + Serial.print("Connecting to "); + Serial.println(ssid); - pinMode(PIR_SENSOR_PIN, INPUT_PULLUP); // declare sensor as input - attachInterrupt(digitalPinToInterrupt(PIR_SENSOR_PIN), pir_ISR, HIGH); - - pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP); // declare sensor as input - attachInterrupt(digitalPinToInterrupt(DOOR_SENSOR_PIN), door_ISR, CHANGE); - - - WiFi.setHostname(hostname); WiFi.begin(ssid, password); Serial.println("Connecting"); while (WiFi.status() != WL_CONNECTED) { @@ -151,11 +148,63 @@ void setup() { Serial.println(""); Serial.print("Connected to WiFi network with IP Address: "); Serial.println(WiFi.localIP()); +} +void setupOTA() { + // Arduino OTA Info + // Port defaults to 3232 + // Hostname defaults to esp3232-[MAC] + String MyName = MakeMine("Door monitor"); + ArduinoOTA.setHostname("DoorMon"); + ArduinoOTA.setPasswordHash("b2cb7bf46d7afe5ad2ed16d87093d342"); + ArduinoOTA + .onStart([]() { + String type; + if (ArduinoOTA.getCommand() == U_FLASH) + type = "sketch"; + else // U_SPIFFS + type = "filesystem"; + + // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end() + Serial.println("Start updating " + type); + }) + .onEnd([]() { + Serial.println("\nEnd"); + }) + .onProgress([](unsigned int progress, unsigned int total) { + Serial.printf("Progress: %u%%\r", (progress / (total / 100))); + }) + .onError([](ota_error_t error) { + Serial.printf("Error[%u]: ", error); + if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); + else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); + else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); + else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); + else if (error == OTA_END_ERROR) Serial.println("End Failed"); + }); + + ArduinoOTA.begin(); +} + +void setup() { + Serial.begin(115200); + + pinMode(PIR_SENSOR_PIN, INPUT_PULLUP); // declare sensor as input + attachInterrupt(digitalPinToInterrupt(PIR_SENSOR_PIN), pir_ISR, CHANGE); + + pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP); // declare sensor as input + attachInterrupt(digitalPinToInterrupt(DOOR_SENSOR_PIN), door_ISR, CHANGE); + + // Set LED to LOW + pinMode(ONBOARD_LED_PIN, OUTPUT); + digitalWrite(ONBOARD_LED_PIN, LOW); + + setupWifi(); + setupOTA(); mqttClient.setServer(mqtt_broker, 1883); String MyName = MakeMine("Door monitor"); - AdvertiseServices(MyName.c_str()); + //AdvertiseServices(MyName.c_str()); //FindServer(); } @@ -182,7 +231,7 @@ void loop() { if (!mqttClient.connected()) { reconnect(); } - + mqttClient.loop(); if (pubDoorState) { if (doorState == LOW) { @@ -194,12 +243,17 @@ void loop() { pubDoorState = false; delay(100); } - + if (pubPirState) { - mqttClient.publish("esp32/motion", "Motion detected at door."); - pubDoorState = false; + if (pirState == HIGH) + { + mqttClient.publish(movementTopic, "Motion detected at door."); + } + + pubPirState = false; delay(100); } // put your main code here, to run repeatedly: + ArduinoOTA.handle(); } \ No newline at end of file diff --git a/esp32-light-switch/esp32-light-switch.ino b/esp32-light-switch/esp32-light-switch.ino index dc1e80c..79a11d2 100644 --- a/esp32-light-switch/esp32-light-switch.ino +++ b/esp32-light-switch/esp32-light-switch.ino @@ -36,7 +36,48 @@ int pos = 0; Servo leftServo; Servo rightServo; - + +/* Returns a semi-unique id for the device. The id is based + on part of a MAC address or chip ID so it won't be + globally unique. */ +uint16_t GetDeviceId() +{ +#if defined(ARDUINO_ARCH_ESP32) + return ESP.getEfuseMac(); +#else + return ESP.getChipId(); +#endif +} + +/* Append a semi-unique id to the name template */ +String MakeMine(const char *NameTemplate) +{ + uint16_t uChipId = GetDeviceId(); + String Result = String(NameTemplate) + String(uChipId, HEX); + return Result; +} + +void AdvertiseServices(const char *MyName) +{ + if (MDNS.begin(MyName)) + { + Serial.println(F("mDNS responder started")); + Serial.print(F("I am: ")); + Serial.println(MyName); + + // Add service to MDNS-SD + //MDNS.addService("sensor", "tcp", 8080); + } + else + { + while (1) + { + Serial.println(F("Error setting up MDNS responder")); + delay(1000); + } + } +} + void callback(char* topic, byte* message, unsigned int length) { Serial.print("Message arrived on topic: "); Serial.print(topic); @@ -57,13 +98,13 @@ void callback(char* topic, byte* message, unsigned int length) { Serial.print("Changing switch to "); if(messageTemp == "on"){ Serial.println("lights on"); - leftServo.write(15); - rightServo.write(30); + leftServo.write(180); + rightServo.write(135); } else if(messageTemp == "off"){ Serial.println("lights off"); - leftServo.write(30); - rightServo.write(15); + leftServo.write(135); + rightServo.write(180); } } } @@ -138,8 +179,11 @@ void setup() { Serial.println("Attaching servo at pin 12"); rightServo.attach(12); - leftServo.write(0); - rightServo.write(45); + leftServo.write(135); + rightServo.write(180); + + String MyName = MakeMine("Door monitor"); + //AdvertiseServices(MyName.c_str()); } void reconnect() { @@ -147,7 +191,8 @@ void reconnect() { while (!mqttClient.connected()) { Serial.print("Attempting MQTT connection..."); // Attempt to connect - if (mqttClient.connect("ESP8266Client")) { + String MyName = MakeMine("Light switch"); + if (mqttClient.connect(MyName.c_str())) { Serial.println("connected"); // Subscribe mqttClient.subscribe(topic); @@ -167,4 +212,5 @@ void loop() { } mqttClient.loop(); + ArduinoOTA.handle(); }