Add will and timesignal
This commit is contained in:
parent
6474447e53
commit
dc5a0e0307
34
buttons.cpp
34
buttons.cpp
|
@ -20,13 +20,14 @@ int led_timeout = 1000;
|
|||
long led_time = 0;
|
||||
|
||||
long last_wifi_check_time = 0;
|
||||
|
||||
long lastReconnectAttempt = 0;
|
||||
long reconnectAttempts = 0;
|
||||
|
||||
long debouncing_time = 1000;
|
||||
volatile unsigned long last_micros;
|
||||
|
||||
long lastTimesignalTime = 0;
|
||||
|
||||
WiFiClient espClient;
|
||||
PubSubClient client(espClient);
|
||||
|
||||
|
@ -98,12 +99,13 @@ void setup_wifi() {
|
|||
|
||||
boolean reconnect() {
|
||||
reconnectAttempts++;
|
||||
#
|
||||
String clientID = "ESP8266Client-";
|
||||
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");
|
||||
client.publish(willTopic, "1", true);
|
||||
client.subscribe(timeTopic);
|
||||
} else {
|
||||
Serial.print("failed, rc = ");
|
||||
Serial.print(client.state());
|
||||
|
@ -113,6 +115,11 @@ boolean reconnect() {
|
|||
return client.connected();
|
||||
}
|
||||
|
||||
void callback(char* topic, byte* payload, unsigned int length) {
|
||||
Serial.println("Timesignal received");
|
||||
lastTimesignalTime = millis();
|
||||
}
|
||||
|
||||
void setup() {
|
||||
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
|
||||
pinMode(LED_PIN, OUTPUT);
|
||||
|
@ -127,6 +134,8 @@ void setup() {
|
|||
setup_ota();
|
||||
|
||||
client.setServer(mqtt_server, 1883);
|
||||
client.setCallback(callback);
|
||||
|
||||
attachInterrupt(INPUT1, debounceInterrupt1, RISING);
|
||||
attachInterrupt(INPUT2, debounceInterrupt2, RISING);
|
||||
attachInterrupt(INPUT3, debounceInterrupt3, RISING);
|
||||
|
@ -162,6 +171,7 @@ void debounceInterrupt4() {
|
|||
}
|
||||
|
||||
void Interrupt1() {
|
||||
Serial.println("Button 1 pressed");
|
||||
client.publish(outTopic, "1", true);
|
||||
digitalWrite(LED_PIN, 1);
|
||||
led_time = millis();
|
||||
|
@ -169,27 +179,32 @@ void Interrupt1() {
|
|||
}
|
||||
|
||||
void Interrupt2() {
|
||||
Serial.println("Button 2 pressed");
|
||||
client.publish(outTopic, "2", true);
|
||||
digitalWrite(LED_PIN, 1);
|
||||
led_time = millis();
|
||||
}
|
||||
|
||||
void Interrupt3() {
|
||||
Serial.println("Button 3 pressed");
|
||||
client.publish(outTopic, "3", true);
|
||||
digitalWrite(LED_PIN, 1);
|
||||
led_time = millis();
|
||||
}
|
||||
|
||||
void Interrupt4() {
|
||||
Serial.println("Button 4 pressed");
|
||||
client.publish(outTopic, "4", true);
|
||||
digitalWrite(LED_PIN, 1);
|
||||
led_time = millis();
|
||||
}
|
||||
|
||||
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... ");
|
||||
if (WiFi.status() != WL_CONNECTED) {
|
||||
Serial.println("WiFi connection lost. Reconnecting...");
|
||||
|
@ -197,12 +212,13 @@ void loop() {
|
|||
} else {
|
||||
Serial.println("OK");
|
||||
}
|
||||
last_wifi_check_time = now;
|
||||
last_wifi_check_time = millis();
|
||||
}
|
||||
|
||||
if (!client.connected()) {
|
||||
if (now - lastReconnectAttempt > 5000) {
|
||||
lastReconnectAttempt = now;
|
||||
if (millis() - lastReconnectAttempt > 5000) {
|
||||
Serial.println("checking MQTT connection");
|
||||
lastReconnectAttempt = millis();
|
||||
// Attempt to reconnect
|
||||
Serial.println("Attempting MQTT reconnect");
|
||||
if (reconnect()) {
|
||||
|
@ -218,7 +234,7 @@ void loop() {
|
|||
client.loop();
|
||||
}
|
||||
|
||||
if (now - led_time > led_timeout) {
|
||||
if (millis() - led_time > led_timeout) {
|
||||
digitalWrite(LED_PIN, 0);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user