This project demonstrates how to send WhatsApp notifications using an Arduino UNO R4 WiFi board with CircuitDigest Cloud's WhatsApp API. The system uses an ultrasonic sensor to detect proximity and automatically sends alert messages when an object comes within a specified distance threshold.
- Introduction
- Components Required
- Circuit Diagram
- Setting Up CircuitDigest Cloud
- Understanding the API
- JSON Payload Structure
- Source Code Explanation
- Programming the Arduino
- Testing and Results
- Source Code
Sending WhatsApp messages from microcontrollers opens powerful possibilities for IoT applications. Whether you want to receive sensor alerts, monitor system status, or control devices remotely, WhatsApp provides a familiar and reliable notification channel. This tutorial shows how to integrate the Arduino UNO R4 WiFi with CircuitDigest Cloud's WhatsApp Notification API to create an intelligent distance monitoring system.
The Arduino continuously measures distance using an HC-SR04 ultrasonic sensor. When an object approaches closer than a predefined threshold, the board sends a WhatsApp message containing the device name, measured distance, and event description. A built-in cooldown mechanism prevents message flooding by limiting notifications to a configurable time interval.
- Arduino UNO R4 WiFi - Microcontroller board with built-in WiFi capability
- HC-SR04 Ultrasonic Sensor - Distance measurement sensor
- Jumper Wires - For connections
- USB Cable - For programming and power
- CircuitDigest Cloud Account - For WhatsApp API access
| HC-SR04 Pin | Arduino Pin |
|---|---|
| VCC | 5V |
| GND | GND |
| TRIG | D9 |
| ECHO | D10 |
Before you can send WhatsApp messages from your Arduino, you need to configure your CircuitDigest Cloud account and obtain an API key.
Visit the CircuitDigest Cloud platform and create a new account if you don't already have one. After email verification, log in to access the dashboard.
Navigate to the WhatsApp API section from the main menu. Click the option to link your phone number and follow the on-screen instructions to connect your WhatsApp account.
The platform will guide you through a verification process to authorize message sending. Once complete, your number appears as verified.
Access the API Keys section from the dashboard menu.
Click "Create New Key" or similar button to generate a unique API key. Copy this key immediately and store it securely, as you'll need it in your Arduino sketch.
Check the API usage limits and pricing details to understand the free tier allocation and any applicable charges for exceeding the quota.
The CircuitDigest WhatsApp API provides a straightforward integration method for IoT devices. Instead of managing WhatsApp's complex Business API infrastructure directly, you send HTTPS POST requests with JSON payloads to CircuitDigest's endpoint. The platform handles authentication, message formatting, and delivery.
POST https://cloud.circuitdigest.com/api/notify/whatsapp
Include your API key in the JSON payload under the api_key field. The server validates this key before processing your request.
CircuitDigest Cloud offers multiple pre-configured message templates for different notification scenarios. Each template includes placeholder variables that you populate with your device data. Common templates include:
- critical_event_alert - For urgent events requiring immediate attention
- threshold_violation_alert - When sensor readings exceed safety limits
- operational_status_update - For routine status reports
You select the desired template using the template_id field in your API request.
The API uses a JSON structure to transmit message content. Think of the JSON payload as a form with fixed field names but dynamic content. The CircuitDigest Cloud platform reads your submitted data, identifies which template you selected, and automatically inserts your variable values into the corresponding placeholders before delivering the message.
The following code block shows the basic structure. Do not modify the field names, as the API requires them.
{
"phone_number": "919876543210",
"template_id": "critical_event_alert",
"api_key": "YOUR_API_KEY",
"variables": {
"device_name": "Proximity Monitor",
"var1": "Object detected",
"var2": "15.2 cm",
"var3": "Main entrance",
"var4": "Immediate action required"
}
}- phone_number - The recipient WhatsApp number, including country code (e.g., 919876543210 for India)
- template_id - Specifies which message template to use from the available options
- api_key - Your authentication key from CircuitDigest Cloud
- variables - Contains the dynamic values that populate the template placeholders
- device_name / var1 / var2 / var3 / var4 - Sensor readings or descriptive text from your application
You can rename the variable fields to match your template requirements. For example, use parameter, measured_value, or location for clarity. The platform automatically maps them to the template's placeholder positions in order.
When your Arduino transmits this JSON payload through an HTTPS POST request, CircuitDigest Cloud receives it and performs the following operations:
- Validates your API key to confirm authentication
- Retrieves the template specified by
template_id - Replaces each
{#var#}placeholder with the corresponding value from yourvariablesobject - Sends the formatted WhatsApp message to the registered recipient
You never construct the message text manually in your firmware. The template system handles all formatting automatically, ensuring consistent and professional notifications.
The following examples demonstrate different payload configurations for various use cases. These are reference examples only. Replace all values with your actual sensor data and device information.
Template: critical_event_alert
Used when an object enters the detection zone, and immediate notification is required.
{
"phone_number": "919876543210",
"template_id": "critical_event_alert",
"api_key": "YOUR_API_KEY",
"variables": {
"device_name": "Entry Sensor",
"var1": "Motion detected",
"var2": "8.5 cm",
"var3": "Front door",
"var4": "Security alert"
}
}Template: threshold_violation_alert
Used when the measured distance crosses a predefined safety limit.
{
"phone_number": "919876543210",
"template_id": "threshold_violation_alert",
"api_key": "YOUR_API_KEY",
"variables": {
"device_name": "Safety Monitor",
"var1": "Threshold exceeded",
"var2": "12.3 cm",
"var3": "Production line",
"var4": "Warning level"
}
}Template: operational_status_update
Used for routine status reports or non-urgent notifications.
{
"phone_number": "919876543210",
"template_id": "operational_status_update",
"api_key": "YOUR_API_KEY",
"variables": {
"device_name": "Distance Sensor",
"var1": "Normal operation",
"var2": "45.0 cm",
"var3": "Parking area",
"var4": "All systems OK"
}
}Remember that these examples show format only. Customize the values to reflect your actual application requirements and sensor readings. The system will accept any text content you provide, so make your messages clear and informative.
Before examining the complete source code, let's understand the program's operational logic. The Arduino continuously measures distance using the ultrasonic sensor, compares the reading against a configured threshold, and triggers a WhatsApp alert when an object comes too close. A cooldown timer prevents message flooding by enforcing a minimum interval between consecutive alerts.
The sketch begins by including the necessary libraries for WiFi connectivity and HTTPS communication:
#include <WiFiS3.h>
#include <WiFiSSLClient.h>WiFiS3.h provides network connectivity functions specific to the Arduino UNO R4 WiFi, while WiFiSSLClient.h enables secure HTTPS communication with the cloud server.
Next, the code defines the GPIO pins connected to the ultrasonic sensor:
const int trigPin = 9;
const int echoPin = 10;The distance threshold and cooldown period are defined as constants for easy modification:
const float DISTANCE_THRESHOLD = 20.0; // cm
const unsigned long COOLDOWN_PERIOD = 20000; // 20 secondsThe program sets alerts to trigger when the distance falls below 20 centimeters, with a 20-second cooldown between messages. Adjust these values based on your specific application needs.
WiFi credentials and API configuration follow:
const char* ssid = "YOUR_WIFI";
const char* password = "YOUR_PASS";
const char* api_key = "YOUR_API_KEY";Replace these placeholder strings with your actual network name, password, and the API key obtained from CircuitDigest Cloud.
A secure WiFi client object handles encrypted communication:
WiFiSSLClient client;The program maintains a timestamp to track the last alert:
unsigned long lastAlertTime = 0;The setup() function executes once at startup. It initializes serial communication for debugging, configures the ultrasonic sensor pins, and establishes WiFi connectivity:
void setup() {
Serial.begin(115200);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}The program waits in a loop until the WiFi connection succeeds before proceeding to the main monitoring loop.
Inside the loop() function, the Arduino repeatedly measures distance by triggering an ultrasonic pulse and timing the echo return:
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
float distance = duration * 0.034 / 2;The pulseIn() function measures how long the echo pin remains high. This duration gets converted to distance in centimeters using the speed of sound (0.034 cm/microsecond) divided by two for the round trip.
The program then checks if the measured distance violates the threshold and whether enough time has elapsed since the last alert:
if (distance < DISTANCE_THRESHOLD &&
(millis() - lastAlertTime > COOLDOWN_PERIOD)) {
sendWhatsApp(distance);
}When both conditions are satisfied, the code calls the sendWhatsApp() function with the measured distance value.
The sendWhatsApp() function handles all API communication. It first establishes a secure connection to the CircuitDigest Cloud server:
if (client.connect("cloud.circuitdigest.com", 443)) {
// Prepare and send request
}Next, it constructs the JSON payload with the template ID and variable values:
String jsonData = "{\"phone_number\":\"919876543210\",";
jsonData += "\"template_id\":\"critical_event_alert\",";
jsonData += "\"api_key\":\"" + String(api_key) + "\",";
jsonData += "\"variables\":{";
jsonData += "\"device_name\":\"Arduino Distance Monitor\",";
jsonData += "\"var1\":\"Object detected at " + String(distance) + " cm\",";
jsonData += "\"var2\":\"Distance threshold violated\",";
jsonData += "\"var3\":\"Laboratory\",";
jsonData += "\"var4\":\"Immediate attention required\"";
jsonData += "}}";Notice how the measured distance value gets inserted dynamically using String concatenation. The rest of the payload contains static configuration.
The function then sends the HTTP headers followed by the JSON body:
client.println("POST /api/notify/whatsapp HTTP/1.1");
client.println("Host: cloud.circuitdigest.com");
client.println("Content-Type: application/json");
client.print("Content-Length: ");
client.println(jsonData.length());
client.println();
client.println(jsonData);After transmission, the code waits for the server response and displays it via serial for debugging purposes:
while (client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
}Finally, the program updates the timestamp to reset the cooldown timer:
lastAlertTime = millis();This comprehensive workflow ensures reliable distance monitoring with controlled notification frequency. The architecture remains simple, maintainable, and easily adaptable to other sensor types or alert conditions.
Follow these steps to install the required software tools, configure the sketch, and upload the firmware to your Arduino UNO R4 WiFi board.
Download and install the latest Arduino IDE from the official website if you haven't already. Open the IDE, navigate to Tools → Board → Boards Manager, search for "Arduino UNO R4", and install the board package. This adds support for the R4 WiFi variant.
Open the sketch file and locate the configuration section near the top. Replace YOUR_WIFI with your network name, YOUR_PASS with the WiFi password, and YOUR_API_KEY with the key you generated from CircuitDigest Cloud. Update the phone number in the payload to match your verified recipient number with the country code.
Connect your Arduino UNO R4 WiFi to your computer using the USB-C cable. From the Tools menu, select Board → Arduino UNO R4 WiFi. Then choose the correct port where your board appears. The IDE should automatically detect it.
Click the Upload button to compile and flash the firmware. Wait for the upload to complete successfully. Open Serial Monitor (Tools → Serial Monitor) and set the baud rate to 115200. You should see the WiFi connection status and distance readings. Place an object within the threshold distance to trigger a test alert.
After successfully uploading the firmware and powering the Arduino, the Arduino WhatsApp alert system initializes WiFi connectivity and begins continuous distance monitoring. The Serial Monitor displays real-time measurements and connection status. When an object approaches closer than the configured threshold, the Arduino immediately formats the API request and transmits it to CircuitDigest Cloud.
The demonstration shows the sensor detecting an approaching object and the corresponding Arduino sending a notification to the smartphone. The message includes the device name, event description, measured distance, and location information as configured in the payload. After sending an alert, the system respects the cooldown period before allowing another notification, preventing excessive messages during sustained proximity events.
You can monitor the serial output to verify distance readings and confirm when alerts are triggered. Successful API requests will show response codes from the server. Any connection failures or authentication errors also appear in the serial log, helping you troubleshoot configuration issues quickly.
The complete Arduino sketch is available in the Source Code folder. Download the .ino file and open it in your Arduino IDE to get started.
This project is licensed under the MIT License - see the LICENSE file for details.
For questions or support, please visit CircuitDigest Cloud or contact the CircuitDigest team.








