I have downloaded a sketch from GitHub, which I am adapting for my needs. https://github.com/UdoK/esp8266_p1meter_sv
It is for a device using ESP8266 to read an electricity meter by serial and sending it on via WiFi using MQTT.
The problem I am having is that this sketch uses WiFiManager to publish a config page when it starts up in AP mode. I used that with difficulty to set the WiFi data so it could connect properly.
And then the device somehow loses the information after some time of running and then it switches back to the AP when I have uploaded a new f/w via OTA. And now it is nearly impossible to make it accept the config data when I finally manage to connect to the AP and open the config page. It is driving me nuts!
So please can someone tell me how I can disable this WiFiManager handling of the login data to WiFi and let me hard code it so I do not have to waste my time when it loses the connection?
I don't think so because when I am able to connect to the AP webpage only the ssid/password fields are empty, whereas the custom fields for MQTT setup are still populated properly.
The author of the sketch has added custom parameters to WiFiManager's AP website for MQTT use and these are perfectly still saved and shown on the page.
Not so with the WiFi data.
And I don't even know how to erase WiFi settings. I have posted earlier another question here regarding the same ESP8266 project where I asked where these settings were stored so I could force them to my own WiFi data.
But people drifted off and there was no real valid reply...
For analysing what is really going on it would make sense that you add a lot of debug-output to your code and then post the code and the debug-output both as code-sections.
Well, I have downloaded the sketch and if I knew how to remove WiFiManager from the code I would do so.
I want to use the sketch for a rather complex electricity meter reading project and that is why I tried it.
When it runs OK it is doing its stuff, but if I leave the device running for a number of hours and then restart it - it has lost its ssid settings and there is big trouble!
That is why I want to get rid of it if possible. Or find where it stores the ssid/password so I can set them to the correct values.
analysing all the details how the WiFi-manager works (seems to be dozens of hours of work)
then modify the WiFi-manager
adding debug-output and record the serial output for really long until loosing WiFi-credentials has happened
not using this WiFi-manager at all
keeping the mqtt-code and the smart-meter-decoding code
add your own set up WiFi and MQTT-interface by using another library like ESPUI and store the values with using the library preferences.
Do you mean the screenshot from Arduino IDE by StefanL38?
If so it is not applicable because I use VSCode with PlatformIO as the IDE...
And these flags seem not to be available for the upload command....
So there is no control on what gets erased when programming the flash. Sigh....
Now I have to dig down into the WiFi stuff and rip out all WiFiManager related stuff and set it to connect to a fixed ssid so I can continue testing.
By the way this is the last part of the debug output when it chokes after I have accessed the config AP page and set the ssid and password entries:
*WM: Sent config page <== This is when I hit Save on the config page
*WM: WiFi save
*WM: Parameter
*WM: host
*WM: 192.168.117.131
*WM: Parameter
*WM: port
*WM: 1883
*WM: Parameter
*WM: user
*WM:
*WM: Parameter
*WM: pass
*WM:
*WM: Sent wifi save page
*WM: Connecting to new AP
*WM: Connecting as wifi client...
*WM: Status:
*WM: 0
*WM: [ERROR] WiFi.begin res:
*WM: 7
*WM: Connection result:
*WM: 255
*WM: Failed to connect.
Notice how the ssid and password are missing from the debug serial output!
and then add some pretty standard "connect to WiFi-code"
// Demo-Code connect an ESP8266 to a WiFi-network using stationmode STA
// stationmode is simply join the WiFi-network as your computer or smartphone does it
// the code has three useful functions one for easy identifiying the code in the flash
// one for non-blocking timing
// a heartbeat-blinker function for indicating the state the code is in
// the code is written with two programming rules:
// 1. put EACH functionality into its OWN function
// 2. give EACH function a SELF-explaining name what the function does
// you should follow these programming rules
#include <ESP8266WiFi.h>
const char *ssid = "FRITZ!Box 7490";
const char *password = "80841232631090916208";
void PrintFileNameDateTime() {
Serial.println( F("Code running comes from file ") );
Serial.println( F(__FILE__));
Serial.print( F(" compiled ") );
Serial.print(F(__DATE__));
Serial.print( F(" ") );
Serial.println(F(__TIME__));
}
boolean TimePeriodIsOver (unsigned long &periodStartTime, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - periodStartTime >= TimePeriod )
{
periodStartTime = currentMillis; // set new expireTime
return true; // more time than TimePeriod) has elapsed since last time if-condition was true
}
else return false; // not expired
}
unsigned long MyTestTimer = 0; // variables MUST be of type unsigned long
const byte OnBoard_LED = 2;
void BlinkHeartBeatLED(int IO_Pin, int BlinkPeriod) {
static unsigned long MyBlinkTimer;
pinMode(IO_Pin, OUTPUT);
if ( TimePeriodIsOver(MyBlinkTimer, BlinkPeriod) ) {
digitalWrite(IO_Pin, !digitalRead(IO_Pin) );
}
}
void ConnectToWiFi() {
WiFi.mode(WIFI_STA);
Serial.println("WiFi.mode(WIFI_STA)");
int myCount = 0;
Serial.print("trying to connect to #");
Serial.print(ssid);
Serial.println("#");
WiFi.begin(ssid, password);
// Wait for connection
while (WiFi.status() != WL_CONNECTED && myCount < 61) {
yield();
BlinkHeartBeatLED(OnBoard_LED, 50); // blink LED fast during attempt to connect
if ( TimePeriodIsOver(MyTestTimer, 500) ) { // once every 500 miliseconds
Serial.print("."); // print a dot
myCount++;
if (myCount > 60) { // after 30 dots = 15 seconds restart
Serial.println();
Serial.print("not yet connected");
}
}
}
if (WiFi.status() == WL_CONNECTED ) {
Serial.println("");
Serial.print("Connected to #");
Serial.print(ssid);
Serial.print("# IP address: ");
Serial.println(WiFi.localIP());
}
}
void printWiFiModeAndIP() {
Serial.println("\n\nWiFi parameters:");
Serial.print("Mode: ");
Serial.println(WiFi.getMode() == WIFI_AP ? "Station" : "Client");
Serial.print("IP address: ");
Serial.println(WiFi.getMode() == WIFI_AP ? WiFi.softAPIP() : WiFi.localIP());
}
void setup() {
delay(1000);
Serial.begin(115200);
delay(1000);
Serial.println( F("Setup-Start") );
PrintFileNameDateTime();
ConnectToWiFi();
printWiFiModeAndIP();
}
void PrintHelloMsg() {
Serial.print( F("Hi there I'm the demo-code my IP address is: ") );
Serial.println(WiFi.localIP());
}
void loop() {
BlinkHeartBeatLED(OnBoard_LED, 500); // change blinking to a lower frequency indicating beeing connected
if ( TimePeriodIsOver(MyTestTimer, 1000) ) {
PrintHelloMsg();
}
}
/*
most ESP8266 boards have a blue LED connected to GPIO-pin 2
This blue LED is used to indicate state connecting to WiFi by blinking fast
state beeing connected to Wifi by blinking with 1 Hz
If the WiFi-connection is successfully established the serial monitor shows
08:44:02.915 -> Setup-Start
08:44:02.915 -> Code running comes from file
08:44:02.915 -> your-path\yourfilename.ino
08:44:02.915 -> compiled date/time of compiling
08:44:02.971 -> WiFi.mode(WIFI_STA)
08:44:02.971 -> trying to connect to #Your SSID#
08:44:03.362 -> ....
08:44:04.215 -> Connected to #Your SSID# IP address: given IP-adress NNN.NNN.NNN.NNN
08:44:04.865 -> Hi there I'm the demo-code my IP address is: NNN.NNN.NNN.NNN
*/
With the ESPUI-library you can create your own webui without html-coding
depending on your C++-knowledge learning how to use the ESPUI-library will take you 1 to 5 hours.
You can use the preferences library for storing MQTT-data
@StefanL38
Thanks for your extensive reply! Much obliged!
Meanwhile I have ifdef:ed away the WiFiManager stuff and managed to load that f/w up and I am again connected!
But my work is not ending there, the plan is to implement my webconfig function which puts a webserver online at port 8088 for config of the device. I wrote that back in 2017 when I designed a temp/humid measurement center with a pretty large number of config items. It involves handling html page code as the base, though....
Now you pointed me towards the alternative ESPUI library, which I will take a dive into to see if it is an alternative to my own old code.
I mistakenly made a reply to my own thread here before realizing that I introduced a new problem in an existing thread.
So I have moved the content of this post into a new thread ESP8266 device will not connect WiFi reliably about WiFi connectivity.
So therefore I erase the content of this post and refer to the new thread instead.