Save mqtt port into eeprom
This commit is contained in:
parent
dc47c61775
commit
25010e63c0
@ -1,14 +1,14 @@
|
|||||||
/* EEPROM LAYOUT
|
/* 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
|
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, 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;
|
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 )
|
if (eeprom.length() > EEPROM_SIZE )
|
||||||
return -EMSGSIZE;
|
return -EMSGSIZE;
|
||||||
@ -25,7 +25,7 @@ int saveConfig(int bootMode, String ssid, String password, String host, String m
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int saveBootMode(int bootMode) {
|
int saveBootMode(uint8_t bootMode) {
|
||||||
EEPROM.write(0, String(bootMode).charAt(0));
|
EEPROM.write(0, String(bootMode).charAt(0));
|
||||||
EEPROM.commit();
|
EEPROM.commit();
|
||||||
delay(100);
|
delay(100);
|
||||||
@ -40,16 +40,17 @@ void readConfElement(char** element, int &i) {
|
|||||||
} while (i < EEPROM_SIZE && eeprom[i - 1] != ';');
|
} while (i < EEPROM_SIZE && eeprom[i - 1] != ';');
|
||||||
eeprom[i - 1] = '\0';
|
eeprom[i - 1] = '\0';
|
||||||
|
|
||||||
if(i == EEPROM_SIZE)
|
if (i >= EEPROM_SIZE)
|
||||||
**element = '\0';
|
**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 i = 2;
|
||||||
|
|
||||||
int boot = EEPROM.read(0);
|
uint8_t boot = EEPROM.read(0);
|
||||||
|
char *mqttPortString = "";
|
||||||
|
|
||||||
if (boot == '1') {
|
if (boot == '1') {
|
||||||
bootMode = BOOTMODE_NORMAL;
|
bootMode = BOOTMODE_NORMAL;
|
||||||
@ -67,4 +68,6 @@ void readEEPROM(int &bootMode, char **ssid, char **password, char **host, char *
|
|||||||
readConfElement(mqttServer, i);
|
readConfElement(mqttServer, i);
|
||||||
readConfElement(mqttUser, i);
|
readConfElement(mqttUser, i);
|
||||||
readConfElement(mqttPasswd, i);
|
readConfElement(mqttPasswd, i);
|
||||||
|
readConfElement(&mqttPortString, i);
|
||||||
|
mqttPort = atoi(mqttPortString);
|
||||||
}
|
}
|
||||||
|
@ -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 == ""){
|
if(server == ""){
|
||||||
Serial.println("Mqtt Server not configured");
|
Serial.println("Mqtt Server not configured");
|
||||||
}
|
}
|
||||||
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
|
// 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);
|
mqtt_temp = new Adafruit_MQTT_Publish(mqtt, TEMPERATURE_FEED);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -13,9 +13,10 @@ 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=\"mqttServer\">MQTTServer :</label><input type=\"text\" name=\"mqttServer\" /> </div>"
|
||||||
"<div><label for=\"host\">MQTTUsername :</label><input type=\"text\" name=\"mqttUser\" /> </div>"
|
"<div><label for=\"mqttUser\">MQTTUsername :</label><input type=\"text\" name=\"mqttUser\" /> </div>"
|
||||||
"<div><label for=\"host\">MQTTPassword :</label><input type=\"text\" name=\"mqttPasswd\" /> </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>"
|
"<div class=\"button\"> <button type=\"submit\">Save</button></div>"
|
||||||
"</form>");
|
"</form>");
|
||||||
}
|
}
|
||||||
@ -39,12 +40,12 @@ void handleSave() {
|
|||||||
String mqttUser;
|
String mqttUser;
|
||||||
String mqttPasswd;
|
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");
|
server.send(500, "text/plain", "Bad arguments\r\n");
|
||||||
return;
|
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");
|
server.send(500, "text/plain", "Cannot Save Credentials (Too long ?Contains \";\"?)\r\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -43,7 +43,7 @@ char eeprom[EEPROM_SIZE];
|
|||||||
#define BOOTMODE_OTA 2
|
#define BOOTMODE_OTA 2
|
||||||
|
|
||||||
double temp;
|
double temp;
|
||||||
int mode;
|
uint8_t mode;
|
||||||
|
|
||||||
/* Set these to your desired credentials. */
|
/* Set these to your desired credentials. */
|
||||||
const char *ssid = "ESPConfigurator";
|
const char *ssid = "ESPConfigurator";
|
||||||
@ -61,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, 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);
|
||||||
int saveBootMode(int bootMode);
|
int saveBootMode(uint8_t bootMode);
|
||||||
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);
|
||||||
|
|
||||||
/* 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 setupMQTT(char *server, char *user, char *passwd, int port);
|
||||||
int publishMQTT(int temp);
|
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) {
|
||||||
@ -115,7 +116,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, "", "", "", "", "", "" );
|
saveBootMode(BOOTMODE_SETUP);
|
||||||
|
|
||||||
ArduinoOTA.onStart([]() {
|
ArduinoOTA.onStart([]() {
|
||||||
Serial.println("Start");
|
Serial.println("Start");
|
||||||
@ -151,6 +152,7 @@ void setup() {
|
|||||||
char *mqttServer;
|
char *mqttServer;
|
||||||
char *mqttUser;
|
char *mqttUser;
|
||||||
char *mqttPasswd;
|
char *mqttPasswd;
|
||||||
|
int mqttPort;
|
||||||
|
|
||||||
delay(1000);
|
delay(1000);
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
@ -165,7 +167,7 @@ void setup() {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
EEPROM.begin(EEPROM_SIZE);
|
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) {
|
if (mode == BOOTMODE_NORMAL || mode == BOOTMODE_OTA) {
|
||||||
Serial.println("Configuration Found !:");
|
Serial.println("Configuration Found !:");
|
||||||
Serial.println(mode);
|
Serial.println(mode);
|
||||||
@ -185,7 +187,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);
|
setupMQTT(mqttServer, mqttUser, mqttPasswd, mqttPort);
|
||||||
|
|
||||||
if (mode == BOOTMODE_OTA) {
|
if (mode == BOOTMODE_OTA) {
|
||||||
setupOTA();
|
setupOTA();
|
||||||
|
Loading…
Reference in New Issue
Block a user