Merge branch 'MQTT_refacto' into 'master'

Mqtt refacto

MQTT and EEPROM code simplification.
Save about 1k

See merge request !6
This commit is contained in:
Mathieu 2016-11-20 00:30:42 +01:00
commit 1a2d3a3eb6
6 changed files with 117 additions and 153 deletions

View File

@ -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);

View File

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

View File

@ -6,10 +6,11 @@ Adafruit_MQTT_Publish *MqttCreatePublisher(const char *fmt, ...);
int MqttConnect();
int MqttIsConnected();
int MqttSetup(char *server, char *user, char *passwd, int port, char * hostname);
int MqttPublish(double temp, double pressure);
int MqttDhtPublish(float temp, float humidity);
int MqttDryPublish(int dry);
int MqttIPPublish(const String &ip);
template<typename T> int MqttPublish(Adafruit_MQTT_Publish *publisher, T value);
int MqttPublishBMP180(double temp, double pressure);
int MqttPublishDHT(float temp, float humidity);
int MqttPublishDry(int dry);
int MqttPublishIp(const String &ip);
void MqttCheckSubscription();
void MqttChangeGpioValue(int gpio, int value);
void MqttChangePWMValue(int gpio, int value);
@ -18,10 +19,11 @@ bool MqttIsConfigured();
int MqttConnect(){return 0;}
int MqttIsConnected(){return 0;}
int MqttSetup(char *server, char *user, char *passwd, int port, char * hostname){return 0;}
int MqttPublish(double temp, double pressure){return 0;}
int MqttDhtPublish(float temp, float humidity){return 0;}
int MqttDryPublish(int dry){return 0;}
int MqttIPPublish(const String &ip){return 0;}
template<typename T> int MqttPublish(Adafruit_MQTT_Publish *publisher, T value){return 0;}
int MqttPublishBMP180(double temp, double pressure){return 0;}
int MqttPublishDHT(float temp, float humidity){return 0;}
int MqttPublishDry(int dry){return 0;}
int MqttPublishIP(const String &ip){return 0;}
void MqttCheckSubscription(){}
void MqttChangeGpioValue(int gpio, int value){}
void MqttChangePWMValue(int gpio, int value){}

View File

@ -127,38 +127,28 @@ int MqttConnect() {
return 0;
}
int MqttPublish(double temp, double pressure) {
template<typename T> int MqttPublish(Adafruit_MQTT_Publish *publisher, T value){
if (MqttConnect() == 0) {
SKETCH_DEBUG_PRINTLN("publishing !");
mqtt_temp->publish(temp);
mqtt_pressure->publish(pressure);
publisher->publish(value);
return 0;
}
return 0;
return -1;
}
int MqttDryPublish(int dry) {
if (MqttConnect() == 0) {
SKETCH_DEBUG_PRINTLN("publishing dry!");
mqtt_dry->publish((dry*100)/1024);
}
return 0;
int MqttPublishBMP180(double temp, double pressure) {
return MqttPublish(mqtt_temp, temp) + MqttPublish(mqtt_pressure, pressure);
}
int MqttIPPublish(const String &ip) {
if (MqttConnect() == 0) {
SKETCH_DEBUG_PRINTLN("publishing IP!");
mqtt_ip->publish(ip.c_str());
}
return 0;
int MqttPublishDry(int dry) {
return MqttPublish(mqtt_dry, (dry*100)/1024);
}
int MqttDhtPublish(float temp, float humidity) {
if (MqttConnect() == 0) {
SKETCH_DEBUG_PRINTLN("publishing DHT!");
mqtt_dht_temp->publish(temp);
mqtt_dht_humidity->publish(humidity);
}
return 0;
int MqttPublishIP(const String &ip) {
return MqttPublish(mqtt_ip, ip.c_str());
}
int MqttPublishDHT(float temp, float humidity) {
return MqttPublish(mqtt_dht_temp, temp) + MqttPublish(mqtt_dht_humidity, humidity);
}
int getGpioFromSubscription(Adafruit_MQTT_Subscribe *subscription, const char *pattern) {

View File

@ -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;
}

View File

@ -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,38 +91,35 @@ 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 ");
//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(".");
}
SKETCH_DEBUG_PRINTLN("");
SKETCH_DEBUG_PRINTLN("WiFi connected");
SKETCH_DEBUG_PRINTF("\nWiFi connected\n");
#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);
}
} else {
SKETCH_DEBUG_PRINTLN("mDNS responder started");
}
SKETCH_DEBUG_PRINTLN("mDNS responder started");
#endif
myIP = WiFi.localIP();
}
@ -130,6 +127,7 @@ void WifiSetup(int bootmode, char *confSsid, char *confPassword, char *confHost,
SKETCH_DEBUG_PRINT("My IP address: ");
SKETCH_DEBUG_PRINTLN(myIP);
}
void OTASetup() {
#ifndef CONFIF_DISABLE_OTA
// Port defaults to 8266
@ -174,7 +172,7 @@ void OTASetup() {
});
ArduinoOTA.begin();
SKETCH_DEBUG_PRINTLN("Ready");
SKETCH_DEBUG_PRINT("IP address: ");
SKETCH_DEBUG_PRINTF("IP address: ");
SKETCH_DEBUG_PRINTLN(WiFi.localIP());
SKETCH_DEBUG_PRINTF("Free Space: %d\n", ESP.getFreeSketchSpace());
#endif
@ -183,20 +181,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,35 +194,33 @@ 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");
}
SKETCH_DEBUG_PRINT("Force Setup Mode ? :");
SKETCH_DEBUG_PRINT(txStatus ? "No" : "Yes");
SKETCH_DEBUG_PRINTLN();
SKETCH_DEBUG_PRINTF("Force Setup Mode ? : %s\n",txStatus ? "No" : "Yes");
if(txStatus == 0){
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);
MqttIPPublish(WiFi.localIP().toString());
MqttSetup(conf.mqttServer, conf.mqttUser, conf.mqttPasswd, conf.mqttPort, conf.host);
MqttPublishIP(WiFi.localIP().toString());
}
if (mode == BOOTMODE_OTA) {
@ -281,7 +263,7 @@ void loop() {
SKETCH_DEBUG_PRINT(" Pressure mB ");
SKETCH_DEBUG_PRINTLN(pressure);
if (mode == BOOTMODE_NORMAL)
MqttPublish(temp, pressure);
MqttPublishBMP180(temp, pressure);
}
if (!DHTGetTempAndHumidity(dhtTemp, dhtHumidity)) {
SKETCH_DEBUG_PRINT("Current T°C ");
@ -289,14 +271,14 @@ void loop() {
SKETCH_DEBUG_PRINT(" Humidity ");
SKETCH_DEBUG_PRINTLN(dhtHumidity);
if (mode == BOOTMODE_NORMAL)
MqttDhtPublish(dhtTemp, dhtHumidity);
MqttPublishDHT(dhtTemp, dhtHumidity);
}
if (!DryGetMeasure(dryness)){
SKETCH_DEBUG_PRINT("Current dryness ");
SKETCH_DEBUG_PRINT((dryness*100)/1024);
SKETCH_DEBUG_PRINTLN("%");
if (mode == BOOTMODE_NORMAL)
MqttDryPublish(dryness);
MqttPublishDry(dryness);
}
nbCycle = 0;
}