cotestatnt / AsyncTelegram2

Powerful, flexible and secure Arduino Telegram BOT library. Hardware independent, it can be used with any MCU capable of handling an SSL connection.
MIT License
85 stars 25 forks source link

echoBot_wifiNINA.io example does not echo.... #134

Closed DK9GZ closed 7 months ago

DK9GZ commented 8 months ago

I am using an Arduino Nano 33 IoT with WifiNINA. (Arduino IDE 2.2.1, WifiNINA 1.8.14, ArduinoJson 7.0.1, Asynctelegram2 2.2.3) The board is equipped with the SSL certificate for api.telegram.org and works fine with "Universal Telegram Library" (echo example and custom code).

Using the echoBot_wifiNINA.io example sends the

BOT @wh41debugbot online /help all commands avalaible.

message to the chat successfully, but does not react to any messages (like "test", "/test", "echo, "/echo", "123") I sent via Telegram. I would expect an echo here.

Enabling debug mode in the lib brings (creds & ids anonymized):

15:21:16.983 -> Attempting to connect to SSID: $SSID 15:21:27.185 -> Connected to WiFi 15:21:27.185 -> Connected to wifi. SSID: $SSID. IP Address: 192.168.10.28 15:21:27.185 -> signal strength (RSSI):-65 dBm 15:21:27.185 -> 15:21:27.185 -> Starting TelegramBot... 15:21:27.185 -> 15:21:27.185 -> Test Telegram connection... 23673 [AsyncTelegram2.cpp:22 checkConnection()] Start handshaking... 15:21:28.666 -> 25165 [AsyncTelegram2.cpp:40 checkConnection()] Connected using Telegram hostname 15:21:28.666 -> Last connection was 25 seconds ago 15:21:28.666 -> 15:21:28.666 -> 25168 [AsyncTelegram2.cpp:93 sendCommand()] Command getMe, payload: 15:21:28.666 -> 15:21:28.666 -> 25169 [AsyncTelegram2.cpp:96 sendCommand()] Command getMe, payload: 15:21:28.666 -> 15:21:28.697 -> 25206 [AsyncTelegram2.cpp:446 getMe()]
15:21:28.697 -> { 15:21:28.697 -> "ok": true, 15:21:28.697 -> "result": { 15:21:28.697 -> "id": $ID, 15:21:28.697 -> "is_bot": true, 15:21:28.697 -> "first_name": "$BOTNAME", 15:21:28.697 -> "username": "$BOTNAME", 15:21:28.697 -> "can_join_groups": true, 15:21:28.697 -> "can_read_all_group_messages": false, 15:21:28.697 -> "supports_inline_queries": false 15:21:28.697 -> } 15:21:28.697 -> } 15:21:28.697 -> OK 15:21:28.697 -> 25220 [AsyncTelegram2.cpp:93 sendCommand()] Command sendMessage, payload: {"chat_id":$ID,"text":"BOT @$BOTNAME online\n/help all commands avalaible.","parse_mode":"HTML"} 15:21:28.742 -> 15:21:28.742 -> 25220 [AsyncTelegram2.cpp:96 sendCommand()] Command sendMessage, payload: {"chat_id":$ID,"text":"BOT @$BOTNAME online\n/help all commands avalaible.","parse_mode":"HTML"} 15:21:28.743 ->

Furthermore, I added in AsynchTelegram2.cpp, line 93: log_debug("Command %s, payload: %s\n", command, payload); Resulting in:

15:21:30.740 -> 27226 [AsyncTelegram2.cpp:93 sendCommand()] Command getUpdates, payload: {"limit":1,"timeout":0,"offset":0} 15:21:30.740 -> 15:21:32.737 -> 29227 [AsyncTelegram2.cpp:93 sendCommand()] Command getUpdates, payload: {"limit":1,"timeout":0,"offset":0} 15:21:32.737 -> 15:21:34.735 -> 31227 [AsyncTelegram2.cpp:93 sendCommand()] Command getUpdates, payload: {"limit":1,"timeout":0,"offset":0} 15:21:34.735 -> 15:21:36.744 -> 33228 [AsyncTelegram2.cpp:93 sendCommand()] Command getUpdates, payload: {"limit":1,"timeout":0,"offset":0} 15:21:36.744 -> 15:21:38.736 -> 35230 [AsyncTelegram2.cpp:93 sendCommand()] Command getUpdates, payload: {"limit":1,"timeout":0,"offset":0}

No errors, just silence. The if (myBot.getNewMessage(msg)) below went never true. Any hint, anyone?

Thanks a lot.

Finally, the complete code:

/*
  Name:        echoBot_wifiNINA.ino
  Created:     27/10/2022
  Author:      Tolentino Cotesta <cotestatnt@yahoo.com>
  Description: a simple example that check for incoming messages
               and reply the sender with the received message.

  !!!!!!!!!!!!!!!!!!!!!!!      IMPORTANT     !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  In order to use this library with WiFiNINA modules,
  you need to upload Telegram server SSL certitficate.

  Host to be added:  api.telegram.org

  How-to:
  https://support.arduino.cc/hc/en-us/articles/360016119219-How-to-add-certificates-to-Wifi-Nina-Wifi-101-Modules-

  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
*/

#include <SPI.h>
#include <WiFiNINA.h>
#include <AsyncTelegram2.h>

// Timezone definition
#include <time.h>
#define MYTZ "CET-1CEST,M3.5.0,M10.5.0/3"

WiFiSSLClient client;
AsyncTelegram2 myBot(client);

const char* ssid  =  "$SSID";     // SSID WiFi network
const char* pass  =  "$PASSWD";     // Password  WiFi network
const char* token =  "$TOKEN";  // Telegram token

// Check the userid with the help of bot @JsonDumpBot or @getidsbot (work also with groups)
// https://t.me/JsonDumpBot  or  https://t.me/getidsbot
int64_t userid = $ID;

int status = WL_IDLE_STATUS;

void printWiFiStatus() {
  Serial.print("Connected to wifi. ");
  Serial.print("SSID: ");
  Serial.print(WiFi.SSID());
  Serial.print(". IP Address: ");
  Serial.println(WiFi.localIP());

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

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);

  // init and wait for serial port to connect (needed for native USB port)
  Serial.begin(115200);
  while (!Serial) ;

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

  String fv = WiFi.firmwareVersion();
  if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
    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, pass);

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

  Serial.println("\nStarting TelegramBot...");
  // Set the Telegram bot properies
  myBot.setUpdateTime(2000);
  myBot.setTelegramToken(token);

  // Set the JSON buffer size for receeved message parsing (default 1024 bytes)
  myBot.setJsonBufferSize(1024);

  // Check if all things are ok
  Serial.print("\nTest Telegram connection... ");
  myBot.begin() ? Serial.println("OK") : Serial.println("NOK");

  char welcome_msg[128];
  snprintf(welcome_msg, 128, "BOT @%s online\n/help all commands avalaible.", myBot.getBotName());

  // Send a message to specific user who has started your bot
  myBot.sendTo(userid, welcome_msg);
}

void loop() {

  static uint32_t ledTime = millis();
  if (millis() - ledTime > 150) {
    ledTime = millis();
    digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  }

  // local variable to store telegram message data
  TBMessage msg;

  // if there is an incoming message...
  if (myBot.getNewMessage(msg)) {
    // Send a message to your public channel
    String message ;
    message += "Message from @";
    message += myBot.getBotName();
    message += ": ";
    message += msg.text;
    Serial.println(message);

    // echo the received message
    myBot.sendMessage(msg, msg.text);
  }
}
cotestatnt commented 8 months ago

Hi @DK9GZ and thanks for the detailed report. I'll try to take a look at it as soon as possible.

DK9GZ commented 8 months ago

I just checked the ublox NINA firmware:

16:43:25.274 -> WiFiNINA firmware check.
16:43:25.274 -> 
16:43:26.039 -> Firmware version installed: 1.5.0
16:43:26.039 -> Latest firmware version available : 1.5.0
16:43:26.039 -> 
16:43:26.039 -> Check result: PASSED

It seems to be the lastest version available (see https://github.com/arduino/nina-fw/releases).

cotestatnt commented 8 months ago

Hi @DK9GZ I've just tried with latest library release and a Nano33 IoT and it works. Could you share exactly the code you are trying (except token off course)

DK9GZ commented 8 months ago

Hi @cotestatnt, thanks for your investigation. The code above is exactly the code I am testing the lib with (excluding credentials). I was confused, then realized that you wrote:

It relies on ArduinoJson v6 library so, in order to use a AsyncTelegram2 object, you need to install the ArduinoJson library first.

So I downgraded my version of the ArduinoJson from 7.0.1 to 6.21.5 - et voilà: the bot answers my calls. It seems that V7 is not compatible with the Async2 lib. A look in the compiler output with V7 gives a lot of red lines (but no errors that would prevent the programming of the Arduino).

I can work with that kind workaround. Thank you!

cotestatnt commented 8 months ago

This means you are not using the latest realease of AsyncTelegram2 (v2.3.0) which is fully compatible with ArduinoJson v7.

Something goes wrong with Arduino library manager: it shows as latest the version 2.2.3 but this is not true :rage:

DK9GZ commented 8 months ago

Oh, I see. My Arduino IDE shows Async 2.2.3 as latest version. I updated it manually to 2.3.0 and ArduinoJson to 7.0.2. Works fine, red lines in compiler output are gone as well.