Door sensor WIP
This commit is contained in:
@@ -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 <WiFi.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <HTTPClient.h>
|
||||
#include <base64.h>
|
||||
#include <ESPmDNS.h>
|
||||
#include <Servo.h>
|
||||
#include <PubSubClient.h>
|
||||
#include <WiFiUdp.h>
|
||||
#include <ArduinoOTA.h>
|
||||
|
||||
IPAddress serverIp;
|
||||
const char* ssid = "Unknown Network";
|
||||
@@ -49,10 +53,12 @@ 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,16 +75,11 @@ 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.");
|
||||
}
|
||||
|
||||
/* 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. */
|
||||
@@ -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();
|
||||
}
|
||||
@@ -196,10 +245,15 @@ void loop() {
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
}
|
||||
@@ -37,6 +37,47 @@ 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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user