Compare commits
No commits in common. "45a21178585ccaccf1c01c672e2a2c00d2072d0a" and "35c8018e22b50fed10eb4f28096eb009b5279a34" have entirely different histories.
45a2117858
...
35c8018e22
|
@ -1,5 +1,3 @@
|
||||||
// Board: Adafruit Feather HUZZAH ESP8266
|
|
||||||
|
|
||||||
#include <ArduinoMqttClient.h>
|
#include <ArduinoMqttClient.h>
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
|
|
||||||
|
@ -13,10 +11,8 @@
|
||||||
#define NUM_SAMPLES 20
|
#define NUM_SAMPLES 20
|
||||||
#define SAMPLE_TIME 100
|
#define SAMPLE_TIME 100
|
||||||
#define WATER_THRESHOLD 750
|
#define WATER_THRESHOLD 750
|
||||||
#define SENSE_THRESHOLD 100
|
|
||||||
|
|
||||||
#define MESSAGE_INTERVAL 1000 * 60 * 20 // 20 minutes
|
#define MESSAGE_INTERVAL 1000 * 60 * 60 // one hour
|
||||||
#define RESTART_INTERVAL 1000 * 60 * 60 * 24 * 7 // 7 days
|
|
||||||
|
|
||||||
WiFiClient wifiClient;
|
WiFiClient wifiClient;
|
||||||
MqttClient mqttClient(wifiClient);
|
MqttClient mqttClient(wifiClient);
|
||||||
|
@ -62,16 +58,6 @@ bool sampleWater() {
|
||||||
return waterDetected;
|
return waterDetected;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool senseCable() {
|
|
||||||
int sample = analogRead(ADC_PIN);
|
|
||||||
|
|
||||||
if (sample < SENSE_THRESHOLD) {
|
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void sendWaterLeakTrue() {
|
void sendWaterLeakTrue() {
|
||||||
mqttClient.beginMessage(topic);
|
mqttClient.beginMessage(topic);
|
||||||
mqttClient.print("{\"id\": \"");
|
mqttClient.print("{\"id\": \"");
|
||||||
|
@ -88,17 +74,8 @@ void sendWaterLeakFalse() {
|
||||||
mqttClient.endMessage();
|
mqttClient.endMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendCableDisconnected() {
|
|
||||||
mqttClient.beginMessage(topic);
|
|
||||||
mqttClient.print("{\"id\": \"");
|
|
||||||
mqttClient.print(deviceName);
|
|
||||||
mqttClient.print("\", \"cable_disconnected\": true}");
|
|
||||||
mqttClient.endMessage();
|
|
||||||
}
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
pinMode(RELAY_PIN, OUTPUT);
|
pinMode(RELAY_PIN, OUTPUT);
|
||||||
relayOff();
|
|
||||||
|
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
delay(1000);
|
delay(1000);
|
||||||
|
@ -115,8 +92,6 @@ void setup() {
|
||||||
delay(500);
|
delay(500);
|
||||||
Serial.print(".");
|
Serial.print(".");
|
||||||
}
|
}
|
||||||
WiFi.setAutoReconnect(true);
|
|
||||||
WiFi.persistent(true);
|
|
||||||
|
|
||||||
Serial.println("");
|
Serial.println("");
|
||||||
Serial.println("[WIFI] Connected to the network");
|
Serial.println("[WIFI] Connected to the network");
|
||||||
|
@ -135,39 +110,25 @@ void setup() {
|
||||||
|
|
||||||
Serial.println("[MQTT] Connected to the MQTT broker.");
|
Serial.println("[MQTT] Connected to the MQTT broker.");
|
||||||
|
|
||||||
Serial.println("Finished setup, turning relay on...");
|
Serial.println("Turning relay on.");
|
||||||
delay(1000);
|
|
||||||
relayOn();
|
relayOn();
|
||||||
delay(5000);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
static unsigned long last_pos_message = 0;
|
static unsigned long last_pos_message = 0;
|
||||||
static unsigned long last_neg_message = 0;
|
static unsigned long last_neg_message = 0;
|
||||||
static unsigned long last_sense_message = 0;
|
|
||||||
static bool hasTriggered = false;
|
|
||||||
|
|
||||||
bool leakDetected = sampleWater();
|
bool leakDetected = sampleWater();
|
||||||
bool cableDetected = senseCable();
|
|
||||||
|
|
||||||
if (leakDetected) {
|
if (leakDetected) {
|
||||||
relayOff();
|
relayOff();
|
||||||
hasTriggered = true;
|
Serial.println("Leak detected");
|
||||||
Serial.println("Leak detected!");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if (!hasTriggered && millis() > RESTART_INTERVAL) {
|
|
||||||
Serial.println("Weekly restarting Arduino...");
|
|
||||||
relayOff();
|
|
||||||
ESP.restart();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mqttClient.poll();
|
mqttClient.poll();
|
||||||
|
|
||||||
if (WiFi.status() != WL_CONNECTED) {
|
if (WiFi.status() != WL_CONNECTED) {
|
||||||
Serial.println("[WIFI] Lost connection. Reconnecting...");
|
Serial.println("[WIFI] Lost connection. Reconnecting...");
|
||||||
WiFi.disconnect();
|
|
||||||
WiFi.begin(SECRET_SSID, SECRET_PASS);
|
WiFi.begin(SECRET_SSID, SECRET_PASS);
|
||||||
return;
|
return;
|
||||||
} else if (!mqttClient.connected()) {
|
} else if (!mqttClient.connected()) {
|
||||||
|
@ -192,12 +153,4 @@ void loop() {
|
||||||
sendWaterLeakFalse();
|
sendWaterLeakFalse();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!cableDetected) {
|
|
||||||
Serial.println("Cable not detected!");
|
|
||||||
if (!last_sense_message || millis() - last_sense_message > MESSAGE_INTERVAL) {
|
|
||||||
last_sense_message = millis();
|
|
||||||
sendCableDisconnected();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user