witnessmenow / Universal-Arduino-Telegram-Bot

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

I cannot communicate with the bot using an Arduino Nano 33 iot #290

Open TheMechatronicEngineer opened 2 years ago

TheMechatronicEngineer commented 2 years ago

I've tried for the first time the example echobot using an arduino nano 33 iot and the classical arduino ide. I have created a bot using botfather. I have uploaded the code of the example on the board. The serial monitor tells me that it's connected to the wifi and shows me the SSID the IP address and the signal strenght but when I try to write something using the telegram bot, nothing happens: I don't receive the echo message in the bot chat. Can anybody help me?

// ----------------------------
// Standard Libraries
// ----------------------------
#include <SPI.h>

// ----------------------------
// Additional Libraries - each one of these will need to be installed.
// ----------------------------
#include <WiFiNINA.h>
// Library for using network deatures of the official Arudino
// Wifi Boards (MKR WiFi 1010, Nano 33 IOT etc)

// Search for "nina" in the Arduino Library Manager
// https://github.com/arduino-libraries/WiFiNINA

#include <UniversalTelegramBot.h>
// Library for connecting to Telegram

// Search for "Telegram" in the Arduino Library Manager
// Install the "Universal Telegram" one by Brian Lough
// https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot

#include <ArduinoJson.h>
// Library used for parsing Json from the API responses

// Search for "Arduino Json" in the Arduino Library manager
// https://github.com/bblanchon/ArduinoJson

// Wifi network station credentials
char ssid[] = "SSID";         // your network SSID (name)
char password[] = "password"; // your network password
// Telegram BOT Token (Get from Botfather)
#define BOT_TOKEN "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

const unsigned long BOT_MTBS = 1000; // mean time between scan messages

int status = WL_IDLE_STATUS;

WiFiSSLClient client;
UniversalTelegramBot bot(BOT_TOKEN, client);
unsigned long bot_lasttime; // last time messages' scan has been done

void handleNewMessages(int numNewMessages)
{
  for (int i = 0; i < numNewMessages; i++)
  {
    bot.sendMessage(bot.messages[i].chat_id, bot.messages[i].text, "");
  }
}

void printWiFiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your board's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

void setup()
{
    //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if (fv < "1.0.0") {
    Serial.println("Please upgrade the firmware");
  }

  // attempt to connect to WiFi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, password);

    // wait 10 seconds for connection:
    delay(10000);
  }
  Serial.println("Connected to wifi");
  printWiFiStatus();
}

void loop()
{
  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();
  }
}
tadcrazio commented 1 year ago

Did you figure it out? I'm having a similar issue with my RP2040. It looks like because we are not setting the ssl cert that telegram API won't work. I don't see a way in Wifi101 or Wifinina to set a cert or fingerprint.

gmcode85 commented 1 year ago

I have the same issue using a Arduino Wifi, with echobot.

I have turned the debug for this library on and I can see it joining the server okay and can send, but cannot receive anything. Has something changed in one of the other libraries used?

I tried this for adding the certificate: https://support.arduino.cc/hc/en-us/articles/360016119219-How-to-add-certificates-to-Wifi-Nina-Wifi-101-Modules

witnessmenow commented 1 year ago

You could maybe try a staticjson buffer instead of a dynamic one in the get updates method, I think esp devices have more dynamic memory than most, I'm not sure though

gmcode85 commented 1 year ago

Hi,

After half a day of debugging and learning about json objects etc, I tracked it down to the following fails on an Uno because an int is only 2 bytes, not 4.

bool UniversalTelegramBot::processResult(JsonObject result, int messageIndex) { int update_id = result["update_id"];

Make it a long the same as "last_message_received", that it compared to and it works and is covered for more devices.

long update_id = result["update_id"];

Hope this helps on IOTs.

garagesteve1155 commented 8 months ago

Anyone ever figure this out? Im having the same issue with my 33 IOT

tadcrazio commented 7 months ago

Anyone ever figure this out? Im having the same issue with my 33 IOT

FWIW I had switched to a different Telegram client, believe it was AsyncTelegram2 and didn't have issues there.