#include <Wire.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <UrlEncode.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
//international country code + phone number
String phoneNumber = "9647700300621";
String apiKey = "6373521";
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);
// 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.print("Message sent successfully");
}
else{
Serial.println("Error sending the message");
Serial.print("HTTP response code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}
bool same_msg=false;
void setup() {
Wire.begin(23, 22);
Serial.begin(115200);
pinMode(14, OUTPUT);//pump relay
pinMode(35, INPUT);//current sensor
pinMode(34, INPUT);//soil sensor
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());
// Send Message to WhatsAPP "Add your name + NV number + Class"
// Example: "Hello From Ebrahim - NV21058 - 12.CCP !"
}
void loop()
{
int16_t i = analogRead(34);
String msg = i < 2165 ? "WET" : i > 3135 ? "DRY" : "OK";
Serial.println(msg);
if(msg=="DRY"&&same_msg)
{sendMessage(msg);
same_msg=true;
}
else
same_msg=false;
delay(500);
}