Send status message every interval (10min). Reboot once per day (hack).
This commit is contained in:
		
							parent
							
								
									ef26d2e297
								
							
						
					
					
						commit
						49a9b45ab9
					
				
							
								
								
									
										15
									
								
								src/main.cpp
									
									
									
									
									
								
							
							
						
						
									
										15
									
								
								src/main.cpp
									
									
									
									
									
								
							| 
						 | 
					@ -10,12 +10,15 @@
 | 
				
			||||||
#define DEBOUNCEMS 15
 | 
					#define DEBOUNCEMS 15
 | 
				
			||||||
#define BOARD "ESP32"
 | 
					#define BOARD "ESP32"
 | 
				
			||||||
#define SERVICE "LEDsActionButton"
 | 
					#define SERVICE "LEDsActionButton"
 | 
				
			||||||
 | 
					#define STATUSINTERVAL 600000
 | 
				
			||||||
 | 
					
 | 
				
			||||||
String toggleURL = "http://moon.basement.lan/l/actionbutton.php?state=";
 | 
					String toggleURL = "http://moon.basement.lan/l/actionbutton.php?state=";
 | 
				
			||||||
String statusURL = "http://moon.basement.lan/l/boardstatus.php";
 | 
					String statusURL = "http://moon.basement.lan/l/boardstatus.php";
 | 
				
			||||||
unsigned short buttonState = HIGH, lastButtonState = HIGH;
 | 
					unsigned short buttonState = HIGH, lastButtonState = HIGH;
 | 
				
			||||||
 | 
					unsigned long lastStatusTime = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void sendToggleMessage(bool state) {
 | 
					void sendToggleMessage(bool state) {
 | 
				
			||||||
 | 
					  Serial.println("Action button triggered");
 | 
				
			||||||
  char webMsg[255] = "";
 | 
					  char webMsg[255] = "";
 | 
				
			||||||
  strcat(webMsg, toggleURL.c_str());
 | 
					  strcat(webMsg, toggleURL.c_str());
 | 
				
			||||||
  strcat(webMsg, (state) ? "true" : "false");
 | 
					  strcat(webMsg, (state) ? "true" : "false");
 | 
				
			||||||
| 
						 | 
					@ -25,7 +28,8 @@ void sendToggleMessage(bool state) {
 | 
				
			||||||
  http.end();
 | 
					  http.end();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void sendDeviceStatus() {
 | 
					unsigned long sendDeviceStatus() {
 | 
				
			||||||
 | 
					  Serial.println("Sending device status");
 | 
				
			||||||
  WiFiClient client;
 | 
					  WiFiClient client;
 | 
				
			||||||
  HTTPClient http;
 | 
					  HTTPClient http;
 | 
				
			||||||
  http.begin(client, statusURL.c_str());
 | 
					  http.begin(client, statusURL.c_str());
 | 
				
			||||||
| 
						 | 
					@ -33,6 +37,7 @@ void sendDeviceStatus() {
 | 
				
			||||||
  String httpData = "mac=" + WiFi.macAddress() + "&board=" + BOARD + "&firmware=" + FIRMWARE + "&service=" + SERVICE;
 | 
					  String httpData = "mac=" + WiFi.macAddress() + "&board=" + BOARD + "&firmware=" + FIRMWARE + "&service=" + SERVICE;
 | 
				
			||||||
  int httpResponseCode = http.POST(httpData);
 | 
					  int httpResponseCode = http.POST(httpData);
 | 
				
			||||||
  http.end();
 | 
					  http.end();
 | 
				
			||||||
 | 
					  return millis();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void setup() {
 | 
					void setup() {
 | 
				
			||||||
| 
						 | 
					@ -41,7 +46,7 @@ void setup() {
 | 
				
			||||||
  Serial.println("MAC Address: " + WiFi.macAddress());
 | 
					  Serial.println("MAC Address: " + WiFi.macAddress());
 | 
				
			||||||
  WiFiManager wifiManager;
 | 
					  WiFiManager wifiManager;
 | 
				
			||||||
  wifiManager.autoConnect("LEDsActionButton");
 | 
					  wifiManager.autoConnect("LEDsActionButton");
 | 
				
			||||||
  sendDeviceStatus();
 | 
					  lastStatusTime = sendDeviceStatus();
 | 
				
			||||||
  ArduinoOTA.onStart([]() {
 | 
					  ArduinoOTA.onStart([]() {
 | 
				
			||||||
    String type;
 | 
					    String type;
 | 
				
			||||||
    if ( ArduinoOTA.getCommand() == U_FLASH )
 | 
					    if ( ArduinoOTA.getCommand() == U_FLASH )
 | 
				
			||||||
| 
						 | 
					@ -68,6 +73,12 @@ unsigned long lastDebounceTime = 0;
 | 
				
			||||||
bool toggleState = false;
 | 
					bool toggleState = false;
 | 
				
			||||||
bool debouncing = false;
 | 
					bool debouncing = false;
 | 
				
			||||||
void loop() {
 | 
					void loop() {
 | 
				
			||||||
 | 
					  if ( millis() > (lastStatusTime + STATUSINTERVAL) ) {
 | 
				
			||||||
 | 
					    lastStatusTime = sendDeviceStatus();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  if ( millis() > 86400000 ) {
 | 
				
			||||||
 | 
					    ESP.restart();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
  unsigned short buttonCheck = digitalRead(BUTTONPIN);
 | 
					  unsigned short buttonCheck = digitalRead(BUTTONPIN);
 | 
				
			||||||
  if ( buttonCheck != lastButtonState ) {
 | 
					  if ( buttonCheck != lastButtonState ) {
 | 
				
			||||||
    debouncing = true;
 | 
					    debouncing = true;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user