Save MQTT config into EEPROM

This commit is contained in:
Mathieu Maret 2016-03-14 01:47:43 +01:00
parent 5007b652d9
commit dc47c61775
4 changed files with 82 additions and 68 deletions

View File

@ -1,14 +1,14 @@
/* EEPROM LAYOUT /* EEPROM LAYOUT
"BOOTMODE;SSID;PASSWORD;HOSTNAME;" "BOOTMODE;SSID;PASSWORD;HOSTNAME;MQTT_SERVER;MQTT_USERNAME;MQTT_PASSWD"
BOOTMODE could be 0 for Setup, 1 for normal use, 2 for OTA BOOTMODE could be 0 for Setup, 1 for normal use, 2 for OTA
Setup mode is trigger by setting GPIO3 to ground or at first boot Setup mode is trigger by setting GPIO3 to ground or at first boot
*/ */
int saveConfig(int bootMode, String ssid, String password, String host ) { int saveConfig(int bootMode, String ssid, String password, String host, String mqttServer, String mqttUser, String mqttpasswd) {
String eeprom; String eeprom;
eeprom = String(bootMode) + ";" + ssid + ";" + password + ";" + host + ";"; eeprom = String(bootMode) + ";" + ssid + ";" + password + ";" + host + ";" + mqttServer + ";" + mqttUser + ";" + mqttpasswd +";";
if (eeprom.length() > EEPROM_SIZE ) if (eeprom.length() > EEPROM_SIZE )
return -EMSGSIZE; return -EMSGSIZE;
@ -25,14 +25,27 @@ int saveConfig(int bootMode, String ssid, String password, String host ) {
return 0; return 0;
} }
int saveBootMode(int bootMode){ int saveBootMode(int bootMode) {
EEPROM.write(0,String(bootMode).charAt(0)); EEPROM.write(0, String(bootMode).charAt(0));
EEPROM.commit(); EEPROM.commit();
delay(100); delay(100);
return 0; return 0;
} }
void readEEPROM(int &bootMode, char **ssid, char **password, char **host) { void readConfElement(char** element, int &i) {
*element = &eeprom[i];
do {
eeprom[i] = EEPROM.read(i);
i++;
} while (i < EEPROM_SIZE && eeprom[i - 1] != ';');
eeprom[i - 1] = '\0';
if(i == EEPROM_SIZE)
**element = '\0';
}
void readEEPROM(int &bootMode, char **ssid, char **password, char **host, char **mqttServer, char **mqttUser, char **mqttPasswd) {
int i = 2; int i = 2;
@ -48,28 +61,10 @@ void readEEPROM(int &bootMode, char **ssid, char **password, char **host) {
return; return;
} }
//Read SSID readConfElement(ssid, i);
*ssid = &eeprom[2]; readConfElement(password, i);
do { readConfElement(host, i);
eeprom[i] = EEPROM.read(i); readConfElement(mqttServer, i);
i++; readConfElement(mqttUser, i);
} while (i < EEPROM_SIZE && eeprom[i - 1] != ';'); readConfElement(mqttPasswd, i);
eeprom[i - 1] = '\0';
//Read password
*password = &eeprom[i];
do {
eeprom[i] = EEPROM.read(i);
i++;
} while (i < EEPROM_SIZE && eeprom[i - 1] != ';');
eeprom[i - 1] = '\0';
//Read HostName
*host = &eeprom[i];
do {
eeprom[i] = EEPROM.read(i);
i++;
} while (i < EEPROM_SIZE && eeprom[i - 1] != ';');
eeprom[i - 1] = '\0';
} }

View File

@ -1,18 +1,46 @@
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
Adafruit_MQTT_Client *mqtt;
Adafruit_MQTT_Publish *mqtt_temp;
const char TEMPERATURE_FEED[] = "/feeds/temperature";
boolean mqttIsConfigured;
int publishMQTT(int temp) {
if (MQTT_connect() == 0) {
Serial.println("publishing !");
mqtt_temp->publish(temp);
}
}
int setupMQTT(char *server, char *user, char *passwd) {
if(server == ""){
Serial.println("Mqtt Server not configured");
}
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
mqtt = new Adafruit_MQTT_Client(&client, server, 1883, user, passwd);
mqtt_temp = new Adafruit_MQTT_Publish(mqtt, TEMPERATURE_FEED);
return 0;
}
// Function to connect and reconnect as necessary to the MQTT server. // Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting. // Should be called in the loop function and it will take care if connecting.
int MQTT_connect() { int MQTT_connect() {
int8_t ret; int8_t ret;
// Stop if already connected. // Stop if already connected.
if (mqtt.connected()) { if (mqtt->connected()) {
return 0; return 0;
} }
uint8_t retries = 3; uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected while ((ret = mqtt->connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret)); Serial.println(mqtt->connectErrorString(ret));
Serial.println("Retrying MQTT connection ..."); Serial.println("Retrying MQTT connection ...");
mqtt.disconnect(); mqtt->disconnect();
delay(100); // wait delay(100); // wait
retries--; retries--;
if (retries == 0) { if (retries == 0) {

View File

@ -13,6 +13,9 @@ void handleSetup() {
"<div><label for=\"ssid\">Wifi SSID :</label> <input type=\"text\" name=\"ssid\" /></div>" "<div><label for=\"ssid\">Wifi SSID :</label> <input type=\"text\" name=\"ssid\" /></div>"
"<div><label for=\"password\">Wifi Password :</label><input type=\"password\" name=\"password\" /> </div>" "<div><label for=\"password\">Wifi Password :</label><input type=\"password\" name=\"password\" /> </div>"
"<div><label for=\"host\">Hostname :</label><input type=\"text\" name=\"host\" /> </div>" "<div><label for=\"host\">Hostname :</label><input type=\"text\" name=\"host\" /> </div>"
"<div><label for=\"host\">MQTTServer :</label><input type=\"text\" name=\"mqttServer\" /> </div>"
"<div><label for=\"host\">MQTTUsername :</label><input type=\"text\" name=\"mqttUser\" /> </div>"
"<div><label for=\"host\">MQTTPassword :</label><input type=\"text\" name=\"mqttPasswd\" /> </div>"
"<div class=\"button\"> <button type=\"submit\">Save</button></div>" "<div class=\"button\"> <button type=\"submit\">Save</button></div>"
"</form>"); "</form>");
} }
@ -32,13 +35,16 @@ void handleSave() {
String password; String password;
String ssid; String ssid;
String hostName; String hostName;
String mqttServer;
String mqttUser;
String mqttPasswd;
if (!server.hasArg("ssid") || !server.hasArg("password") || !server.hasArg("host")) { if (!server.hasArg("ssid") || !server.hasArg("password") || !server.hasArg("host") || !server.hasArg("mqttServer") || !server.hasArg("mqttUser") || !server.hasArg("mqttPasswd") ) {
server.send(500, "text/plain", "Bad arguments\r\n"); server.send(500, "text/plain", "Bad arguments\r\n");
return; return;
} }
if (saveConfig(BOOTMODE_NORMAL, server.arg("ssid"), server.arg("password"), server.arg("host")) < 0) { if (saveConfig(BOOTMODE_NORMAL, server.arg("ssid"), server.arg("password"), server.arg("host"), server.arg("mqttServer"), server.arg("mqttUser"), server.arg("mqttPasswd")) < 0) {
server.send(500, "text/plain", "Cannot Save Credentials (Too long ?Contains \";\"?)\r\n"); server.send(500, "text/plain", "Cannot Save Credentials (Too long ?Contains \";\"?)\r\n");
return; return;
} }

View File

@ -26,18 +26,13 @@
#include "Adafruit_MQTT.h" #include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h" #include "Adafruit_MQTT_Client.h"
#define AIO_SERVER "192.168.0.250"
#define AIO_SERVERPORT 1883 // use 8883 for SSL
#define AIO_USERNAME ""
#define AIO_PASSWORD ""
//#define ENABLE_EXTRA_GPIO //#define ENABLE_EXTRA_GPIO
#define EEPROM_SIZE 512 #define EEPROM_SIZE 1024
char eeprom[EEPROM_SIZE]; char eeprom[EEPROM_SIZE];
#define WEB_DELAY_MS 100 #define WEB_DELAY_MS 100
#define SAMPLING_PERIODE_MS 60000 #define SAMPLING_PERIODE_MS 6000
/* I2C pin used*/ /* I2C pin used*/
#define SDA 2 #define SDA 2
@ -50,21 +45,6 @@ char eeprom[EEPROM_SIZE];
double temp; double temp;
int mode; int mode;
// Store the MQTT server, username, and password in flash memory.
// This is required for using the Adafruit MQTT library.
const char MQTT_SERVER[] PROGMEM = AIO_SERVER;
const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME;
const char MQTT_PASSWORD[] PROGMEM = AIO_PASSWORD;
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_USERNAME, MQTT_PASSWORD);
const char TEMPERATURE_FEED[] PROGMEM = "/feeds/temperature";
Adafruit_MQTT_Publish mqtt_temp = Adafruit_MQTT_Publish(&mqtt, TEMPERATURE_FEED);
/* Set these to your desired credentials. */ /* Set these to your desired credentials. */
const char *ssid = "ESPConfigurator"; const char *ssid = "ESPConfigurator";
@ -81,15 +61,16 @@ void handleNotFound();
void setupWebServer(int bootmode); void setupWebServer(int bootmode);
/* EEPROM decl */ /* EEPROM decl */
int saveConfig(int bootMode, String ssid, String password, String host ); int saveConfig(int bootMode, String ssid, String password, String host, String mqttServer, String mqttUser, String mqttpasswd);
int saveBootMode(int bootMode); int saveBootMode(int bootMode);
void readEEPROM(int &bootMode, char **ssid, char **password, char **host); void readEEPROM(int &bootMode, char **ssid, char **password, char **host, char **mqttServer, char **mqttUser, char **mqttPasswd);
/* BMP180 decl */ /* BMP180 decl */
int getTemperature(double &t); int getTemperature(double &t);
/* MQTT decl */ /* MQTT decl */
int MQTT_connect(); int MQTT_connect();
int setupMQTT(char *server, char *user, char *passwd);
int publishMQTT(int temp);
void setupWifi(int bootmode, int forceSetup, char *confSsid, char *confPassword, char *confHost) { void setupWifi(int bootmode, int forceSetup, char *confSsid, char *confPassword, char *confHost) {
IPAddress myIP; IPAddress myIP;
@ -134,7 +115,7 @@ void setupOTA() {
//Disable OTA mode to avoid forever loop //Disable OTA mode to avoid forever loop
//Force BOOTMODE_SETUP in case eeprom layout have changed //Force BOOTMODE_SETUP in case eeprom layout have changed
saveConfig(BOOTMODE_SETUP, "", "", "" ); saveConfig(BOOTMODE_SETUP, "", "", "", "", "", "" );
ArduinoOTA.onStart([]() { ArduinoOTA.onStart([]() {
Serial.println("Start"); Serial.println("Start");
@ -167,6 +148,9 @@ void setup() {
char *confSsid; char *confSsid;
char *confPassword; char *confPassword;
char *confHost; char *confHost;
char *mqttServer;
char *mqttUser;
char *mqttPasswd;
delay(1000); delay(1000);
Serial.begin(115200); Serial.begin(115200);
@ -181,13 +165,16 @@ void setup() {
#endif #endif
EEPROM.begin(EEPROM_SIZE); EEPROM.begin(EEPROM_SIZE);
readEEPROM(mode, &confSsid, &confPassword, &confHost); readEEPROM(mode, &confSsid, &confPassword, &confHost, &mqttServer, &mqttUser, &mqttPasswd);
if (mode == BOOTMODE_NORMAL || mode == BOOTMODE_OTA) { if (mode == BOOTMODE_NORMAL || mode == BOOTMODE_OTA) {
Serial.println("Configuration Found !:"); Serial.println("Configuration Found !:");
Serial.println(mode); Serial.println(mode);
Serial.println(confSsid); Serial.println(confSsid);
Serial.println(confPassword); Serial.println(confPassword);
Serial.println(confHost); Serial.println(confHost);
Serial.println(mqttServer);
Serial.println(mqttUser);
Serial.println(mqttPasswd);
Serial.println(); Serial.println();
} else { } else {
Serial.println("No configuration saved"); Serial.println("No configuration saved");
@ -198,6 +185,7 @@ void setup() {
Serial.println(); Serial.println();
setupWifi(mode, txStatus == 0, confSsid, confPassword, confHost); setupWifi(mode, txStatus == 0, confSsid, confPassword, confHost);
setupMQTT(mqttServer, mqttUser, mqttPasswd);
if (mode == BOOTMODE_OTA) { if (mode == BOOTMODE_OTA) {
setupOTA(); setupOTA();
@ -218,12 +206,9 @@ void loop() {
nbCycle++; nbCycle++;
if (nbCycle > SAMPLING_PERIODE_MS / WEB_DELAY_MS) { if (nbCycle > SAMPLING_PERIODE_MS / WEB_DELAY_MS) {
if (getTemperature(temp) == 0) { if (getTemperature(temp) == 0) {
Serial.print("Sampling :"); Serial.print("Current T°C ");
Serial.println(temp); Serial.println(temp);
if (MQTT_connect() == 0){ publishMQTT(temp);
Serial.println("publishing !");
mqtt_temp.publish(temp);
}
} else { } else {
Serial.println("Cannot get T°C"); Serial.println("Cannot get T°C");
} }