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 <ESP8266WiFi.h>
|
||||
|
||||
|
@ -13,10 +11,8 @@
|
|||
#define NUM_SAMPLES 20
|
||||
#define SAMPLE_TIME 100
|
||||
#define WATER_THRESHOLD 750
|
||||
#define SENSE_THRESHOLD 100
|
||||
|
||||
#define MESSAGE_INTERVAL 1000 * 60 * 20 // 20 minutes
|
||||
#define RESTART_INTERVAL 1000 * 60 * 60 * 24 * 7 // 7 days
|
||||
#define MESSAGE_INTERVAL 1000 * 60 * 60 // one hour
|
||||
|
||||
WiFiClient wifiClient;
|
||||
MqttClient mqttClient(wifiClient);
|
||||
|
@ -62,16 +58,6 @@ bool sampleWater() {
|
|||
return waterDetected;
|
||||
}
|
||||
|
||||
bool senseCable() {
|
||||
int sample = analogRead(ADC_PIN);
|
||||
|
||||
if (sample < SENSE_THRESHOLD) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
void sendWaterLeakTrue() {
|
||||
mqttClient.beginMessage(topic);
|
||||
mqttClient.print("{\"id\": \"");
|
||||
|
@ -88,17 +74,8 @@ void sendWaterLeakFalse() {
|
|||
mqttClient.endMessage();
|
||||
}
|
||||
|
||||
void sendCableDisconnected() {
|
||||
mqttClient.beginMessage(topic);
|
||||
mqttClient.print("{\"id\": \"");
|
||||
mqttClient.print(deviceName);
|
||||
mqttClient.print("\", \"cable_disconnected\": true}");
|
||||
mqttClient.endMessage();
|
||||
}
|
||||
|
||||
void setup() {
|
||||
pinMode(RELAY_PIN, OUTPUT);
|
||||
relayOff();
|
||||
|
||||
Serial.begin(115200);
|
||||
delay(1000);
|
||||
|
@ -115,8 +92,6 @@ void setup() {
|
|||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
WiFi.setAutoReconnect(true);
|
||||
WiFi.persistent(true);
|
||||
|
||||
Serial.println("");
|
||||
Serial.println("[WIFI] Connected to the network");
|
||||
|
@ -135,39 +110,25 @@ void setup() {
|
|||
|
||||
Serial.println("[MQTT] Connected to the MQTT broker.");
|
||||
|
||||
Serial.println("Finished setup, turning relay on...");
|
||||
delay(1000);
|
||||
Serial.println("Turning relay on.");
|
||||
relayOn();
|
||||
delay(5000);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
static unsigned long last_pos_message = 0;
|
||||
static unsigned long last_neg_message = 0;
|
||||
static unsigned long last_sense_message = 0;
|
||||
static bool hasTriggered = false;
|
||||
|
||||
bool leakDetected = sampleWater();
|
||||
bool cableDetected = senseCable();
|
||||
|
||||
if (leakDetected) {
|
||||
relayOff();
|
||||
hasTriggered = true;
|
||||
Serial.println("Leak detected!");
|
||||
}
|
||||
|
||||
|
||||
if (!hasTriggered && millis() > RESTART_INTERVAL) {
|
||||
Serial.println("Weekly restarting Arduino...");
|
||||
relayOff();
|
||||
ESP.restart();
|
||||
Serial.println("Leak detected");
|
||||
}
|
||||
|
||||
mqttClient.poll();
|
||||
|
||||
if (WiFi.status() != WL_CONNECTED) {
|
||||
Serial.println("[WIFI] Lost connection. Reconnecting...");
|
||||
WiFi.disconnect();
|
||||
WiFi.begin(SECRET_SSID, SECRET_PASS);
|
||||
return;
|
||||
} else if (!mqttClient.connected()) {
|
||||
|
@ -192,12 +153,4 @@ void loop() {
|
|||
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