Add will and timesignal

This commit is contained in:
cazagen 2019-01-25 23:26:42 +00:00
parent 6474447e53
commit dc5a0e0307
Signed by: cazagen
GPG Key ID: C326FEEE25FF7915

View File

@ -20,13 +20,14 @@ int led_timeout = 1000;
long led_time = 0; long led_time = 0;
long last_wifi_check_time = 0; long last_wifi_check_time = 0;
long lastReconnectAttempt = 0; long lastReconnectAttempt = 0;
long reconnectAttempts = 0; long reconnectAttempts = 0;
long debouncing_time = 1000; long debouncing_time = 1000;
volatile unsigned long last_micros; volatile unsigned long last_micros;
long lastTimesignalTime = 0;
WiFiClient espClient; WiFiClient espClient;
PubSubClient client(espClient); PubSubClient client(espClient);
@ -98,12 +99,13 @@ void setup_wifi() {
boolean reconnect() { boolean reconnect() {
reconnectAttempts++; reconnectAttempts++;
#
String clientID = "ESP8266Client-"; String clientID = "ESP8266Client-";
clientID += String(random(0xffff), HEX); clientID += String(random(0xffff), HEX);
if (client.connect(clientID.c_str())) { if (client.connect(clientID.c_str(), NULL, NULL, willTopic, 0, 1, "0")) {
Serial.println("MQTT Connected"); Serial.println("MQTT Connected");
client.publish(willTopic, "1", true);
client.subscribe(timeTopic);
} else { } else {
Serial.print("failed, rc = "); Serial.print("failed, rc = ");
Serial.print(client.state()); Serial.print(client.state());
@ -113,6 +115,11 @@ boolean reconnect() {
return client.connected(); return client.connected();
} }
void callback(char* topic, byte* payload, unsigned int length) {
Serial.println("Timesignal received");
lastTimesignalTime = millis();
}
void setup() { void setup() {
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
pinMode(LED_PIN, OUTPUT); pinMode(LED_PIN, OUTPUT);
@ -127,6 +134,8 @@ void setup() {
setup_ota(); setup_ota();
client.setServer(mqtt_server, 1883); client.setServer(mqtt_server, 1883);
client.setCallback(callback);
attachInterrupt(INPUT1, debounceInterrupt1, RISING); attachInterrupt(INPUT1, debounceInterrupt1, RISING);
attachInterrupt(INPUT2, debounceInterrupt2, RISING); attachInterrupt(INPUT2, debounceInterrupt2, RISING);
attachInterrupt(INPUT3, debounceInterrupt3, RISING); attachInterrupt(INPUT3, debounceInterrupt3, RISING);
@ -162,6 +171,7 @@ void debounceInterrupt4() {
} }
void Interrupt1() { void Interrupt1() {
Serial.println("Button 1 pressed");
client.publish(outTopic, "1", true); client.publish(outTopic, "1", true);
digitalWrite(LED_PIN, 1); digitalWrite(LED_PIN, 1);
led_time = millis(); led_time = millis();
@ -169,27 +179,32 @@ void Interrupt1() {
} }
void Interrupt2() { void Interrupt2() {
Serial.println("Button 2 pressed");
client.publish(outTopic, "2", true); client.publish(outTopic, "2", true);
digitalWrite(LED_PIN, 1); digitalWrite(LED_PIN, 1);
led_time = millis(); led_time = millis();
} }
void Interrupt3() { void Interrupt3() {
Serial.println("Button 3 pressed");
client.publish(outTopic, "3", true); client.publish(outTopic, "3", true);
digitalWrite(LED_PIN, 1); digitalWrite(LED_PIN, 1);
led_time = millis(); led_time = millis();
} }
void Interrupt4() { void Interrupt4() {
Serial.println("Button 4 pressed");
client.publish(outTopic, "4", true); client.publish(outTopic, "4", true);
digitalWrite(LED_PIN, 1); digitalWrite(LED_PIN, 1);
led_time = millis(); led_time = millis();
} }
void loop() { void loop() {
long now = millis(); if (millis() - lastTimesignalTime > 180000) {
ESP.restart();
}
if (now - last_wifi_check_time > WIFI_TIMEOUT) { if (millis() - last_wifi_check_time > WIFI_TIMEOUT) {
Serial.print("Checking WiFi... "); Serial.print("Checking WiFi... ");
if (WiFi.status() != WL_CONNECTED) { if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi connection lost. Reconnecting..."); Serial.println("WiFi connection lost. Reconnecting...");
@ -197,12 +212,13 @@ void loop() {
} else { } else {
Serial.println("OK"); Serial.println("OK");
} }
last_wifi_check_time = now; last_wifi_check_time = millis();
} }
if (!client.connected()) { if (!client.connected()) {
if (now - lastReconnectAttempt > 5000) { if (millis() - lastReconnectAttempt > 5000) {
lastReconnectAttempt = now; Serial.println("checking MQTT connection");
lastReconnectAttempt = millis();
// Attempt to reconnect // Attempt to reconnect
Serial.println("Attempting MQTT reconnect"); Serial.println("Attempting MQTT reconnect");
if (reconnect()) { if (reconnect()) {
@ -218,7 +234,7 @@ void loop() {
client.loop(); client.loop();
} }
if (now - led_time > led_timeout) { if (millis() - led_time > led_timeout) {
digitalWrite(LED_PIN, 0); digitalWrite(LED_PIN, 0);
} }