[GH-ISSUE #475] not act shouldSaveConfig #398

Closed
opened 2026-02-28 01:25:07 +03:00 by kerem · 0 comments
Owner

Originally created by @iranweld on GitHub (Dec 24, 2017).
Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/475

My Dear

in this file shouldSaveConfig not recall void writeBotTokenToEeprom(int offset) for save data in memory
PLZ help me where is this problem

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>

#include <EEPROM.h>

#include <DNSServer.h> //Local DNS Server used for redirecting all rs to the configuration portal
#include <ESP8266WebServer.h> //Local WebServer used to serve the configuration portal
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager WiFi Configuration Magic

bool Start = false;

bool shouldSaveConfig = false;

#define BOT_TOKEN_LENGTH 46

const int ledPin = D7;
const int resetConfigPin = D3; //When high will reset the wifi manager config
int i=0;
char botToken[BOT_TOKEN_LENGTH] = "";

WiFiClientSecure client;
UniversalTelegramBot *bot;

int Bot_mtbs = 1000; //mean time between scan messages
long Bot_lasttime; //last time messages' scan has been done

int ledStatus = 0;

//flag for saving data

//callback notifying us of the need to save config
void saveConfigCallback () {
Serial.println("Should save config");
shouldSaveConfig = true;
}

void readBotTokenFromEeprom(int offset){
for(int i = offset; i<BOT_TOKEN_LENGTH; i++ ){
botToken[i] = EEPROM.read(i);
}
EEPROM.commit();
}

void writeBotTokenToEeprom(int offset){
for(int i = offset; i<BOT_TOKEN_LENGTH; i++ ){
EEPROM.write(i, botToken[i]);
Serial.print("Wrote: ");
Serial.println(botToken[i]);
Serial.println(botToken);
}
EEPROM.commit();
}

void handleNewMessages(int numNewMessages) {
Serial.println("handleNewMessages");
Serial.println(String(numNewMessages));
for(int i=0; i<numNewMessages; i++) {
String chat_id = String(bot->messages[i].chat_id);
String text = bot->messages[i].text;
if (text == "/ledon") {
digitalWrite(ledPin, LOW); // turn the LED on (HIGH is the voltage level)
ledStatus = 1;
bot->sendMessage(chat_id, "Led is ON", "");
}
if (text == "/ledoff") {
ledStatus = 0;
digitalWrite(ledPin, HIGH); // turn the LED off (LOW is the voltage level)
bot->sendMessage(chat_id, "Led is OFF", "");
}
if (text == "/status") {
if(ledStatus==1){
bot->sendMessage(chat_id, "Led is ON", "");
} else {
bot->sendMessage(chat_id, "Led is OFF", "");
}
}
if (text == "/start") {
String welcome = "Welcome from FlashLedBot, your personal Bot on ESP8266\n";
welcome = welcome + "/ledon : to switch the Led ON\n";
welcome = welcome + "/ledoff : to switch the Led OFF\n";
welcome = welcome + "/status : Returns current status of LED\n";
bot->sendMessage(chat_id, welcome, "Markdown");
}
}
}

void setup() {
Serial.begin(115200);

EEPROM.begin(BOT_TOKEN_LENGTH);
pinMode(resetConfigPin, INPUT);
pinMode(ledPin, OUTPUT); // initialize digital ledPin as an output.
delay(10);
digitalWrite(ledPin, HIGH); // initialize pin as off

Serial.println("read bot token");
readBotTokenFromEeprom(0);
Serial.println(botToken);

WiFiManager wifiManager;
wifiManager.setSaveConfigCallback(saveConfigCallback);
//Adding an additional config on the WIFI manager webpage for the bot token
WiFiManagerParameter custom_bot_id("botid", "Bot Token", botToken, 50);
wifiManager.addParameter(&custom_bot_id);
//If it fails to connect it will create a TELEGRAM-BOT access point
wifiManager.autoConnect("Telegram_Control");

strcpy(botToken, custom_bot_id.getValue());
if (shouldSaveConfig) {
for (int i = 0; i < 520; ++i) { EEPROM.write(i, 0); }
writeBotTokenToEeprom(0);
}

bot = new UniversalTelegramBot(botToken, client);
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
IPAddress ip = WiFi.localIP();
Serial.println(ip);

}

void loop() {
if ( digitalRead(resetConfigPin) == LOW ) {
Serial.println("Reset");
WiFi.disconnect();
Serial.println("Dq");
delay(500);
ESP.reset();
delay(5000);
}
if (millis() > Bot_lasttime + Bot_mtbs) {
int numNewMessages = bot->getUpdates(bot->last_message_received + 1);
while(numNewMessages) {
Serial.println("got response");
handleNewMessages(numNewMessages);
numNewMessages = bot->getUpdates(bot->last_message_received + 1);
}
Bot_lasttime = millis();
}
}

Originally created by @iranweld on GitHub (Dec 24, 2017). Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/475 My Dear in this file shouldSaveConfig not recall void writeBotTokenToEeprom(int offset) for save data in memory PLZ help me where is this problem #include <ESP8266WiFi.h> #include <WiFiClientSecure.h> #include <UniversalTelegramBot.h> #include <EEPROM.h> #include <DNSServer.h> //Local DNS Server used for redirecting all rs to the configuration portal #include <ESP8266WebServer.h> //Local WebServer used to serve the configuration portal #include <WiFiManager.h> //https://github.com/tzapu/WiFiManager WiFi Configuration Magic bool Start = false; bool shouldSaveConfig = false; #define BOT_TOKEN_LENGTH 46 const int ledPin = D7; const int resetConfigPin = D3; //When high will reset the wifi manager config int i=0; char botToken[BOT_TOKEN_LENGTH] = ""; WiFiClientSecure client; UniversalTelegramBot *bot; int Bot_mtbs = 1000; //mean time between scan messages long Bot_lasttime; //last time messages' scan has been done int ledStatus = 0; //flag for saving data //callback notifying us of the need to save config void saveConfigCallback () { Serial.println("Should save config"); shouldSaveConfig = true; } void readBotTokenFromEeprom(int offset){ for(int i = offset; i<BOT_TOKEN_LENGTH; i++ ){ botToken[i] = EEPROM.read(i); } EEPROM.commit(); } void writeBotTokenToEeprom(int offset){ for(int i = offset; i<BOT_TOKEN_LENGTH; i++ ){ EEPROM.write(i, botToken[i]); Serial.print("Wrote: "); Serial.println(botToken[i]); Serial.println(botToken); } EEPROM.commit(); } void handleNewMessages(int numNewMessages) { Serial.println("handleNewMessages"); Serial.println(String(numNewMessages)); for(int i=0; i<numNewMessages; i++) { String chat_id = String(bot->messages[i].chat_id); String text = bot->messages[i].text; if (text == "/ledon") { digitalWrite(ledPin, LOW); // turn the LED on (HIGH is the voltage level) ledStatus = 1; bot->sendMessage(chat_id, "Led is ON", ""); } if (text == "/ledoff") { ledStatus = 0; digitalWrite(ledPin, HIGH); // turn the LED off (LOW is the voltage level) bot->sendMessage(chat_id, "Led is OFF", ""); } if (text == "/status") { if(ledStatus==1){ bot->sendMessage(chat_id, "Led is ON", ""); } else { bot->sendMessage(chat_id, "Led is OFF", ""); } } if (text == "/start") { String welcome = "Welcome from FlashLedBot, your personal Bot on ESP8266\n"; welcome = welcome + "/ledon : to switch the Led ON\n"; welcome = welcome + "/ledoff : to switch the Led OFF\n"; welcome = welcome + "/status : Returns current status of LED\n"; bot->sendMessage(chat_id, welcome, "Markdown"); } } } void setup() { Serial.begin(115200); EEPROM.begin(BOT_TOKEN_LENGTH); pinMode(resetConfigPin, INPUT); pinMode(ledPin, OUTPUT); // initialize digital ledPin as an output. delay(10); digitalWrite(ledPin, HIGH); // initialize pin as off Serial.println("read bot token"); readBotTokenFromEeprom(0); Serial.println(botToken); WiFiManager wifiManager; wifiManager.setSaveConfigCallback(saveConfigCallback); //Adding an additional config on the WIFI manager webpage for the bot token WiFiManagerParameter custom_bot_id("botid", "Bot Token", botToken, 50); wifiManager.addParameter(&custom_bot_id); //If it fails to connect it will create a TELEGRAM-BOT access point wifiManager.autoConnect("Telegram_Control"); strcpy(botToken, custom_bot_id.getValue()); if (shouldSaveConfig) { for (int i = 0; i < 520; ++i) { EEPROM.write(i, 0); } writeBotTokenToEeprom(0); } bot = new UniversalTelegramBot(botToken, client); Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); IPAddress ip = WiFi.localIP(); Serial.println(ip); } void loop() { if ( digitalRead(resetConfigPin) == LOW ) { Serial.println("Reset"); WiFi.disconnect(); Serial.println("Dq"); delay(500); ESP.reset(); delay(5000); } if (millis() > Bot_lasttime + Bot_mtbs) { int numNewMessages = bot->getUpdates(bot->last_message_received + 1); while(numNewMessages) { Serial.println("got response"); handleNewMessages(numNewMessages); numNewMessages = bot->getUpdates(bot->last_message_received + 1); } Bot_lasttime = millis(); } }
kerem closed this issue 2026-02-28 01:25:07 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/WiFiManager#398
No description provided.