Selasa, 25 April 2023

WHATSAPP-arduino

 

How to send messages to WhatsApp or Telegram from ESP32

https://www.engineersgarage.com/send-messages-to-whatsapp-telegram-from-esp32/

In many Internet-of-Things (IoT) applications, it’s necessary to send alerts to the user in the form of messages. Typically, text messages are sent via WiFi or SMS. But sometimes text messaging is unavailable between devices. So, what if these messages could be sent via the IoT to a user’s WhatsApp or Telegram account instead? 

In this project, we’ll program an ESP32 board to directly communicate messages to a WhatsApp number or a Telegram account. We will use Whatabot API for both tasks. We are choosing ESP32 because it’s a WiFi development board and requires no extra components to connect with a WiFi access point. Now, let’s get started. 

Components required
1. ESP32 x1
2. Micro-USB cable for ESP32 x1
3. Smartphone x1 

Circuit connections
There are no circuit connections for this project. What’s required is an ESP32/ESP8266 board and a MicroUSB cable. The cable is necessary to upload the sketch to the board from a host computer. To communicate, you’ll also need a smartphone device with an active SIM and either the WhatsApp or Telegram app installed.

Whatabot API
Whatabot is a third-party service that allows real-time messages to be sent via WhatsApp or Telegram. Messages are communicated as HTTP GET requests from a microcontroller or an app. 

API is specifically designed for use with IoT devices and web applications. It’s an easy and excellent way to communicate messages to a registered number or Telegram account. The API operates in real-time, communicating messages from a microcontroller to the smartphone of the user instantly. 

To begin, register your WhatsaApp number or Telegram account with API via your smartphone.

Registering your WhatsApp number
To register your WhatsApp number with Whatabot API, create a Whatabot contact in your smartphone. The Whatabot contact number is +54 9 1123998026. You can also register your WhatsApp number on this link

Next, send this message to the Whatabot account: “I allow whatabot to send me messages.”

The API will return a link for the given mobile number containing an API key. The link must be used to send messages to the given WhatsApp number. It contains three variables — an API key, a text message, and a mobile number.

Libraries required
Next, you’ll need to install the required libraries on Arduino IDE. This sketch uses the WiFi.h, HTTPClient.h, and UrlEncode.h libraries. The WiFi.h and HTTPClient.h are installed by default.

To install UrlEncode.h, navigate to Tools->Manage Libraries. Search for “URLencode.” Scroll down to the URLencode library, which is written by Masayuki Sugahara. Install the library by clicking on the “install” button.

Sending messages to a WhatsApp number
To send a default message from ESP32 to the Whatabot-registered WhatsApp number, upload the following sketch…

#include <WiFi.h>   
#include <HTTPClient.h>
#include <UrlEncode.h>
const char* ssid = “SSID”;
const char* password = “PASSWORD”;
// add +international_country_code + phone number
String mobile_number = “91***-***-****”;
String api_key = “APIKEY”;
void send_whatsapp_message(String message){
  String API_URL = “https://api.whatabot.net/whatsapp/sendMessage?apikey=”+ api_key + “&text=”+ urlEncode(message) +”&phone=”+mobile_number;
  HTTPClient http;
  http.begin(API_URL);

  http.addHeader(“Content-Type”, “application/x-www-form-urlencoded”);
  int http_response_code = http.GET();
  if (http_response_code == 200){
    Serial.print(“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(“Connected to Wi-Fi IP Address: “);
  Serial.print(“IP Address: “);
  Serial.println(WiFi.localIP());
  send_whatsapp_message(“Hi, This is ESP32!”);
}
void loop() {
}

Note: for this sketch, be sure to replace the given “SSID” and “PASSWORD” with your personal WiFi access point’s SSID and network key. You also must replace the mobile number and API key with your registered WhatsApp number.

The code
The sketch starts with importing the WiFi.h, HTTPClient.h, and UrlEncode.h libraries.

  • WiFi.h is required to connect to a WiFi access point
  • HTTPClient.h is required to process the HTTP GET request
  • UrlEncode.h is required to encode the URL, according to web standards

Next, the variables are declared to store the SSID and password for the WiFi access point. This is followed by declaring the variables to store the WhatsApp number — and the API key received when registering the mobile number to Whatabot.

The function send_whatsapp_message() is defined and it takes a string argument. In this function, a string variable is defined according to the link received upon registering the WhatsApp number to the Whatabot API. The string contains three GET request variables — the API key, the text message to be sent, and the mobile number.

The text message is set to the argument of the function and it’s encoded within the URL by using the urlEncode() function. An object of the HTTPClient class is instantiated. It’s used to initiate the HTTP request using the http.begin() method (with URL string defined as an argument).

An HTTP header is defined using the http.addHeader() method. A variable http_response_code is defined to get the HTTP response code by calling the http.GET() method. If the response code is 200, this means the HTTP request is successfully executed and the WhatsApp message has been sent. Otherwise, an error code is printed to the console. A call to http.end() ends the HTTP header.

The code to send the message (from ESP32 to the registered WhatsApp number) is written in the setup() function because it’s only to be executed once. The loop() function remains empty.

In the setup() function, the baud rate for serial communication is set to 115200 bps. The WiFi is initiated using the WiFi.begin() method. The sketch waits until the WiFi is connected before continuing.

Once the WiFi is connected, the IP address of the router is printed to the console (i.e. the serial monitor). A call to send_whatsapp_message() is done with the message provided as a string argument.

The string argument for this sketch is: “Hi, This is ESP32!” If the message is successfully sent to the WhatsApp number, a success message is printed to the console.

Results
To execute the code, open Arduino IDE’s serial monitor and reset the ESP32. The sketch is executed once by the board and the message is delivered to the WhatsApp number.

Managing Whatabot for Whatsapp
If you forget your API key, you can resend the message, “I allow whatabot to send me messages” to the Whatabot contact number. You will receive another link with the API key.

If you want to change the API key, you can send the message, “I want to update my apikey” to the Whatabot number and you will receive a link with a new API key. And if you want to delete your WhatsApp Whatabot account, simply send the message, “I want to delete my account” to the Whatabot and your WhatsApp Whatabot account will be deleted.

Registering Telegram to Whatabot API
As mentioned, it’s also possible to send messages to a Telegram account from a microcontroller or web application using Whatabot API. To do so, first register your Telegram account with Whatabot API. In Telegram app, search for “@whatabot_bot” (and remember to include the “@” symbol).

Next, send the message “I allow whatabot to send me messages” to the Whatabot number. You’ll receive a link in return containing a chat ID and API key.

Sending messages to Telegram
To send a default message from ESP32 to the Whatabot-registered Telegram account, upload the following sketch…

#include <WiFi.h>   
#include <HTTPClient.h>
#include <UrlEncode.h>
const char* ssid = “SSID”;
const char* password = “PASSWORD”;
// add Telegram chat ID + API Key
String chat_id = “CHATID”;
String api_key = “APIKEY”;

void send_telegram_message(String message){
  String API_URL = “https://api.whatabot.net/telegram/sendMessage?apikey=”+ api_key + “&text=”+ urlEncode(message) +”&chatId=”+chat_id;
  HTTPClient http;
  http.begin(API_URL);
  http.addHeader(“Content-Type”, “application/x-www-form-urlencoded”);
  int http_response_code = http.GET();
  if (http_response_code == 200){
    Serial.print(“Telegram 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(“Connected to Wi-Fi IP Address: “);
  Serial.print(“IP Address: “);
  Serial.println(WiFi.localIP());
  send_telegram_message(“Hi, This is ESP32!”);
}

void loop() {
}

Note: be sure to  you need to replace “SSID” and “PASSWORD” with SSID and network key of your own WiFi access point. You will also need to replace chat ID and API key of your own registered account in the above sketch.

Note: for this sketch, be sure to replace the given “SSID” and “PASSWORD” with your personal WiFi access point’s SSID and network key. You also must replace the chat ID and API key with your registered Telegram account.

The code
The sketch for sending a message to Telegram is similar to the sketch for sending a message to a WhatsApp number, except it uses a different URL for the HTTP request.

The sketch starts with importing the WiFi.h, HTTPClient.h, and UrlEncode.h libraries.

  • WiFi.h is required to connect to a WiFi access point
  • HTTPClient.h is required to process the HTTP GET request
  • UrlEncode.h is required to encode the URL, according to web standards

Next, the variables are declared to store the SSID and password for the WiFi access point. This is followed by declaring the variables to store the chat ID for the Telegram account — and the API key received by registering the account to Whatabot.

The function send_telegram_message() is defined and it takes a string argument. In the function, a string variable is defined according to the link received upon registering the Telegram account with the Whatabot API. The string contains three GET request variables — the chat ID, the API key, and the text message to be sent.

The text message is set to the argument of the function and it’s encoded within the URL by using the urlEncode() function. An object of the HTTPClient class is instantiated. It’s used to initiate the HTTP request using the http.begin() method (with URL string defined as an argument).

An HTTP header is defined using the http.addHeader() method. A variable http_response_code is defined to get the HTTP response code by calling the http.GET() method. If the response code is 200, this means the HTTP request is successfully executed and the Telegram message has been sent. Otherwise, an error code is printed to the console. A call to http.end() ends the HTTP header.

The code to send the message (from ESP32 to the registered Telegram account) is written in the setup() function because it’s only to be executed once. The loop() function remains empty.

In the setup() function, the baud rate for serial communication is set to 115200 bps. The WiFi is initiated using the WiFi.begin() method. The sketch waits until the WiFi is connected before continuing.

Once the WiFi is connected, the IP address of the router is printed to the console (i.e. the serial monitor). A call to send_telegram_message() is done with the message provided as a string argument.

The string argument for this sketch is: “Hi, This is ESP32!” If the message is successfully sent to the Telegram account, a success message is printed to the console.

Results
To execute the code, open Arduino IDE’s serial monitor and reset the ESP32. The sketch is executed once by the board and the message is delivered to the Telegram account.

Managing Whatabot for Telegram
If you forget your API key, you can resend the message, “I allow whatabot to send me messages” to the Whatabot contact number. You will receive another link with the API key.

If you want to change the API key, you can send the message, “I want to update my apikey” to the Whatabot number and you will receive a link with a new API key. And if you want to delete your Telegram Whatabot account, simply send the message, “I want to delete my account” to the Whatabot and your WhatsApp Whatabot account will be deleted.

Tidak ada komentar:

Posting Komentar