wrote by Nebojsa from Croatia
Using the Telegram application to communicate with the KC868-A4 provides many possibilities.
For example, I chose one we have several interesting solutions that could find in practical situations and projects. Of course everyone has the opportunity to freely adapt to their needs.
In this example I used resources from KC868-A4:
- 4 relays (DO)
- 4 digital input’s (DI)
- temperature sensor DS1820
Task description:
The system should control the heating of a certain space so that it maintains the temperature within the set limits. If the temperature falls below the lower setpoint, an Alarm message should be sent to the user on the Telegram and the heating switched on. Heating should work not only until the temperature rises above the lower, but until the setting reaches the upper limit temperature, after which the heating should be turned off.
Setting the lower and upper temperature is enabled via a Telegram message sent to the KC868-A4.
Example of setting the lower temperature limit when heating is to be started:
/set_tL24 (will set to 24.000C) /set_tL1.50 (will set to 1.500C)
For easier communication, the entered messages are no case sensitive and not ‘/’ necessary.
Example of setting the upper temperature limit when heating is to be switched off:
/set_tH26 (will set to 26.000C) /set_tH5 (will set to 5.000C)
Relays DO1 – DO4 can be set in ON or OFF commands via Telegram:
/r1on r2on R3on r4on (as see no case sensitive, no ‘/’ necessary
/r1off r2off R3off r4off
/allon allon Allon (all relays ON)
/alloff alloff Alloff (all relays Off)
Read current room temperature:
/temp temp Temp
If we want to see the status of all settings and values:
/stat stat Stat
The automatic or manual mode can also be set via the Telegram command:
/setm setm – set automatic mode in which the temperature sensor automatically regulates
the temperature and switches the heating ON or OFF via relay 1.
/seta seta – set the manual mode in which we manually switch the heating ON and OFF via relay 1.
( /r1on)( /r1off). The system will send an Alarm message that the temperature is
below the set lower limit.
If an undefined command is entered, the system will return a message:
All available commands can be called up in the Telegram via the command: /Start or start)
Arduino IDE program :
Preparing Arduino IDE
We’ll program the ESP32 board using Arduino IDE, so make sure you have it installed in your Arduino IDE.
Universal Telegram Bot Library
To interact with the Telegram bot, we’ll use the Universal Telegram Bot Library created by Brian Lough that provides an easy interface for the Telegram Bot API.
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> ZIP Library...
- Add the library you’ve just downloaded.
Important: don’t install the library through the Arduino Library Manager because it might install a deprecated version.
For all the details about the library, take a look at the Universal Arduino Telegram Bot Library GitHub page.
ArduinoJson Library
You also have to install the ArduinoJson library. Follow the next steps to install the library.
- Go to Sketch > Include Library> Manage Libraries.
- Search for “ArduinoJson”.
- Install the library.
We’re using ArduinoJson library version 6.19.2. (at the moment)
References:
Full Arduino IDE sketch:
/*
* Project adapted for Kincony KC868-A4 (esp32 based)controller
*
Complete project details at:
https://RandomNerdTutorials.com/telegram-group-esp32-esp8266/
Project created using Brian Lough’s Universal Telegram Bot Library:
https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot
eaxmple: Telegram and KC868-A4
using mentioned sources, project designed:
Nebojsa Urosevic
*/
#include <WiFi.h> // WiFi using library
#include <WiFiClientSecure.h> // Library for security over WiFi
#include <UniversalTelegramBot.h> // Universal Telegram Bot Library written by Brian Lough: https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot
#include <ArduinoJson.h> // Library for JSON message exchange
#include <DS18B20.h> // Library for DS18B20 temperature and humidity sensor
DS18B20 ds(13);
const char* ssid = “xxxxxxxxxxx”; // Replace with your network credentials
const char* password = “**********”; // Replace with your network credentials
// Initialize Telegram BOT
// xxxxx replace with your Bot Token (Get from Botfather)
#define BOTtoken “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”
// yyyyyy replace with your DBot (us command /getid)
#define CHAT_ID “yyyyyyyyyyyyy”
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
// Checks for new messages every 1 second.
int botRequestDelay = 1000;
unsigned long lastTimeBotRan;
int numNewMessages;
const int Rel01 = 2;
const int Rel02 = 15;
const int Rel03 = 5;
const int Rel04 = 4;
const int DI1 = 36;
const int DI2 = 39;
const int DI3 = 27;
const int DI4 = 14;
float TempC;
float tempF;
String t;
bool pin = LOW;
String st1 = “_tl”; // Command Temperature SET T-Low
String st2 = “_th”; // Command Temperature SET T-High
char s = ‘_’; // SET command recognition
int pos; // Position in parse string
String SetTempL; // Pick up Low Limit temperature from Telegram (string)
float STempL = 24; // default – numeric of Low Limit temp (Float)
String SetTempH; // Pick up High Limit temperature from Telegram (string)
float STempH = 26; // default – numeric of High Limit temp (Float)
String Msg; // message
int n = 1; // number of notification
byte vDO1; // Save current (DO) relay’s status
byte vDO2;
byte vDO3;
byte vDO4;
byte vDI1; // Save current DI status
byte vDI2;
byte vDI3;
byte vDI4;
byte k = false; // status true/false
byte startheat = 0;
byte ma = 1; // Set ma=0 (_setm)manual mode; ma=1 (_seta)automatic mode
void setup()
{
Serial.begin(115200);
pinMode(Rel01, OUTPUT);
pinMode(Rel02, OUTPUT);
pinMode(Rel03, OUTPUT);
pinMode(Rel04, OUTPUT);
pinMode(DI1,INPUT);
pinMode(DI2,INPUT);
pinMode(DI3,INPUT);
pinMode(DI4,INPUT);
uint8_t address[8];
ds.getAddress(address);
Serial.print(“Power Mode: “);
if (ds.getPowerMode())
{
Serial.println(“External”);
}
else
{
Serial.println(“Parasite”);
}
TempC = ds.getTempC();
tempF = ds.getTempF();
Serial.print(“Temperature:”);
Serial.print(TempC);
//Serial.print(” “);
Serial.print(“°C “); // ° ascii 248 left alt + num 248
Serial.print(tempF);
Serial.print(“°F “);
Serial.println();
// Connect to Wi-Fi
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
//#ifdef ESP32
client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
//#endif
Serial.println();
// Connect to Wifi network:
Serial.print(“Connecting to Wifi SSID “);
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(“.”);
delay(500);
}
Serial.print(“\nWiFi connected. IP address: \n”);
// Print ESP32 Local IP Address
Serial.println(WiFi.localIP());
bot.sendMessage(CHAT_ID, “Welcome, Bot Started…”, “”);
Serial.println(“Welcome, Bot Started…”);
byte vDI1 = digitalRead(DI1);
byte vDI2 = digitalRead(DI2);
byte vDI3 = digitalRead(DI3);
byte vDI4 = digitalRead(DI4);
}
//—————————————————————————–
void loop()
{
if (millis() > lastTimeBotRan + botRequestDelay)
{
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
lastTimeBotRan = millis();
tempControl();
}
}
void tempControl()
{
TempC = ds.getTempC();
t = TempC;
t = “temp: ” + t + “°C”;
//MAKNI SetTempL.concat(STempL); // Reconvert Float temp. to String
Serial.print(“****** LowLimit: “);
Serial.print(STempL);
Serial.print(” Current: “);
Serial.print(TempC);
Serial.print(” HighLimit: “);
Serial.println(STempH);
if (ma == 1)
{
if ((TempC < STempL) && (startheat == 0))
{
Serial.println(” ** Temp < than Lower limit “);
pin = HIGH;
digitalWrite(Rel01, pin); // Start heating
t = t + ” < set: ” + STempL;
// bot.sendMessage(CHAT_ID, “Start heating: ” + t, “”);
Serial.println(“>> Start heating: ” + t);
startheat = 1;
}
if ((TempC > STempH) && (startheat == 1))
{
Serial.println(” ** Temp. > UpperTemp “);
/* Serial.print(TempC);
Serial.print(” “);
Serial.print(startheat);
Serial.print(” “);
Serial.println(STempH);
*/
pin = LOW;
digitalWrite(Rel01, pin); // Stop heating
t = t + ” > set: ” + STempL;
// bot.sendMessage(CHAT_ID, “Stop heating: ” + t, “”);
startheat = 0;
Serial.println(“>> Stop heating: ” + t);
//n = 1; // n = how many messages will sent to Telegram
}
}
if (ma == 0)
{
if (TempC < STempL && n > 0) // if real temp < set temp (Heating)
{
n -= 1;
t = t + ” < set: ” + STempL;
bot.sendMessage(CHAT_ID,”** ALARM** ” + t, “”);
}
}
}
void handleNewMessages(int numNewMessages) // Handle what happens when you receive new messages
{
for (int i = 0; i < numNewMessages; i++)
{
// Chat id of the requester
String chat_id = String(bot.messages[i].chat_id);
if (chat_id != CHAT_ID)
{
bot.sendMessage(chat_id, “** Unauthorized user!”, “”);
Serial.println(“** Unauthorized user !!!”);
continue;
}
// Print the received message
String text = bot.messages[i].text;
text.toLowerCase(); // convert all inputs in lower case
Serial.println(“Received command: <” + text + “>”);
String from_name = bot.messages[i].from_name;
//test if normal command or set variable
pos = text.indexOf(s); // test if ‘_’ existing in command
if (pos < 0) // This is not SET command
{
text.toLowerCase();
if (text == “/start” || text == “start” || text == “?”)
{
k = 1;
String welcome = “Welcome, ” + from_name + “.\n”;
welcome += “Use the following commands:\n\n”;
welcome += “/r1on or r1on to turn Relay1 ON \n”;
welcome += “/r1off or r1off to turn Relay1 OFF \n\n”;
welcome += “/r2on or r2on to turn Relay2 ON \n”;
welcome += “/r2off or r2off to turn Relay2 OFF \n\n”;
welcome += “/r3on or r3on to turn Relay3 ON \n”;
welcome += “/r3off or r3off to turn Relay3 OFF \n\n”;
welcome += “/r4on or r4on to turn Relay4 ON \n”;
welcome += “/r4off or r4off off to turn Relay4 OFF \n\n”;
welcome += “/allon or allon to turn all Relays ON \n”;
welcome += “/alloff or alloff to turn all Relays OFF \n”;
welcome += “/temp or temp read current temp \n”;
welcome += “/set_tLxx.xx set Low Limit temperature \n”;
welcome += “/set_tHxx.xx set High Limit Temperature \n\n”;
welcome += “/setm or setm Manual mode control \n”;
welcome += “/seta or seta Automatic mode control \n\n”;
welcome += “/stat to request relays state \n”;
bot.sendMessage(chat_id, welcome, “”);
}
// ————————————————————
// Set mode: Manual
if (text == “/setm” || text == “setm”)
{
k = 1;
startheat = 0;
ma = 0;
bot.sendMessage(chat_id, “Set mode: MANUAL”, “”);
Serial.println(“Set mode: MANUAL”);
}
// Set mode: Automatic (Temp sensor will regulate temperature automaticaly
if (text == “/seta” || text == “seta”)
{
k = 1;
startheat = 0;
ma = 1;
bot.sendMessage(chat_id, “Set mode: Automatic”, “”);
Serial.println(“Set mode: AUTOMATIC”);
}
// Relay01 ON
// text.toLowerCase();
if (text == “/r1on” || text == “r1on”)
{
k = 1;
bot.sendMessage(chat_id, “Relay_01 set ON”, “”);
Serial.print(“Relay_01 set ON”);
//Serial.println(STempL);
pin = HIGH;
digitalWrite(Rel01, pin);
vDO1 = 1;
}
// Relay01 OFF
if (text == “/r1off” || text == “r1off”)
{
k = 1;
startheat = 0;
bot.sendMessage(chat_id, “Relay_01 set OFF”, “”);
Serial.println(“Relay_01 set OFF”);
pin = LOW;
digitalWrite(Rel01, pin);
vDO1 = 0;
}
// Relay02 ON
if (text == “/r2on” || text == “r2on”)
{
k = 1;
bot.sendMessage(chat_id, “Relay_02 set ON”, “”);
Serial.println(“Relay_02 set ON”);
pin = HIGH;
digitalWrite(Rel02, pin);
vDO2 = 1;
}
// Relay02 OFF
if (text == “/r2off” || text == “r2off”)
{
k = 1;
bot.sendMessage(chat_id, “Relay_02 set OFF”, “”);
Serial.println(“Relay_01 set OFF”);
pin = LOW;
digitalWrite(Rel02, pin);
vDO2 = 0;
}
// Relay03 ON
if (text == “/r3on” || text == “r3on”)
{
k = 1;
bot.sendMessage(chat_id, “Relay_03 set ON”, “”);
Serial.println(“Relay_03 set ON”);
pin = HIGH;
digitalWrite(Rel03, pin);
}
// Relay03 OFF
if (text == “/r3off” || text == “r3off”)
{
k = 1;
bot.sendMessage(chat_id, “Relay_03 set OFF”, “”);
Serial.println(“Relay_03 set OFF”);
pin = LOW;
digitalWrite(Rel03, pin);
}
// Relay04 ON
if (text == “/r4on” || text == “r4on”)
{
k = 1;
bot.sendMessage(chat_id, “Relay_04 set ON”, “”);
Serial.println(“Relay_04 set ON”);
pin = HIGH;
digitalWrite(Rel04, pin);
}
// Relay04 OFF
if (text == “/r4off” || text == “r4off”)
{
k = 1;
bot.sendMessage(chat_id, “Relay_04 set OFF”, “”);
Serial.println(“Relay_04 set OFF”);
pin = LOW;
digitalWrite(Rel04, pin);
}
// All On
if (text == “/allon” || text == “allon”)
{
k = 1;
bot.sendMessage(chat_id, “All relay’s set ON”, “”);
Serial.println(“All relay’s set ON”);
pin = HIGH;
digitalWrite(Rel01, pin);
digitalWrite(Rel02, pin);
digitalWrite(Rel03, pin);
digitalWrite(Rel04, pin);
}
// ALL Off
if (text == “/alloff” || text == “alloff”)
{
k = 1;
startheat = 0;
bot.sendMessage(chat_id, “All relay’s set OFF”, “”);
Serial.println(“All relay’s set OFF”);
pin = LOW;
digitalWrite(Rel01, pin);
digitalWrite(Rel02, pin);
digitalWrite(Rel03, pin);
digitalWrite(Rel04, pin);
}
// Temperature
if (text == “/temp” || text == “temp”)
{
k = 1;
TempC = ds.getTempC();
t = TempC;
t = “Temperature: ” + t + “°C”;
bot.sendMessage(chat_id, t, “”);
// bot.sendMessage(chat_id, “Temperature”, “t”);
Serial.println(t + “\n”);
}
// Status request
if (text == “/stat” || text == “stat”)
{
k = 1;
if (ma == 0)
{
Msg = “”;
Msg += “Control set: MANUAL \n”;
}
if (ma == 1)
{
Msg = “”;
Msg += “Control set: AUTOMATIC \n”;
}
if (digitalRead(Rel01))
{
//bot.sendMessage(chat_id, “Relay01 is ON”, “”);
//Msg = “”;
Msg += “Relay01 is ON \n”;
Serial.println(“Relay01 is ON”);
}
else
{
//bot.sendMessage(chat_id, “Relay01 is OFF”, “”);
Msg += “Relay01 is OFF \n”;
Serial.println(“Relay01 is OFF”);
}
if (digitalRead(Rel02))
{
//bot.sendMessage(chat_id, “Relay02 is ON”, “”);
Msg += “Relay02 is ON \n”;
Serial.println(“Relay01 is ON”);
}
else
{
//bot.sendMessage(chat_id, “Relay02 is OFF”, “”);
Msg += “Relay02 is OFF \n”;
Serial.println(“Relay02 is OFF”);
}
if (digitalRead(Rel03))
{
//bot.sendMessage(chat_id, “Relay03 is ON”, “”);
Msg += “Relay03 is ON \n”;
Serial.println(“Relay03 is ON”);
}
else{
//bot.sendMessage(chat_id, “Relay03 is OFF”, “”);
Msg += “Relay03 is OFF \n”;
Serial.println(“Relay03 is OFF”);
}
if (digitalRead(Rel04))
{
//bot.sendMessage(chat_id, “Relay04 is ON”, “”);
Msg += “Relay04 is ON \n”;
Serial.println(“Relay04 is ON”);
}
else
{
//bot.sendMessage(chat_id, “Relay04 is OFF”, “”);
Msg += “Relay04 is OFF \n”;
Serial.println(“Relay04 is OFF”);
}
bot.sendMessage(chat_id, Msg, “”);
if (digitalRead(DI1))
{
// bot.sendMessage(chat_id, “Digital input 1 is Open”, “”);
Msg = “”;
Msg += “Digital input 1 is Open \n”;
Serial.println(“Digital input 1 is Open”);
}
else
{
//bot.sendMessage(chat_id, “Digital input 1 is Close”, “”);
Msg += “Digital input 1 is Close \n”;
Serial.println(“Digital input 1 is Close”);
}
if (digitalRead(DI2))
{
//bot.sendMessage(chat_id, “Digital input 2 is Open”, “”);
Msg += “Digital input 2 is Open \n”;
Serial.println(“Digital input 2 is Open”);
}
else
{
//bot.sendMessage(chat_id, “Digital input 2 is Close”, “”);
Msg += “Digital input 2 is Close \n”;
Serial.println(“Digital input 2 is Close”);
}
if (digitalRead(DI3))
{
//bot.sendMessage(chat_id, “Digital input 3 is Open”, “”);
Msg += “Digital input 3 is Open \n”;
Serial.println(“Digital input 3 is Open”);
}
else
{
//bot.sendMessage(chat_id, “Digital input 3 is Close”, “”);
Msg += “Digital input 3 is Close \n”;
Serial.println(“Digital input 3 is Close”);
}
if (digitalRead(DI4))
{
//bot.sendMessage(chat_id, “Digital input 4 is Open”, “”);
Msg += “Digital input 4 is Open \n”;
Serial.println(“Digital input 4 is Open”);
}
else
{
//bot.sendMessage(chat_id, “Digital input 4 is Close”, “”);
Msg += “Digital input 4 is Close \n”;
Serial.println(“Digital input 4 is Close”);
}
bot.sendMessage(chat_id, Msg, “”);
TempC = ds.getTempC();
t = TempC;
t = t + “°C”;
//MAKNI STempL = SetTempL.toFloat();
SetTempL = “”;
SetTempL.concat(STempL); // Reconvert Float temp. to String
//MAKNI STempH = SetTempH.toFloat();
SetTempH = “”;
SetTempH.concat(STempH); // Reconvert Float temp. to String
Serial.print(“Temp Lower Limit set: “);
Serial.print(SetTempL);
Serial.println(“°C”);
Serial.print(“Temp Upper Limit set: “);
Serial.print(SetTempH);
Serial.println(“°C”);
Serial.println(“Current temperature ” + t);
bot.sendMessage(chat_id, “Temp Lower Limit set: ” + SetTempL + “°C”, “”);
bot.sendMessage(chat_id, “Temp Upper Limit set: ” + SetTempH + “°C”, “”);
bot.sendMessage(chat_id, “Current temperature: ” + t, “”);
}
}
else // if pos > 0 set command
{
pos = text.indexOf(st1);
if (pos > 0)
{
k = 1;
SetTempL = text.substring(pos+3);
STempL = SetTempL.toFloat();
SetTempL = “”;
SetTempL.concat(STempL); // Reconvert Float temp. to String
bot.sendMessage(chat_id, “Low temperature limit SET: ” + SetTempL + “°C”, “”);
Serial.print(“Low temperature limit SET: “);
Serial.println(STempL);
}
pos = text.indexOf(st2);
if (pos > 0)
{
k = 1;
SetTempH = text.substring(pos+3);
STempH = SetTempH.toFloat();
SetTempH = “”;
SetTempH.concat(STempH); // Reconvert Float temp. to String
bot.sendMessage(chat_id, “High temperatue limit SET: ” + SetTempH + “°C”, “”);
Serial.print(“High temperature limit SET: “);
Serial.println(STempH);
}
}
if (k == 1)
{
k = 0;
}
else
{
bot.sendMessage(chat_id, “** Unrecognized command”, “”);
Serial.println(“** Unrecognized command”);
}
}
}