witnessmenow / Universal-Arduino-Telegram-Bot

Use Telegram on your Arduino (ESP8266 or Wifi-101 boards)
MIT License
1.09k stars 302 forks source link

esp32: Universal-Arduino-Telegram-Bot with BluetoothSerial failed #292

Open zenbooster opened 1 year ago

zenbooster commented 1 year ago

Hello! I use ESP-WROOM-32 (ESP32 DEVKITV1).

#define TEST 1 // 0 - WebServer (OK) or 1 - Telegram Bot (ERROR)
// If commented out, it will work:
#define BT_BEGIN // SerialBT.begin(HOSTNAME, true);

#include <WiFiManager.h>
#if TEST == 0
#include <WebServer.h>
#elif TEST == 1
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#endif
// in the board manager - esp32 1.0.6
// otherwise it won't work in master mode
#include <BluetoothSerial.h>

#define LED_BUILTIN 2

#define HOSTNAME "test"
#define WIFI_SSID HOSTNAME
#define WIFI_PASS "password"

BluetoothSerial SerialBT;
uint8_t address[6] = {0x20, 0x21, 0x04, 0x08, 0x39, 0x93};
bool connected = false;

WiFiManager wifiManager;
#if TEST == 0
WebServer webServer(80);
String responseHTML = "<!DOCTYPE html><html>"
                      "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"
                      "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}"
                      "</style></head>"
                      "<body><h1>ESP32 Web Server</h1>"
                      "<p>Hello World</p>"
                      "</body></html>";
#elif TEST == 1
#define BOT_TOKEN "..."
#define CHAT_ID "..."
const unsigned long BOT_MTBS = 250; // mean time between scan messages

WiFiClientSecure secured_client;
UniversalTelegramBot tgb(BOT_TOKEN, secured_client);
unsigned long tgb_lasttime; // last time messages' scan has been done

void tgb_handle_new_messages(int numNewMessages)
{
  Serial.print("handleNewMessages ");
  Serial.println(numNewMessages);
  for (int i = 0; i < numNewMessages; i++)
  {
    String chat_id = String(tgb.messages[i].chat_id);
    if (chat_id != CHAT_ID )
    {
      tgb.sendMessage(chat_id, "Unauthorized user", "");
    }
    else
    {
      String text = tgb.messages[i].text;
      String from_name = tgb.messages[i].from_name;
      if (from_name == "")
        from_name = "Guest";
      if (text == "/cmd")
      {
        tgb.sendMessage(chat_id, "under construction", "");
      }
      if (text == "/status")
      {
        tgb.sendMessage(chat_id, "under construction", "");
      }
      if (text == "/start")
      {
        String welcome = "Welcome to telegram bot, " + from_name + ".\n";
        welcome += "/cmd : to select meditation treshold\n";
        welcome += "/status : Returns current status\n";
        tgb.sendMessage(chat_id, welcome, "Markdown");
      }
    }
  }
}
#endif

void setup()
{
  Serial.begin(115200);
  wifiManager.setHostname(HOSTNAME);
  wifiManager.autoConnect(WIFI_SSID, WIFI_PASS);

#if TEST == 0
  webServer.onNotFound([]() {
    webServer.send(200, "text/html", responseHTML);
  });
  webServer.begin();
#elif TEST == 1
  secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org

  //p_tgb = new UniversalTelegramBot(BOT_TOKEN, secured_client);
  tgb_lasttime = 0;
#endif

#ifdef BT_BEGIN
  SerialBT.begin(HOSTNAME, true);
#endif
  Serial.println(F("The device started in master mode, make sure remote BT device is on!"));
}

void loop()
{
#if TEST == 0
  webServer.handleClient();
#elif TEST == 1
  if (millis() - tgb_lasttime > BOT_MTBS)
  {
    int numNewMessages = tgb.getUpdates(tgb.last_message_received + 1);
    while (numNewMessages)
    {
      Serial.println("got response");
      tgb_handle_new_messages(numNewMessages);
      numNewMessages = tgb.getUpdates(tgb.last_message_received + 1);
    }
    tgb_lasttime = millis();
  }
#endif
  // The error occurs without this:
  /*if (SerialBT.available())
  {
    digitalWrite(LED_BUILTIN, HIGH);
    // ...
  }
  else
  if (!SerialBT.connected())
  {
    digitalWrite(LED_BUILTIN, LOW);

    connected = SerialBT.connect(address);

    if(connected) {
      Serial.println("Connected Succesfully!");
    } else {
        Serial.println(F("Failed to connect. Make sure remote device is available and in range."));
    }
  }*/
}
ets Jun  8 2016 00:22:57

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1216
ho 0 tail 12 room 4
load:0x40078000,len:10944
load:0x40080400,len:6388
entry 0x400806b4
E (64) psram: PSRAM ID read error: 0xffffffff
*wm:[1] AutoConnect 
*wm:[2] ESP32 event handler enabled 
*wm:[2] Setting Hostnames:  test
*wm:[2] Setting WiFi hostname 
*wm:[2] Connecting as wifi client... 
*wm:[2] setSTAConfig static ip not set, skipping 
*wm:[1] Connecting to SAVED AP: zenbooster
*wm:[1] connectTimeout not set, ESP waitForConnectResult... 
*wm:[2] Connection result: WL_CONNECTED
*wm:[1] AutoConnect: SUCCESS 
*wm:[2] Connected in 2924 ms
*wm:[1] STA IP Address: 192.168.1.130
The device started in master mode, make sure remote BT device is on!
*wm:[2] [EVENT] WIFI_REASON:  200
*wm:[2] [EVENT] WIFI_REASON:  200
*wm:[2] [EVENT] WIFI_REASON:  201
*wm:[2] [EVENT] WIFI_REASON: NO_AP_FOUND 
*wm:[2] [EVENT] WIFI_REASON:  201
*wm:[2] [EVENT] WIFI_REASON: NO_AP_FOUND 
*wm:[2] [EVENT] WIFI_REASON:  201
*wm:[2] [EVENT] WIFI_REASON: NO_AP_FOUND 
*wm:[2] [EVENT] WIFI_REASON:  201
*wm:[2] [EVENT] WIFI_REASON: NO_AP_FOUND 
*wm:[2] [EVENT] WIFI_REASON:  201
*wm:[2] [EVENT] WIFI_REASON: NO_AP_FOUND 
*wm:[2] [EVENT] WIFI_REASON:  201
*wm:[2] [EVENT] WIFI_REASON: NO_AP_FOUND