2018-06-23 21:49:11 +02:00
/*
2018-06-24 12:13:46 +02:00
* To upload through terminal you can use : curl - F " image=@firmware.bin " http : //<ip>:8080/update
2018-06-24 12:01:58 +02:00
*
*
2018-06-24 12:03:05 +02:00
* bin file location on windows : C : \ Users \ < your username > \ AppData \ Local \ Temp \ arduino_build_ < id >
2018-06-24 12:01:58 +02:00
*/
2018-06-23 21:49:11 +02:00
# include <ESP8266WiFi.h>
# include <ESP8266WebServer.h>
# include <ESP8266mDNS.h>
# include <ESP8266HTTPUpdateServer.h>
2019-02-03 16:12:19 +01:00
# include <WiFiClient.h>
2023-02-09 16:36:51 +01:00
# include <WiFiManager.h> // WiFiManager from bib manager by tzapu
2019-02-03 16:12:19 +01:00
2022-05-15 22:34:36 +02:00
# include "config.h"
# include "config_user.h"
2022-09-13 10:48:58 +02:00
//*************************************************************************//
2022-05-15 22:34:36 +02:00
ESP8266WebServer httpServer ( WEB_UPDATER_HTTP_PORT ) ;
2018-06-23 21:49:11 +02:00
ESP8266HTTPUpdateServer httpUpdater ;
2019-08-04 12:46:42 +02:00
String _webUpdater_ip = " 127.0.0.1 " ;
String _webUpdater_dev = " unknown " ;
2022-11-15 14:39:21 +01:00
2022-11-24 19:17:21 +01:00
float _webUpdater_sensValues [ VALUES ] ;
2022-05-15 22:34:36 +02:00
2022-09-12 15:26:30 +02:00
# define TR_TD_START_STR "<tr><td>"
2023-02-09 16:36:51 +01:00
# define TR_TD_END_STR "< / td>< / tr>"
# define TD_TD_MID_STR "< / td><td>"
2018-07-11 11:49:58 +02:00
2022-08-28 13:47:10 +02:00
boolean wuValidData = false ;
2022-09-14 20:05:50 +02:00
uint32_t _wifi_reconnect_cnt = 0 ;
2022-09-13 10:48:58 +02:00
//*************************************************************************//
2023-02-09 16:36:51 +01:00
void setupWebUpdater ( String device , String ip ) {
2019-08-04 12:46:42 +02:00
debug ( " Starting WebUpdater... " + ip ) ;
_webUpdater_ip = ip ;
_webUpdater_dev = device ;
2018-06-23 21:49:11 +02:00
httpUpdater . setup ( & httpServer ) ;
2018-07-11 11:49:58 +02:00
2022-05-14 19:38:58 +02:00
httpServer . on ( " / " , showHTMLMain ) ;
2022-11-15 09:46:50 +01:00
# ifndef DISABLE_WIFIMANAGER
2022-05-14 19:38:58 +02:00
httpServer . on ( " /resetWifiManager " , resetWifiManager ) ;
2022-11-15 09:46:50 +01:00
# endif
2022-05-15 22:34:36 +02:00
# ifdef HOMEBRIDGE_WEBSTAT
httpServer . on ( " /hbWebstat " , hb_webstat_send ) ;
# endif
2022-05-24 11:03:52 +02:00
# ifdef DEBUG_WINDSPEED_MEASUREMENT
httpServer . on ( " /measWind " , measureWindspeed ) ;
# endif
2023-02-09 16:24:55 +01:00
# ifdef WEB_RESET
httpServer . on ( " /resetESP " , resetESP ) ;
# endif
2022-09-12 19:31:16 +02:00
# ifdef USE_LOGGER
httpServer . on ( " /showlog " , showLog ) ;
# endif
2018-06-23 21:49:11 +02:00
httpServer . begin ( ) ;
2019-08-04 12:46:42 +02:00
debug ( " HTTPUpdateServer ready! " ) ;
2018-06-23 21:49:11 +02:00
}
2022-09-13 10:48:58 +02:00
//*************************************************************************//
2023-02-09 16:36:51 +01:00
void doWebUpdater ( void ) {
2018-06-23 21:49:11 +02:00
digitalWrite ( D0 , HIGH ) ;
httpServer . handleClient ( ) ;
}
2018-07-11 11:49:58 +02:00
2022-09-14 20:05:50 +02:00
//*************************************************************************//
2022-11-15 14:39:21 +01:00
# ifdef SHOW_SENSOR_DATA_ON_WEBUPDATER_MAIN_PAGE
2023-02-09 16:36:51 +01:00
void sentWindspeed ( float ws ) {
2022-09-14 20:24:43 +02:00
_webUpdater_sensValues [ SENSOR_WINDSPEED ] = ws ;
}
2022-11-15 14:39:21 +01:00
# endif
2022-09-14 20:24:43 +02:00
2023-02-09 16:36:51 +01:00
void setWifiReconnectCnt ( uint32_t wrc ) {
2022-09-14 20:05:50 +02:00
_wifi_reconnect_cnt = wrc ;
}
2022-11-15 14:39:21 +01:00
# ifdef SHOW_SENSOR_DATA_ON_WEBUPDATER_MAIN_PAGE
2023-02-09 16:36:51 +01:00
void setSensorData ( float sensorValues [ ] ) {
for ( uint8_t i = 0 ; i < 5 and wuValidData = = false ; i + + ) {
if ( sensorValues [ i ] ! = 0 and sensorValues [ i ] ! = nanf ( " no value " ) and ( not isnan ( sensorValues [ i ] ) ) ) {
wuValidData = true ; // at least one value is not zero, the data
2022-08-28 13:47:10 +02:00
}
}
2023-02-09 16:36:51 +01:00
for ( uint8_t i = 0 ; i < VALUES ; i + + ) {
if ( sensorValues [ i ] ! = nanf ( " no value " ) and ( not isnan ( sensorValues [ i ] ) ) ) {
2022-09-14 12:42:01 +02:00
// only copy real float values
2022-09-15 10:23:02 +02:00
//debug(String(i) + "=" + String(sensorValues[i]));
2022-09-14 12:42:01 +02:00
_webUpdater_sensValues [ i ] = sensorValues [ i ] ;
}
2018-07-11 11:49:58 +02:00
}
}
2022-11-15 14:39:21 +01:00
# endif
2018-07-11 11:49:58 +02:00
2022-09-13 10:48:58 +02:00
//*************************************************************************//
2023-02-09 16:36:51 +01:00
void showHTMLMain ( void ) {
2022-08-28 13:47:10 +02:00
2024-02-03 14:32:53 +01:00
String message = " <html><head><title>OKO Weatherstation " + String ( VERSION_STRING ) + " - " + String ( _webUpdater_dev ) + " </title> "
" <meta http-equiv= \" refresh \" content= \" 20 \" > "
" </head><body> "
" OKO Weatherstation " + String ( VERSION_STRING ) + " - " + String ( _webUpdater_dev ) + " <br> "
" <br><a href= \" http:// "
2023-02-09 16:36:51 +01:00
+ _webUpdater_ip + " :8080/update \" >firmware update</a><br><br> "
2022-09-12 20:27:42 +02:00
# ifdef USE_LOGGER
2023-02-09 16:36:51 +01:00
" <br><a href= \" http:// "
+ _webUpdater_ip + " :8080/showlog \" >logfile</a><br> "
2022-09-12 20:27:42 +02:00
# endif
# ifdef DEBUG_WINDSPEED_MEASUREMENT
2023-02-09 16:36:51 +01:00
" <br><a href= \" http:// "
+ _webUpdater_ip + " :8080/measWind \" >manual wind measurement</a><br> "
2022-09-12 20:27:42 +02:00
# endif
# ifdef HOMEBRIDGE_WEBSTAT
2023-02-09 16:36:51 +01:00
" <br><a href= \" http:// "
2024-02-03 11:42:49 +01:00
+ _webUpdater_ip + " :8080/hbWebstat \" >homebridge webstat</a><br> "
2022-09-12 20:27:42 +02:00
# endif
2023-02-09 16:24:55 +01:00
# ifdef WEB_RESET
2023-02-09 16:36:51 +01:00
" <br><a href= \" http:// "
+ _webUpdater_ip + " :8080/resetESP \" >reset ESP</a><br> "
2023-02-09 16:24:55 +01:00
# endif
2022-11-15 14:39:21 +01:00
# ifdef SHOW_SENSOR_DATA_ON_WEBUPDATER_MAIN_PAGE
2023-02-09 16:36:51 +01:00
" <br><br><table> " TR_TD_START_STR
2023-02-18 16:58:03 +01:00
+ " temperature " + TD_TD_MID_STR + String ( _webUpdater_sensValues [ SENSOR_TEMPERATURE ] ) + TR_TD_END_STR TR_TD_START_STR + " humidity " + TD_TD_MID_STR + String ( _webUpdater_sensValues [ SENSOR_HUMIDITY ] ) + TR_TD_END_STR TR_TD_START_STR + " light " + TD_TD_MID_STR + String ( _webUpdater_sensValues [ SENSOR_LIGHT ] ) + TR_TD_END_STR TR_TD_START_STR + " windspeed " + TD_TD_MID_STR + String ( _webUpdater_sensValues [ SENSOR_WINDSPEED ] ) + TR_TD_END_STR TR_TD_START_STR + " pressure " + TD_TD_MID_STR + String ( _webUpdater_sensValues [ SENSOR_PRESSURE ] ) + TR_TD_END_STR TR_TD_START_STR + " batvoltage " + TD_TD_MID_STR + String ( _webUpdater_sensValues [ SENSOR_VOLTAGE ] ) + TR_TD_END_STR TR_TD_START_STR + " millis " + TD_TD_MID_STR + String ( millis ( ) ) + TR_TD_END_STR TR_TD_START_STR + " valid " + TD_TD_MID_STR + String ( wuValidData ) + TR_TD_END_STR TR_TD_START_STR + " wifi rssi " + TD_TD_MID_STR + WiFi . RSSI ( ) + TR_TD_END_STR TR_TD_START_STR + " wifi rec cnt " + TD_TD_MID_STR + String ( _wifi_reconnect_cnt ) + TR_TD_END_STR " </table> "
2022-11-15 14:39:21 +01:00
# endif
2022-11-15 09:46:50 +01:00
# ifndef DISABLE_WIFIMANAGER
2023-02-09 16:36:51 +01:00
" <br><br><br><br><br><a href= \" http:// "
+ _webUpdater_ip + " :8080/resetWifiManager \" >reset WiFi Manager</a><br> "
2022-11-15 09:46:50 +01:00
# endif
2023-02-09 16:36:51 +01:00
" </body></html> " ;
2018-07-11 11:49:58 +02:00
httpServer . send ( 200 , " text/html " , message ) ;
}
2022-05-14 19:38:58 +02:00
2022-09-13 10:48:58 +02:00
//*************************************************************************//
2022-09-12 15:26:30 +02:00
# ifdef HOMEBRIDGE_WEBSTAT
2023-02-09 16:36:51 +01:00
void hb_webstat_send ( void ) {
2023-02-18 16:58:03 +01:00
String msg = hb_ws_msg_start +
hb_ws_msg_temp + String ( _webUpdater_sensValues [ SENSOR_TEMPERATURE ] , 2 ) + " , " +
hb_ws_msg_humi + String ( _webUpdater_sensValues [ SENSOR_HUMIDITY ] , 2 ) + " , " +
hb_ws_msg_light + String ( _webUpdater_sensValues [ SENSOR_LIGHT ] , 0 ) + " , " + // The light level for the homebridge-http-lux2 plugin is only able to parse integer values
hb_ws_msg_windspeed + String ( _webUpdater_sensValues [ SENSOR_WINDSPEED ] , 2 ) + " , " +
hb_ws_msg_pressure + String ( _webUpdater_sensValues [ SENSOR_PRESSURE ] , 2 ) + " , " +
hb_ws_msg_voltage + String ( _webUpdater_sensValues [ SENSOR_VOLTAGE ] , 2 ) + " , " +
hb_ws_msg_timestamp + String ( millis ( ) ) + " , " +
hb_ws_msg_valid + String ( wuValidData ) +
hb_ws_msg_end ;
2022-05-16 13:34:33 +02:00
httpServer . send ( 200 , " text/html " , msg ) ;
2022-05-15 22:34:36 +02:00
}
2022-09-12 15:26:30 +02:00
# endif
2022-05-15 22:34:36 +02:00
2022-09-13 10:48:58 +02:00
//*************************************************************************//
2023-02-09 16:24:55 +01:00
# ifdef WEB_RESET
2023-02-09 16:36:51 +01:00
void resetESP ( ) {
2023-02-09 16:24:55 +01:00
2024-02-03 14:32:53 +01:00
String message = " <html><head><title>OKO Weatherstation " + String ( VERSION_STRING ) + " - " + String ( _webUpdater_dev ) + " - reset WiFi manager</title> "
" <meta http-equiv= \" refresh \" content= \" 20 \" > "
" </head><body> "
" OKO Weatherstation " + String ( VERSION_STRING ) + " - " + String ( _webUpdater_dev ) + " <br> "
" Rebooting...<br> "
" </body></html> " ;
2023-02-09 16:24:55 +01:00
httpServer . send ( 200 , " text/html " , message ) ;
delay ( 5000 ) ;
2023-02-09 16:36:51 +01:00
# ifdef ENABLE_WATCHDOG
// loop endless, watchdog will reset the device
while ( 1 = = 1 ) { }
# endif
2023-02-09 16:24:55 +01:00
// manual reset after restart is required
ESP . restart ( ) ;
}
# endif
2022-11-15 09:46:50 +01:00
# ifndef DISABLE_WIFIMANAGER
2023-02-09 16:36:51 +01:00
void resetWifiManager ( ) {
2022-05-14 19:38:58 +02:00
2024-02-03 14:32:53 +01:00
String message = " <html><head><title>OKO Weatherstation " + String ( VERSION_STRING ) + " - " + String ( _webUpdater_dev ) + " - reset WiFi manager</title> "
" <meta http-equiv= \" refresh \" content= \" 20 \" > "
" </head><body> "
" Reset WifiManager config.<br> "
" OKO Weatherstation " + String ( VERSION_STRING ) + " - " + String ( _webUpdater_dev ) + " <br> "
" Rebooting...<br> "
" </body></html> " ;
2022-05-14 19:38:58 +02:00
httpServer . send ( 200 , " text/html " , message ) ;
// Erase WiFi Credentials, enable, compile, flash, disable and reflash.
2022-05-15 22:34:36 +02:00
WiFiManager wifiManager ;
wifiManager . resetSettings ( ) ;
2022-05-14 19:38:58 +02:00
delay ( 5000 ) ;
2022-05-14 19:54:51 +02:00
2023-02-09 16:36:51 +01:00
# ifdef ENABLE_WATCHDOG
// loop endless, watchdog will reset the device
while ( 1 = = 1 ) { }
# endif
2022-05-15 22:34:36 +02:00
// manual reset after restart is required
2022-05-14 19:38:58 +02:00
ESP . restart ( ) ;
}
2022-05-24 11:03:52 +02:00
2022-11-15 09:46:50 +01:00
# endif
2022-09-13 10:48:58 +02:00
//*************************************************************************//
2022-09-12 15:26:30 +02:00
# ifdef DEBUG_WINDSPEED_MEASUREMENT
# ifdef SENSOR_WIND
2023-02-09 16:36:51 +01:00
void measureWindspeed ( ) {
2022-05-24 11:03:52 +02:00
// read from windspeed sensorSTATUS_LED_PIN
digitalWrite ( STATUS_LED_PIN , HIGH ) ;
debug ( " Starting windspeed measurement " ) ;
float tmp_windspeed = wind_speed ( ) ;
digitalWrite ( STATUS_LED_PIN , LOW ) ;
2024-02-03 14:32:53 +01:00
String message = " <html><head><title>OKO Weatherstation " + String ( VERSION_STRING ) + " - " + String ( _webUpdater_dev ) + " - manual wind measurement</title> "
" </head><body> "
" OKO Weatherstation " + String ( VERSION_STRING ) + " - " + String ( _webUpdater_dev ) + " <br> "
" Windsensor measurement result: "
2023-02-09 16:36:51 +01:00
+ String ( tmp_windspeed ) + " <br> "
" </body></html> " ;
2022-05-24 11:03:52 +02:00
httpServer . send ( 200 , " text/html " , message ) ;
}
2022-09-12 15:26:30 +02:00
# endif
# endif
2022-09-12 19:31:16 +02:00
2022-09-13 10:48:58 +02:00
//*************************************************************************//
2022-09-12 19:31:16 +02:00
# ifdef USE_LOGGER
2022-09-13 13:17:38 +02:00
# define LOGFILE_SIZE 25
2022-09-12 19:31:16 +02:00
String logfile [ LOGFILE_SIZE ] ;
uint16_t logger_pos = 0 ;
2023-02-09 16:36:51 +01:00
void logdata ( String s ) {
2022-09-12 19:31:16 +02:00
logfile [ logger_pos ] = s ;
logger_pos + + ;
2023-02-09 16:36:51 +01:00
if ( logger_pos > LOGFILE_SIZE - 1 ) {
for ( uint16_t i = 1 ; i < LOGFILE_SIZE ; i + + ) {
logfile [ i - 1 ] = logfile [ i ] ; // overwrite previous element with current element
2022-09-12 19:31:16 +02:00
}
2023-02-09 16:36:51 +01:00
logger_pos - - ; // reduce the position in the log
2022-09-12 19:31:16 +02:00
}
}
2023-02-09 16:36:51 +01:00
void showLog ( ) {
2024-02-03 14:32:53 +01:00
String message = " <html><head><title>OKO Weatherstation " + String ( VERSION_STRING ) + " - " + String ( _webUpdater_dev ) + " - logfile</title> "
" <meta http-equiv= \" refresh \" content= \" " + String ( WEB_LOGGER_UPDATE_S ) + " \" > "
" </head><body> "
" OKO Weatherstation " + String ( VERSION_STRING ) + " - " + String ( _webUpdater_dev ) + " <br> "
" Logfile data:<br> " ;
2022-09-12 19:31:16 +02:00
2023-02-09 16:36:51 +01:00
for ( uint16_t lp = 0 ; lp < logger_pos ; lp + + ) {
2022-09-12 19:31:16 +02:00
message = message + logfile [ lp ] + " <br> " ;
}
message = message + " </body></html> " ;
httpServer . send ( 200 , " text/html " , message ) ;
}
# endif
2022-09-13 10:48:58 +02:00
//*************************************************************************//