weatherstation/firmware/webUpdater.ino

206 lines
6.1 KiB
Arduino
Raw Normal View History

/*
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
*/
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPUpdateServer.h>
#include <WiFiClient.h>
#include "config.h"
#include "config_user.h"
#ifdef USE_WIFIMANAGER
#include <WiFiManager.h> // WiFiManager from bib manager
#else
unsigned int _wifi_connect_cnt = 0;
#endif
ESP8266WebServer httpServer(WEB_UPDATER_HTTP_PORT);
ESP8266HTTPUpdateServer httpUpdater;
String _webUpdater_ip = "127.0.0.1";
String _webUpdater_dev = "unknown";
float _webUpdater_sensValues[VALUES];
const String hb_ws_msg_start = "{";
const String hb_ws_msg_temp = "\"temperature\": ";
const String hb_ws_msg_humi = "\"humidity\": ";
const String hb_ws_msg_light = "\"lightlevel\": ";
const String hb_ws_msg_windspeed = "\"windspeed\": ";
const String hb_ws_msg_pressure = "\"pressure\": ";
const String hb_ws_msg_timestamp = "\"timestamp\": ";
const String hb_ws_msg_valid = "\"valid\": ";
const String hb_ws_msg_end = "}";
#define TR_TD_START_STR "<tr><td>"
#define TR_TD_END_STR "</td></tr>"
#define TD_TD_MID_STR "</td><td>"
2018-07-11 11:49:58 +02:00
boolean wuValidData = false;
void setupWebUpdater(String device, String ip)
{
debug("Starting WebUpdater... " + ip);
_webUpdater_ip = ip;
_webUpdater_dev = device;
httpUpdater.setup(&httpServer);
2018-07-11 11:49:58 +02:00
httpServer.on("/", showHTMLMain);
#ifdef USE_WIFIMANAGER
httpServer.on("/resetWifiManager", resetWifiManager);
#endif
#ifdef HOMEBRIDGE_WEBSTAT
httpServer.on("/hbWebstat", hb_webstat_send);
#endif
#ifdef DEBUG_WINDSPEED_MEASUREMENT
#ifdef SENSOR_WIND
httpServer.on("/measWind", measureWindspeed);
#endif
#endif
2018-07-11 11:49:58 +02:00
httpServer.begin();
debug("HTTPUpdateServer ready!");
}
void doWebUpdater(void)
{
digitalWrite(D0, HIGH);
httpServer.handleClient();
}
2018-07-11 11:49:58 +02:00
#ifdef USE_WIFIMANAGER
void setSensorData(float sensorValues[])
#else
void setSensorData(float sensorValues[], unsigned int wifi_conn_cnt)
#endif
{
for (uint8_t i = 0; i < 5 and wuValidData == false; i++)
{
2022-08-29 12:42:49 +02:00
if (sensorValues[i] != 0)
{
wuValidData = true; // at least one value is not zero, the data is valid
}
}
for (uint8_t i = 0; i < VALUES; i++) {
_webUpdater_sensValues[i] = sensorValues[i];
2018-07-11 11:49:58 +02:00
}
#ifndef USE_WIFIMANAGER
_wifi_connect_cnt = wifi_conn_cnt;
#endif
2018-07-11 11:49:58 +02:00
}
void showHTMLMain(void)
{
String message = "<html><head><title>OKO Weatherstation - " + String(_webUpdater_dev) + "</title>"
"<meta http-equiv=\"refresh\" content=\"20\">"
"</head><body>"
"<br><a href=\"http://" + _webUpdater_ip + ":8080/update\">firmware update</a><br><br>"
2018-07-11 11:49:58 +02:00
"<table>"
TR_TD_START_STR + "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_BAT_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
#ifndef USE_WIFIMANAGER
TR_TD_START_STR + "wifi conn cnt" + TD_TD_MID_STR + _wifi_connect_cnt + TR_TD_END_STR
#endif
2018-07-11 11:49:58 +02:00
"</table>"
"</body></html>";
httpServer.send(200, "text/html", message);
}
#ifdef HOMEBRIDGE_WEBSTAT
void hb_webstat_send(void)
{
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 +
2022-05-16 14:00:35 +02:00
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) +
", " +
2022-05-19 19:41:36 +02:00
hb_ws_msg_pressure +
String(_webUpdater_sensValues[SENSOR_PRESSURE], 2) +
", " +
hb_ws_msg_timestamp +
String(millis()) +
", " +
hb_ws_msg_valid +
String(wuValidData) +
hb_ws_msg_end;
httpServer.send(200, "text/html", msg);
}
#endif
#ifdef USE_WIFIMANAGER
void resetWifiManager()
{
String message = "<html><head><title>OKO Weatherstation - " + String(_webUpdater_dev) + "</title>"
"<meta http-equiv=\"refresh\" content=\"20\">"
"</head><body>"
"Reset WifiManager config.<br>"
"Rebooting...<br>"
"</body></html>";
httpServer.send(200, "text/html", message);
// Erase WiFi Credentials, enable, compile, flash, disable and reflash.
WiFiManager wifiManager;
wifiManager.resetSettings();
delay(5000);
// manual reset after restart is required
ESP.restart();
}
#endif
#ifdef DEBUG_WINDSPEED_MEASUREMENT
#ifdef SENSOR_WIND
void measureWindspeed()
{
// 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);
String message = "<html><head><title>OKO Weatherstation - " + String(_webUpdater_dev) + "</title>"
"</head><body>"
2022-05-24 11:07:51 +02:00
"Windsensor measurement result: " + String(tmp_windspeed) + "<br>"
"</body></html>";
httpServer.send(200, "text/html", message);
}
#endif
#endif