Compare commits
10 Commits
abbf6a8546
...
94a6929fea
| Author | SHA1 | Date | |
|---|---|---|---|
| 94a6929fea | |||
| c382ae2888 | |||
| 7a493759ba | |||
| 5ccd15dde3 | |||
|
|
f7ab7f61a2 | ||
|
|
9a128704ff | ||
|
|
150a9aae20 | ||
|
|
2fb449522d | ||
|
|
79d440646d | ||
|
|
c23cf033de |
3
.vscode/settings.json
vendored
Normal file
3
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"CodeGPT.apiKey": "CodeGPT Plus Beta"
|
||||||
|
}
|
||||||
@@ -11,6 +11,7 @@
|
|||||||
[env:esp32doit-devkit-v1]
|
[env:esp32doit-devkit-v1]
|
||||||
platform = espressif32
|
platform = espressif32
|
||||||
board = esp32doit-devkit-v1
|
board = esp32doit-devkit-v1
|
||||||
|
monitor_speed = 115200
|
||||||
framework = arduino
|
framework = arduino
|
||||||
lib_deps =
|
lib_deps =
|
||||||
knolleary/PubSubClient@^2.8
|
knolleary/PubSubClient@^2.8
|
||||||
|
|||||||
115
src/main.cpp
115
src/main.cpp
@@ -6,8 +6,7 @@
|
|||||||
const int PIR_PIN=23;
|
const int PIR_PIN=23;
|
||||||
const char* ssid = "JoNelsDarlings";
|
const char* ssid = "JoNelsDarlings";
|
||||||
const char* password = "Paris2016";
|
const char* password = "Paris2016";
|
||||||
const char* mqtt_server = "mqtt.ddns.kawomi.com";
|
const char* mqttServerName = "mqtt.ddns.kawomi.com";
|
||||||
|
|
||||||
|
|
||||||
WiFiClient espClient;
|
WiFiClient espClient;
|
||||||
PubSubClient mqttClient(espClient);
|
PubSubClient mqttClient(espClient);
|
||||||
@@ -16,7 +15,6 @@ unsigned long lastMsg = 0;
|
|||||||
char msg[MSG_BUFFER_SIZE];
|
char msg[MSG_BUFFER_SIZE];
|
||||||
|
|
||||||
void setup_wifi() {
|
void setup_wifi() {
|
||||||
|
|
||||||
delay(10);
|
delay(10);
|
||||||
// We start by connecting to a WiFi network
|
// We start by connecting to a WiFi network
|
||||||
Serial.println();
|
Serial.println();
|
||||||
@@ -39,37 +37,19 @@ void setup_wifi() {
|
|||||||
Serial.println(WiFi.localIP());
|
Serial.println(WiFi.localIP());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void send_mqtt(String status, long minutes=0) {
|
||||||
|
Serial.print("MQTT Send status ");
|
||||||
|
Serial.print(status);
|
||||||
|
Serial.print(" mins ");
|
||||||
|
Serial.println(minutes);
|
||||||
|
|
||||||
void reconnect() {
|
JsonDocument doc;
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void send_mqtt(String value) {
|
|
||||||
const size_t CAPACITY = JSON_OBJECT_SIZE(1);
|
|
||||||
StaticJsonDocument<CAPACITY> doc;
|
|
||||||
|
|
||||||
// create an object
|
|
||||||
JsonObject object = doc.to<JsonObject>();
|
JsonObject object = doc.to<JsonObject>();
|
||||||
object["status"] = value.c_str();
|
object["status"] = status.c_str();
|
||||||
|
if (minutes > 0) {
|
||||||
|
object["minutes"] = minutes;
|
||||||
|
}
|
||||||
|
|
||||||
// serialize the object and send the result to Serial
|
|
||||||
static char sensorStatus[150];
|
static char sensorStatus[150];
|
||||||
serializeJson(doc, sensorStatus);
|
serializeJson(doc, sensorStatus);
|
||||||
Serial.println(sensorStatus);
|
Serial.println(sensorStatus);
|
||||||
@@ -77,28 +57,89 @@ void send_mqtt(String value) {
|
|||||||
mqttClient.publish("Address/Livorno/Kitchen/PIR", sensorStatus, true);
|
mqttClient.publish("Address/Livorno/Kitchen/PIR", sensorStatus, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
pinMode(LED_BUILTIN,OUTPUT);
|
pinMode(LED_BUILTIN,OUTPUT);
|
||||||
pinMode(PIR_PIN,INPUT);
|
pinMode(PIR_PIN,INPUT_PULLUP);
|
||||||
|
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
setup_wifi();
|
setup_wifi();
|
||||||
mqttClient.setServer(mqtt_server, 1883);
|
const int mqttPort=1883;
|
||||||
|
mqttClient.setServer(mqttServerName, mqttPort);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const int millisPerSecond=1000;
|
||||||
|
long previousMotionMillis=millis();
|
||||||
|
short previousState=-1;
|
||||||
|
long previousNoMoNotifyMinutes=0;
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
if (!mqttClient.connected()) {
|
if (!mqttClient.connected()) {
|
||||||
reconnect();
|
reconnect();
|
||||||
}
|
}
|
||||||
mqttClient.loop();
|
mqttClient.loop();
|
||||||
|
|
||||||
int pir = digitalRead(PIR_PIN);
|
short currentState = digitalRead(PIR_PIN);
|
||||||
if (pir == HIGH) {
|
// This out pin triggering is dependent not only on movement, but also on light levels which can be configured in the app.
|
||||||
|
// I set it to 255 and "below" - this triggered it for me.....
|
||||||
|
//send_mqtt("occupancy_state_debug", currentState);
|
||||||
|
|
||||||
|
// When occupancy is detected, reset the timer
|
||||||
|
if (currentState == HIGH) {
|
||||||
|
previousMotionMillis=millis();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send messages only on status change, we don't want to flood the network...
|
||||||
|
if (currentState != previousState) {
|
||||||
|
Serial.print("Status changed; currentState: ");
|
||||||
|
Serial.print(currentState);
|
||||||
|
Serial.print(" previousState: ");
|
||||||
|
Serial.println(previousState);
|
||||||
|
if (currentState == HIGH) {
|
||||||
send_mqtt("motion");
|
send_mqtt("motion");
|
||||||
digitalWrite(LED_BUILTIN,HIGH);
|
digitalWrite(LED_BUILTIN,HIGH);
|
||||||
delay(1000);
|
} else {
|
||||||
|
send_mqtt("no_motion_immediate");
|
||||||
digitalWrite(LED_BUILTIN,LOW);
|
digitalWrite(LED_BUILTIN,LOW);
|
||||||
}
|
}
|
||||||
|
previousState = currentState;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send no_motion messages every minute, indicating how long we didn't see occupancy
|
||||||
|
const int secondsPerMinute=60;
|
||||||
|
long minutesSinceLastMotion=(millis()-previousMotionMillis)/(millisPerSecond*secondsPerMinute);
|
||||||
|
if ((previousNoMoNotifyMinutes != minutesSinceLastMotion) && (0 != minutesSinceLastMotion)) {
|
||||||
|
send_mqtt("no_motion", minutesSinceLastMotion);
|
||||||
|
previousNoMoNotifyMinutes = minutesSinceLastMotion;
|
||||||
|
}
|
||||||
|
|
||||||
|
delay(50);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user