Mod edit: Phone number removed.
Hi! Totally new to this scene so bear with me
I have tried to configure standalone ESP32 board to send Whatsapp message when pin 6 in my board gets 3V3 signal. I get board to send me message when board comes online but when I add defition of what is INPUT in the board (or I think it does that) then it goes to some kind of loop and does not even send the initial message.
Here is my code:
#include <WiFi.h>
#include <HTTPClient.h>
#include <UrlEncode.h>
const char* ssid = "xxxxxxx";
const char* password = "xxxxxxxx";
// +international_country_code + phone number
// Example: Portugal xxxxxxxx
String phoneNumber = "xxxxxx";
String apiKey = "xxxxxxxx";
// Pin definition
const int waterLevelPin = 6; // 6 pin for water level signal
bool previousWaterLevelState = LOW; // Tracks the previous state of the pin
bool messageSent = false; // Ensure only one message per state change
void sendMessage(String message) {
// Data to send with HTTP POST
String url = "https://api.callmebot.com/whatsapp.php?phone=" + phoneNumber + "&text=" + urlEncode(message) + "&apikey=" + apiKey;
HTTPClient http;
http.begin(url);
// Specify content-type header
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Send HTTP POST request
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);
}
// Free resources
http.end();
}
void setup() {
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());
// Set up the water level pin as an input
pinMode(waterLevelPin, INPUT);
---> If I comment this "pinMode" then I can get initial message but if I remove comment and leav code to run then I get into the boot loop
// Send an initial message
sendMessage("Kaivon vedenkorkeus anturi yhdistetty verkkoon.");
}
void loop() {
// Read the current state of the water level sensor (6 pin)
bool currentWaterLevelState = digitalRead(waterLevelPin);
// Check if the state has changed
if (currentWaterLevelState != previousWaterLevelState) {
// If the signal is HIGH (water level is high)
if (currentWaterLevelState == HIGH && !messageSent) {
sendMessage("Kaivon veden taso korkealla!"); // Water level high message
messageSent = true; // Mark message as sent
}
// If the signal is LOW (water level has dropped)
else if (currentWaterLevelState == LOW && messageSent) {
sendMessage("Kaivon veden taso laskenut."); // Water level dropped message
messageSent = false; // Reset message sent flag
}
// Update the previous state
previousWaterLevelState = currentWaterLevelState;
}
// Add a small delay to avoid bouncing issues
delay(1000);
}
This is my board: