Dokumentation um Webinterface, Einstellungen und OTA-Update erweitert
This commit is contained in:
1 parent
1014df8ec7
commit
0d2ff764ea
13 files changed
+531
-85
No files matched your search
+39
-14
@@ -11,12 +11,19 @@ bfs::Ms4525do pres;
|
||||
Adafruit_BMP085 bmp;
|
||||
|
||||
float pressure_avg[10];
|
||||
float basicaltitude;
|
||||
void init_2ndi2c(){
|
||||
Wire1.begin(dp_sda, dp_scl);
|
||||
float basicaltitude = 0.0f;
|
||||
bool pressure_sensor_ready = false;
|
||||
|
||||
bool init_2ndi2c(){
|
||||
if (!Wire1.begin(dp_sda, dp_scl))
|
||||
{
|
||||
Serial.println("Error starting I2C");
|
||||
return false;
|
||||
}
|
||||
Wire1.setClock(400000);
|
||||
return true;
|
||||
}
|
||||
void init_airspeed_sensor(void)
|
||||
bool init_airspeed_sensor(void)
|
||||
{
|
||||
|
||||
/*
|
||||
@@ -27,10 +34,9 @@ void init_airspeed_sensor(void)
|
||||
if (!pres.Begin())
|
||||
{
|
||||
Serial.println("Error communicating with sensor");
|
||||
while (1)
|
||||
{
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int get_airspeed_kmh(void)
|
||||
@@ -51,10 +57,22 @@ float get_airspeed(void)
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
void init_pressure_sensor(void)
|
||||
bool init_pressure_sensor(void)
|
||||
{
|
||||
bmp.begin(BMP085_ULTRAHIGHRES, &Wire1);
|
||||
if (!bmp.begin(BMP085_ULTRAHIGHRES, &Wire1))
|
||||
{
|
||||
Serial.println("Error communicating with pressure sensor");
|
||||
return false;
|
||||
}
|
||||
basicaltitude=bmp.readPressure();
|
||||
if (basicaltitude <= 0.0f)
|
||||
{
|
||||
Serial.println("Invalid initial pressure");
|
||||
return false;
|
||||
}
|
||||
for (float &sample : pressure_avg)
|
||||
sample = basicaltitude;
|
||||
pressure_sensor_ready = true;
|
||||
Serial.print("Pressure = ");
|
||||
Serial.print(basicaltitude);
|
||||
Serial.println(" Pa");
|
||||
@@ -64,21 +82,28 @@ void init_pressure_sensor(void)
|
||||
Serial.print("Altitude = ");
|
||||
Serial.print(get_altitude(basicaltitude));
|
||||
Serial.println(" meters");
|
||||
return true;
|
||||
}
|
||||
|
||||
float get_pressure()
|
||||
{
|
||||
if (!pressure_sensor_ready)
|
||||
return 0.0f;
|
||||
|
||||
float pressure;
|
||||
pressure = bmp.readPressure();
|
||||
memcpy(pressure_avg, &pressure_avg[1], sizeof(pressure_avg) - sizeof(int));
|
||||
if (pressure <= 0.0f)
|
||||
return pressure_avg[9];
|
||||
|
||||
memmove(pressure_avg, &pressure_avg[1], sizeof(pressure_avg) - sizeof(pressure_avg[0]));
|
||||
pressure_avg[9] = pressure;
|
||||
return pressure;
|
||||
}
|
||||
|
||||
float get_pressure_avg(void)
|
||||
{
|
||||
float total = 0.0f;
|
||||
int i;
|
||||
float total;
|
||||
for (i = 0; i <10; i++)
|
||||
{
|
||||
total = total + pressure_avg[i];
|
||||
@@ -97,14 +122,14 @@ float get_vario_avg(void)
|
||||
float vario;
|
||||
|
||||
int i;
|
||||
float total;
|
||||
float total = 0.0f;
|
||||
for (i = 0; i <9; i++)
|
||||
{
|
||||
int n=i+1;
|
||||
vario = get_altitude(pressure_avg[n]) - get_altitude(pressure_avg[i]);
|
||||
total = total + vario;
|
||||
}
|
||||
return (total / 10);
|
||||
return (total / 9);
|
||||
}
|
||||
|
||||
float get_altitude(float pressure)
|
||||
@@ -115,5 +140,5 @@ float get_altitude(float pressure)
|
||||
}
|
||||
|
||||
float get_altitude_diff(void){
|
||||
return bmp.readPressure() - basicaltitude;
|
||||
return get_altitude(pressure_avg[9]) - get_altitude(basicaltitude);
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
#ifndef WINDENTELEMETRY_AIRSPEED_H
|
||||
#define WINDENTELEMETRY_AIRSPEED_H
|
||||
|
||||
void init_2ndi2c(void);
|
||||
void init_airspeed_sensor(void);
|
||||
bool init_2ndi2c(void);
|
||||
bool init_airspeed_sensor(void);
|
||||
|
||||
int get_airspeed_kmh(void);
|
||||
/*!
|
||||
@@ -10,7 +10,7 @@ int get_airspeed_kmh(void);
|
||||
*/
|
||||
float get_airspeed(void);
|
||||
|
||||
void init_pressure_sensor(void);
|
||||
bool init_pressure_sensor(void);
|
||||
float get_pressure(void);
|
||||
float get_pressure_adv(void);
|
||||
float get_altitude(float pressure);
|
||||
|
||||
@@ -0,0 +1,242 @@
|
||||
#include "webinterface.h"
|
||||
#include <Arduino.h>
|
||||
#include <Preferences.h>
|
||||
#include <Update.h>
|
||||
#include <WebServer.h>
|
||||
#include <WiFi.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
constexpr char default_access_point_password[] = "telemetry";
|
||||
constexpr char settings_namespace[] = "telemetry";
|
||||
constexpr char default_callsign[] = "D1234";
|
||||
constexpr int default_min_speed = 80;
|
||||
constexpr int default_max_speed = 150;
|
||||
constexpr unsigned int default_active_time_seconds = 90;
|
||||
|
||||
String default_access_point_name()
|
||||
{
|
||||
#ifdef DEVICE_SAILPLANE
|
||||
return String("Sailplane-") + default_callsign;
|
||||
#else
|
||||
return "Winde-Telemetry";
|
||||
#endif
|
||||
}
|
||||
|
||||
WebServer server(80);
|
||||
Preferences preferences;
|
||||
TelemetrySettings *active_settings = nullptr;
|
||||
bool restart_after_update = false;
|
||||
bool update_failed = false;
|
||||
bool access_point_restart_pending = false;
|
||||
|
||||
String html_escape(const String &value)
|
||||
{
|
||||
String escaped = value;
|
||||
escaped.replace("&", "&");
|
||||
escaped.replace("<", "<");
|
||||
escaped.replace(">", ">");
|
||||
escaped.replace("\"", """);
|
||||
escaped.replace("'", "'");
|
||||
return escaped;
|
||||
}
|
||||
|
||||
void send_page(const String &message = String())
|
||||
{
|
||||
String page;
|
||||
page.reserve(2600);
|
||||
page += F("<!doctype html><html lang='de'><head><meta charset='utf-8'>"
|
||||
"<meta name='viewport' content='width=device-width,initial-scale=1'>"
|
||||
"<title>Winden-Telemetry</title><style>"
|
||||
"body{font-family:sans-serif;max-width:38rem;margin:2rem auto;padding:0 1rem}"
|
||||
"label{display:block;margin-top:1rem}input{box-sizing:border-box;width:100%;padding:.6rem;font-size:1rem}"
|
||||
"button{margin-top:1rem;padding:.65rem 1rem;font-size:1rem}"
|
||||
".msg{padding:.6rem;background:#e5f4e8}small{color:#555}"
|
||||
"</style></head><body><h1>Winden-Telemetry</h1>");
|
||||
if (message.length() > 0)
|
||||
{
|
||||
page += F("<p class='msg'>");
|
||||
page += html_escape(message);
|
||||
page += F("</p>");
|
||||
}
|
||||
page += F("<form method='post' action='/settings'>"
|
||||
"<label>WLAN-Name<input name='ap_name' maxlength='32' required value='");
|
||||
page += html_escape(active_settings->access_point_name);
|
||||
page += F("'></label><label>WLAN-Passwort<input name='ap_password' type='password' minlength='8' maxlength='63' required value='");
|
||||
page += html_escape(active_settings->access_point_password);
|
||||
page += F("'></label><label>Callsign<input name='callsign' maxlength='16' required value='");
|
||||
page += html_escape(active_settings->callsign);
|
||||
page += F("'></label><label>Min Speed (km/h)<input name='min' type='number' min='0' max='300' required value='");
|
||||
page += String(active_settings->min_speed);
|
||||
page += F("'></label><label>Max Speed (km/h)<input name='max' type='number' min='1' max='300' required value='");
|
||||
page += String(active_settings->max_speed);
|
||||
page += F("'></label><label>Uebertragungszeit (Sekunden)<input name='active_time' type='number' min='1' max='600' required value='");
|
||||
page += String(active_settings->active_time_seconds);
|
||||
page += F("'></label><button type='submit'>Speichern</button></form>"
|
||||
"<h2>Firmware aktualisieren</h2><form method='post' action='/update' enctype='multipart/form-data'>"
|
||||
"<input name='firmware' type='file' accept='.bin' required><button type='submit'>Upload starten</button>"
|
||||
"</form><p><small>Access Point: ");
|
||||
page += html_escape(active_settings->access_point_name);
|
||||
page += F("</small></p></body></html>");
|
||||
server.send(200, "text/html; charset=utf-8", page);
|
||||
}
|
||||
|
||||
bool parse_settings(TelemetrySettings &settings)
|
||||
{
|
||||
String callsign = server.arg("callsign");
|
||||
String access_point_name = server.arg("ap_name");
|
||||
String access_point_password = server.arg("ap_password");
|
||||
int min_speed = server.arg("min").toInt();
|
||||
int max_speed = server.arg("max").toInt();
|
||||
int active_time_seconds = server.arg("active_time").toInt();
|
||||
callsign.trim();
|
||||
access_point_name.trim();
|
||||
access_point_password.trim();
|
||||
if (access_point_name.length() == 0 || access_point_name.length() > 32 || access_point_password.length() < 8 || access_point_password.length() > 63 || callsign.length() == 0 || callsign.length() > 16 || min_speed < 0 || max_speed > 300 ||
|
||||
min_speed >= max_speed || active_time_seconds < 1 || active_time_seconds > 600)
|
||||
return false;
|
||||
|
||||
settings.callsign = callsign;
|
||||
settings.access_point_name = access_point_name;
|
||||
settings.access_point_password = access_point_password;
|
||||
settings.min_speed = min_speed;
|
||||
settings.max_speed = max_speed;
|
||||
settings.active_time_seconds = active_time_seconds;
|
||||
return true;
|
||||
}
|
||||
|
||||
void handle_root()
|
||||
{
|
||||
send_page();
|
||||
}
|
||||
|
||||
void handle_settings()
|
||||
{
|
||||
TelemetrySettings updated = *active_settings;
|
||||
if (!parse_settings(updated))
|
||||
{
|
||||
server.send(400, "text/plain; charset=utf-8", "Ungueltige Einstellungen");
|
||||
return;
|
||||
}
|
||||
|
||||
active_settings->callsign = updated.callsign;
|
||||
active_settings->access_point_name = updated.access_point_name;
|
||||
active_settings->access_point_password = updated.access_point_password;
|
||||
active_settings->min_speed = updated.min_speed;
|
||||
active_settings->max_speed = updated.max_speed;
|
||||
active_settings->active_time_seconds = updated.active_time_seconds;
|
||||
preferences.putString("callsign", active_settings->callsign);
|
||||
preferences.putString("ap_name", active_settings->access_point_name);
|
||||
preferences.putString("ap_password", active_settings->access_point_password);
|
||||
preferences.putInt("min", active_settings->min_speed);
|
||||
preferences.putInt("max", active_settings->max_speed);
|
||||
preferences.putUInt("active_time", active_settings->active_time_seconds);
|
||||
send_page("Einstellungen gespeichert.");
|
||||
access_point_restart_pending = true;
|
||||
}
|
||||
|
||||
void handle_update_upload()
|
||||
{
|
||||
HTTPUpload &upload = server.upload();
|
||||
if (upload.status == UPLOAD_FILE_START)
|
||||
{
|
||||
restart_after_update = false;
|
||||
update_failed = false;
|
||||
Serial.printf("Firmware-Upload: %s\n", upload.filename.c_str());
|
||||
if (!Update.begin(UPDATE_SIZE_UNKNOWN))
|
||||
{
|
||||
update_failed = true;
|
||||
Update.printError(Serial);
|
||||
}
|
||||
}
|
||||
else if (upload.status == UPLOAD_FILE_WRITE)
|
||||
{
|
||||
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize)
|
||||
{
|
||||
update_failed = true;
|
||||
Update.printError(Serial);
|
||||
}
|
||||
}
|
||||
else if (upload.status == UPLOAD_FILE_END)
|
||||
{
|
||||
if (Update.end(true))
|
||||
{
|
||||
restart_after_update = true;
|
||||
Serial.printf("Firmware-Upload abgeschlossen: %u Bytes\n", upload.totalSize);
|
||||
}
|
||||
else
|
||||
{
|
||||
update_failed = true;
|
||||
Update.printError(Serial);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void handle_update_result()
|
||||
{
|
||||
if (!update_failed && restart_after_update)
|
||||
{
|
||||
server.send(200, "text/plain; charset=utf-8", "Upload erfolgreich. Neustart...");
|
||||
if (restart_after_update)
|
||||
{
|
||||
delay(500);
|
||||
ESP.restart();
|
||||
}
|
||||
}
|
||||
else
|
||||
server.send(500, "text/plain; charset=utf-8", "Firmware-Upload fehlgeschlagen");
|
||||
}
|
||||
} // namespace
|
||||
|
||||
bool webinterface_begin(TelemetrySettings &settings)
|
||||
{
|
||||
active_settings = &settings;
|
||||
preferences.begin(settings_namespace, false);
|
||||
settings.callsign = preferences.getString("callsign", default_callsign);
|
||||
settings.access_point_name = preferences.getString("ap_name", default_access_point_name());
|
||||
settings.access_point_password = preferences.getString("ap_password", default_access_point_password);
|
||||
settings.min_speed = preferences.getInt("min", default_min_speed);
|
||||
settings.max_speed = preferences.getInt("max", default_max_speed);
|
||||
settings.active_time_seconds = preferences.getUInt("active_time", default_active_time_seconds);
|
||||
if (settings.access_point_name.length() == 0 || settings.access_point_name.length() > 32 || settings.access_point_password.length() < 8 || settings.access_point_password.length() > 63 || settings.callsign.length() == 0 || settings.min_speed < 0 || settings.min_speed >= settings.max_speed ||
|
||||
settings.max_speed > 300 || settings.active_time_seconds < 1 || settings.active_time_seconds > 600)
|
||||
{
|
||||
settings.callsign = default_callsign;
|
||||
settings.access_point_name = default_access_point_name();
|
||||
settings.access_point_password = default_access_point_password;
|
||||
settings.min_speed = default_min_speed;
|
||||
settings.max_speed = default_max_speed;
|
||||
settings.active_time_seconds = default_active_time_seconds;
|
||||
}
|
||||
|
||||
WiFi.mode(WIFI_AP);
|
||||
bool ap_started = WiFi.softAP(settings.access_point_name, settings.access_point_password);
|
||||
if (!ap_started)
|
||||
return false;
|
||||
|
||||
server.on("/", HTTP_GET, handle_root);
|
||||
server.on("/settings", HTTP_POST, handle_settings);
|
||||
server.on("/update", HTTP_POST, handle_update_result, handle_update_upload);
|
||||
server.begin();
|
||||
Serial.print("Webinterface: http://");
|
||||
Serial.println(WiFi.softAPIP());
|
||||
return true;
|
||||
}
|
||||
|
||||
void webinterface_handle(void)
|
||||
{
|
||||
server.handleClient();
|
||||
if (access_point_restart_pending)
|
||||
{
|
||||
access_point_restart_pending = false;
|
||||
WiFi.softAPdisconnect(true);
|
||||
delay(100);
|
||||
if (!WiFi.softAP(active_settings->access_point_name, active_settings->access_point_password))
|
||||
Serial.println("Access Point konnte nicht neu gestartet werden");
|
||||
}
|
||||
}
|
||||
|
||||
const TelemetrySettings &webinterface_settings(void)
|
||||
{
|
||||
return *active_settings;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#ifndef WINDENTELEMETRY_WEBINTERFACE_H
|
||||
#define WINDENTELEMETRY_WEBINTERFACE_H
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
struct TelemetrySettings
|
||||
{
|
||||
String callsign;
|
||||
String access_point_name;
|
||||
String access_point_password;
|
||||
int min_speed;
|
||||
int max_speed;
|
||||
unsigned int active_time_seconds;
|
||||
};
|
||||
|
||||
bool webinterface_begin(TelemetrySettings &settings);
|
||||
void webinterface_handle(void);
|
||||
const TelemetrySettings &webinterface_settings(void);
|
||||
|
||||
#endif
|
||||
+3
-10
@@ -8,15 +8,14 @@ void init_servo(void)
|
||||
{
|
||||
myservo.setPeriodHertz(50);
|
||||
myservo.attach(servoPin);
|
||||
set_tacho(0, 70, 150);
|
||||
}
|
||||
|
||||
void set_tacho(int speed, int speed_min, int speed_max)
|
||||
{
|
||||
int val = map(speed, -25, 220, 0, 155);
|
||||
#ifdef DEBUG
|
||||
Serial.print("Tacho Mapping: ")
|
||||
Serial.println(val);
|
||||
Serial.print("Tacho Mapping: ");
|
||||
Serial.println(val);
|
||||
#endif
|
||||
myservo.write(val);
|
||||
pixels.clear();
|
||||
@@ -72,11 +71,5 @@ void init_pixel(void)
|
||||
|
||||
int setoffsetpixel(int pixel)
|
||||
{
|
||||
int newpixel;
|
||||
newpixel = pixel + pixeloffset;
|
||||
if (newpixel >= NUMPIXELS)
|
||||
{
|
||||
newpixel = newpixel - NUMPIXELS;
|
||||
}
|
||||
return newpixel;
|
||||
return (pixel + pixeloffset + NUMPIXELS) % NUMPIXELS;
|
||||
}
|
||||
@@ -2,6 +2,12 @@
|
||||
#define WINDENTELEMETRY_CONFIG_H
|
||||
|
||||
#define LORA_FREQ 868.0
|
||||
#define TELEMETRY_CALLSIGN "D1234"
|
||||
#define TELEMETRY_SEND_INTERVAL_MS 1000UL
|
||||
#define TELEMETRY_ACTIVE_TIME_MS 90000UL
|
||||
#define TELEMETRY_LOW_SPEED_KMH 30
|
||||
#define default_min_tow_speed 80
|
||||
#define default_max_tow_speed 150
|
||||
#ifdef HELTEC_WIFI_LORA_32_V3
|
||||
// SX1262 on HELTEC_WIFI_LORA_32_V3 has the following connections:
|
||||
// NSS pin: 8
|
||||
@@ -27,8 +33,6 @@
|
||||
/**difference pressure**/
|
||||
#define dp_scl 20
|
||||
#define dp_sda 19
|
||||
#define default_min_tow_speed 80
|
||||
#define default_max_tow_speed 150
|
||||
#endif
|
||||
|
||||
#ifdef DEVICE_WINDE
|
||||
|
||||
Reference in new issue
Block a user