Browse Source

Put config into a struct

gpiosensors
Mathieu Maret 5 years ago
parent
commit
3691745aec
  1. 27
      WifiControlSensor/EEPROM.h
  2. 56
      WifiControlSensor/EEPROM.ino
  3. 47
      WifiControlSensor/WebServer.ino
  4. 56
      WifiControlSensor/WifiControlSensor.ino

27
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);
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(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);

56
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);
}

47
WifiControlSensor/WebServer.ino

@ -9,7 +9,7 @@ void WebHandleRoot() {
server.send(200, "text/html",
"<head><meta http-equiv=\"refresh\" content=\"" + String(CONFIG_SAMPLING_PERIODE_MS / 1000) + "\" ></head>"
"<h1>You are connected to " + String(hostName) + "</h1>"
"<h1>You are connected to " + String(conf.host) + "</h1>"
"<fieldset>"
"<legend>Sensors</legend>"
#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", "<form action=\"/save\" method=\"get\">"
"<fieldset>"
"<legend>Wifi configuration:</legend>"
"<div><label for=\"ssid\">Wifi SSID :</label> <br/><input list=\"scan_ssid\" type=\"text\" name=\"ssid\" value=\"" + String(confSsid) + "\" /></div>"
"<div><label for=\"ssid\">Wifi SSID :</label> <br/><input list=\"scan_ssid\" type=\"text\" name=\"ssid\" value=\"" + String(conf.ssid) + "\" /></div>"
"" + ssidlist + ""
"<div><label for=\"password\">Wifi Password :</label><br/><input type=\"password\" name=\"password\" style=\"border-color:red\" /> </div>"
"<div><label for=\"host\">Hostname :</label><br/><input type=\"text\" name=\"host\" value=\"" + String(confHost) + "\" /> </div>"
"<div><label for=\"host\">Hostname :</label><br/><input type=\"text\" name=\"host\" value=\"" + String(conf.host) + "\" /> </div>"
"</fieldset>"
"<fieldset>"
"<legend>IP Configuration</legend>"
"<div><input type=\"radio\" name=\"ip_config\" value=\"0\" checked>DHCP <input type=\"radio\" name=\"ip_config\" value=\"1\">Static</div>"
"<div><label for=\"ip\">Ip :</label><br/><input type=\"text\" name=\"ip\" value=\"" + (ip == 0 ? "192.168.0.123": IPAddress(ip).toString()) + "\" /> </div>"
"<div><label for=\"gw\">Gateway :</label><br/><input type=\"text\" name=\"gw\" value=\"" + (gw == 0 ? "192.168.0.250": IPAddress(gw).toString()) + "\" /> </div>"
"<div><label for=\"mask\">Netmask :</label><br/><input type=\"text\" name=\"mask\" value=\"" + (mask == 0 ? "255.255.255.0": IPAddress(mask).toString()) + "\" /> </div>"
"<div><label for=\"mask\">DNS :</label><br/><input type=\"text\" name=\"dns\" value=\"" + (dns == 0 ? "192.168.0.250": IPAddress(dns).toString()) + "\" /> </div>"
"<div><label for=\"mask\">DNS2 :</label><br/><input type=\"text\" name=\"dns2\" value=\"" + (dns2 == 0 ? "": IPAddress(dns2).toString()) + "\" /> </div>"
"<div><label for=\"ip\">Ip :</label><br/><input type=\"text\" name=\"ip\" value=\"" + (conf.ip == 0 ? "192.168.0.123": IPAddress(conf.ip).toString()) + "\" /> </div>"
"<div><label for=\"gw\">Gateway :</label><br/><input type=\"text\" name=\"gw\" value=\"" + (conf.gw == 0 ? "192.168.0.250": IPAddress(conf.gw).toString()) + "\" /> </div>"
"<div><label for=\"mask\">Netmask :</label><br/><input type=\"text\" name=\"mask\" value=\"" + (conf.mask == 0 ? "255.255.255.0": IPAddress(conf.mask).toString()) + "\" /> </div>"
"<div><label for=\"mask\">DNS :</label><br/><input type=\"text\" name=\"dns\" value=\"" + (conf.dns == 0 ? "192.168.0.250": IPAddress(conf.dns).toString()) + "\" /> </div>"
"<div><label for=\"mask\">DNS2 :</label><br/><input type=\"text\" name=\"dns2\" value=\"" + (conf.dns2 == 0 ? "": IPAddress(conf.dns2).toString()) + "\" /> </div>"
"</fieldset>"
"<fieldset>"
"<legend>MQTT:</legend>"
"<div><label for=\"mqttServer\">Server :</label><br/><input type=\"text\" name=\"mqttServer\" value=\"" + String(mqttServer) + "\" /> </div>"
"<div><label for=\"mqttUser\">Username :</label><br/><input type=\"text\" name=\"mqttUser\" value=\"" + String(mqttUser) + "\" /> </div>"
"<div><label for=\"mqttServer\">Server :</label><br/><input type=\"text\" name=\"mqttServer\" value=\"" + String(conf.mqttServer) + "\" /> </div>"
"<div><label for=\"mqttUser\">Username :</label><br/><input type=\"text\" name=\"mqttUser\" value=\"" + String(conf.mqttUser) + "\" /> </div>"
"<div><label for=\"mqttPasswd\">Password :</label><br/><input type=\"password\" name=\"mqttPasswd\" style=\"border-color:red\" /> </div>"
"<div><label for=\"mqttPort\">Port :</label><br/><input type=\"text\" name=\"mqttPort\" value=\"" + String(mqttPort) + "\" /> (8883 for secure Mqtts) </div>"
"<div><label for=\"mqttPort\">Port :</label><br/><input type=\"text\" name=\"mqttPort\" value=\"" + String(conf.mqttPort) + "\" /> (8883 for secure Mqtts) </div>"
"</fieldset>"
"<div class=\"button\"> <button type=\"submit\">Save</button></div>"
"</form>");
@ -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<uint32_t>(ip), static_cast<uint32_t>(gw),
static_cast<uint32_t>(mask), static_cast<uint32_t>(dns), static_cast<uint32_t>(dns2)) < 0) {
static_cast<uint32_t>(mask), static_cast<uint32_t>(dns), static_cast<uint32_t>(dns2)};
if (EepromSaveConfig(newConf) < 0) {
WebSendError("Cannot Save Credentials (Too long ?Contains \";\"?)\r\n");
return;
}

56
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());
}

Loading…
Cancel
Save