IoT: Whatsapp Notification Method 2: CallMeBoT

https://www.callmebot.com/blog/free-api-whatsapp-messages/

Lets look at the Conection Diagram-

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

// Function to convert a single character to its percent-encoded form
void url_encode_char(char c, char *output) {
    sprintf(output, "%%%02X", (unsigned char)c);
}

// Function to determine if a character is safe (unreserved) in URL
int is_unreserved_char(char c) {
    if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
        return 1;
    }
    return 0;
}

// Function to URL-encode a string
char *url_encode(const char *str) {
    size_t len = strlen(str);
    // Allocate enough space: worst case all characters need encoding (3 chars each)
    size_t max_encoded_len = len * 3 + 1;
    char *encoded = malloc(max_encoded_len);
    if (encoded == NULL) {
        printf("Memory allocation failed.\n");
        exit(1);
    }

    char *p = encoded;

    for (size_t i = 0; i < len; i++) {
        char c = str[i];
        if (is_unreserved_char(c)) {
            *p++ = c;  // Safe character, copy as is
        } else {
            // Encode character
            sprintf(p, "%%%02X", (unsigned char)c);
            p += 3;
        }
    }
    *p = '\0'; // Null-terminate the string
    return encoded;
}

int main() {
    const char *original = "Hello World! & Welcome to IoTthingHuB.";
    char *encoded = url_encode(original);

    printf("Original: %s\n", original);
    printf("URL Encoded: %s\n", encoded);

    free(encoded);
    return 0;
}
#include <DHT.h>
// DHT11 Configuration
#define DHTPIN 2             // GPIO2 for DHT11 Sensor
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

dht.begin();

  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();

  if (isnan(temperature) || isnan(humidity)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
// WiFi Credentials
const char* ssid =  "Your_WiFi_Name" ;        // Your WiFi Name
const char* password = "Your_WiFi_Password";  // Your WiFi Password

// CallMeBot API
String phoneNumber = "880xxxxxxxxxx";         // Your full WhatsApp number with country code ignore + sign
String apiKey = "xxxxxxx";                    // Your API Key

WiFiClient client;
HTTPClient http;

Serial.begin(115200);
  WiFi.begin(ssid, password);

  Serial.print("Connecting to WiFi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nWiFi connected");

String message = "🌡 Temp: " + String(temperature, 2) + "°C \r\n 💧 Humidity: " + String(humidity, 2) + "%";

  Serial.println("Sending to WhatsApp: " + message);
  String WhatsAppmsg = url_encode (message);    // From the C Function

  if (WiFi.status() == WL_CONNECTED) {
    String url = "http://api.callmebot.com/whatsapp.php?phone=" + phoneNumber +
                 "&text=" + WhatsAppmsg + 
                 "&apikey=" + apiKey;

    http.begin(client, url);
    int httpCode = http.GET();

    if (httpCode > 0) {
      Serial.println("Message sent successfully.");
    } else {
      Serial.print("Error sending message: ");
      Serial.println(http.errorToString(httpCode).c_str());
    }
    http.end();