Door sensor WIP

This commit is contained in:
2023-10-02 18:58:50 +02:00
parent 34b4b58ef2
commit 33ab43eddf
2 changed files with 132 additions and 32 deletions

View File

@@ -6,13 +6,17 @@
#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG #define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
#define DOOR_SENSOR_PIN 19 #define DOOR_SENSOR_PIN 19
#define PIR_SENSOR_PIN 13 #define PIR_SENSOR_PIN 13
#define ONBOARD_LED_PIN 2
#include <WiFi.h> #include <WiFi.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <HTTPClient.h> #include <HTTPClient.h>
#include <base64.h> #include <base64.h>
#include <ESPmDNS.h> #include <ESPmDNS.h>
#include <Servo.h>
#include <PubSubClient.h> #include <PubSubClient.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
IPAddress serverIp; IPAddress serverIp;
const char* ssid = "Unknown Network"; const char* ssid = "Unknown Network";
@@ -30,7 +34,7 @@ PubSubClient mqttClient(espClient);
long lastMsg = 0; long lastMsg = 0;
char msg[50]; char msg[50];
int doorState = LOW; int doorState = LOW;
int pirState = LOW; int pirState = LOW;
int doorPrevState = LOW; int doorPrevState = LOW;
int pirPrevState = LOW; int pirPrevState = LOW;
@@ -49,12 +53,14 @@ void IRAM_ATTR pir_ISR() {
{ {
pirState = digitalRead(PIR_SENSOR_PIN); pirState = digitalRead(PIR_SENSOR_PIN);
if (pirState != pirPrevState) { if (pirState != pirPrevState) {
Serial.print("PIR state: ");
Serial.println(pirState);
digitalWrite(ONBOARD_LED_PIN, pirState);
pubPirState = true; pubPirState = true;
pirPrevState = pirState; pirPrevState = pirState;
} }
delay(250);
} }
last_motion_time = motion_time; last_motion_time = motion_time;
} }
@@ -69,14 +75,9 @@ void IRAM_ATTR door_ISR() {
pubDoorState = true; pubDoorState = true;
doorPrevState = doorState; doorPrevState = doorState;
} }
delay(250);
} }
last_button_time = button_time;
}
void IRAM_ATTR window_ISR() { last_button_time = button_time;
mqttClient.publish("esp32/temperature", "The window is closed.");
} }
/* Returns a semi-unique id for the device. The id is based /* Returns a semi-unique id for the device. The id is based
@@ -131,17 +132,13 @@ void FindServer()
Serial.println("Server IP: " + serverIp.toString()); Serial.println("Server IP: " + serverIp.toString());
} }
void setup() { void setupWifi() {
Serial.begin(115200); 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); WiFi.begin(ssid, password);
Serial.println("Connecting"); Serial.println("Connecting");
while (WiFi.status() != WL_CONNECTED) { while (WiFi.status() != WL_CONNECTED) {
@@ -151,11 +148,63 @@ void setup() {
Serial.println(""); Serial.println("");
Serial.print("Connected to WiFi network with IP Address: "); Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP()); 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); mqttClient.setServer(mqtt_broker, 1883);
String MyName = MakeMine("Door monitor"); String MyName = MakeMine("Door monitor");
AdvertiseServices(MyName.c_str()); //AdvertiseServices(MyName.c_str());
//FindServer(); //FindServer();
} }
@@ -182,7 +231,7 @@ void loop() {
if (!mqttClient.connected()) { if (!mqttClient.connected()) {
reconnect(); reconnect();
} }
mqttClient.loop(); mqttClient.loop();
if (pubDoorState) { if (pubDoorState) {
if (doorState == LOW) { if (doorState == LOW) {
@@ -194,12 +243,17 @@ void loop() {
pubDoorState = false; pubDoorState = false;
delay(100); delay(100);
} }
if (pubPirState) { if (pubPirState) {
mqttClient.publish("esp32/motion", "Motion detected at door."); if (pirState == HIGH)
pubDoorState = false; {
mqttClient.publish(movementTopic, "Motion detected at door.");
}
pubPirState = false;
delay(100); delay(100);
} }
// put your main code here, to run repeatedly: // put your main code here, to run repeatedly:
ArduinoOTA.handle();
} }

View File

@@ -36,7 +36,48 @@ int pos = 0;
Servo leftServo; Servo leftServo;
Servo rightServo; 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) { void callback(char* topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: "); Serial.print("Message arrived on topic: ");
Serial.print(topic); Serial.print(topic);
@@ -57,13 +98,13 @@ void callback(char* topic, byte* message, unsigned int length) {
Serial.print("Changing switch to "); Serial.print("Changing switch to ");
if(messageTemp == "on"){ if(messageTemp == "on"){
Serial.println("lights on"); Serial.println("lights on");
leftServo.write(15); leftServo.write(180);
rightServo.write(30); rightServo.write(135);
} }
else if(messageTemp == "off"){ else if(messageTemp == "off"){
Serial.println("lights off"); Serial.println("lights off");
leftServo.write(30); leftServo.write(135);
rightServo.write(15); rightServo.write(180);
} }
} }
} }
@@ -138,8 +179,11 @@ void setup() {
Serial.println("Attaching servo at pin 12"); Serial.println("Attaching servo at pin 12");
rightServo.attach(12); rightServo.attach(12);
leftServo.write(0); leftServo.write(135);
rightServo.write(45); rightServo.write(180);
String MyName = MakeMine("Door monitor");
//AdvertiseServices(MyName.c_str());
} }
void reconnect() { void reconnect() {
@@ -147,7 +191,8 @@ void reconnect() {
while (!mqttClient.connected()) { while (!mqttClient.connected()) {
Serial.print("Attempting MQTT connection..."); Serial.print("Attempting MQTT connection...");
// Attempt to connect // Attempt to connect
if (mqttClient.connect("ESP8266Client")) { String MyName = MakeMine("Light switch");
if (mqttClient.connect(MyName.c_str())) {
Serial.println("connected"); Serial.println("connected");
// Subscribe // Subscribe
mqttClient.subscribe(topic); mqttClient.subscribe(topic);
@@ -167,4 +212,5 @@ void loop() {
} }
mqttClient.loop(); mqttClient.loop();
ArduinoOTA.handle();
} }