#include <WiFi.h>
#include <HTTPClient.h>
#include <UrlEncode.h>

#include "DHTesp.h"
int pin = 15;
DHTesp sensor;


const char* ssid = "Wokwi-GUEST";
const char* password = "";
 
 
String phoneNumber = "212705489106";
String apiKey = "2693228";
 
void sendMessage(String message){
 
  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.print("Message sent successfully");
  } else {
    Serial.println("Error sending the message");
    Serial.print("HTTP response code: ");
    Serial.println(httpResponseCode);
  }
  
  http.end();
}
 
void setup() {
  Serial.begin(115200);
  
  sensor.setup(pin,DHTesp::DHT22);
  
  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());
  
  
  TempAndHumidity data = sensor.getTempAndHumidity();

  sendMessage("Temp:"+String(data.temperature,2)+"C" + "\n" + "Humidity:"+String(data.humidity,1)+"%");
}
 
void loop() {

}