mirror of
https://github.com/UtilitechAS/amsreader-firmware.git
synced 2026-02-11 02:41:04 +00:00
Merge branch 'master' into low_power
This commit is contained in:
@@ -3,15 +3,16 @@
|
||||
Stream* HanConfigAp::debugger;
|
||||
|
||||
bool HanConfigAp::hasConfig() {
|
||||
return config.hasConfig();
|
||||
return config->hasConfig();
|
||||
}
|
||||
|
||||
void HanConfigAp::setup(int accessPointButtonPin, Stream* debugger)
|
||||
void HanConfigAp::setup(int accessPointButtonPin, configuration* config, Stream* debugger)
|
||||
{
|
||||
this->debugger = debugger;
|
||||
this->config = config;
|
||||
|
||||
// Test if we're missing configuration
|
||||
if (!config.hasConfig())
|
||||
if (!config->hasConfig())
|
||||
{
|
||||
print("No config. We're booting as AP. Look for SSID ");
|
||||
println(this->AP_SSID);
|
||||
@@ -20,8 +21,7 @@ void HanConfigAp::setup(int accessPointButtonPin, Stream* debugger)
|
||||
else
|
||||
{
|
||||
// Load the configuration
|
||||
config.load();
|
||||
if (this->debugger) config.print(this->debugger);
|
||||
if (this->debugger) config->print(this->debugger);
|
||||
|
||||
if (accessPointButtonPin != INVALID_BUTTON_PIN)
|
||||
{
|
||||
|
||||
@@ -24,15 +24,16 @@
|
||||
|
||||
class HanConfigAp {
|
||||
public:
|
||||
void setup(int accessPointButtonPin, Stream* debugger);
|
||||
void setup(int accessPointButtonPin, configuration* config, Stream* debugger);
|
||||
bool loop();
|
||||
bool hasConfig();
|
||||
configuration config;
|
||||
bool isActivated = false;
|
||||
|
||||
private:
|
||||
const char* AP_SSID = "AMS2MQTT";
|
||||
|
||||
configuration* config;
|
||||
|
||||
// DNS server
|
||||
const byte DNS_PORT = 53;
|
||||
DNSServer dnsServer;
|
||||
|
||||
@@ -24,11 +24,18 @@ bool configuration::save()
|
||||
address += saveString(address, ssid);
|
||||
address += saveString(address, ssidPassword);
|
||||
address += saveByte(address, meterType);
|
||||
address += saveString(address, mqtt);
|
||||
address += saveInt(address, mqttPort);
|
||||
address += saveString(address, mqttClientID);
|
||||
address += saveString(address, mqttPublishTopic);
|
||||
address += saveString(address, mqttSubscribeTopic);
|
||||
|
||||
|
||||
if(mqttHost) {
|
||||
address += saveBool(address, true);
|
||||
address += saveString(address, mqttHost);
|
||||
address += saveInt(address, mqttPort);
|
||||
address += saveString(address, mqttClientID);
|
||||
address += saveString(address, mqttPublishTopic);
|
||||
address += saveString(address, mqttSubscribeTopic);
|
||||
} else {
|
||||
address += saveBool(address, false);
|
||||
}
|
||||
|
||||
if (isSecure()) {
|
||||
address += saveBool(address, true);
|
||||
@@ -46,6 +53,7 @@ bool configuration::save()
|
||||
}
|
||||
|
||||
address += saveInt(address, fuseSize);
|
||||
address += saveInt(address, distSys);
|
||||
|
||||
bool success = EEPROM.commit();
|
||||
EEPROM.end();
|
||||
@@ -59,13 +67,13 @@ bool configuration::load()
|
||||
int address = EEPROM_CONFIG_ADDRESS;
|
||||
bool success = false;
|
||||
|
||||
ssid = (char*)String("").c_str();
|
||||
ssidPassword = (char*)String("").c_str();
|
||||
ssid = 0;
|
||||
ssidPassword = 0;
|
||||
meterType = (byte)0;
|
||||
mqtt = (char*)String("").c_str();
|
||||
mqttClientID = (char*)String("").c_str();
|
||||
mqttPublishTopic = (char*)String("").c_str();
|
||||
mqttSubscribeTopic = (char*)String("").c_str();
|
||||
mqttHost = 0;
|
||||
mqttClientID = 0;
|
||||
mqttPublishTopic = 0;
|
||||
mqttSubscribeTopic = 0;
|
||||
mqttUser = 0;
|
||||
mqttPass = 0;
|
||||
mqttPort = 1883;
|
||||
@@ -73,25 +81,30 @@ bool configuration::load()
|
||||
authUser = 0;
|
||||
authPass = 0;
|
||||
fuseSize = 0;
|
||||
distSys = 0;
|
||||
|
||||
EEPROM.begin(EEPROM_SIZE);
|
||||
int cs = EEPROM.read(address);
|
||||
if (cs >= 71)
|
||||
if (cs == EEPROM_CHECK_SUM)
|
||||
{
|
||||
address++;
|
||||
|
||||
address += readString(address, &ssid);
|
||||
address += readString(address, &ssidPassword);
|
||||
address += readByte(address, &meterType);
|
||||
address += readString(address, &mqtt);
|
||||
address += readInt(address, &mqttPort);
|
||||
address += readString(address, &mqttClientID);
|
||||
address += readString(address, &mqttPublishTopic);
|
||||
address += readString(address, &mqttSubscribeTopic);
|
||||
|
||||
bool mqtt = false;
|
||||
address += readBool(address, &mqtt);
|
||||
if(mqtt) {
|
||||
address += readString(address, &mqttHost);
|
||||
address += readInt(address, &mqttPort);
|
||||
address += readString(address, &mqttClientID);
|
||||
address += readString(address, &mqttPublishTopic);
|
||||
address += readString(address, &mqttSubscribeTopic);
|
||||
}
|
||||
|
||||
bool secure = false;
|
||||
address += readBool(address, &secure);
|
||||
|
||||
if (secure)
|
||||
{
|
||||
address += readString(address, &mqttUser);
|
||||
@@ -103,9 +116,6 @@ bool configuration::load()
|
||||
mqttPass = 0;
|
||||
}
|
||||
|
||||
success = true;
|
||||
}
|
||||
if(cs >= 72) {
|
||||
address += readByte(address, &authSecurity);
|
||||
if (authSecurity > 0) {
|
||||
address += readString(address, &authUser);
|
||||
@@ -114,9 +124,11 @@ bool configuration::load()
|
||||
authUser = 0;
|
||||
authPass = 0;
|
||||
}
|
||||
}
|
||||
if(cs >= 73) {
|
||||
|
||||
address += readInt(address, &fuseSize);
|
||||
address += readByte(address, &distSys);
|
||||
|
||||
success = true;
|
||||
}
|
||||
EEPROM.end();
|
||||
return success;
|
||||
@@ -177,11 +189,13 @@ void configuration::print(Stream* debugger)
|
||||
debugger->printf("ssid: %s\r\n", this->ssid);
|
||||
debugger->printf("ssidPassword: %s\r\n", this->ssidPassword);
|
||||
debugger->printf("meterType: %i\r\n", this->meterType);
|
||||
debugger->printf("mqtt: %s\r\n", this->mqtt);
|
||||
debugger->printf("mqttPort: %i\r\n", this->mqttPort);
|
||||
debugger->printf("mqttClientID: %s\r\n", this->mqttClientID);
|
||||
debugger->printf("mqttPublishTopic: %s\r\n", this->mqttPublishTopic);
|
||||
debugger->printf("mqttSubscribeTopic: %s\r\n", this->mqttSubscribeTopic);
|
||||
if(this->mqttHost) {
|
||||
debugger->printf("mqttHost: %s\r\n", this->mqttHost);
|
||||
debugger->printf("mqttPort: %i\r\n", this->mqttPort);
|
||||
debugger->printf("mqttClientID: %s\r\n", this->mqttClientID);
|
||||
debugger->printf("mqttPublishTopic: %s\r\n", this->mqttPublishTopic);
|
||||
debugger->printf("mqttSubscribeTopic: %s\r\n", this->mqttSubscribeTopic);
|
||||
}
|
||||
|
||||
if (this->isSecure())
|
||||
{
|
||||
@@ -197,6 +211,7 @@ void configuration::print(Stream* debugger)
|
||||
debugger->printf("authPass: %s\r\n", this->authPass);
|
||||
}
|
||||
debugger->printf("fuseSize: %i\r\n", this->fuseSize);
|
||||
debugger->printf("distSys: %i\r\n", this->distSys);
|
||||
|
||||
debugger->println("-----------------------------------------------");
|
||||
}
|
||||
@@ -236,7 +251,7 @@ int configuration::readString(int pAddress, char* pString[])
|
||||
int configuration::saveString(int pAddress, char* pString)
|
||||
{
|
||||
int address = 0;
|
||||
int length = strlen(pString) + 1;
|
||||
int length = pString ? strlen(pString) + 1 : 0;
|
||||
EEPROM.put(pAddress + address, length);
|
||||
address++;
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ class configuration {
|
||||
public:
|
||||
char* ssid;
|
||||
char* ssidPassword;
|
||||
char* mqtt;
|
||||
char* mqttHost;
|
||||
int mqttPort;
|
||||
char* mqttClientID;
|
||||
char* mqttPublishTopic;
|
||||
@@ -30,6 +30,7 @@ public:
|
||||
char* authPass;
|
||||
|
||||
int fuseSize;
|
||||
byte distSys;
|
||||
|
||||
bool hasConfig();
|
||||
bool isSecure();
|
||||
@@ -41,7 +42,7 @@ protected:
|
||||
|
||||
private:
|
||||
const int EEPROM_SIZE = 512;
|
||||
const byte EEPROM_CHECK_SUM = 73; // Used to check if config is stored. Change if structure changes
|
||||
const byte EEPROM_CHECK_SUM = 75; // Used to check if config is stored. Change if structure changes
|
||||
const int EEPROM_CONFIG_ADDRESS = 0;
|
||||
|
||||
int saveString(int pAddress, char* pString);
|
||||
|
||||
Reference in New Issue
Block a user