Hi All,
Does the Telegram Universal Bot work with the SIM800L?, I am trying to convert the ESP32 Motion CAM from WiFi to the SIM800L, I saw a post that mentioned the Universal Bot works on TLS 1.2 and the SIM800L does not support that, is that true?
Regards,
Gavin
Hi Gavin.
I’m not sure.
I never looked at that subject in detail.
But, from a quick search, it seems that SIM800L only supports TLS1.0.
Regards,
Sara
Hi Sara,
Thanks, I tried to have a go at it but it keeps crashing, not sure why its crashing, not sure if anyone else has tried it?, I am using the
<TinyGsmClient.h> and
<HTTPClient.h>
and wondering if I should rather use AT Commands?, also I am struggling to find out what I can use here?
//client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
Hi.
You don’t need to add anything to that line.
Just make sure you have the latest version of the library installed. Follow the following steps to install it:
Follow the next steps to install the latest release of the library.
- Click here to download the Universal Arduino Telegram Bot library.
- Go to Sketch > Include Library > Add.ZIP Library...
- Add the library you’ve just downloaded.
Regards,
Sara
Thanks!,
I tried a few things and keep getting the following error
Connecting to APN: iphone.vodacom.za OK
[E][sccb.c:115] SCCB_Write(): SCCB_Write Failed addr:0x30, reg:0x12, data:0x80, ret:-1
[E][camera.c:1113] camera_probe(): Detected camera not supported.
[E][camera.c:1379] esp_camera_init(): Camera probe failed with error 0x20004
Camera init failed with error 0x20004Initializing modem…
My Code is below, perhaps you might spot something?
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include “soc/soc.h”
#include “soc/rtc_cntl_reg.h”
#include “esp_camera.h”
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
// Use @myidbot to find out the chat ID of an individual or a group
// Also note that you need to click “start” on a bot before it can
// message you
String chatId = “XXXXXXXXXXXXX”;
// Initialize Telegram BOT
String BOTtoken = “XXXXXXXXXXXXXXXXXXXXX”;
bool sendPhoto = false;
//CAMERA_MODEL_AI_THINKER
#define PWDN_GPIO_NUM 32
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 0
#define SIOD_GPIO_NUM 26
#define SIOC_GPIO_NUM 27
#define Y9_GPIO_NUM 35
#define Y8_GPIO_NUM 34
#define Y7_GPIO_NUM 39
#define Y6_GPIO_NUM 36
#define Y5_GPIO_NUM 21
#define Y4_GPIO_NUM 19
#define Y3_GPIO_NUM 18
#define Y2_GPIO_NUM 5
#define VSYNC_GPIO_NUM 25
#define HREF_GPIO_NUM 23
#define PCLK_GPIO_NUM 22
#define FLASH_LED_PIN 4
bool flashState = LOW;
// Motion Sensor
bool motionEnabled = false;
bool motionDetected = false;
int botRequestDelay = 1000; // mean time between scan messages
long lastTimeBotRan; // last time messages’ scan has been done
#define MODEM_TX 13
#define MODEM_RX 12
// Configure TinyGSM library
#define TINY_GSM_MODEM_SIM800 // Modem is SIM800
#define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb
#define SerialAT Serial1
// Your GPRS credentials (leave empty, if not needed)
const char apn[] = “iphone.vodacom.za”; // use https://wiki.apnchanger.org
const char gprsUser[] = “”; // GPRS User
const char gprsPass[] = “”; // GPRS Password
#include <TinyGsmClient.h>
#include <HTTPClient.h>
#ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, SerialMon);
TinyGsm modem(debugger);
#else
TinyGsm modem(SerialAT);
#endif
TinyGsmClientSecure client(modem);
UniversalTelegramBot bot (BOTtoken, client);
void handleNewMessages(int numNewMessages);
String sendPhotoTelegram();
// Indicates when motion is detected
static void IRAM_ATTR detectsMovement(void * arg){
//Serial.println(“MOTION DETECTED!!!”);
motionDetected = true;
}
void setup(){
//client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
Serial.begin(115200);
pinMode(FLASH_LED_PIN, OUTPUT);
digitalWrite(FLASH_LED_PIN, flashState);
// Restart SIM800 module, it takes quite some time
// To skip it, call init() instead of restart()
SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
Serial.println(“Initializing modem…”);
delay(1000);
modem.restart();
Serial.print(“Connecting to APN: “);
Serial.print(apn);
if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
Serial.println(” fail”);
}
else {
Serial.println(” OK”);
}
camera_config_t config;
config.ledc_channel = LEDC_CHANNEL_0;
config.ledc_timer = LEDC_TIMER_0;
config.pin_d0 = Y2_GPIO_NUM;
config.pin_d1 = Y3_GPIO_NUM;
config.pin_d2 = Y4_GPIO_NUM;
config.pin_d3 = Y5_GPIO_NUM;
config.pin_d4 = Y6_GPIO_NUM;
config.pin_d5 = Y7_GPIO_NUM;
config.pin_d6 = Y8_GPIO_NUM;
config.pin_d7 = Y9_GPIO_NUM;
config.pin_xclk = XCLK_GPIO_NUM;
config.pin_pclk = PCLK_GPIO_NUM;
config.pin_vsync = VSYNC_GPIO_NUM;
config.pin_href = HREF_GPIO_NUM;
config.pin_sscb_sda = SIOD_GPIO_NUM;
config.pin_sscb_scl = SIOC_GPIO_NUM;
config.pin_pwdn = PWDN_GPIO_NUM;
config.pin_reset = RESET_GPIO_NUM;
config.xclk_freq_hz = 20000000;
config.pixel_format = PIXFORMAT_JPEG;
//init with high specs to pre-allocate larger buffers
if(psramFound()){
config.frame_size = FRAMESIZE_UXGA;
config.jpeg_quality = 10; //0-63 lower number means higher quality
config.fb_count = 2;
} else {
config.frame_size = FRAMESIZE_SVGA;
config.jpeg_quality = 12; //0-63 lower number means higher quality
config.fb_count = 1;
}
// camera init
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
Serial.printf(“Camera init failed with error 0x%x”, err);
delay(1000);
ESP.restart();
}
// Drop down frame size for higher initial frame rate
sensor_t * s = esp_camera_sensor_get();
s->set_framesize(s, FRAMESIZE_CIF); // UXGA|SXGA|XGA|SVGA|VGA|CIF|QVGA|HQVGA|QQVGA
// PIR Motion Sensor mode INPUT_PULLUP
//err = gpio_install_isr_service(0);
err = gpio_isr_handler_add(GPIO_NUM_15, &detectsMovement, (void *) 15);
if (err != ESP_OK){
Serial.printf(“handler add failed with error 0x%x \r\n”, err);
}
err = gpio_set_intr_type(GPIO_NUM_15, GPIO_INTR_POSEDGE);
if (err != ESP_OK){
Serial.printf(“set intr type failed with error 0x%x \r\n”, err);
}
bot.sendMessage(chatId, “Bot started up”, “”);
}
void loop(){
if (sendPhoto){
Serial.println(“Preparing photo”);
sendPhotoTelegram();
sendPhoto = false;
}
if(motionDetected && motionEnabled){
bot.sendMessage(chatId, “Motion detected!!”, “”);
Serial.println(“Motion Detected”);
sendPhotoTelegram();
motionDetected = false;
}
if (millis() > lastTimeBotRan + botRequestDelay){
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);
}
lastTimeBotRan = millis();
}
}
String sendPhotoTelegram(){
const char* myDomain = “api.telegram.org”;
String getAll = “”;
String getBody = “”;
camera_fb_t * fb = NULL;
fb = esp_camera_fb_get();
if(!fb) {
Serial.println(“Camera capture failed”);
delay(1000);
ESP.restart();
return “Camera capture failed”;
}
Serial.println(“Connect to ” + String(myDomain));
if (client.connect (myDomain, 443)) {
Serial.println(“Connection successful”);
String head = “–RandomNerdTutorials\r\nContent-Disposition: form-data; name=\”chat_id\”; \r\n\r\n” + chatId + “\r\n–RandomNerdTutorials\r\nContent-Disposition: form-data; name=\”photo\”; filename=\”esp32-cam.jpg\”\r\nContent-Type: image/jpeg\r\n\r\n”;
String tail = “\r\n–RandomNerdTutorials–\r\n”;
uint16_t imageLen = fb->len;
uint16_t extraLen = head.length() + tail.length();
uint16_t totalLen = imageLen + extraLen;
client.println(“POST /bot”+BOTtoken+”/sendPhoto HTTP/1.1”);
client.println(“Host: ” + String(myDomain));
client.println(“Content-Length: ” + String(totalLen));
client.println(“Content-Type: multipart/form-data; boundary=RandomNerdTutorials”);
client.println();
client.print(head);
uint8_t *fbBuf = fb->buf;
size_t fbLen = fb->len;
for (size_t n=0;n<fbLen;n=n+1024) {
if (n+1024<fbLen) {
client.write(fbBuf, 1024);
fbBuf += 1024;
}
else if (fbLen%1024>0) {
size_t remainder = fbLen%1024;
client.write(fbBuf, remainder);
}
}
client.print(tail);
esp_camera_fb_return(fb);
int waitTime = 10000; // timeout 10 seconds
long startTimer = millis();
boolean state = false;
while ((startTimer + waitTime) > millis()){
Serial.print(“.”);
delay(100);
while (client.available()){
char c = client.read();
if (c == ‘\n’){
if (getAll.length()==0) state=true;
getAll = “”;
}
else if (c != ‘\r’){
getAll += String(c);
}
if (state==true){
getBody += String(c);
}
startTimer = millis();
}
if (getBody.length()>0) break;
}
client.stop();
Serial.println(getBody);
}
else {
getBody=”Connected to api.telegram.org failed.”;
Serial.println(“Connected to api.telegram.org failed.”);
}
return getBody;
}
void handleNewMessages(int numNewMessages){
Serial.print(“Handle New Messages: “);
Serial.println(numNewMessages);
for (int i = 0; i < numNewMessages; i++){
// Chat id of the requester
String chat_id = String(bot.messages[i].chat_id);
if (chat_id != chatId){
bot.sendMessage(chat_id, “Unauthorized user”, “”);
continue;
}
// Print the received message
String text = bot.messages[i].text;
Serial.println(text);
String fromName = bot.messages[i].from_name;
if (text == “/flash”) {
flashState = !flashState;
digitalWrite(FLASH_LED_PIN, flashState);
}
if (text == “/photo”) {
sendPhoto = true;
Serial.println(“New photo request”);
}
if (text == “/motion_on”) {
motionEnabled = true;
bot.sendMessage(chatId, “Motion notifications ON.”, “”);
}
if (text == “/motion_off”) {
motionEnabled = false;
bot.sendMessage(chatId, “Motion notifications OFF.”, “”);
}
if (text == “/motion_state”) {
if (motionEnabled == false) {
bot.sendMessage(chatId, “Motion notifications are OFF.”, “”);
}
else{
bot.sendMessage(chatId, “Motion notifications are ON.”, “”);
}
}
if (text == “/start”){
String welcome = “Welcome ” + fromName + ” to the Trysome-CAM Telegram bot.\n”;
welcome += “/photo : takes a new photo\n”;
welcome += “/flash : toggle flash LED\n”;
welcome += “/motion_on : enables motion sensor\n”;
welcome += “/motion_off: disables motion sensor \n”;
welcome += “/motion_state: get motion sensor state (ON or OFF)”;
bot.sendMessage(chatId, welcome, “”);
}
}
}
Hi.
That is a problem related to the camera. Here’s what might be the issue:
- the camera is not properly connected to the board;
- the pin definition on your code is wrong;
- insufficient or not stable power supply to your board.
Regards,
Sara
Hi Sara,
I tried a new ESP-CAM Board and it works 100% with the Wi-Fi example, when I try the code above all I got in the terminal program was below:
Connecting to APN: iphone.vodacom.za OK
I think there is something wrong in the code which i am trying to figure out. I need to comment out:
//client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
As I keep getting a compile error no matter what I try.
Hi.
How are you connecting the ESP32-CAM to the SIM800L module?
If for some reason, you’re using I2C communication protocol, it may interfere with the ESP32-CAM initialization.
As for the certificate error, you need to update your library as I mentioned earlier.
Regards,
Sara
Hi Sara,
I am connecting using SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
I am using the latest library but it can’t compile the program unless I comment out the line
Hi Sara,
//CAMERA_MODEL_AI_THINKER
#define PWDN_GPIO_NUM 32
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 0
#define SIOD_GPIO_NUM 26
#define SIOC_GPIO_NUM 27
#define Y9_GPIO_NUM 35
#define Y8_GPIO_NUM 34
#define Y7_GPIO_NUM 39
#define Y6_GPIO_NUM 36
#define Y5_GPIO_NUM 21
#define Y4_GPIO_NUM 19
#define Y3_GPIO_NUM 18
#define Y2_GPIO_NUM 5
#define VSYNC_GPIO_NUM 25
#define HREF_GPIO_NUM 23
#define PCLK_GPIO_NUM 22
#define FLASH_LED_PIN 4
I have TX from SIM800 in Pin 13 and RX on PIn 12 and the Motion Sensor on PIn 15
Hi.
It might be that those pins are interfering with the camera initialization. Or the serial communication itself.
Or, if you’re not powering the SIM800L module with an external power supply, the ESP32-CAM might not be able to provide enough power and you’ll get that error. How are you powering the SIM800L module?
Regards,
Sara
Hi Sara,
I can’t seem to come right, are there other pins that might work?
I am powering the SIM800L from an external voltage and there is sufficient voltage and current
Any help would be greatly appreciated
Hi.
To be honest, I don’t know. I never tried to use SIM800L with the ESP32-CAM.
I guess you’ll have to try it yourself. But, I’m not sure if that will solve the issue 😐
Regards,
Sara
I am using Lilygo Sim800L T-Call, got Telegram, SMS, Websocket, Webserver, Access Point all working fine.
using this for the SSL.
Hi again.
I thought you were using a SIM800L module with the ESP32-CAM, not an ESP32 with the SIM800L included.
As I mentioned, I never tried that approach.
I found the following, that might help in some way:
- https://github.com/DmitryLapshov/esp32cam_sim800Lv2_proof_of_concept
- https://forum.arduino.cc/t/esp32-cam-esp32-sim800l-web-server-help-needed/615352
Regards,
Sara
Hi Sara,
It definitely the ESP32 CAM that I am busy with.
I have did get the device to send a SMS using the following commands, so I know the GSM is working but for the rest I am at a loss at the moment
res = modem.sendSMS(“27823212162”, String(“Hello from ESP32 CAM”));
Hi NUR AZLIN AIRUL AZLIN MOHAMED ZOHDI
Would you be able to share the Telegram code so I can see what you have done?, still a little confusing for me why its not working and that would help allot. I will share the code once I have it working
Regards,
Gavin
//Telegram group
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
#define BOTtoken “your token”
#define CHAT_ID “-your chat id” // make sure not to miss the dash(-)
int botRequestDelay = 1000;
unsigned long lastTimeBotRan;
const char hostname0[] = “www.howsmyssl.com”;
const char hostname3[] = “api.telegram.org”;
const int port0 = 443;
// Set serial for debug console (to Serial Monitor)
#define SerialMon Serial
// Set serial for AT commands (to SIM800 module)
#define SerialAT Serial1
//GSM related include files
#include <TinyGsmClient.h>
#include <TinyGsmClientSIM800.h>
#include <ArduinoHttpClient.h>
#include “SSLClient.h” // use this library – link given in earlier comment
#include “ca_cert.h” // put the https://api.telegram.org cert in this header file
TinyGsm modem(SerialAT);
TinyGsmClient client(modem);
SSLClient secure_presentation_layer(&client);
SSLClient telegramSSL(&client);
HttpClient http_client = HttpClient(secure_presentation_layer, hostname0, port0); // for testing SSL
HttpClient bot_telegram = HttpClient(telegramSSL, hostname3, port0);
UniversalTelegramBot bot(BOTtoken, bot_telegram);
void setup() {
telegramSSL.setCACert(TELEGRAM_CERTIFICATE_ROOT);
everything else are the same as per Rui & Sara sample tutorial
/cheers
Hi NUR AZLIN AIRUL AZLIN MOHAMED ZOHDI thanks for the info, somehow I am battling to compile, I am using Platform IO and when it “Linking” i get a whole bunch of errors relating to the SSLClient.
Any ideas what could cause it?, I have used the same SSLClient.h file in the reference you sent
Hi,
You can email me (fbi1671@gmail.com) your code, maybe I could have a look at it.
fbi1671@gmail.com
Hi NUR AZLIN AIRUL AZLIN MOHAMED ZOHDI
I just sent the Drop Box link to your email….;-) – please check your junk mail in case it lands there…;-)
You are also mixing up the
TinyGsmClientSecure client(modem); and
SSLClient secure_presentation_layer(&client);
SSLClient telegramSSL(&client);
SSLClient is an alternative to TinyGsmClientSecure.
I could not get Telegram to run when using TinyGsmClientSecure, but works well when using SSLClient.
Would I then change it to:
TinyGsmClient client(modem);
SSLClient secure_presentation_layer(&client);
SSLClient telegramSSL(&client);
HttpClient http_client = HttpClient(secure_presentation_layer, hostname0, port0); // for testing SSL
HttpClient bot_telegram = HttpClient(telegramSSL, hostname3, port0);
UniversalTelegramBot bot(BOTtoken, bot_telegram);
I have looked to see where to generate a ca-cert for telegram but not sure where to look? or how to do it?
Open your telegram in a browser, and click the padlock icon. should be able to get the ca_cert from there. download it and open using a text editor. copy and paste in the header file.
Hi Sara,
I have managed to get this going thanks to Ahmad help!, only issue is I can’t seem to send a Photo with the SIM800L not sure if it is the 2G Network or if I need to convert the image to a base 64 before I can send.
As for the rest it’s working…;-)
Regards,
Gavin