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
"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
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;
eeprom = String(bootMode) + ";" + ssid + ";" + password + ";" + host + ";";
eeprom = String(bootMode) + ";" + ssid + ";" + password + ";" + host + ";" + mqttServer + ";" + mqttUser + ";" + mqttpasswd +";";
if (eeprom.length() > EEPROM_SIZE )
return -EMSGSIZE;
@ -25,14 +25,27 @@ int saveConfig(int bootMode, String ssid, String password, String host ) {
return 0;
}
int saveBootMode(int bootMode){
EEPROM.write(0,String(bootMode).charAt(0));
int saveBootMode(int bootMode) {
EEPROM.write(0, String(bootMode).charAt(0));
EEPROM.commit();
delay(100);
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;
@ -48,28 +61,10 @@ void readEEPROM(int &bootMode, char **ssid, char **password, char **host) {
return;
}
//Read SSID
*ssid = &eeprom[2];
do {
eeprom[i] = EEPROM.read(i);
i++;
} while (i < EEPROM_SIZE && eeprom[i - 1] != ';');
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';
readConfElement(ssid, i);
readConfElement(password, i);
readConfElement(host, i);
readConfElement(mqttServer, i);
readConfElement(mqttUser, i);
readConfElement(mqttPasswd, i);
}

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.
// Should be called in the loop function and it will take care if connecting.
int MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
if (mqtt->connected()) {
return 0;
}
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
while ((ret = mqtt->connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt->connectErrorString(ret));
Serial.println("Retrying MQTT connection ...");
mqtt.disconnect();
mqtt->disconnect();
delay(100); // wait
retries--;
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=\"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\">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>"
"</form>");
}
@ -32,13 +35,16 @@ void handleSave() {
String password;
String ssid;
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");
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");
return;
}

View File

@ -26,18 +26,13 @@
#include "Adafruit_MQTT.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 EEPROM_SIZE 512
#define EEPROM_SIZE 1024
char eeprom[EEPROM_SIZE];
#define WEB_DELAY_MS 100
#define SAMPLING_PERIODE_MS 60000
#define SAMPLING_PERIODE_MS 6000
/* I2C pin used*/
#define SDA 2
@ -50,21 +45,6 @@ char eeprom[EEPROM_SIZE];
double temp;
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. */
const char *ssid = "ESPConfigurator";
@ -81,15 +61,16 @@ void handleNotFound();
void setupWebServer(int bootmode);
/* 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);
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 */
int getTemperature(double &t);
/* MQTT decl */
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) {
IPAddress myIP;
@ -134,7 +115,7 @@ void setupOTA() {
//Disable OTA mode to avoid forever loop
//Force BOOTMODE_SETUP in case eeprom layout have changed
saveConfig(BOOTMODE_SETUP, "", "", "" );
saveConfig(BOOTMODE_SETUP, "", "", "", "", "", "" );
ArduinoOTA.onStart([]() {
Serial.println("Start");
@ -167,6 +148,9 @@ void setup() {
char *confSsid;
char *confPassword;
char *confHost;
char *mqttServer;
char *mqttUser;
char *mqttPasswd;
delay(1000);
Serial.begin(115200);
@ -181,13 +165,16 @@ void setup() {
#endif
EEPROM.begin(EEPROM_SIZE);
readEEPROM(mode, &confSsid, &confPassword, &confHost);
readEEPROM(mode, &confSsid, &confPassword, &confHost, &mqttServer, &mqttUser, &mqttPasswd);
if (mode == BOOTMODE_NORMAL || mode == BOOTMODE_OTA) {
Serial.println("Configuration Found !:");
Serial.println(mode);
Serial.println(confSsid);
Serial.println(confPassword);
Serial.println(confHost);
Serial.println(mqttServer);
Serial.println(mqttUser);
Serial.println(mqttPasswd);
Serial.println();
} else {
Serial.println("No configuration saved");
@ -198,6 +185,7 @@ void setup() {
Serial.println();
setupWifi(mode, txStatus == 0, confSsid, confPassword, confHost);
setupMQTT(mqttServer, mqttUser, mqttPasswd);
if (mode == BOOTMODE_OTA) {
setupOTA();
@ -218,12 +206,9 @@ void loop() {
nbCycle++;
if (nbCycle > SAMPLING_PERIODE_MS / WEB_DELAY_MS) {
if (getTemperature(temp) == 0) {
Serial.print("Sampling :");
Serial.print("Current T°C ");
Serial.println(temp);
if (MQTT_connect() == 0){
Serial.println("publishing !");
mqtt_temp.publish(temp);
}
publishMQTT(temp);
} else {
Serial.println("Cannot get T°C");
}