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>
|
2022-05-14 19:54:51 +02:00
|
|
|
#include <WiFiManager.h> // WiFiManager from bib manager
|
2019-02-03 16:12:19 +01:00
|
|
|
|
2022-05-15 22:34:36 +02:00
|
|
|
#include "config.h"
|
|
|
|
#include "config_user.h"
|
|
|
|
|
|
|
|
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-05-15 22:34:36 +02:00
|
|
|
float _webUpdater_sensValues[VALUES];
|
|
|
|
|
|
|
|
String hb_ws_msg_start = "{";
|
|
|
|
String hb_ws_msg_temp = "\"temperature\": ";
|
|
|
|
String hb_ws_msg_humi = "\"humidity\": ";
|
2022-05-16 11:01:06 +02:00
|
|
|
String hb_ws_msg_light = "\"lightlevel\": ";
|
2022-05-15 22:34:36 +02:00
|
|
|
String hb_ws_msg_windspeed = "\"windspeed\": ";
|
2022-05-19 19:41:36 +02:00
|
|
|
String hb_ws_msg_pressure = "\"pressure\": ";
|
|
|
|
String hb_ws_msg_timestamp = "\"timestamp\": ";
|
2022-05-15 22:34:36 +02:00
|
|
|
String hb_ws_msg_end = "}";
|
2018-07-11 11:49:58 +02:00
|
|
|
|
2019-08-04 12:46:42 +02:00
|
|
|
void setupWebUpdater(String device, String ip)
|
2018-06-23 21:49:11 +02:00
|
|
|
{
|
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);
|
|
|
|
httpServer.on("/resetWifiManager", resetWifiManager);
|
2022-05-15 22:34:36 +02:00
|
|
|
#ifdef HOMEBRIDGE_WEBSTAT
|
|
|
|
httpServer.on("/hbWebstat", hb_webstat_send);
|
|
|
|
#endif
|
2018-07-11 11:49:58 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void doWebUpdater(void)
|
|
|
|
{
|
|
|
|
digitalWrite(D0, HIGH);
|
|
|
|
httpServer.handleClient();
|
|
|
|
}
|
2018-07-11 11:49:58 +02:00
|
|
|
|
2022-05-15 22:34:36 +02:00
|
|
|
void setSensorData(float sensorValues[])
|
|
|
|
{
|
2019-08-04 12:46:42 +02:00
|
|
|
for (uint8_t i = 0; i < VALUES; i++) {
|
|
|
|
_webUpdater_sensValues[i] = sensorValues[i];
|
2018-07-11 11:49:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-15 22:34:36 +02:00
|
|
|
void showHTMLMain(void)
|
|
|
|
{
|
2019-08-04 12:46:42 +02:00
|
|
|
String message = "<html><head><title>OKO Weatherstation - " + String(_webUpdater_dev) + "</title>"
|
2018-07-11 12:09:29 +02:00
|
|
|
"<meta http-equiv=\"refresh\" content=\"20\">"
|
|
|
|
"</head><body>"
|
2019-08-04 12:46:42 +02:00
|
|
|
"<br><a href=\"http://" + _webUpdater_ip + ":8080/update\">firmware update</a><br><br>"
|
2018-07-11 11:49:58 +02:00
|
|
|
"<table>"
|
2019-08-04 12:46:42 +02:00
|
|
|
"<tr><td>temperature</td><td>" + String(_webUpdater_sensValues[SENSOR_TEMPERATURE]) + "</td></tr>"
|
|
|
|
"<tr><td>humidity</td><td>" + String(_webUpdater_sensValues[SENSOR_HUMIDITY]) + "</td></tr>"
|
|
|
|
"<tr><td>light</td><td>" + String(_webUpdater_sensValues[SENSOR_LIGHT]) + "</td></tr>"
|
|
|
|
"<tr><td>windspeed</td><td>" + String(_webUpdater_sensValues[SENSOR_WINDSPEED]) + "</td></tr>"
|
|
|
|
"<tr><td>pressure</td><td>" + String(_webUpdater_sensValues[SENSOR_PRESSURE]) + "</td></tr>"
|
|
|
|
"<tr><td>batvoltage</td><td>" + String(_webUpdater_sensValues[SENSOR_BAT_VOLTAGE]) + "</td></tr>"
|
2018-07-11 11:49:58 +02:00
|
|
|
"</table>"
|
|
|
|
"</body></html>";
|
|
|
|
|
|
|
|
httpServer.send(200, "text/html", message);
|
|
|
|
}
|
2022-05-14 19:38:58 +02:00
|
|
|
|
2022-05-15 22:34:36 +02:00
|
|
|
void hb_webstat_send(void)
|
|
|
|
{
|
2022-05-16 13:34:33 +02:00
|
|
|
unsigned int timestamp = 0;
|
|
|
|
for (int i = 0; i < VALUES; i++)
|
|
|
|
{
|
|
|
|
timestamp += int(_webUpdater_sensValues[i]) + millis();
|
|
|
|
}
|
|
|
|
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
|
2022-05-16 13:34:33 +02:00
|
|
|
", " +
|
|
|
|
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 +
|
2022-05-16 13:34:33 +02:00
|
|
|
String(timestamp) +
|
|
|
|
hb_ws_msg_end;
|
|
|
|
|
|
|
|
httpServer.send(200, "text/html", msg);
|
2022-05-15 22:34:36 +02:00
|
|
|
}
|
|
|
|
|
2022-05-14 19:38:58 +02:00
|
|
|
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.
|
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
|
|
|
|
2022-05-15 22:34:36 +02:00
|
|
|
// manual reset after restart is required
|
2022-05-14 19:38:58 +02:00
|
|
|
ESP.restart();
|
|
|
|
}
|