output config as a web endpoint & added station id

master
Kenneth Barbour 2020-06-06 23:17:49 -04:00
parent 63a8bce948
commit c9ba9f6bb9
6 changed files with 181 additions and 2 deletions

File diff suppressed because one or more lines are too long

View File

@ -47,6 +47,12 @@ const configurables = [
{
group: "Networking",
properties: [
{
name: "station_id",
title: "Station Identifier",
value: Config.properties.station_id,
placeholder: "Unidentified",
},
{
name: "mdns_hostname",
title: "mDNS Hostname",

107
src/config.cpp 100644
View File

@ -0,0 +1,107 @@
#include "config.h"
#include <Arduino.h>
#include <EEPROM.h>
cUWeatherConfig Config;
const uint8_t CONFIG_SIG[2] = {0xAB, 0xCD};
const uint8_t CONFIG_VERSION = 1;
uint8_t CONFIG_EEPROM_ADDR = 0;
void cUWeatherConfig::begin()
{
if (!load()) {
Serial.println("Unable to find config. Loading defaults");
loadDefaults();
save();
}
}
void cUWeatherConfig::loadDefaults()
{
memset(&_data, 0, sizeof(uweather_config_t));
_data.sig[0] = CONFIG_SIG[0];
_data.sig[1] = CONFIG_SIG[1];
_data.version = CONFIG_VERSION;
_data.flags = 1; //TODO what does this mean;
}
bool cUWeatherConfig::load()
{
EEPROM.get(CONFIG_EEPROM_ADDR, _data);
// check signature
if (_data.sig[0] != CONFIG_SIG[0] || _data.sig[1] != CONFIG_SIG[1]) {
return false; // signature mismatch
}
// check version
if (_data.version > CONFIG_VERSION) {
// version is too new, something is wrong
Serial.print("ERROR: loaded config version ");
Serial.print(_data.version);
Serial.print(", expected ");
Serial.println(CONFIG_VERSION);
return false;
} else if (_data.version < CONFIG_VERSION) {
// TODO: bring config up to date, if possible
_data.version = CONFIG_VERSION;
Serial.print("Upgraded to config version ");
Serial.println(CONFIG_VERSION);
}
return true;
}
bool cUWeatherConfig::save()
{
EEPROM.put(CONFIG_EEPROM_ADDR, _data);
return true;
}
size_t cUWeatherConfig::print(Stream& out)
{
uint8_t buff[256];
size_t len = 0;
sprintf((char*)buff,
"{\"station_id\": \"%s\", \"mdns_hostname\": \"%s\", \"ntp_host\": \"%s\", \"mqtt_host\": \"%s\", \"enable_http\": %s }",
(char*) _data.station_id,
(char*) _data.mdns_hostname,
(char*) _data.ntp_host,
(char*) _data.mqtt_host,
(_data.flags && 1) ?"true":"false"
);
len += out.print((char*) buff);
return len;
}
bool cUWeatherConfig::setStationId(const char* buff)
{
if (strlen(buff) >= 16) return false;
strncpy((char*)_data.station_id, buff, 16);
return true;
}
bool cUWeatherConfig::setMDNSHostname(const char* buff)
{
if (strlen(buff) >= 16) return false;
strncpy((char*)_data.mdns_hostname, buff, 16);
return true;
}
bool cUWeatherConfig::setNTPHost(const char* buff)
{
if (strlen(buff) >= 32) return false;
strncpy((char*)_data.ntp_host, buff, 32);
return true;
}
bool cUWeatherConfig::setMQTTHost(const char* buff)
{
if (strlen(buff) >= 32) return false;
strncpy((char*)_data.mqtt_host, buff, 32);
return true;
}

38
src/config.h 100644
View File

@ -0,0 +1,38 @@
#ifndef _UWEATHER_CONFIG_H_
#define _UWEATHER_CONFIG_H_
#include <Arduino.h>
typedef struct uweather_config_t {
uint8_t sig[2];
uint8_t version;
uint8_t station_id[16];
uint8_t mdns_hostname[16];
uint8_t ntp_host[32];
uint8_t mqtt_host[32];
uint8_t flags;
};
class cUWeatherConfig
{
public:
uweather_config_t _data;
void begin();
void loadDefaults();
bool load();
bool save();
size_t print(Stream& );
bool enableHttp(bool);
bool isHttpEnabled();
bool setStationId(const char*);
bool setMDNSHostname(const char*);
bool setNTPHost(const char*);
bool setMQTTHost(const char*);
};
extern cUWeatherConfig Config;
#endif /** _UWEATHER_CONFIG_H_ include guard */

View File

@ -5,6 +5,7 @@
#include "web.h"
#include "update.h"
#include "bme280.h"
#include "config.h"
#ifndef SERIAL_BAUD
#define SERIAL_BAUD 74880
@ -35,10 +36,14 @@ void setup()
// Print out some system information to the serial port
uweather_print_version_info(Serial);
// Load configuration
Serial.print( F("Loading config"));
Config.begin();
// Attempt to connect to WiFi
Serial.print( F("Attempting to connect to WiFi ..."));
WiFi.begin();
WiFi.begin("The 517","Walnut Giraffe Thunderstorm"); // TODO [nocommit]
uint8_t connectResult = WiFi.waitForConnectResult();
if (connectResult != WL_CONNECTED) {
Serial.println( F("Unable to connect"));

View File

@ -4,6 +4,7 @@
#include <ESP8266WiFi.h>
#include <FS.h>
#include <QueryString.h>
#include "config.h"
/**
* How long to wait after the OK to shutdown before no longer
@ -126,6 +127,26 @@ void handle_wifi_state(HttpRequest& req, HttpResponse& res)
}
}
void handle_config(HttpRequest& req, HttpResponse& res)
{
res.headers.set("Content-Type","application/json");
Config.print(content);
return;
}
void handle_config_set(HttpRequest& req, HttpResponse& res)
{
if (req.getMessageLength() > 128)
return client_error("Message is too long", res);
const char* key = req.getUrl()+8;
Serial.println(req.getMessage());
if (strcmp("station_id", key)) {
Serial.println("Saving station id");
// strncpy(config.station_id, req.getMessage(), 15);
//config_save();
}
}
/**
* Handle any request for a static asset
* TODO: remove WString.h dependency
@ -176,6 +197,8 @@ void serve_static(const char* path, HttpResponse& res)
Route routes[] = {
{ GET, "/", handle_index},
{ GET, "/setup", handle_setup},
{ GET, "/config", handle_config},
{ GET | PUT | POST | DELETE, "/config/#", handle_config_set},
{ GET, "/wifi/scan", handle_wifi_scan},
{ GET | POST | DELETE, "/wifi", handle_wifi_state},
{ GET | POST, "/firmware", handle_firmware},