77#include < WiFi.h>
88#include < HTTPClient.h>
99#include < WebServer.h>
10+ #include < ESPmDNS.h>
1011#include < AutoConnect.h>
12+ #include " HTTPUpdateServer.h"
1113#include < AutoConnectCredential.h>
1214#include < ArduinoJson.h>
1315
1618
1719// Time is in milliseconds
1820#define LED_TICKER 33
21+ #define LED_BUILTIN 2
1922#define BUTTON_PIN 32
2023#define DEFAULT_UPDATE_INTERVAL 60000
2124#define LIVE_SENSOR_INTERVAL 1000
2225#define SETTINGS_FILE " /settings.txt"
23- #define DATA_TRANSMISSION_TIMEOUT 20 // arbitrary number
26+ #define DATA_TRANSMISSION_TIMEOUT 32 // arbitrary number
2427#define REBOOT_BUTTON_HOLD_DURATION 3000
2528#define FACTORY_RESET_BUTTON_HOLD_DURATION 10000
2629
2730
28-
2931byte modules[MAX_SENSORS]; // array with address listings of connected sensor modules
3032AsyncDelay delay_sensor_update; // delay timer for asynchronous update interval
3133AsyncDelay delay_sensor_view; // 1 second delay for real time sensor viewing
@@ -44,6 +46,8 @@ String nodeLEDSetting = "On";
4446unsigned long currentUpdateRate = DEFAULT_UPDATE_INTERVAL;
4547
4648WebServer server; // HTTP server to serve web UI
49+ HTTPUpdateServer updateServer (true ); // OTA update handler, true param is for serial debug
50+ AutoConnectAux update (" /update" , " Update" );
4751AutoConnect Portal (server); // AutoConnect handler object
4852AutoConnectConfig portalConfig (" MainModuleAP" , " 12345678" );
4953
@@ -130,17 +134,17 @@ void asyncBlink(unsigned long ms = 0)
130134
131135// Button input checking function
132136void checkButton (){
133- static unsigned long pushedDownTime = NULL ;
134- if (pushedDownTime == NULL && digitalRead (BUTTON_PIN) == LOW){ // Button being pressed
137+ static unsigned long pushedDownTime = 0 ;
138+ if (pushedDownTime == 0 && digitalRead (BUTTON_PIN) == LOW){ // Button being pressed
135139 pushedDownTime = millis ();
136140
137- }else if (pushedDownTime != NULL && digitalRead (BUTTON_PIN) == LOW){ // Pressing the button, change the LED light according to the pressing time.
141+ }else if (pushedDownTime != 0 && digitalRead (BUTTON_PIN) == LOW){ // Pressing the button, change the LED light according to the pressing time.
138142 unsigned int pressingDuration = millis () - pushedDownTime;
139143 if (pressingDuration > REBOOT_BUTTON_HOLD_DURATION && pressingDuration < FACTORY_RESET_BUTTON_HOLD_DURATION){
140144 asyncBlink (FACTORY_RESET_BUTTON_HOLD_DURATION - pressingDuration);
141145 }
142146 }
143- else if (pushedDownTime != NULL && digitalRead (BUTTON_PIN) == HIGH ){ // Button released, check the pressed duration and peform action
147+ else if (pushedDownTime != 0 && digitalRead (BUTTON_PIN) == HIGH ){ // Button released, check the pressed duration and peform action
144148 unsigned int pressingDuration = millis () - pushedDownTime;
145149
146150 if (pressingDuration > FACTORY_RESET_BUTTON_HOLD_DURATION){
@@ -153,7 +157,7 @@ void checkButton(){
153157 ESP.restart ();
154158 }
155159
156- pushedDownTime = NULL ;
160+ pushedDownTime = 0 ;
157161 }
158162}
159163
@@ -618,6 +622,10 @@ void setup()
618622 server.on (" /save_settings" , handle_SaveSettings);
619623 server.on (" /getJSON" , handle_getSensorJSON);
620624
625+ // setup update server
626+ updateServer.setup (&server);
627+ Serial.println (" OTA update server started." );
628+
621629 // setup AutoConnect with a configuration
622630 portalConfig.title = " Main Module v1.0" ;
623631 portalConfig.apid = " MainModule-" + String ((uint32_t )(ESP.getEfuseMac () >> 32 ), HEX);
@@ -628,6 +636,8 @@ void setup()
628636 portalConfig.tickerPort = LED_TICKER;
629637 portalConfig.tickerOn = HIGH;
630638 Portal.config (portalConfig);
639+ // add update page aux
640+ Portal.join ({update});
631641
632642 // load custom page JSON and build pages into memory
633643 if (!Portal.load (customPageJSON))
@@ -653,10 +663,24 @@ void setup()
653663 Serial.print (" IP: " );
654664 Serial.println (WiFi.localIP ());
655665 Serial.println ();
666+ // initialize MDNS
667+ String mdnshostname = nodeName;
668+ mdnshostname.toLowerCase ();
669+ if (MDNS.begin (mdnshostname.c_str ()))
670+ {
671+ MDNS.addService (" http" ," tcp" ,80 );
672+ Serial.println (" MDNS transponder started." );
673+ Serial.println (" Access at http://" + mdnshostname + " .local" );
674+ }
675+ else
676+ {
677+ Serial.println (" MDNS Initialization failed. Service will not be available." );
678+ }
679+
656680 }
657681 else
658682 {
659- Serial.println (" Connection failed, rebooting." );
683+ Serial.println (" Portal initialization failed, rebooting." );
660684 ESP.restart ();
661685 }
662686
0 commit comments