diff --git a/WifiControlSensor/EEPROM.h b/WifiControlSensor/EEPROM.h index 3dc1730..663173d 100644 --- a/WifiControlSensor/EEPROM.h +++ b/WifiControlSensor/EEPROM.h @@ -1,12 +1,23 @@ #pragma once -int EepromSaveConfig(uint8_t bootMode, String ssid, String password, String host, - String mqttServer, String mqttUser, String mqttPasswd, - int mqttPort, int ip_config, uint32_t ip, uint32_t gw, - uint32_t mask, uint32_t dns, uint32_t dns2); -int EepromSaveBootMode(uint8_t bootMode); -void EepromReadConfig(uint8_t &bootMode, char **ssid, char **password, char **host, - char **mqttServer, char **mqttUser, char **mqttPasswd, - int &mqttPort, int &ip_config, uint32_t &ip, uint32_t &gw, - uint32_t &mask, uint32_t &dns, uint32_t &dns2); +typedef struct productConfig_t { + uint8_t bootMode; + char *ssid; + char *password; + char *host; + char *mqttServer; + char *mqttUser; + char *mqttPasswd; + int mqttPort; + int ip_mode; + uint32_t ip; + uint32_t gw; + uint32_t mask; + uint32_t dns; + uint32_t dns2; +} productConfig; + +int EepromSaveConfig(productConfig &config); +int EepromSaveBootMode(uint8_t bootMode); +void EepromReadConfig(productConfig &config); diff --git a/WifiControlSensor/EEPROM.ino b/WifiControlSensor/EEPROM.ino index 3fd567c..7ef05be 100644 --- a/WifiControlSensor/EEPROM.ino +++ b/WifiControlSensor/EEPROM.ino @@ -8,19 +8,16 @@ char eeprom[CONFIG_EEPROM_SIZE]; -int EepromSaveConfig(uint8_t bootMode, String ssid, String password, String host, - String mqttServer, String mqttUser, String mqttPasswd, - int mqttPort, int ip_config, uint32_t ip, uint32_t gw, - uint32_t mask, uint32_t dns, uint32_t dns2) { +int EepromSaveConfig(productConfig &config) { String eeprom; - eeprom = String(bootMode) + ";" + ssid + ";" + password + ";" - + host + ";" + mqttServer + ";" - + mqttUser + ";" + mqttPasswd + ";" - + String(mqttPort) + ";" - + String(ip_config) + ";" - + ip + ";" + gw + ";" + mask + ";" - + dns + ";" + dns2 + ";"; + eeprom = String(config.bootMode) + ";" + config.ssid + ";" + config.password + ";" + + config.host + ";" + config.mqttServer + ";" + + config.mqttUser + ";" + config.mqttPasswd + ";" + + String(config.mqttPort) + ";" + + String(config.ip_mode) + ";" + + config.ip + ";" + config.gw + ";" + config.mask + ";" + + config.dns + ";" + config.dns2 + ";"; if (eeprom.length() > CONFIG_EEPROM_SIZE ) return -EMSGSIZE; @@ -59,10 +56,7 @@ void readConfElement(char** element, int &i) { } -void EepromReadConfig(uint8_t &bootMode, char **ssid, char **password, char **host, - char **mqttServer, char **mqttUser, char **mqttPasswd, - int &mqttPort, int &ip_config, uint32_t &ip, uint32_t &gw, - uint32_t &mask, uint32_t &dns, uint32_t &dns2) { +void EepromReadConfig(productConfig &config) { int i = 2; @@ -70,34 +64,34 @@ void EepromReadConfig(uint8_t &bootMode, char **ssid, char **password, char **ho char *tmpString; if (boot == '1') { - bootMode = BOOTMODE_NORMAL; + config.bootMode = BOOTMODE_NORMAL; } else if (boot == '2') { - bootMode = BOOTMODE_OTA; + config.bootMode = BOOTMODE_OTA; } else { //Do not need to parse EEPROM when not configured - bootMode = BOOTMODE_SETUP; + config.bootMode = BOOTMODE_SETUP; return; } - readConfElement(ssid, i); - readConfElement(password, i); - readConfElement(host, i); - readConfElement(mqttServer, i); - readConfElement(mqttUser, i); - readConfElement(mqttPasswd, i); + readConfElement(&config.ssid, i); + readConfElement(&config.password, i); + readConfElement(&config.host, i); + readConfElement(&config.mqttServer, i); + readConfElement(&config.mqttUser, i); + readConfElement(&config.mqttPasswd, i); readConfElement(&tmpString, i); - mqttPort = atoi(tmpString); + config.mqttPort = atoi(tmpString); readConfElement(&tmpString, i); - ip_config = atoi(tmpString); + config.ip_mode = atoi(tmpString); readConfElement(&tmpString, i); - ip = atoi(tmpString); + config.ip = atoi(tmpString); readConfElement(&tmpString, i); - gw = atoi(tmpString); + config.gw = atoi(tmpString); readConfElement(&tmpString, i); - mask = atoi(tmpString); + config.mask = atoi(tmpString); readConfElement(&tmpString, i); - dns = atoi(tmpString); + config.dns = atoi(tmpString); readConfElement(&tmpString, i); - dns2 = atoi(tmpString); + config.dns2 = atoi(tmpString); } diff --git a/WifiControlSensor/WebServer.ino b/WifiControlSensor/WebServer.ino index e3ed495..f9a0335 100644 --- a/WifiControlSensor/WebServer.ino +++ b/WifiControlSensor/WebServer.ino @@ -9,7 +9,7 @@ void WebHandleRoot() { server.send(200, "text/html", "" - "

You are connected to " + String(hostName) + "

" + "

You are connected to " + String(conf.host) + "

" "
" "Sensors" #ifdef CONFIG_ENABLE_BMP180 @@ -65,49 +65,33 @@ void WebBuildSSIDList(String &datalist){ } void WebHandleSetup() { - uint8_t mode; - char *confSsid = ""; - char *confPassword = ""; - char *confHost = ""; - char *mqttServer = ""; - char *mqttUser = ""; - char *mqttPasswd = ""; - int mqttPort = 1883; - int ip_config = 0; - uint32_t ip = 0; - uint32_t gw = 0; - uint32_t mask = 0; - uint32_t dns = 0; - uint32_t dns2 = 0; - String ssidlist; WebBuildSSIDList(ssidlist); - EepromReadConfig(mode, &confSsid, &confPassword, &confHost, &mqttServer, &mqttUser, &mqttPasswd, mqttPort, ip_config, ip, gw, mask, dns, dns2); server.send(200, "text/html", "
" "
" "Wifi configuration:" - "

" + "

" "" + ssidlist + "" "

" - "

" + "

" "
" "
" "IP Configuration" "
DHCP Static
" - "

" - "

" - "

" - "

" - "

" + "

" + "

" + "

" + "

" + "

" "
" "
" "MQTT:" - "

" - "

" + "

" + "

" "

" - "

(8883 for secure Mqtts)
" + "

(8883 for secure Mqtts)
" "
" "
" "
"); @@ -166,11 +150,12 @@ void WebHandleSave() { } } - if (EepromSaveConfig(BOOTMODE_NORMAL, server.arg("ssid"), server.arg("password"), - server.arg("host"), server.arg("mqttServer"), server.arg("mqttUser"), - server.arg("mqttPasswd"), server.arg("mqttPort").toInt(), + productConfig newConf = {BOOTMODE_NORMAL, strdup(server.arg("ssid").c_str()), strdup(server.arg("password").c_str()), + strdup(server.arg("host").c_str()), strdup(server.arg("mqttServer").c_str()), strdup(server.arg("mqttUser").c_str()), + strdup(server.arg("mqttPasswd").c_str()), server.arg("mqttPort").toInt(), server.arg("ip_config").toInt(), static_cast(ip), static_cast(gw), - static_cast(mask), static_cast(dns), static_cast(dns2)) < 0) { + static_cast(mask), static_cast(dns), static_cast(dns2)}; + if (EepromSaveConfig(newConf) < 0) { WebSendError("Cannot Save Credentials (Too long ?Contains \";\"?)\r\n"); return; } diff --git a/WifiControlSensor/WifiControlSensor.ino b/WifiControlSensor/WifiControlSensor.ino index 5924330..cdacbb6 100644 --- a/WifiControlSensor/WifiControlSensor.ino +++ b/WifiControlSensor/WifiControlSensor.ino @@ -63,7 +63,7 @@ double temp, pressure; float dhtTemp, dhtHumidity; int dryness; uint8_t mode; -const char *hostName = ""; +productConfig conf = {BOOTMODE_SETUP, "", "", "", "", "", "", 0, 0, 0, 0, 0, 0, 0}; /* Set these to your desired credentials. */ const char *ssid = CONFIG_SSID_NAME; @@ -91,23 +91,23 @@ void onLongButtonPressed(uint8_t pin){ } #endif -void WifiSetup(int bootmode, char *confSsid, char *confPassword, char *confHost, int ip_config, uint32_t ip, uint32_t gw, uint32_t mask, uint32_t dns, uint32_t dns2) { +void WifiSetup(productConfig conf) { IPAddress myIP; - if (bootmode == BOOTMODE_SETUP) { + if (conf.bootMode == BOOTMODE_SETUP) { SKETCH_DEBUG_PRINT("Configuring access point..."); - SKETCH_DEBUG_PRINTLN(ssid); + SKETCH_DEBUG_PRINTLN(conf.ssid); /* You can set a password to the AP here */ - WiFi.softAP(ssid); + WiFi.softAP(conf.ssid); myIP = WiFi.softAPIP(); } else { SKETCH_DEBUG_PRINTLN("Disable previous AP mode "); WiFi.softAPdisconnect(true); SKETCH_DEBUG_PRINTLN("Connecting to Wifi..."); - if(ip_config == 1){ + if(conf.ip_mode == 1){ SKETCH_DEBUG_PRINTLN("Use static ip configuration"); - WiFi.config(IPAddress(ip), IPAddress(gw), IPAddress(mask), IPAddress(dns), IPAddress(dns2)); + WiFi.config(IPAddress(conf.ip), IPAddress(conf.gw), IPAddress(conf.mask), IPAddress(conf.dns), IPAddress(conf.dns2)); } - WiFi.begin(confSsid, confPassword); + WiFi.begin(conf.ssid, conf.password); while (WiFi.status() != WL_CONNECTED) { delay(500); SKETCH_DEBUG_PRINT("."); @@ -116,7 +116,7 @@ void WifiSetup(int bootmode, char *confSsid, char *confPassword, char *confHost, SKETCH_DEBUG_PRINTLN("WiFi connected"); #ifdef CONFIG_ENABLE_MDNS - if (!MDNS.begin(confHost)) { + if (!MDNS.begin(conf.host)) { SKETCH_DEBUG_PRINTLN("Error setting up MDNS responder!"); while (1) { delay(1000); @@ -183,20 +183,6 @@ void OTASetup() { void setup() { pinMode(3, OUTPUT); - char *confSsid = ""; - char *confPassword = ""; - char *confHost = ""; - char *mqttServer = ""; - char *mqttUser = ""; - char *mqttPasswd = ""; - int mqttPort ; - int ip_mode = 0; - uint32_t ip = 0; - uint32_t gw = 0; - uint32_t mask = 0; - uint32_t dns = 0; - uint32_t dns2 = 0; - delay(1000); SKETCH_DEBUG_INIT(115200); SKETCH_DEBUG_PRINTLN(); @@ -210,19 +196,19 @@ void setup() { #endif EEPROM.begin(CONFIG_EEPROM_SIZE); - EepromReadConfig(mode, &confSsid, &confPassword, &confHost, &mqttServer, &mqttUser, &mqttPasswd, mqttPort, ip_mode, ip, gw, mask, dns, dns2); + EepromReadConfig(conf); - hostName = confHost; + mode = conf.bootMode; if (mode == BOOTMODE_NORMAL || mode == BOOTMODE_OTA) { SKETCH_DEBUG_PRINTLN("Configuration Found !:"); - SKETCH_DEBUG_PRINTLN(mode); - SKETCH_DEBUG_PRINTLN(confSsid); - SKETCH_DEBUG_PRINTLN(confPassword); - SKETCH_DEBUG_PRINTLN(confHost); - SKETCH_DEBUG_PRINTLN(mqttServer); - SKETCH_DEBUG_PRINTLN(mqttUser); - SKETCH_DEBUG_PRINTLN(mqttPasswd); - SKETCH_DEBUG_PRINTLN(mqttPort); + SKETCH_DEBUG_PRINTLN(conf.bootMode); + SKETCH_DEBUG_PRINTLN(conf.ssid); + SKETCH_DEBUG_PRINTLN(conf.password); + SKETCH_DEBUG_PRINTLN(conf.host); + SKETCH_DEBUG_PRINTLN(conf.mqttServer); + SKETCH_DEBUG_PRINTLN(conf.mqttUser); + SKETCH_DEBUG_PRINTLN(conf.mqttPasswd); + SKETCH_DEBUG_PRINTLN(conf.mqttPort); SKETCH_DEBUG_PRINTLN(); } else { SKETCH_DEBUG_PRINTLN("No configuration saved"); @@ -235,9 +221,9 @@ void setup() { mode = BOOTMODE_SETUP; } - WifiSetup(mode, confSsid, confPassword, confHost, ip_mode, ip, gw, mask, dns, dns2); + WifiSetup(conf); if (mode == BOOTMODE_NORMAL) { - MqttSetup(mqttServer, mqttUser, mqttPasswd, mqttPort, confHost); + MqttSetup(conf.mqttServer, conf.mqttUser, conf.mqttPasswd, conf.mqttPort, conf.host); MqttPublishIP(WiFi.localIP().toString()); }