Can someone give me a simple code of sending a "Hi" message on Whatsapp or Telegram using esp8266 or esp32. I had tried many things but failed with esp8266. Should use esp32 ?
Here are the codes I had used :
#include <ESP8266WiFi.h>
#include <SoftwareSerial.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
const char* ssid = "XXXXX";
const char* password = "XXXXX;
#define BOTtoken "XXXXX";
#define CHAT_ID "XXXXX";
SoftwareSerial serial(5, 4); // RX, TX
X509List cert(TELEGRAM_CERTIFICATION_ROOT);
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
void setup() {
Serial.begin(115200);
serial.begin(9600);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {
// Read float value from Arduino via RX pin
float receivedFloat = receiveFloatData();
if (!isnan(receivedFloat)) {
// Construct the message to be sent
String message = "Name: Sam\n" + String(receivedFloat);
// Send the message to the specified Telegram group
sendTelegramMessage(message);
delay(5000); // Adjust the delay as needed
}
}
float receiveFloatData() {
float data = 0.0;
while (serial.available() > 0) {
char incomingByte = serial.read();
if (incomingByte == '\n') {
break; // End of the float value
}
data = data * 10.0 + (incomingByte - '0');
}
return data;
}
void sendTelegramMessage(String message) {
if (bot.sendMessage(chatId, message, "Markdown")) {
Serial.println("Message sent successfully");
} else {
Serial.println("Failed to send message");
}
}
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <SoftwareSerial.h>
const char* ssid = "XXXXX"; // Enter your WIFI SSID
const char* password = "XXXXX"; // Enter your WIFI Password
#define BOTtoken "XXXXX" // Enter the bottoken you got from botfather
#define CHAT_ID "XXXXX" // Enter your CHAT_ID you got from CHAT_ID bot
SoftwareSerial mySerial(5, 4); // RX, TX
X509List cert(TELEGRAM_CERTIFICATE_ROOT);
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
// Function declaration for sendMessage
void sendMessage(String message);
void setup() {
Serial.begin(115200);
mySerial.begin(9600);
configTime(0, 0, "pool.ntp.org");
client.setTrustAnchors(&cert);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
int a = 0;
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
a++;
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
sendMessage("Wifi Connected!");
sendMessage("System has Started!!");
}
void loop() {
String msg = mySerial.readStringUntil('\r');
Serial.println(msg);
sendMessage(msg);
delay(5000);
}
// Function definition for sendMessage
void sendMessage(String message) {
int response = bot.sendMessage(CHAT_ID, message, "Markdown");
if (response == 200) {
Serial.println("Message sent successfully");
}
else {
Serial.print("Failed to send message. HTTP error code: ");
Serial.println(response);
}
}
#include <ESP8266WiFi.h>
#include <SoftwareSerial.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
const char* ssid = "XXXXX";
const char* password = "XXXXX";
const char* botToken = "XXXXX";
const char* chatId = "XXXXX";
SoftwareSerial serial(5, 4); // RX, TX
WiFiClientSecure client;
UniversalTelegramBot bot(botToken, client);
void setup() {
Serial.begin(115200);
serial.begin(9600);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {
// Read float value from Arduino via RX pin
float receivedFloat = receiveFloatData();
if (!isnan(receivedFloat)) {
// Construct the message to be sent
String message = "Name: Sam\n" + String(receivedFloat);
// Send the message to the specified Telegram group
sendTelegramMessage(message);
delay(5000); // Adjust the delay as needed
}
}
float receiveFloatData() {
float data = 0.0;
while (serial.available() > 0) {
char incomingByte = serial.read();
if (incomingByte == '\n') {
break; // End of the float value
}
data = data * 10.0 + (incomingByte - '0');
}
return data;
}
void sendTelegramMessage(String message) {
if (bot.sendMessage(chatId, message, "Markdown")) {
Serial.println("Message sent successfully");
} else {
Serial.println("Failed to send message");
}
}
bot.sendMessage(CHAT_ID, "ALERT! MOTION DETECTED!!", "");
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <UrlEncode.h>
const char* ssid = "XXXXX";
const char* password = "XXXXX";
// add +international_country_code + phone number
String mobile_number = "XXXXX";
String api_key = "XXXXX";
void send_whatsapp_message(String message) {
String API_URL = "https://api.whatabot.net/whatsapp/sendMessage?apikey=" + api_key + "&text=" + urlEncode(message) + "&phone=" + mobile_number;
WiFiClient client;
HTTPClient http;
if (!http.begin(client, API_URL)) {
Serial.println("Failed to begin HTTP connection");
return;
}
int http_response_code = http.GET();
if (http_response_code == HTTP_CODE_OK) {
Serial.println("WhatsApp message sent successfully");
} else {
Serial.println("Error sending the message");
Serial.print("HTTP response code: ");
Serial.println(http_response_code);
}
http.end();
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("Connecting to Wi-Fi Access Point");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to Wi-Fi IP Address:");
Serial.println(WiFi.localIP());
send_whatsapp_message("Hi, This is ESP8266!");
}
void loop() {
}