Trying to add code on esp32 to SimpleServer example sketch to enable text input Telegram credentials over web browser instead of hard coding on to the sketch. Using Preferences.h library saving credentials to retentive flash.
The code works fine with hard coded Chat_ID and BOTtoken credentials (uncommented line 45 & 46, #define ........, as well as commented line 199, 213, 251, 255). It does not work with (commented line 45 & 46, //#define ........, as well as uncommented line 199, 213, 251, 255) soft credential text inputs on the web page over acquired IP address on the browser after connecting to local network. http://IPADDRESS. It shows on serial monitor that it has successfully acquired credentials. Not too conversant with Arduino coding and need help. Perhaps something to do with type of variable I have tried to use (String) because the credentials show up on the serial monitor without quotes "". Here is the code:
//
// A simple server implementation showing how to:
// * serve static messages
// * read GET and POST parameters
// * handle missing pages / 404s
//
#include <Arduino.h>
#ifdef ESP32
#include <WiFi.h>
#include <AsyncTCP.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#endif
#include <ESPAsyncWebSrv.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <SimpleTimer.h>
#include <Preferences.h>
////#include <AsyncElegantOTA.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <Arduino.h>
Preferences preferences;
AsyncWebServer server(80);
String CHAT_ID = "";
String BOTtoken = "";
String CHATID;
String TOKEN;
String chat_id;
#define BOTtoken "YOUR_TOKEN"
#define CHAT_ID "YOUR_CHATID"
unsigned long lastTime = 0;
unsigned long timerDelay = 10;
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
const char* PARAM_INPUT_1 = "CHATID";
const char* PARAM_INPUT_2 = "TOKEN";
const char* PARAM_MESSAGE = "message";
////#define BOTtoken "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
////#define CHAT_ID "XXXXXXXXX"
void notFound(AsyncWebServerRequest *request) {
request->send(404, "text/plain", "Not found");
}
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
html {
font-family: Arial;
display: inline-block;
margin: 0px auto;
text-align: center;
}
h2 { font-size: 3.0rem; }
p { font-size: 3.0rem; }
.units { font-size: 1.2rem; }
.ds-labels{
font-size: 1.5rem;
vertical-align:middle;
padding-bottom: 15px;
}
</style>
</head>
<body>
<h2>Telegram Bot Credentials</h2>
<p>
<form action="/get">
Telegram chat_Id: <input type="text" name="CHATID">
<input type="submit" value="Submit">
</form><br>
<form action="/get">
Telegram HTTP API Token: <input type="text" name="TOKEN">
<input type="submit" value="Submit">
</form><br>
<br><i class="" style=" color:#0796f5;"></i></br>
<br><span class="ds-labels">Telegram chat_Id: </span>
<span id="chatid">%CHATID%</span>
<br> <span class="ds-labels">Telegram HTTP API Token: </span>
<span id="token">%TOKEN%</span>
</p>
</body>
<script>
setInterval(function ( ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("chatid").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/chatid", true);
xhttp.send();
}, 10000) ;
setInterval(function ( ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("token").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/token", true);
xhttp.send();
}, 10000) ;
</script>
</html>)rawliteral";
// Replaces placeholder with DS18B20 values
String processor(const String & var){
Serial.println(var);
if(var == "CHATID"){
return CHAT_ID;
return String();
}
if(var == "TOKEN"){
return BOTtoken;
return String();
}
}
const unsigned long BOT_MTBS = 1000; // mean time between scan messages
unsigned long bot_lasttime; // last time messages' scan has been done
WiFiClientSecure secured_client;
UniversalTelegramBot bot(BOTtoken, secured_client);
void handleNewMessages(int numNewMessages)
{
Serial.print("handleNewMessages ");
Serial.println(numNewMessages);
String answer;
for (int i = 0; i < numNewMessages; i++)
{
telegramMessage &msg = bot.messages[i];
Serial.println("Received " + msg.text);
if (msg.text == "/help")
answer = "So you need _help_, uh? me too! use /start or /status";
else if (msg.text == "/start")
answer = "Welcome my new friend! You are the first *" + msg.from_name + "* I've ever met";
else if (msg.text == "/status")
answer = "All is good here, thanks for asking!";
else
answer = "Say what?";
bot.sendMessage(msg.chat_id, answer, "Markdown");
}
}
void bot_setup()
{
const String commands = F("["
"{\"command\":\"help\", \"description\":\"Get bot usage help\"},"
"{\"command\":\"start\", \"description\":\"Message sent when you open a chat with a bot\"},"
"{\"command\":\"status\",\"description\":\"Answer device current status\"}" // no comma on last command
"]");
bot.setMyCommands(commands);
//bot.sendMessage("25235518", "Hola amigo!", "Markdown");
}
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.printf("WiFi Failed!\n");
return;
}
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/html", index_html, processor);
});
server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) {
String inputMessage1;
String inputMessage2;
String inputParam1;
String inputParam2;
// GET input1 value on <ESP_IP>/get?input1=<inputMessage>
if (request->hasParam(PARAM_INPUT_1)) {
inputMessage1 = request->getParam(PARAM_INPUT_1)->value();
inputParam1 = PARAM_INPUT_1;
CHATID = (inputMessage1); ////.c_str());
////CHAT_ID = String(CHATID);
Serial.print("CHAT_ID=");
Serial.println(CHAT_ID);
preferences.begin("wifiCredentials",false);
preferences.putString("CHAT_ID",CHAT_ID);
Serial.print("Successfully put Chat_ID = ");
Serial.println(CHAT_ID);
preferences.end();
}
// GET input2 value on <ESP_IP>/get?input2=<inputMessage>
else if (request->hasParam(PARAM_INPUT_2)) {
inputMessage2 = request->getParam(PARAM_INPUT_2)->value();
inputParam2 = PARAM_INPUT_2;
TOKEN = (inputMessage2); ////.c_str());
////BOTtoken = String(TOKEN);
Serial.print("BOTtoken=");
Serial.println(BOTtoken);
preferences.begin("wifiCredentials",false);
preferences.putString("BOTtoken",BOTtoken);
Serial.print("Successfully put BOTtoken = ");
Serial.println(BOTtoken);
preferences.end();
}
else {
inputMessage1 = "No chat_id";
inputMessage2 = "No Token";
inputParam1 = "none";
inputParam2 = "none";
}
Serial.println(inputMessage1);
request->send(200, "text/html", "HTTP GET request sent to your ESP on input field ("
+ inputParam1 + ") with value: " + inputMessage1 +
"<br><a href=\"/\">Return to Home Page</a>");
Serial.println(inputMessage2);
request->send(200, "text/html", "HTTP GET request sent to your ESP on input field ("
+ inputParam2 + ") with value: " + inputMessage2 +
"<br><a href=\"/\">Return to Home Page</a>");
});
server.onNotFound(notFound);
server.on("/CHATID", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", CHATID.c_str());
});
server.on("/TOKEN", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", TOKEN.c_str());
});
// Start server
////AsyncElegantOTA.begin(&server);
server.begin();
delay(100);
preferences.begin("wifiCredentials",true);
////CHAT_ID = preferences.getString("CHAT_ID",CHAT_ID); ////.c_str();
Serial.print("Retrieved CHAT_ID = ");
Serial.println(CHAT_ID);
////BOTtoken = preferences.getString("BOTtoken",BOTtoken); ////.c_str();
Serial.print("Retrieved BOTtoken = ");
Serial.println(BOTtoken);
preferences.end();
delay(2000);
uint32_t chipId = 0;
// Checks for new messages every 1 second.
int botRequestDelay = 1000;
unsigned long lastTimeBotRan;
////bot_setup();
}
void loop() {
////wm.process();
Serial.print("CHAT_ID = ");
Serial.println(CHAT_ID);
Serial.print("BOTtoken = ");
Serial.println(BOTtoken);
delay(10000);
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();
}
}