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";
|
|
|
|
|
const char* mqtt_server = "mqtt.ddns.kawomi.com";
|
|
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void reconnect() {
|
|
|
|
|
// Loop until we're reconnected
|
|
|
|
|
while (!mqttClient.connected()) {
|
|
|
|
|
Serial.print("Attempting MQTT connection...");
|
|
|
|
|
// Create a random client ID
|
|
|
|
|
String clientId = "ESP32Client-";
|
|
|
|
|
clientId += String(random(0xffff), HEX);
|
|
|
|
|
// Attempt to connect
|
|
|
|
|
if (mqttClient.connect(clientId.c_str())) {
|
|
|
|
|
Serial.println("MQTT connected");
|
|
|
|
|
// Once connected, publish an announcement...
|
|
|
|
|
} else {
|
|
|
|
|
Serial.print("MQTT failed, rc=");
|
|
|
|
|
Serial.print(mqttClient.state());
|
|
|
|
|
Serial.println(" try again in 5 seconds");
|
|
|
|
|
// Wait 5 seconds before retrying
|
|
|
|
|
delay(5000);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-03 15:37:32 -05:00
|
|
|
void send_mqtt(String status, long time_min=0) {
|
2025-04-04 17:26:26 -05:00
|
|
|
Serial.print("MQTT Send status ");
|
|
|
|
|
Serial.print(status);
|
|
|
|
|
Serial.print(" mins ");
|
|
|
|
|
Serial.println(time_min);
|
|
|
|
|
|
2025-04-03 14:35:44 -05:00
|
|
|
const size_t CAPACITY = JSON_OBJECT_SIZE(1);
|
|
|
|
|
StaticJsonDocument<CAPACITY> doc;
|
|
|
|
|
|
|
|
|
|
// create an object
|
|
|
|
|
JsonObject object = doc.to<JsonObject>();
|
2025-04-03 15:37:32 -05:00
|
|
|
object["status"] = status.c_str();
|
|
|
|
|
if (time_min > 0) {
|
|
|
|
|
object["minutes"] = time_min;
|
|
|
|
|
}
|
2025-04-03 14:35:44 -05:00
|
|
|
|
|
|
|
|
// serialize the object and send the result to Serial
|
|
|
|
|
static char sensorStatus[150];
|
|
|
|
|
serializeJson(doc, sensorStatus);
|
|
|
|
|
Serial.println(sensorStatus);
|
|
|
|
|
|
|
|
|
|
mqttClient.publish("Address/Livorno/Kitchen/PIR", sensorStatus, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
mqttClient.setServer(mqtt_server, 1883);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-03 15:37:32 -05:00
|
|
|
long previousMotionMillis=millis();
|
|
|
|
|
short previousState=digitalRead(PIR_PIN);
|
|
|
|
|
long previousNoMoNotifyMins=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);
|
2025-04-04 17:26:26 -05:00
|
|
|
delay(5000);
|
2025-04-03 15:37:32 -05:00
|
|
|
digitalWrite(LED_BUILTIN,LOW);
|
|
|
|
|
previousMotionMillis=millis();
|
|
|
|
|
}
|
|
|
|
|
previousState = currentState;
|
2025-04-03 14:35:44 -05:00
|
|
|
}
|
2025-04-03 15:37:32 -05:00
|
|
|
|
|
|
|
|
long minsSinceLastMotion=(millis()-previousMotionMillis)/(1000*60);
|
2025-04-04 17:26:26 -05:00
|
|
|
if ((previousNoMoNotifyMins != minsSinceLastMotion) && (0 != minsSinceLastMotion)) {
|
2025-04-03 15:37:32 -05:00
|
|
|
send_mqtt("no_motion",minsSinceLastMotion);
|
|
|
|
|
previousNoMoNotifyMins = minsSinceLastMotion;
|
|
|
|
|
}
|
2025-04-04 17:26:26 -05:00
|
|
|
|
|
|
|
|
delay(20);
|
2025-04-03 14:35:44 -05:00
|
|
|
}
|