Save mqtt port into eeprom

This commit is contained in:
Mathieu Maret 2016-03-14 17:18:36 +01:00
parent dc47c61775
commit 25010e63c0
4 changed files with 28 additions and 22 deletions

View File

@ -1,14 +1,14 @@
/* EEPROM LAYOUT
"BOOTMODE;SSID;PASSWORD;HOSTNAME;MQTT_SERVER;MQTT_USERNAME;MQTT_PASSWD"
"BOOTMODE;SSID;PASSWORD;HOSTNAME;MQTT_SERVER;MQTT_USERNAME;MQTT_PASSWD;MQTT_PORT;"
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, String mqttServer, String mqttUser, String mqttpasswd) {
int saveConfig(uint8_t bootMode, String ssid, String password, String host, String mqttServer, String mqttUser, String mqttPasswd, int mqttPort) {
String eeprom;
eeprom = String(bootMode) + ";" + ssid + ";" + password + ";" + host + ";" + mqttServer + ";" + mqttUser + ";" + mqttpasswd +";";
eeprom = String(bootMode) + ";" + ssid + ";" + password + ";" + host + ";" + mqttServer + ";" + mqttUser + ";" + mqttPasswd + ";" + String(mqttPort) + ";";
if (eeprom.length() > EEPROM_SIZE )
return -EMSGSIZE;
@ -25,7 +25,7 @@ int saveConfig(int bootMode, String ssid, String password, String host, String m
return 0;
}
int saveBootMode(int bootMode) {
int saveBootMode(uint8_t bootMode) {
EEPROM.write(0, String(bootMode).charAt(0));
EEPROM.commit();
delay(100);
@ -40,16 +40,17 @@ void readConfElement(char** element, int &i) {
} while (i < EEPROM_SIZE && eeprom[i - 1] != ';');
eeprom[i - 1] = '\0';
if(i == EEPROM_SIZE)
if (i >= EEPROM_SIZE)
**element = '\0';
}
void readEEPROM(int &bootMode, char **ssid, char **password, char **host, char **mqttServer, char **mqttUser, char **mqttPasswd) {
void readEEPROM(uint8_t &bootMode, char **ssid, char **password, char **host, char **mqttServer, char **mqttUser, char **mqttPasswd, int &mqttPort) {
int i = 2;
int boot = EEPROM.read(0);
uint8_t boot = EEPROM.read(0);
char *mqttPortString = "";
if (boot == '1') {
bootMode = BOOTMODE_NORMAL;
@ -67,4 +68,6 @@ void readEEPROM(int &bootMode, char **ssid, char **password, char **host, char *
readConfElement(mqttServer, i);
readConfElement(mqttUser, i);
readConfElement(mqttPasswd, i);
readConfElement(&mqttPortString, i);
mqttPort = atoi(mqttPortString);
}

View File

@ -14,13 +14,13 @@ int publishMQTT(int temp) {
}
}
int setupMQTT(char *server, char *user, char *passwd) {
int setupMQTT(char *server, char *user, char *passwd, int port) {
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 = new Adafruit_MQTT_Client(&client, server, port, user, passwd);
mqtt_temp = new Adafruit_MQTT_Publish(mqtt, TEMPERATURE_FEED);
return 0;

View File

@ -13,9 +13,10 @@ 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><label for=\"mqttServer\">MQTTServer :</label><input type=\"text\" name=\"mqttServer\" /> </div>"
"<div><label for=\"mqttUser\">MQTTUsername :</label><input type=\"text\" name=\"mqttUser\" /> </div>"
"<div><label for=\"mqttPasswd\">MQTTPassword :</label><input type=\"text\" name=\"mqttPasswd\" /> </div>"
"<div><label for=\"mqttPort\">MQTTPort :</label><input type=\"text\" name=\"mqttPort\" value=\"1883\" /> </div>"
"<div class=\"button\"> <button type=\"submit\">Save</button></div>"
"</form>");
}
@ -39,12 +40,12 @@ void handleSave() {
String mqttUser;
String mqttPasswd;
if (!server.hasArg("ssid") || !server.hasArg("password") || !server.hasArg("host") || !server.hasArg("mqttServer") || !server.hasArg("mqttUser") || !server.hasArg("mqttPasswd") ) {
if (!server.hasArg("ssid") || !server.hasArg("password") || !server.hasArg("host") || !server.hasArg("mqttServer") || !server.hasArg("mqttUser") || !server.hasArg("mqttPasswd") || !server.hasArg("mqttPort") ) {
server.send(500, "text/plain", "Bad arguments\r\n");
return;
}
if (saveConfig(BOOTMODE_NORMAL, server.arg("ssid"), server.arg("password"), server.arg("host"), server.arg("mqttServer"), server.arg("mqttUser"), server.arg("mqttPasswd")) < 0) {
if (saveConfig(BOOTMODE_NORMAL, server.arg("ssid"), server.arg("password"), server.arg("host"), server.arg("mqttServer"), server.arg("mqttUser"), server.arg("mqttPasswd"), server.arg("mqttPort").toInt()) < 0) {
server.send(500, "text/plain", "Cannot Save Credentials (Too long ?Contains \";\"?)\r\n");
return;
}

View File

@ -43,7 +43,7 @@ char eeprom[EEPROM_SIZE];
#define BOOTMODE_OTA 2
double temp;
int mode;
uint8_t mode;
/* Set these to your desired credentials. */
const char *ssid = "ESPConfigurator";
@ -61,15 +61,16 @@ void handleNotFound();
void setupWebServer(int bootmode);
/* EEPROM decl */
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, char **mqttServer, char **mqttUser, char **mqttPasswd);
int saveConfig(uint8_t bootMode, String ssid, String password, String host, String mqttServer, String mqttUser, String mqttpasswd, int mqttPort);
int saveBootMode(uint8_t bootMode);
void readEEPROM(uint8_t &bootMode, char **ssid, char **password, char **host, char **mqttServer, char **mqttUser, char **mqttPasswd, int &mqttPort);
/* BMP180 decl */
int getTemperature(double &t);
/* MQTT decl */
int MQTT_connect();
int setupMQTT(char *server, char *user, char *passwd);
int setupMQTT(char *server, char *user, char *passwd, int port);
int publishMQTT(int temp);
void setupWifi(int bootmode, int forceSetup, char *confSsid, char *confPassword, char *confHost) {
@ -115,7 +116,7 @@ void setupOTA() {
//Disable OTA mode to avoid forever loop
//Force BOOTMODE_SETUP in case eeprom layout have changed
saveConfig(BOOTMODE_SETUP, "", "", "", "", "", "" );
saveBootMode(BOOTMODE_SETUP);
ArduinoOTA.onStart([]() {
Serial.println("Start");
@ -151,6 +152,7 @@ void setup() {
char *mqttServer;
char *mqttUser;
char *mqttPasswd;
int mqttPort;
delay(1000);
Serial.begin(115200);
@ -165,7 +167,7 @@ void setup() {
#endif
EEPROM.begin(EEPROM_SIZE);
readEEPROM(mode, &confSsid, &confPassword, &confHost, &mqttServer, &mqttUser, &mqttPasswd);
readEEPROM(mode, &confSsid, &confPassword, &confHost, &mqttServer, &mqttUser, &mqttPasswd, mqttPort);
if (mode == BOOTMODE_NORMAL || mode == BOOTMODE_OTA) {
Serial.println("Configuration Found !:");
Serial.println(mode);
@ -185,7 +187,7 @@ void setup() {
Serial.println();
setupWifi(mode, txStatus == 0, confSsid, confPassword, confHost);
setupMQTT(mqttServer, mqttUser, mqttPasswd);
setupMQTT(mqttServer, mqttUser, mqttPasswd, mqttPort);
if (mode == BOOTMODE_OTA) {
setupOTA();