Hello to all,
a new quetsion regarding wifimanager and custom parameter.
I want to start the setup with a button again.
So when i press the button the wifi captive portal with the custom parameter should start again to set other input for custom parameter and/or wifi network.
Everything is ok at the first time when i start the setup, but when it runs and i press the button and the captive portal starts again after opening it, i get the error below.
So it seems something with saving or loading the parameters from the flash is wrong.
Because after pressing the button, first i am able to connect to the capitive portal then the error excaption code comes and then the ESP8266 starts again and everything is fine.
I get the error (part of the serial after pressing the button and when i connect to the portal):
*WM: [2] <- Request redirected to captive portal
*WM: [2] NUM CLIENTS: 1
*WM: [2] <- HTTP Wifi
*WM: [2] Scan is cached 32409 ms ago
*WM: [1] 3 networks found
*WM: [2] AP: -xxxxxxxxxxx
*WM: [2] AP: -xxxxxxxxxxx
*WM: [2] AP: -xxxxxxxxxxx
--------------- CUT HERE FOR EXCEPTION DECODER ---------------
Exception (28):
epc1=0x40221b78 epc2=0x00000000 epc3=0x00000000 excvaddr=0x0000017c depc=0x00000000stack>>>
ctx: cont
sp: 3ffff940 end: 3fffffc0 offset: 0190
3ffffad0: 00000004 402549ba 3ffffb0c 402129b4
3ffffae0: 00000004 0000017c 3ffffb00 40212720
3ffffaf0: 402561fd 0000017c 3ffffb54 40203d45
3ffffb00: 00000000 00000040 00000000 007d697b
3ffffb10: 00000004 03000000 3ffffb54 402129b4
3ffffb20: 3fff1014 3ffef2fc 00000001 40204ef9
3ffffb30: 00000ea0 00000001 00000001 00000009
3ffffb40: 4025623e 00000fa0 3ffffbe4 402125e8
3ffffb50: 00000f89 3fff13e4 0040004f 80000000
3ffffb60: 3fff3d7c 005c005f 80000000 33373031
3ffffb70: 40256200 00000001 3ffffbe4 00000009
3ffffb80: ffffffff 00000058 0000000c 00000037
3ffffb90: ffffffff 00000011 00000047 00000053
3ffffba0: 3ffffbc0 00000000 3ffffbd8 00000000
3ffffbb0: 3ffef3fc 402549b2 3ffef2fc 4020aa10
3ffffbc0: 3fff1294 0075007f 80000000 00000000
3ffffbd0: 3fff36a4 3fff2658 3fff17fc 00e900ef
3ffffbe0: 80000000 3fff457c 0f920f9f 80000000
3ffffbf0: 3ffef3fc 00000001 3fff25fc 4021cafc
3ffffc00: 0000026b 00000000 00000000 401000e9
3ffffc10: 00000000 00000000 3fff14a4 40205a3d
3ffffc20: 00000000 3fff36a4 3fff365c 40209824
3ffffc30: 00000000 00000000 00000000 69746365
3ffffc40: 00000000 00000000 401002bd 00000000
3ffffc50: 0001cdc1 00000000 00000000 00000001
3ffffc60: 00000000 00000000 3fff365c 00000000
3ffffc70: 3ffef3fc 3fff3680 3fff365c 4020b532
3ffffc80: 4021da5c 00000000 00001388 4020e563
3ffffc90: 00000000 00000000 3fff125c 00000000
3ffffca0: 3ffef3fc 00000001 3ffef2fc 4020b7b4
3ffffcb0: 3ffef3fc 00000001 3ffef2fc 402071a8
3ffffcc0: 00000000 00000000 00000001 401001a8
3ffffcd0: 000e000f 00000000 3ffef2fc 00000000
3ffffce0: 3ffef3fc 00000001 3ffef2fc 00000000
3ffffcf0: 3ffef3fc 00000001 3ffef2fc 4020b9f4
3ffffd00: 402595c4 40100c00 3ffef2fc 40203c5c
3ffffd10: 3ffe8a13 00000000 00000000 3ffe8626
3ffffd20: 3fffdad0 3ffef2fc 3ffef2fc 3ffe8626
3ffffd30: 3fffdad0 3ffef2fc 3ffef2fc 4020bbae
3ffffd40: 3fffdad0 40203e73 00000020 00000073
3ffffd50: 3ffffe20 3ffe8a09 3ffef2fc 40205308
3ffffd60: 3ffe8a13 3ffe8a0a 4021c7dc 4021c7c8
3ffffd70: 00000100 3ffe863a 3ffef600 3ffe8626
3ffffd80: 3fffdad0 3ffef2fc 3ffef600 40202bcb
3ffffd90: 40201028 3ffffde0 4021c7dc 4021c7c8
3ffffda0: 00000100 3fff0374 0000017c 40217b4d
3ffffdb0: 00000000 00000001 00000000 00000080
3ffffdc0: 3fff08d2 00000080 00000000 00000000
3ffffdd0: ffffffff 00000000 4021730c 3fff0894
3ffffde0: 00000000 00000100 00002000 00000030
3ffffdf0: 003f80fc 5a7b4bed 3ffe8d00 002f8000
3ffffe00: 3fff0374 00000000 00000000 40217eec
3ffffe10: 00000000 00000000 3ffffe26 3ffffe20
3ffffe20: 3ffe8a05 3ffe8a00 3fff3294 00000014
3ffffe30: 00000001 3ffe88f3 00100000 00000100
3ffffe40: 00000002 3ffffea0 003f80fc 3ffe89fb
3ffffe50: 3ffe89f6 3fff3274 00000014 00000001
3ffffe60: 3ffe88f3 3ffe89f3 3ffe89ee 3fff311c
3ffffe70: 00000008 00000001 3ffe88f3 3ffe89e8
3ffffe80: 3ffe89e3 3fff113c 00000014 00000001
3ffffe90: 3ffe88f3 3ffe89de 3ffe89d9 3fff2bfc
3ffffea0: 00000006 00000001 3ffe88f3 3ffe89d2
3ffffeb0: 3ffe89cd 3fff110c 00000028 00000001
3ffffec0: 3ffe88f3 3ffef2fc 00000001 3ffef778
3ffffed0: 00000000 0000017c 3fff0374 3ffef778
3ffffee0: 00000001 3fff0374 3fff036c 40215059
3ffffef0: 3fffdad0 00000000 3ffef764 4021d22c
3fffff00: 3ffe8b4a 3ffef4a8 3ffef764 3ffef778
3fffff10: 3fffdad0 00000000 3ffef764 40202f98
3fffff20: 00000024 ffffffff 0000000c 00000000
3fffff30: 0000d85b 7fc00000 00000000 00000000
3fffff40: 00000000 4021dbb8 3ffef600 3ffef548
3fffff50: ffffffff 20202020 302e3820 20200030
3fffff60: 007a1200 e191681a 31202000 302e3538
3fffff70: 00000000 37202020 0030302e 41a00000
3fffff80: 00000000 00000000 00000001 401001a8
3fffff90: 3fffdad0 00000000 3ffef764 3ffef778
3fffffa0: 3fffdad0 00000000 3ffef764 40213944
3fffffb0: feefeffe feefeffe 3ffe865c 40100d0d
<<<stack<<<
--------------- CUT HERE FOR EXCEPTION DECODER ---------------
H!⸮ԅe⸮⸮⸮⸮H&9mounting FS...
mounted file system
*WM: [3] allocating params bytes: 20
*WM: [2] Added Parameter: server
*WM: [2] Added Parameter: port
*WM: [2] Added Parameter: topic
*WM: [2] Added Parameter: ID
*WM: [2] Added Parameter: user
*WM: [3] Updated _max_params: 10
*WM: [3] re-allocating params bytes: 40
*WM: [2] Added Parameter: pass
*WM: [1] AutoConnect
*WM: [1] No Credentials are Saved, skipping connect
*WM: [2] Starting Config Portal
Anyboday an idea how to solve this?
here is my code;
#include <FS.h> //this needs to be first, or it all crashes and burns...
#ifdef ESP32
#include <SPIFFS.h>
#endif
//needed for library
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson
#include <DNSServer.h>
#include <PubSubClient.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define resteButton 16 //D0
char configFileName[] = "/config.json";
#define MQTTSERVER "192.168.1.101" // Home Server
#define MQTTSERVERPORT "1883"
#define MQTTTOPIC "Test"
#define MQTTID "1" // Leave blank to auto generate one
#define MQTTUSER "username"
#define MQTTPASSWORD "password"
char mqtt_server[20] = MQTTSERVER; // Placeholders for MQTT server settings
char mqtt_port[6] = MQTTSERVERPORT;
char mqtt_user[20] = MQTTUSER;
char mqtt_pass[20] = MQTTPASSWORD;
char mqtt_ID[20] = MQTTID;
char mqtt_topic[20] = MQTTTOPIC;
unsigned long lastMsg = 0;
#define MSG_BUFFER_SIZE (50)
char msg[MSG_BUFFER_SIZE];
int value = 0;
int restebuttonstat = 0;
uint32_t delayMS;
//flag for saving data
bool shouldSaveConfig = false;
WiFiClient espClient;
PubSubClient client(espClient);
WiFiManager wifiManager;
//callback notifying us of the need to save config
void saveConfigCallback () {
Serial.println("Should save config");
shouldSaveConfig = true;
}
void reset_setting() {
// Reset Wifi settings for testing
wifiManager.resetSettings();
//clean FS for testing
SPIFFS.format();
}
void wifimqtt() {
//read configuration from FS json
Serial.println("mounting FS...");
if (SPIFFS.begin()) {
Serial.println("mounted file system");
if (SPIFFS.exists(configFileName)) {
//file exists, reading and loading
Serial.println("reading config file");
File configFile = SPIFFS.open(configFileName, "r");
if (configFile) {
Serial.println("opened config file");
size_t size = configFile.size();
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
#ifdef ARDUINOJSON_VERSION_MAJOR >= 6
DynamicJsonDocument json(1024);
auto deserializeError = deserializeJson(json, buf.get());
serializeJson(json, Serial);
if ( ! deserializeError ) {
#else
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
json.printTo(Serial);
if (json.success()) {
#endif
Serial.println("\nparsed json");
strcpy(mqtt_server, json["mqtt_server"]);
strcpy(mqtt_port, json["mqtt_port"]);
strcpy(mqtt_topic, json["mqtt_topic"]);
strcpy(mqtt_ID, json["mqtt_ID"]);
strcpy(mqtt_user, json["mqtt_user"]);
strcpy(mqtt_pass, json["mqtt_pass"]);
} else {
Serial.println("failed to load json config");
}
configFile.close();
}
}
} else {
Serial.println("failed to mount FS");
}
//end read
// The extra parameters to be configured (can be either global or just in the setup)
// After connecting, parameter.getValue() will get you the configured value
// id/name placeholder/prompt default length
WiFiManagerParameter custom_mqtt_server("server", "mqtt server", mqtt_server, 40);
WiFiManagerParameter custom_mqtt_port("port", "mqtt port", mqtt_port, 6);
WiFiManagerParameter custom_mqtt_topic("topic", "mqtt topic", mqtt_topic, 20);
WiFiManagerParameter custom_mqtt_ID("ID", "mqtt ID", mqtt_ID, 8);
WiFiManagerParameter custom_mqtt_user("user", "mqtt user", mqtt_user, 20);
WiFiManagerParameter custom_mqtt_pass("pass", "mqtt pass", mqtt_pass, 20);
//set config save notify callback
wifiManager.setSaveConfigCallback(saveConfigCallback);
//set static ip
// wifiManager.setSTAStaticIPConfig(IPAddress(10,0,1,99), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
//add all your parameters here
wifiManager.addParameter(&custom_mqtt_server);
wifiManager.addParameter(&custom_mqtt_port);
wifiManager.addParameter(&custom_mqtt_topic);
wifiManager.addParameter(&custom_mqtt_ID);
wifiManager.addParameter(&custom_mqtt_user);
wifiManager.addParameter(&custom_mqtt_pass);
//set minimum quality of signal so it ignores AP's under that quality
//defaults to 8%
//wifiManager.setMinimumSignalQuality();
//sets timeout until configuration portal gets turned off
//useful to make it all retry or go to sleep
//in seconds
//wifiManager.setTimeout(120);
//fetches ssid and pass and tries to connect
//if it does not connect it starts an access point with the specified name
//here "AutoConnectAP"
//and goes into a blocking loop awaiting configuration
if (!wifiManager.autoConnect("AutoConnectAP", "password")) {
Serial.println("failed to connect and hit timeout");
delay(3000);
//reset and try again, or maybe put it to deep sleep
ESP.reset();
delay(5000);
}
shouldSaveConfig = true;
//if you get here you have connected to the WiFi
Serial.println("connected to: )");
String ssid = WiFi.SSID();
String password = WiFi.psk();
Serial.print(ssid);
Serial.println(" ; ");
Serial.println(password);
//read updated parameters
strcpy(mqtt_server, custom_mqtt_server.getValue());
strcpy(mqtt_port, custom_mqtt_port.getValue());
strcpy(mqtt_topic, custom_mqtt_topic.getValue());
strcpy(mqtt_ID, custom_mqtt_ID.getValue());
strcpy(mqtt_user, custom_mqtt_user.getValue());
strcpy(mqtt_pass, custom_mqtt_pass.getValue());
// strcpy(blynk_token, custom_blynk_token.getValue());
//save the custom parameters to FS
if (shouldSaveConfig) {
Serial.println("saving config");
#ifdef ARDUINOJSON_VERSION_MAJOR >= 6
DynamicJsonDocument json(1024);
#else
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
#endif
json["mqtt_server"] = mqtt_server;
json["mqtt_port"] = mqtt_port;
json["mqtt_topic"] = mqtt_topic;
json["mqtt_ID"] = mqtt_ID;
json["mqtt_user"] = mqtt_user;
json["mqtt_pass"] = mqtt_pass;
File configFile = SPIFFS.open(configFileName, "w");
if (!configFile) {
Serial.println("failed to open config file for writing");
}
#ifdef ARDUINOJSON_VERSION_MAJOR >= 6
serializeJson(json, Serial);
serializeJson(json, configFile);
#else
json.printTo(Serial);
json.printTo(configFile);
#endif
configFile.close();
//end save
}
if (WiFi.status() == WL_CONNECTED) {
wifiManager.stopWebPortal();
wifiManager.stopConfigPortal();
}
Serial.println("local ip");
Serial.println(WiFi.localIP());
// client.setServer(mqtt_server, 12025);
String mqtt_prt_x_s(mqtt_port);
const uint16_t mqtt_port_x = strtol(mqtt_prt_x_s.c_str(), NULL, 10);
const char* mqtt_server_conn = mqtt_server;
client.setServer(mqtt_server_conn, mqtt_port_x);
Serial.println("MQTT Server startet with: ");
Serial.print(mqtt_server_conn);
Serial.print(" ; ");
Serial.println(mqtt_port_x);
client.setCallback(callback);
WiFi.setAutoReconnect(true);
WiFi.persistent(true);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
delay(500);
wifimqtt();
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("connected");
// Once connected, publish an announcement...
//client.publish("test1", "hello world");
// ... and resubscribe
//client.subscribe("Moisture_in");
} else {
client.setCallback(callback);
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void callback(char* topic, byte * payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
String message = message + (char) payload[i]; // convert *byte to string
Serial.print(message);
}
Serial.println();
}
void loop() {
restebuttonstat = digitalRead(resteButton);
if (restebuttonstat == HIGH) {
// Reset settings for testing
reset_setting();
wifimqtt();
}
if (!client.connected()) {
reconnect();
}
client.loop();
Serial.println(F("Hello"));
client.publish(mqtt_topic, "Hello");
}
thanks and have a nice day