Skip to content

Commit fd78ecf

Browse files
committed
Improve timing code and remove overflow protection
I had no clue my code was already "safe" from millis' overflow. "Improve" my timing code a bit and remove overflow protection(auto restart)
1 parent a1c5247 commit fd78ecf

File tree

1 file changed

+5
-12
lines changed

1 file changed

+5
-12
lines changed

ESP8266_SmartExtension.ino

+5-12
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Ticker wifiReconnectTimer;
2929
byte buttonState0 = 0;
3030
byte lastButtonState0 = 0;
3131

32-
unsigned long switch_prevMillis = 0, heartbeat_prevMillis = 0, currentMillis;
32+
unsigned long switch_prevMillis = 0, heartbeat_prevMillis = 0;
3333

3434
#define switchInterval 200
3535
#define heartbeatInterval 15000
@@ -71,24 +71,17 @@ void loop()
7171
{
7272
ArduinoOTA.handle();
7373

74-
currentMillis = millis();
75-
76-
if (currentMillis - switch_prevMillis >= switchInterval)
74+
if (millis() - switch_prevMillis >= switchInterval)
7775
{
78-
switch_prevMillis = currentMillis;
76+
switch_prevMillis = millis();
7977
switchpolling();
8078
}
8179

82-
if (currentMillis - heartbeat_prevMillis >= heartbeatInterval)
80+
if (millis() - heartbeat_prevMillis >= heartbeatInterval)
8381
{
84-
heartbeat_prevMillis = currentMillis;
82+
heartbeat_prevMillis = millis();
8583
mqttClient.publish("noderelay/heartbeat", MQTT_QOS, false, "Hi"); //publish to topic
8684
}
87-
88-
if(currentMillis > 4094967296) //overflow protection
89-
{
90-
ESP.restart();
91-
}
9285
}
9386

9487
void switchpolling()

0 commit comments

Comments
 (0)