Hi need help with the coding.
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include "PCF8574.h"
// Wifi network station credentials
#define WIFI_SSID "xxx"
#define WIFI_PASSWORD "xxx"
// Telegram BOT Token (Get from Botfather)
#define BOT_TOKEN "xxx"
PCF8574 pcf8574_1(0x20);
const unsigned long BOT_MTBS = 1000; // mean time between scan messages
X509List cert(TELEGRAM_CERTIFICATE_ROOT);
WiFiClientSecure secured_client;
UniversalTelegramBot bot(BOT_TOKEN, secured_client);
unsigned long bot_lasttime; // last time messages' scan has been done
const int RelayPins[] = {P0, P1, P2, P3, P4, P5, P6, P7,}; // Array of relay pins
const int NumRelays = sizeof(RelayPins) / sizeof(RelayPins[0]);
int RelayStatus[NumRelays] = {0}; // Array to store relay statuses
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";
for (int j = 0; j < NumRelays; j++)
{
if (text == "/Relay" + String(j) + "on")
{
pcf8574_1.digitalWrite(RelayPins[j], HIGH); // turn the Relay on (HIGH is the voltage level)
RelayStatus[j] = 1;
bot.sendMessage(chat_id, "Relay " + String(j) + " is ON", "");
}
if (text == "/Relay" + String(j) + "off")
{
RelayStatus[j] = 0;
pcf8574_1.digitalWrite(RelayPins[j], LOW); // turn the Relay off (LOW is the voltage level)
bot.sendMessage(chat_id, "Relay " + String(j) + " is OFF", "");
}
if (text == "/status" + String(j))
{
if (RelayStatus[j])
{
bot.sendMessage(chat_id, "Relay " + String(j) + " is ON", "");
}
else
{
bot.sendMessage(chat_id, "Relay " + String(j) + " is OFF", "");
}
}
}
if (text == "/start")
{
String welcome = "Welcome to Relay Control, " + from_name + ".\n";
welcome += "This is Relay switching Bot.\n\n";
for (int j = 0; j < NumRelays; j++)
{
welcome += "/Relay" + String(j) + "on : to switch Relay " + String(j) + " ON\n";
welcome += "/Relay" + String(j) + "off : to switch Relay " + String(j) + " OFF\n";
welcome += "/status" + String(j) + " : Returns current status of Relay " + String(j) + "\n\n";
}
bot.sendMessage(chat_id, welcome, "Markdown");
}
}
}
void setup()
{
Serial.begin(115200);
Serial.println();
for (int i = 0; i < NumRelays; i++)
{
pcf8574_1.pinMode(RelayPins[i], OUTPUT); // initialize digital RelayPins as outputs.
pcf8574_1.digitalWrite(RelayPins[i], LOW); // initialize pins as off (LOW is the voltage level)
}
// attempt to connect to Wifi network:
configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
secured_client.setTrustAnchors(&cert); // Add root certificate for api.telegram.org
Serial.print("Connecting to Wifi SSID ");
Serial.print(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(500);
}
Serial.print("\nWiFi connected. IP address: ");
Serial.println(WiFi.localIP());
// Check NTP/Time, usually it is instantaneous and you can delete the code below.
Serial.print("Retrieving time: ");
time_t now = time(nullptr);
while (now < 24 * 3600)
{
Serial.print(".");
delay(100);
now = time(nullptr);
}
Serial.println(now);
}
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();
}
}
I am using this code but the code is not working.Even if i am getting the response that relay is off but the relay is not turning off.