|
|
|
@ -1,4 +1,5 @@ |
|
|
|
|
#include <ESP8266WiFi.h> |
|
|
|
|
#include <Schedule.h> |
|
|
|
|
#include <PubSubClient.h> |
|
|
|
|
#include <ArduinoOTA.h> |
|
|
|
|
#include "config.h" |
|
|
|
@ -28,6 +29,12 @@ volatile unsigned long last_micros; |
|
|
|
|
|
|
|
|
|
long lastTimesignalTime = 0; |
|
|
|
|
|
|
|
|
|
bool button1_pressed = 0; |
|
|
|
|
bool button2_pressed = 0; |
|
|
|
|
bool button3_pressed = 0; |
|
|
|
|
bool button4_pressed = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
WiFiClient espClient; |
|
|
|
|
PubSubClient client(espClient); |
|
|
|
|
|
|
|
|
@ -36,7 +43,6 @@ void setup_ota() { |
|
|
|
|
// Hostname defaults to esp8266-[ChipID]
|
|
|
|
|
ArduinoOTA.setHostname(wifi_hostname); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ArduinoOTA.setPassword(ota_password); |
|
|
|
|
|
|
|
|
|
ArduinoOTA.onStart([]() { |
|
|
|
@ -74,12 +80,11 @@ void setup_ota() { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void setup_wifi() { |
|
|
|
|
|
|
|
|
|
delay(10); |
|
|
|
|
// We start by connecting to a WiFi network
|
|
|
|
|
Serial.println(); |
|
|
|
|
Serial.print("Connecting to "); |
|
|
|
|
Serial.println(ssid); |
|
|
|
|
|
|
|
|
|
WiFi.hostname(wifi_hostname); |
|
|
|
|
WiFi.mode(WIFI_STA); |
|
|
|
|
WiFi.begin(ssid, password); |
|
|
|
@ -107,7 +112,7 @@ boolean reconnect() { |
|
|
|
|
client.publish(willTopic, "1", true); |
|
|
|
|
client.subscribe(timeTopic); |
|
|
|
|
} else { |
|
|
|
|
Serial.print("failed, rc = "); |
|
|
|
|
Serial.print("MQTT failed, rc="); |
|
|
|
|
Serial.print(client.state()); |
|
|
|
|
Serial.println("Trying again"); |
|
|
|
|
} |
|
|
|
@ -121,7 +126,7 @@ void callback(char* topic, byte* payload, unsigned int length) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void setup() { |
|
|
|
|
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
|
|
|
|
|
pinMode(BUILTIN_LED, OUTPUT); |
|
|
|
|
pinMode(LED_PIN, OUTPUT); |
|
|
|
|
pinMode(INPUT1, INPUT_PULLUP); |
|
|
|
|
pinMode(INPUT2, INPUT_PULLUP); |
|
|
|
@ -143,56 +148,59 @@ void setup() { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void debounceInterrupt1() { |
|
|
|
|
Serial.println("debounce 1"); |
|
|
|
|
if((long)(micros() - last_micros) >= debouncing_time * 1000) { |
|
|
|
|
Interrupt1(); |
|
|
|
|
schedule_function(Button1); |
|
|
|
|
last_micros = micros(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void debounceInterrupt2() { |
|
|
|
|
Serial.println("debounce 2"); |
|
|
|
|
if((long)(micros() - last_micros) >= debouncing_time * 1000) { |
|
|
|
|
Interrupt2(); |
|
|
|
|
schedule_function(Button2); |
|
|
|
|
last_micros = micros(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void debounceInterrupt3() { |
|
|
|
|
Serial.println("debounce 3"); |
|
|
|
|
if((long)(micros() - last_micros) >= debouncing_time * 1000) { |
|
|
|
|
Interrupt3(); |
|
|
|
|
schedule_function(Button3); |
|
|
|
|
last_micros = micros(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void debounceInterrupt4() { |
|
|
|
|
Serial.println("debounce 4"); |
|
|
|
|
if((long)(micros() - last_micros) >= debouncing_time * 1000) { |
|
|
|
|
Interrupt4(); |
|
|
|
|
schedule_function(Button4); |
|
|
|
|
last_micros = micros(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Interrupt1() { |
|
|
|
|
void Button1() { |
|
|
|
|
Serial.println("Button 1 pressed"); |
|
|
|
|
client.publish(outTopic, "1", true); |
|
|
|
|
digitalWrite(LED_PIN, 1); |
|
|
|
|
led_time = millis(); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Interrupt2() { |
|
|
|
|
void Button2() { |
|
|
|
|
Serial.println("Button 2 pressed"); |
|
|
|
|
client.publish(outTopic, "2", true); |
|
|
|
|
digitalWrite(LED_PIN, 1); |
|
|
|
|
led_time = millis(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Interrupt3() { |
|
|
|
|
void Button3() { |
|
|
|
|
Serial.println("Button 3 pressed"); |
|
|
|
|
client.publish(outTopic, "3", true); |
|
|
|
|
digitalWrite(LED_PIN, 1); |
|
|
|
|
led_time = millis(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Interrupt4() { |
|
|
|
|
void Button4() { |
|
|
|
|
Serial.println("Button 4 pressed"); |
|
|
|
|
client.publish(outTopic, "4", true); |
|
|
|
|
digitalWrite(LED_PIN, 1); |
|
|
|
@ -201,6 +209,7 @@ void Interrupt4() { |
|
|
|
|
|
|
|
|
|
void loop() { |
|
|
|
|
if (millis() - lastTimesignalTime > 180000) { |
|
|
|
|
Serial.println("Timesignal is missing, restarting"); |
|
|
|
|
ESP.restart(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -217,13 +226,12 @@ void loop() { |
|
|
|
|
|
|
|
|
|
if (!client.connected()) { |
|
|
|
|
if (millis() - lastReconnectAttempt > 5000) { |
|
|
|
|
Serial.println("checking MQTT connection"); |
|
|
|
|
lastReconnectAttempt = millis(); |
|
|
|
|
// Attempt to reconnect
|
|
|
|
|
Serial.println("Attempting MQTT reconnect"); |
|
|
|
|
if (reconnect()) { |
|
|
|
|
reconnectAttempts = 0; |
|
|
|
|
lastReconnectAttempt = 0; |
|
|
|
|
lastReconnectAttempt = millis(); |
|
|
|
|
} |
|
|
|
|
if (reconnectAttempts > 5) { |
|
|
|
|
Serial.println("mqtt failed too many times, restarting wifi"); |
|
|
|
|