Hallo,
wir stehen aktuell vor dem Problem, dass wir einen String aus einer Funktion außerhalb der setup und loop Funktion übergeben müssen. Für unser Projekt auf dem ESP32 benötigt der UniversalTelegramBot den Bot Token, welcher über den WiFiManager in einer json Datei gespeichert wird. Soweit funktioniert der Code auch, der Token und die ChatID lassen sich über den Webbrowser auf den ESP übertragen, aber leider ist uns nicht klar wie wir ihn dann zu Begin an den TelegramBot übergeben können. Eventuell kann uns hierbei jemand weiterhelfen?
#include <FS.h> //this needs to be first, or it all crashes and burns...
#include <SPIFFS.h>
FS* filesystem = &SPIFFS;
//nötige Bibliotheken
#include <DNSServer.h>
#include <WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#include <WiFi.h> //https://github.com/esp8266/Arduino
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson
//hier werden die nötigen Pins definiert
#define PIN_D0 0
#define LED_BUILTIN 1 // Pin D1 mapped to pin GPIO1/ADC12 of ESP32, control on-board LED
#define REED 34 // Pin D34 mapped to reed contact
#define WifiReset 35 // Pin D35 mapped to wifi/reset button
#define RedLed 32 // Pin D32 mapped to the red led
#define GreenLed 33 // Pin D33 mapped to the green led
#define mosi 23 // Pin D23 mapped to mosi from rfid
#define rst 22 // Pin D22 mapped to reset from rfid
#define sda 21 // Pin D21 mapped to sda from rfid
#define miso 19 // Pin D19 mapped to miso from rfid
#define sck 18 // Pin D18 mapped to sck from rfi
#define test 27
const int TRIGGER_PIN = PIN_D0; // Pin D0 mapped to pin GPIO0/BOOT/ADC11/TOUCH1 of ESP32
//define your default values here, if there are different values in config.json, they are overwritten.
char telegram_chatID[40];
char telegram_token[70];
const char* telegramFile()
{
//read configuration from FS json
Serial.println("mounting FS...");
if (SPIFFS.begin()) {
Serial.println("mounted file system");
if (SPIFFS.exists("/config.json")) {
//file exists, reading and loading
Serial.println("reading config file");
File configFile = SPIFFS.open("/config.json", "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);
DynamicJsonDocument json(1024);
auto deserializeError = deserializeJson(json, buf.get());
serializeJson(json, Serial);
if ( ! deserializeError ) {
Serial.println("\nparsed json");
strcpy(telegram_token, json["telegram_token"]);
Serial.print(telegram_token);
} else {
Serial.println("failed to load json config");
}
configFile.close();
}
}
} else {
Serial.println("failed to mount FS");
}
//end read
static char telegram_token_secret[60];
strcpy(telegram_token_secret, telegram_token);
return telegram_token_secret;
}
//static char telegram_token_secure[60];
//strcpy(telegram_token_secure, telegramFile());
//static telegram_token_secure = telegramFile();
WiFiClientSecure secured_client;
UniversalTelegramBot bot(telegramFile(), secured_client);
...
In dieser Variante bootet der ESP32 erst gar nicht.