Introduction: WhatsApp Messaging Made Easy: Interact With ESP8266 and CallMeBot

About: Hi my name is Rohan Barnwal and I am from India. I love to create projects related to Microcontrollers such as Arduino, NodeMCU, ESP Boards, Attiny85 etc. I have created more 200 projects till now and still co…

Connect ESP8266 & Call Me Bot to send WhatsApp messages. Instant notifications, IoT alerts, and updates at your fingertips.

In the dynamic world of IoT. staying connected and informed is vital for a seamless user experience. Real-time WhatsApp notifications offer numerous benefits, from improving user experience and enabling prompt responses to enhancing remote monitoring capabilities. By leveraging this powerful communication channel, you can add convenience, facilitate quick action, and stay updated on the go. In this tutorial, we will explore how to integrate real-time WhatsApp notifications into your IoT projects using the ESP8266 and Call Me Bot, unlocking a world of possibilities.

  • Improved User Experience: Real-time WhatsApp notifications provides a user-friendly and familiar communication platform. By receiving instant updates on WhatsApp, user can effortlessly stay informed about their IoT projects without the need for additional apps or interfaces. This streamlined experience enhances usability and eliminates the need for continuous monitoring.
  • Prompt Responses: With real-time WhatsApp notifications, you can promptly respond to critical events or alerts. Whether it's a security breach, a change in environmental conditions, or an important status update, receiving notifications directly on WhatsApp allow you to take immediate action. This reduces response time, ensuring that you can address issues swiftly and efficiently.
  • Enhanced Remote Monitoring: Real-time WhatsApp notifications enable remote monitoring of your IoT projects. No matter where you are, you can receive notifications about device status, sensor readings, or system malfunctions. This level of remote monitoring empowers you to keep a close eye on your projects, detect anomalies, and take necessary actions, even when you are physically away.
  • Convenience and Accessibility: WhatsApp is widely used messaging platform with a vast use base. Leveraging this platform for IoT notifications adds convenience as users can receive alerts and updates in a familiar environment. Additionally, WhatsApp notifications can be accessed from multiple devices, ensuring that you stay updated on the go, whether it's on your smartphone, tablet, or computer.

Seamless Integration: Integrating real-time WhatsApp notifications into your IoT projects is a straightforward process. By leveraging the ESP8266 and the Call Me Bot service, you can easily establish a connection and send notifications with just a few lines of code. This seamless integration allows you to focus on the core functionalities of your IoT project while ensuring effective communication with stakeholders.

Real-time WhatsApp notifications provide a plethora of benefits for your IoT projects. From improving user experience and enabling prompt responses to enhancing remote monitoring capabilities, this powerful communication channel adds convenience, facilitates quick action, and allows users to stay updated on the go. By following this tutorial and integrating the ESP8266 with the Call Me Bot service, you can unlock the full potential of real-time WhatsApp notifications and elevate your IoT experience to new heights.

Prerequisites:

To follow this tutorial, you will need:

  • An NodeMCU ESP8266.
  • Arduino IDE installed on your computer.
  • USB cable for connection the ESP8266 NodeMCU to your computer.
  • Access to a stable internet connection.
  • A Call Me Bot and a WhatsApp account


Supplies

Materials and Software Used:


To create this project and implement real-time WhatsApp notifications using the ESP8266 and Call Me Bot, you will need the following materials:


1. ESP8266 Development Board: The ESP8266 is a low-cost Wi-Fi module that provides connectivity capabilities to your IoT projects.


2. USB Cable: A USB cable is required to connect the ESP8266 board to your computer for programming and power supply.


3. Wi-Fi Network: You will need access to a Wi-Fi network with internet connectivity to connect the ESP8266 board and send WhatsApp notifications.


4. Smartphone with WhatsApp: A smartphone with the WhatsApp application installed is necessary to receive the notifications.


5. Computer: A computer with the Arduino IDE installed will be used for programming the ESP8266 board.


Software Used:


1. Arduino IDE: The Arduino Integrated Development Environment (IDE) is an open-source software used for writing, compiling, and uploading code to the ESP8266 board.


2. ESP8266WiFi Library: This library provides the necessary functions and methods for connecting the ESP8266 board to a Wi-Fi network.


3. ESP8266HTTPClient Library: The ESP8266HTTPClient library allows you to send HTTP requests to web servers.


4. WiFiClient Library: The WiFiClient library provides the client-side functionalities for making HTTP requests.


5. UrlEncode Library: The UrlEncode library is used to encode the WhatsApp message before sending it as an HTTP request.


6. Call Me Bot API: The Call Me Bot API is a service that allows you to send WhatsApp messages programmatically. You will need to sign up and obtain an API key from their website.


By utilizing these materials and software, you can seamlessly integrate real-time WhatsApp notifications into your IoT projects and unlock the benefits of instant communication and remote monitoring.

Step 1: Setting Up the Arduino IDE and ESP8266:

Before we dive into the code, let's ensure that we have the Arduino IDE configured for ESP8266 development. Install the EP8266 board library according to the manufacturer's instructions.

Install ESP8266 Add-on in Arduino IDE

To install the ESP8266 board in your Arduino IDE, follow these next instructions:

  • In your Arduino IDE, go to File > Preferences


  • Enter http://arduino.esp8266.com/stable/package_esp8266com_index.json into the “Additional Boards Manager URLs” field as shown in the figure below. Then, click the “OK” button:

  • Open the Boards Manager. Go to Tools > Board > Boards Manager…


  • Search for ESP8266 and press install button for the “ESP8266 by ESP8266 Community“:


  • That’s it. It should be installed after a few seconds.


  • Connect your ESP8266 module to your computer using a USB cable and select the appropriate board and COM port in the Arduino IDE.


Step 2: Getting the CallMeBot API KEY

Before starting using the API, you need to get the CallmeBot WhatsApp API key.

  • Add the phone number +34 644 44 21 48 to your Phone Contacts. (Name it as you wish);


  • Send the following message: “I allow callmebot to send me messages” to the new Contact


  • Wait until you receive the message “API Activated for your phone number. Your APIKEY is XXXXXX” from the bot.


Step 3: Code Explanation and Implementation

Now, let's explore the code and understand how it enables real-time WhatsApp notifications.

#include <ESP8266WiFi.h>

#include <ESP8266HTTPClient.h>

#include <WiFiClient.h>

#include <UrlEncode.h>

These lines include the necessary libraries for the code to work. The ESP8266WiFi library provides functions for connecting to a Wi-Fi network. ESP8266HTTPClient allows making HTTP requests. WiFiClient provides functionality for handling Wi-Fi connections, and UrlEncode is used for URL encoding

const char* ssid = "XYZ";

const char* password = "ABC";

These lines define the Wi-Fi network credentials. Replace "XYZ" with your own SSID (Wi-Fi network name), and "ABC" with your network password.

String phoneNumber = "";

String apiKey = "";

These lines define the phone number and API key. The phoneNumber variable stores the recipient's phone number in international format. The apiKey variable stores the API key used to authenticate and authorize the requests. and don't forget to add international code before phone number.

void sendMessage(String message){

This line declares the sendMessage() function, which takes a string parameter message. This function will be used to send a WhatsApp message with the provided message content.

String url = "http://api.callmebot.com/whatsapp.php?phone=" + phoneNumber + "&apikey=" + apiKey + "&text=" + urlEncode(message);

This line constructs the URL for sending the WhatsApp message. It concatenates the base URL http://api.callmebot.com/whatsapp.php with the phone number, API key, and encoded message. The urlEncode() function is used to properly encode the message content.

WiFiClient client;

HTTPClient http;

http.begin(client, url);

These lines create instances of the WiFiClient and HTTPClient classes. The client object is used for the HTTP connection, while the http object is responsible for handling the HTTP requests. The begin() method is called to initialize the http object with the provided client and url.

http.addHeader("Content-Type", "application/x-www-form-urlencoded");

This line adds a header to the HTTP request specifying the content type as application/x-www-form-urlencoded. This header informs the server about the format of the data being sent.

int httpResponseCode = http.POST(url);

This line sends the HTTP POST request to the server using the POST() method of the http object. It returns the HTTP response code, which is stored in the httpResponseCode variable.

if (httpResponseCode == 200){

Serial.print("Message sent successfully");

}

else{

Serial.println("Error sending the message");

Serial.print("HTTP response code: ");

Serial.println(httpResponseCode);

}

These lines check the value of httpResponseCode to determine the success or failure of the message sending. If the code is 200, the message is considered sent successfully, and a success message is printed to the Serial Monitor. Otherwise, an error message along with the actual HTTP response code is displayed.

http.end();

This line releases the resources used by the http object, terminating the HTTP connection.

void setup() {

Serial.begin(115200);

Serial.begin(115200);: This line initializes the serial communication between the ESP8266 and the computer at a baud rate of 115200. It allows for communication between the ESP8266 and the Arduino IDE Serial Monitor for debugging and output purposes.

WiFi.begin(ssid, password);

Serial.println("Connecting");

Serial.begin(115200);: This line initializes the serial communication between the ESP8266 and the computer at a baud rate of 115200. It allows for communication between the ESP8266 and the Arduino IDE Serial Monitor for debugging and output purposes.

WiFi.begin(ssid, password);

Serial.println("Connecting");

WiFi.begin(ssid, password);: This line initiates the process of connecting to a Wi-Fi network. It takes the ssid (Wi-Fi network name) and password as parameters to authenticate and establish a connection.

Serial.println("Connecting");: This line prints the message "Connecting" to the serial monitor to indicate that the ESP8266 is attempting to establish a Wi-Fi connection.

while(WiFi.status() != WL_CONNECTED) {

delay(500);

Serial.print(".");

}

while(WiFi.status() != WL_CONNECTED) {: This line starts a while loop that continues until the ESP8266 successfully connects to the Wi-Fi network.

delay(500);: This line introduces a delay of 500 milliseconds between each iteration of the while loop. It helps in preventing excessive network connection attempts.

Serial.print(".");: This line prints a dot symbol to the serial monitor during each iteration of the while loop, indicating an ongoing connection attempt.

Serial.println("");

Serial.print("Connected to WiFi network with IP Address: ");

Serial.println(WiFi.localIP());

Serial.println("");: This line adds a newline character to the serial monitor to move to the next line after the while loop.

Serial.print("Connected to WiFi network with IP Address: ");: This line prints the message "Connected to WiFi network with IP Address: " to the serial monitor.

Serial.println(WiFi.localIP());: This line prints the IP address assigned to the ESP8266 by the Wi-Fi network. It provides the IP address information to the serial monitor.

// Send Message to WhatsApp

sendMessage("HELLO");

}

sendMessage("HELLO");: This line calls the sendMessage function, passing the string "Kiven aan Rohan" as the message parameter. It sends the WhatsApp message using the Call Me Bot service to the specified phone number with the provided API key.

Real-time WhatsApp notifications provide a multitude of benefits for IoT projects, ranging from improved user experience and prompt responses to enhanced remote monitoring capabilities. By integrating real-time WhatsApp notifications into your projects, you can elevate the level of convenience, enable quick action, and stay updated on the go.

With the ESP8266 and the Call Me Bot service, the process of integrating real-time WhatsApp notifications becomes seamless. By following this tutorial, you have learned how to establish a connection and send notifications with just a few lines of code. This enables effective communication with stakeholders while focusing on the core functionalities of your IoT projects.

Real-time WhatsApp notifications not only enhance user engagement and streamline communication but also empower you to respond swiftly to critical events and stay connected to your IoT projects from anywhere. Whether it's improving user experience, facilitating quick action, or ensuring timely monitoring, real-time WhatsApp notifications offer significant benefits to enhance your IoT projects' functionality and usability.

So, why wait? Start incorporating real-time WhatsApp notifications into your IoT projects today and experience the convenience, efficiency, and connectedness it brings. With the power of real-time communication at your fingertips, you can take your IoT projects to new heights and unlock their full potential.

Remember, the possibilities are endless, and with each project, you gain valuable insights and skills to explore even more exciting IoT applications. Embrace the world of real-time WhatsApp notifications and embark on your journey to create innovative and connected IoT experiences.