2025-04-03 14:35:44 -05:00
|
|
|
#include <Arduino.h>
|
|
|
|
|
#include <WiFi.h>
|
|
|
|
|
#include <PubSubClient.h>
|
|
|
|
|
#include <ArduinoJson.h>
|
|
|
|
|
|
|
|
|
|
const int PIR_PIN=23;
|
|
|
|
|
const char* ssid = "JoNelsDarlings";
|
|
|
|
|
const char* password = "Paris2016";
|
2025-04-04 19:52:08 -05:00
|
|
|
const char* mqttServerName = "mqtt.ddns.kawomi.com";
|
2025-04-03 14:35:44 -05:00
|
|
|
|
|
|
|
|
WiFiClient espClient;
|
|
|
|
|
PubSubClient mqttClient(espClient);
|
|
|
|
|
unsigned long lastMsg = 0;
|
|
|
|
|
#define MSG_BUFFER_SIZE (50)
|
|
|
|
|
char msg[MSG_BUFFER_SIZE];
|
|
|
|
|
|
|
|
|
|
void setup_wifi() {
|
|
|
|
|
delay(10);
|
|
|
|
|
// We start by connecting to a WiFi network
|
|
|
|
|
Serial.println();
|
|
|
|
|
Serial.print("Connecting to ");
|
|
|
|
|
Serial.println(ssid);
|
|
|
|
|
|
|
|
|
|
WiFi.mode(WIFI_STA);
|
|
|
|
|
WiFi.begin(ssid, password);
|
|
|
|
|
|
|
|
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
|
|
|
delay(500);
|
|
|
|
|
Serial.print(".");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
randomSeed(micros());
|
|
|
|
|
|
|
|
|
|
Serial.println("");
|
|
|
|
|
Serial.println("WiFi connected");
|
|
|
|
|
Serial.println("IP address: ");
|
|
|
|
|
Serial.println(WiFi.localIP());
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-04 19:52:08 -05:00
|
|
|
void send_mqtt(String status, long minutes=0) {
|
2025-04-04 17:26:26 -05:00
|
|
|
Serial.print("MQTT Send status ");
|
|
|
|
|
Serial.print(status);
|
|
|
|
|
Serial.print(" mins ");
|
2025-04-04 19:52:08 -05:00
|
|
|
Serial.println(minutes);
|
2025-04-04 17:26:26 -05:00
|
|
|
|
2025-04-04 19:52:08 -05:00
|
|
|
JsonDocument doc;
|
2025-04-03 14:35:44 -05:00
|
|
|
JsonObject object = doc.to<JsonObject>();
|
2025-04-03 15:37:32 -05:00
|
|
|
object["status"] = status.c_str();
|
2025-04-04 19:52:08 -05:00
|
|
|
if (minutes > 0) {
|
|
|
|
|
object["minutes"] = minutes;
|
2025-04-03 15:37:32 -05:00
|
|
|
}
|
2025-04-03 14:35:44 -05:00
|
|
|
|
|
|
|
|
static char sensorStatus[150];
|
|
|
|
|
serializeJson(doc, sensorStatus);
|
|
|
|
|
Serial.println(sensorStatus);
|
|
|
|
|
|
|
|
|
|
mqttClient.publish("Address/Livorno/Kitchen/PIR", sensorStatus, true);
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-04 19:52:08 -05:00
|
|
|
void reconnect() {
|
|
|
|
|
int numRetries=0;
|
|
|
|
|
|
|
|
|
|
while (!mqttClient.connected()) {
|
|
|
|
|
numRetries++;
|
|
|
|
|
Serial.print("Attempting MQTT connection...");
|
|
|
|
|
|
|
|
|
|
// Create a random client ID
|
|
|
|
|
String clientId = "ESP32Client-";
|
|
|
|
|
clientId += String(random(0xffff), HEX);
|
|
|
|
|
|
|
|
|
|
if (mqttClient.connect(clientId.c_str())) {
|
|
|
|
|
Serial.println("MQTT connected");
|
|
|
|
|
} else {
|
|
|
|
|
Serial.print("MQTT failed, rc=");
|
|
|
|
|
Serial.print(mqttClient.state());
|
|
|
|
|
Serial.println(" try again in 5 seconds");
|
|
|
|
|
|
|
|
|
|
if (numRetries>5){
|
|
|
|
|
Serial.println("Too many retries, restarting the ESP...");
|
|
|
|
|
ESP.restart();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
delay(5000);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
send_mqtt("ready");
|
|
|
|
|
}
|
2025-04-03 14:35:44 -05:00
|
|
|
|
|
|
|
|
void setup() {
|
|
|
|
|
pinMode(LED_BUILTIN,OUTPUT);
|
2025-04-03 15:37:32 -05:00
|
|
|
pinMode(PIR_PIN,INPUT_PULLUP);
|
2025-04-03 14:35:44 -05:00
|
|
|
|
|
|
|
|
Serial.begin(115200);
|
|
|
|
|
setup_wifi();
|
2025-04-04 19:52:08 -05:00
|
|
|
const int mqttPort=1883;
|
|
|
|
|
mqttClient.setServer(mqttServerName, mqttPort);
|
2025-04-03 14:35:44 -05:00
|
|
|
}
|
|
|
|
|
|
2025-04-04 19:52:08 -05:00
|
|
|
const int millisPerSecond=1000;
|
2025-04-03 15:37:32 -05:00
|
|
|
long previousMotionMillis=millis();
|
2025-04-04 19:52:08 -05:00
|
|
|
short previousState=0;
|
|
|
|
|
long previousNoMoNotifyMinutes=0;
|
2025-04-03 14:35:44 -05:00
|
|
|
void loop() {
|
|
|
|
|
if (!mqttClient.connected()) {
|
|
|
|
|
reconnect();
|
|
|
|
|
}
|
|
|
|
|
mqttClient.loop();
|
|
|
|
|
|
2025-04-03 15:37:32 -05:00
|
|
|
short currentState = digitalRead(PIR_PIN);
|
|
|
|
|
if (currentState != previousState) {
|
2025-04-04 17:26:26 -05:00
|
|
|
Serial.print("Status changed; currentState: ");
|
|
|
|
|
Serial.print(currentState);
|
|
|
|
|
Serial.print(" previousState: ");
|
|
|
|
|
Serial.println(previousState);
|
2025-04-03 15:37:32 -05:00
|
|
|
if (currentState == HIGH) {
|
|
|
|
|
send_mqtt("motion");
|
|
|
|
|
digitalWrite(LED_BUILTIN,HIGH);
|
|
|
|
|
previousMotionMillis=millis();
|
|
|
|
|
}
|
|
|
|
|
previousState = currentState;
|
2025-04-03 14:35:44 -05:00
|
|
|
}
|
2025-04-03 15:37:32 -05:00
|
|
|
|
2025-04-04 19:52:08 -05:00
|
|
|
const int secondsPerMinute=60;
|
|
|
|
|
long minutesSinceLastMotion=(millis()-previousMotionMillis)/(millisPerSecond*secondsPerMinute);
|
|
|
|
|
if ((previousNoMoNotifyMinutes != minutesSinceLastMotion) && (0 != minutesSinceLastMotion)) {
|
|
|
|
|
send_mqtt("no_motion",minutesSinceLastMotion);
|
|
|
|
|
previousNoMoNotifyMinutes = minutesSinceLastMotion;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const int ledDelaySeconds=5;
|
|
|
|
|
if ((millis()-previousMotionMillis)/(millisPerSecond) >= ledDelaySeconds) {
|
|
|
|
|
digitalWrite(LED_BUILTIN,LOW);
|
2025-04-03 15:37:32 -05:00
|
|
|
}
|
2025-04-04 17:26:26 -05:00
|
|
|
|
|
|
|
|
delay(20);
|
2025-04-03 14:35:44 -05:00
|
|
|
}
|