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