This is code for my arduino , but when it comes to send telegram message serial monitor show error:
WiFiEsp] Connecting to api.telegram.org
[WiFiEsp] Data packet send error (2)
[WiFiEsp] Failed to write to socket 3
[WiFiEsp] Disconnecting 3
#include <WiFiEsp.h>
#include <WiFiEspServer.h>
#include <DHT.h>
#include <UniversalTelegramBot.h>
#define DHTPIN 2 // DHT11 data pin
#define DHTTYPE DHT11 // DHT11 sensor type
#define MQ2PIN A0 // MQ2 sensor analog pin
#define LED_PIN 3
// Telegram Bot credentials
#define TELEGRAM_BOT_TOKEN "xxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
#define CHAT_ID "xxxxxxxxxxxxxxxxx"
char ssid[] = "Internet";
char pass[] = "Password";
int status = WL_IDLE_STATUS;
WiFiEspServer server(80);
WiFiEspClient client;
int threshold = 150; // Adjust threshold as needed
bool alert = false;
DHT dht(DHTPIN, DHTTYPE);
UniversalTelegramBot bot(TELEGRAM_BOT_TOKEN, client);
void setup() {
Serial.begin(9600);
Serial1.begin(115200);
pinMode(MQ2PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
dht.begin();
WiFi.init(&Serial1);
// Check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
while (true);
}
// Attempt to connect to WiFi network
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(5000);
}
// Start the web server
server.begin();
}
void loop() {
static unsigned long previousMillis = 0;
unsigned long currentMillis = millis();
// Refresh webpage every 5 seconds
if (currentMillis - previousMillis >= 7000) {
previousMillis = currentMillis;
WiFiEspClient client = server.available();
if (client) {
Serial.println("New client");
String response = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
response += "<html><body>";
response += "<meta http-equiv='refresh' content='7'>";
response += "<h1>DHT11 and MQ2 Sensor Readings</h1>";
response += "<p>Temperature: ";
response += String(readTemperature());
response += "°C</p>";
response += "<p>Humidity: ";
response += String(readHumidity());
response += "%</p>";
response += "<p>Smoke Value: ";
int gasReading = readGas();
response += String(gasReading);
response += "</p>";
if (gasReading > threshold) {
response +=("<p style='color:red'>Gas Alert! Ventilate immediately!</p>");
digitalWrite(LED_PIN, HIGH); // Turn on LED if gas exceeds threshold
// Send Telegram message
sendTelegramMessage("Gas Alert! Ventilate immediately!");
response += "<p>Telegram sent";
Serial.println("Telegram sent");
} else {
digitalWrite(LED_PIN, LOW); // Turn off LED if gas is below threshold
}
response += "</body></html>";
client.print(response);
delay(1);
client.stop(); // Close the connection after sending the response
Serial.println("Client disconnected");
}
}
}
float readTemperature() {
return dht.readTemperature();
}
float readHumidity() {
return dht.readHumidity();
}
int readGas() {
return analogRead(MQ2PIN);
}
void sendTelegramMessage(String message) {
bot.sendMessage(CHAT_ID, message, "");
}