ESP32-WROOM-32 sending WA when pin HIGH

Mod edit: Phone number removed.

Hi! Totally new to this scene so bear with me :slightly_smiling_face:

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:

Do you know if your sensor provides a pull-up or pull down ?

Pins 6 to 11 are connected to the internal SPI flash and is not recommended for general use:

I have no idea. Only manual that I have found is this. Sam manual also came with sensor.

https://www.google.com/url?sa=t&source=web&rct=j&opi=89978449&url=https://www.rotomon.fi/wp-content/uploads/2016/09/Asennus-ja-kaytto-ohje-langaton-pintavahti-RF-412.pdf&ved=2ahUKEwjkuYnThMGJAxW_HRAIHboJMw8QFnoECBcQAQ&usg=AOvVaw2vIMtyJRix-fgMqNKnEGYS

It says:
External alarm.
The display unit has a relay (NO/NC) with potiential free poles for connecting external alarms (Fig. 6A).

Ok, good to know! I have tried pin 8 and maybe 4. I will try to change the pin in the code.

And I'm trying it without the sensor by connecting jump wires between pins. Power to board comes from USB but it will be changed to power from sensor. Boot loop occurs with or without jump wires.

try using

  pinMode(waterLevelPin, INPUT_PULLUP);       

the pin will be HIGH by default and goes LOW if the modules goes LOW. (use NO or NC to make that work)