You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Great library that i could run multiple task without blocking each other, its also work perfectly using firebase as my server. But found some issue that UniversalTelegramBot from couldn't work with this library. It does compiled without any problems, but there's no sign that it is connected with the telegram server. Here's my code.
#include <Arduino.h>
#include <Scheduler.h>
#include <ArduinoJson.h>
#include <SoftwareSerial.h>
#include <WiFiManager.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
SoftwareSerial stm(14, 12);
DynamicJsonDocument senD(768);
DynamicJsonDocument geT(768);
float voltage = 0.00;
float current = 0.00;
bool state = 0;
int b, num, treshold;
String err;
WiFiManager wm;
#define BOT_TOKEN "------------------------------------"
X509List cert(TELEGRAM_CERTIFICATE_ROOT);
WiFiClientSecure secured_client;
UniversalTelegramBot bot(BOT_TOKEN, secured_client);
class Telegram : public Task {
protected:
void handleNewMessages(int numNewMessages){
Serial.print("handleNewMessages ");
Serial.println(numNewMessages);
for (int i = 0; i < numNewMessages; i++) {
String chat_id = bot.messages[i].chat_id;
String text = bot.messages[i].text;
String from_name = bot.messages[i].from_name;
if (from_name == "")
from_name = "Guest";
if (text == "/Pasang")
{
digitalWrite(2, LOW); // turn the LED on (HIGH is the voltage level)
bot.sendMessage(chat_id, "Connected!", "");
state = 1;
}
if (text == "/Putus")
{
// ledStatus = 0;
digitalWrite(2, HIGH); // turn the LED off (LOW is the voltage level)
bot.sendMessage(chat_id, "Disconnected!", "");
state = 0;
}
if (text == "/Status")
{
if (digitalRead(2))
{
if(current > treshold){
bot.sendMessage(chat_id, "Voltage :" + String(voltage) + "\nCurrent :" + String(current) + "| Power : " + String(treshold) + "\nSomething suspicious!", "");
} else{
bot.sendMessage(chat_id, "Voltage :" + String(voltage) + "\nCurrent :" + String(current) + "| Power : " + String(treshold), "");
}
}
else
{
bot.sendMessage(chat_id, "Voltage :" + String(voltage) + "\nCurrent :" + String(current) + "| Power : " + String(treshold), "");
}
}
}
}
void setup(){
configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
secured_client.setTrustAnchors(&cert); // Add root certificate for api.telegram.org
}
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();
}
}
private:
const unsigned long BOT_MTBS = 100; // mean time between scan messages
unsigned long bot_lasttime; // last time messages' scan has been done
} telegram;
class ConnectWiFi : public Task {
protected:
void setup(){
//reset settings - for testing
//wm.resetSettings();
WiFiManagerParameter v("mcb", "Input The Treshold", "", 50);
wm.addParameter(&v);
wm.setDebugOutput(false);
wm.setConfigPortalTimeout(120);
if (WiFi.status() != WL_CONNECTED) {
if (!wm.autoConnect()) {
Serial.println("failed to connect and hit timeout");
delay(3000);
//reset and try again, or maybe put it to deep sleep
ESP.restart();
delay(5000);
}
treshold = atoi(v.getValue());
}
}
void loop(){
if (WiFi.status() != WL_CONNECTED) {
if (!wm.autoConnect()) {
Serial.println("failed to connect and hit timeout");
delay(3000);
//reset and try again, or maybe put it to deep sleep
ESP.restart();
delay(5000);
}
}
Serial.print("WiFi connected at ");
Serial.println(WiFi.localIP());
delay(5000);
}
} wifi_conn;
class printSerial : public Task {
protected:
void loop() {
Serial.println("Random : " + String(state));
if(err!=""){
Serial.print(F("deserializeJson() failed with code "));
Serial.println(err);
}
Serial.println("Get: " + String(num));
delay(500);
}
} printSerial;
class Json : public Task {
protected:
void loop() {
b = state;
senD["num2"] = b;
serializeJson(senD, stm);
DeserializationError error = deserializeJson(geT, stm);
err = "";
if (error) {
err = error.c_str();
// return;
}
num = geT["num"];
state = !state;
digitalWrite(2, !state);
delay(500);
}
} Json;
void setup() {
WiFi.mode(WIFI_STA);
Serial.begin(115200);
stm.begin(9600);
pinMode(2, OUTPUT);
delay(1000);
Scheduler.start(&telegram);
Scheduler.start(&wifi_conn);
Scheduler.start(&printSerial);
Scheduler.start(&Json);
Scheduler.begin();
}
void loop() {}
The text was updated successfully, but these errors were encountered:
Great library that i could run multiple task without blocking each other, its also work perfectly using firebase as my server. But found some issue that UniversalTelegramBot from couldn't work with this library. It does compiled without any problems, but there's no sign that it is connected with the telegram server. Here's my code.
The text was updated successfully, but these errors were encountered: