#include <SoftwareSerial.h>
#include <DHT.h>
// Define pins and variables
#define DHTPIN 2 // DHT11 data pin connected to digital pin 2
#define DHTTYPE DHT11 // DHT 11 sensor
DHT dht(DHTPIN, DHTTYPE);
// Initialize SIM800L on software serial
SoftwareSerial sim800l(10, 11); // RX, TX (connect to TX and RX of SIM800L)
void setup() {
Serial.begin(9600); // Serial monitor for debugging
sim800l.begin(9600); // SIM800L baud rate
dht.begin();
delay(1000); // Allow time for SIM800L to initialize
// Test SIM800L connection
sim800l.println("AT");
delay(1000);
if (sim800l.available()) {
Serial.println("SIM800L Ready!");
} else {
Serial.println("SIM800L not responding");
}
}
void loop() {
// Read temperature and humidity
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Check if data is valid
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Format message
String message = "Temp: " + String(temperature) + "°C, Humidity: " + String(humidity) + "%";
Serial.println("Sending SMS: " + message);
// Send SMS
sendSMS("+1234567890", message); // Replace with recipient phone number
delay(60000); // Send data every 60 seconds
}
// Function to send SMS
void sendSMS(String phoneNumber, String message) {
sim800l.println("AT+CMGF=1"); // Set SMS mode to text
delay(1000);
sim800l.print("AT+CMGS=\"");
sim800l.print(phoneNumber); // Phone number
sim800l.println("\"");
delay(1000);
sim800l.println(message); // SMS content
delay(100);
sim800l.write(26); // ASCII code for CTRL+Z to send
delay(1000);
Serial.println("SMS sent!");
}