How to send WhatsApp Messages from ESP8266 NodeMCU

You can use the instructions below to send WhatsApp Messages from a ESP8266 NodeMCU Board.

You can use the Arduino IDE to program the board and send WhatsApp Messages (or Images) using the TextMeBot API.

Requirements:

You need to get an APIkey first. Click here to get one.
– Connect your phone number to the API (follow the instructions received by email)
– You need an ESP8266 (obviously)
– Arduino IDE or similar

Introduction

To send messages using the TextMeBot API you need to make a GET or a POST request to the following URL: 

http://api.textmebot.com/send.php?recipient={{RECIPIENT_PHONE_NUMBER}}&apikey={{APIKEY}}&text={{TEXT_MESSAGE}}

{{RECIPIENT_PHONE_NUMBER}} = The phone number that you want to send the messaage to
{{APIKEY}} = Your APIkey
{{TEXT_MESSAGE}} = Text message to send to the number.

Installing the URLEncode Library

As we’ve seen previously, the text message to be sent has to to be URLencoded. URL encoding converts characters into a format that can be transmitted over HTTP.

This will allow us to include characters like ç, ª, º, à, ü in our messages or even send emojis. 

You can encode the message yourself, or you can use a library, which is much simpler. We’ll use the UrlEncode library that can be installed on your Arduino IDE.

Go to Sketch > Include Library > Manage Libraries and search for URLEncode library by Masayuki Sugahara as shown below.

Install URL Encode Library Arduino IDE

Send a WhatsApp Message (ESP8266 Code)

The following code will send a Whatsapp message as soon as the ESP6266 board boots. This is a simple expample but it will help you to understand how to code the board to send a test whatsapp message.
 
#include 
#include 
#include 
#include 

const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";

// +international_country_code + phone number
// Germany +49, example: +49123123123
String phoneNumber = "REPLACE_WITH_YOUR_PHONE_NUMBER";
String apiKey = "REPLACE_WITH_API_KEY";

void sendMessage(String message){

  // Data to send with HTTP POST (You can also use GET)
  String url = "http://api.textmebot.com/send.php?recipient" + phoneNumber + "&apikey=" + apiKey + "&text=" + urlEncode(message);
  WiFiClient client;    
  HTTPClient http;
  http.begin(client, url);

  // Specify content-type header
  http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  
  // Send HTTP POST request
  int httpResponseCode = http.POST(url);
  if (httpResponseCode == 200){
    Serial.print("Message sent successfully");
  }
  else{
    Serial.println("Error sending the message");
    Serial.print("HTTP response code: ");
    Serial.println(httpResponseCode);
  }

  // Free resources
  http.end();
}

void setup() {
  Serial.begin(115200);

  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());

  // Send Message to WhatsAPP
  sendMessage("Hello from ESP8266!");
}

void loop() {
  
}

Code Details

Sending a message to WhatsApp using the TextMeBot API is quite simple. You just need to make an HTTP POST  or GET request using the API endpoint.

First, include the necessary libraries:

#include <ESP8266WiFi.h> 
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <UrlEncode.h>

Insert your network credentials on the following variables to connect the board to your WiFi:

const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";

Insert your phone number and APIkey. The phone number should be in international format (including the + sign).

String phoneNumber = "REPLACE_WITH_YOUR_PHONE_NUMBER";
String apiKey
= "REPLACE_WITH_YOUR_API_KEY";
sendMessage()

We create a function called sendMessage() that you can call later to send messages to WhatsApp. This function accepts as an argument the message you want to send.

void sendMessage(String message){

Inside the function, we prepare the URL for the request with your information, phone number, API key, and message.

As we’ve seen previously, the message needs to be URL encoded. We’ve included the UrlEncode library to do that. It contains a function called urlEncode() that encodes whatever message we pass as argument (urlEncode(message)).

  String url = "http://api.textmebot.com/send.php?recipient" + phoneNumber + "&apikey=" + apiKey + "&text=" + urlEncode(message);
Create and start an HTTPClient on that URL:
 
HTTPClient http;
http
.begin(url);

Specify the content type:

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

Finally, send the HTTP post request. The following line sends the request and saves the response code:

int httpResponseCode = http.POST(url);

If the response code is 200, it means the post request was successful. Otherwise, something went wrong.

// Send HTTP POST request
int httpResponseCode = http.POST(url);
if (httpResponseCode == 200){
Serial
.print("Message sent successfully");
}
else{
Serial
.println("Error sending the message");
Serial
.print("HTTP response code: ");
Serial
.println(httpResponseCode);
}

Finally, free up the resources:

// Free resources
http
.end();
setup()

In the setup(), initialize the Serial Monitor for debugging purposes.

Serial.begin(115200);

Connect to your local network and print the board IP address.

WiFi.begin(ssid, password);
Serial
.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial
.print(".");
}
Serial
.println("");
Serial
.print("Connected to WiFi network with IP Address: ");
Serial
.println(WiFi.localIP());

Then, we can send a message to WhatsApp by simply calling the sendMessage() function. In this case, we’re sending the message Hello from ESP8266!

// Send Message to WhatsAPP
sendMessage("Hello from ESP8266!");

If you have questions, don’t hesitate to contact support@textmebot.com