Send WhatApp Message from ESP8266

Dear all,
I am trying to send a message from ESP8266 to WhatsApp. But it fails with the message: HTTP response code:400.

I have tried the same sketch to send messages to WhatsApp with ESP32, but failed when using ESP8266.

Please experts can show where the sketch error is. Thank you

Here is the sketch :

#if defined(ESP32)   
  #include <WiFi.h>
  #include <HTTPClient.h
#elif defined(ESP8266)
  #include <ESP8266WiFi.h>
  #include <ESP8266HTTPClient.h>
#endif
#include <WiFiClient.h>
#include <UrlEncode.h>  

#define Sw1  4        // pin sensor Sw1
int stat_Sw1=0;       // variabel status sensor Sw1
//--------------------------------------------------------------------
#define WIFI_SSID "REPLACE_WITH_YOUR_SSID" //nama SSID dari WiFi yg ada
#define WIFI_PASSWORD "REPLACE_WITH_YOUR_PASSWORD"  //password akses WiFi
//-----------------------
String mobile_number = "+REPLACE_WITH_YOUR_PHONE_NUMBER";
String api_key = "REPLACE_WITH_API_KEY";

/*==================================================================*/
void sendMessage(String message){
  String API_url = "https://api.callmebot.com/whatsapp.php?phone=" + mobile_number + "&apikey=" + api_key + "&text=" + urlEncode(message);
  HTTPClient http;
  WiFiClient client;
  http.begin(client, API_url);
  //-----------------------
  http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  int http_response_code = http.POST(API_url);
  if (http_response_code == 200){
    Serial.println("Whatsapp message sent successfully");
    Serial.println("-----------------");
  }
  else{
    Serial.println("Error sending the message");
    Serial.print("HTTP response code: ");
    Serial.println(http_response_code);
  }
  http.end();
}
/*==================================================================*/
void setup() {
  Serial.begin(115200);
  pinMode(Sw1, INPUT_PULLUP);  
  /* --------------- Koneksi ke jaringan WiFi lokal ----------------*/
  Serial.print("Connecting to Wi-Fi Access Point "); 
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED)  {
    Serial.print("."); 
    delay(300);
  }
  Serial.println("WiFi connected."); Serial.print("Alamat IP : "); 
  Serial.println(WiFi.localIP()); Serial.println("-----------------"); 
  sendMessage("Send WA message from ESP8266");
}
/*==================================================================*/
void loop() {
  
}

And this is the error :
Error sending the message
HTTP response code: 400

Isn't a 400 a page not found?

No, 400 is "Bad request" (page not found is 404).

Looks like there's something wrong with the API call (but I don't know what, because I have never user those APIs).

Are you using this API?

did you try to print the url to the serial monitor?
Does it look like the example given there?

(https://api.callmebot.com/whatsapp.php?phone=+34123123123&text=This+is+a+test+from+CallMeBot&apikey=1234567890)

If that translates to: same code is working on Esp32 but not on Esp8266, you probably need to update your Esp8266 core.

And that failed because the code would not compile.

  #include <HTTPClient.h

This line is missing a ">" at the end.

Fix that and hopefully it will compile for ESP32 and you can test it and see if you get the same error that you got with ESP8266. Let us know.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.