#include WiFi.h
#include HTTPClient.h
const char* ssid = "your_wifi_ssid";
const char* password = "your_wifi_password";
const char* apiKey = "your_callmebot_api_key";
void setup() {
Serial.begin(115200);
delay(100);
// Connect to Wi-Fi
Serial.println();
Serial.println("Connecting to WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
// Your message to be sent
String message = "Hello from ESP32!";
// Construct the API URL
String url = "https://api.callmebot.com/whatsapp.php?phone=YOUR_PHONE_NUMBER&text=" + message + "&apikey=" + apiKey;
Serial.print("Sending request to: ");
Serial.println(url);
// Send HTTP POST request
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println(payload); // Print response payload
} else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
}
delay(60000); // Send the message every 1 minute
}
Bobby explaining how my button works
#include WiFi.h
#include HTTPClient.h
#include UrlEncode.h
const char* ssid = "---------";
const char* password = "---------;
const int buttonPin = 27;
String phoneNumber = "-----------";
String apiKey = "--------";
bool buttonPressed = false;
unsigned long lastButtonPressTime = 0;
const unsigned long debounceDelay = 50; // Debounce delay in milliseconds
void sendMessage(String message) {
// Data to send with HTTP POST
String url = "https://api.callmebot.com/whatsapp.php?phone=" + phoneNumber + "&apikey=" + apiKey + "&text=" + urlEncode(message);
HTTPClient http;
http.begin(url);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpResponseCode = http.POST(url);
if (httpResponseCode == 200) {
Serial.println("Message sent successfully");
} else {
Serial.println("Error sending the message");
Serial.print("HTTP response code: ");
Serial.println(httpResponseCode);
}
http.end();
}
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
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());
}
void loop() {
bool currentButtonState = digitalRead(buttonPin) == LOW;
if (currentButtonState != buttonPressed) {
lastButtonPressTime = millis();
}
buttonPressed = currentButtonState;
if (buttonPressed && (millis() - lastButtonPressTime > debounceDelay)) {
sendMessage("Hello, Finn");
while (digitalRead(buttonPin) == LOW) {
delay(50); // Wait until the button is released
}
}
}
When the button is pressed, the microcontroller sends a message directly to your phone