#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>
const char* ssid = "Wokwi-GUEST"; // Simulated SSID in Wokwi
const char* password = ""; // No password for simulated WiFi
const char* serverName = "http://soufiane-arduino.000webhostapp.com/dht/rest_api.php";
const char* infobipApiUrl = "https://xl8n1l.api.infobip.com/sms/2/text/advanced"; // Infobip SMS API URL
const char* infobipApiKey = "073aa53348374e76e1d348bf747e79c8-78009172-1923-4828-b6c7-bcb8ec314656"; // Replace with your Infobip API key
const char* sender = "Arduino"; // SMS sender name
const char* recipient = "213549660362"; // Replace with the recipient's phone number
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
// Connect to WiFi
Serial.print("Connecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("Connected to WiFi");
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Send temperature and humidity data to server
HTTPClient http;
http.begin(serverName);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String httpRequestData = "temperature=" + String(temperature) + "&humidity=" + String(humidity);
int httpResponseCode = http.POST(httpRequestData);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println(httpResponseCode);
Serial.println(response);
} else {
Serial.print("Error sending HTTP request. Response code: ");
Serial.println(httpResponseCode);
}
http.end();
// Check if temperature exceeds 70 and send SMS alert
if (temperature > 70) {
sendSMSAlert(temperature, humidity);
}
} else {
Serial.println("WiFi not connected");
}
delay(10000); // Send data every 10 seconds
}
void sendSMSAlert(float temperature, float humidity) {
HTTPClient http;
http.begin(infobipApiUrl);
http.addHeader("Content-Type", "application/json");
http.addHeader("Authorization", "App " + String(infobipApiKey));
String message = "Alert! Temperature: " + String(temperature) + "°C, Humidity: " + String(humidity) + "%";
String jsonPayload = "{\"messages\":[{\"from\":\"" + String(sender) + "\",\"destinations\":[{\"to\":\"" + String(recipient) + "\"}],\"text\":\"" + message + "\"}]}";
Serial.println("Sending SMS alert...");
Serial.println("Payload: " + jsonPayload);
int httpResponseCode = http.POST(jsonPayload);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println("SMS sent successfully.");
Serial.println(response);
} else {
Serial.print("Error sending SMS. Response code: ");
Serial.println(httpResponseCode);
Serial.println(http.errorToString(httpResponseCode).c_str());
}
http.end();
}