Gayathri Krishnan
Published © GPL3+

ESP32 + DHT11: SMS Temperature Alert System

A compact IoT project that monitors real-time temperature using the DHT11 sensor and sends SMS via Twilio when readings cross safe limits

BeginnerWork in progress1 hour31
ESP32 + DHT11: SMS Temperature Alert System

Things used in this project

Hardware components

DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
ESP32
Espressif ESP32
×1

Software apps and online services

SMS Messaging API
Twilio SMS Messaging API
Arduino IDE
Arduino IDE

Story

Read more

Schematics

dht_revised_PCaYYsLVFL.pdf

Code

SMS temperature alert using DHT11 sensor and ESP 32

Arduino
1)Before running this code, create a Twilio account, get your Account SID, Auth Token, and a Twilio phone number.
2) Replace them in the code below with your own credentials.
#include <WiFi.h>
#include <HTTPClient.h>
#include "DHT.h"
#define DHTTYPE DHT11
#define DHTPIN 4
DHT dht(DHTPIN, DHTTYPE);
float humidity ;
float tempC ;

float upperLimit = 8.0;
float lowerLimit = 2.0;
unsigned long lastAlertTime = 0;  
unsigned long alertCooldown = 60000; 

const char* ssid = "YOUR_WIFI_CONNECTION";
const char* password = "YOUR_WIFI_PASSWORD";


const char* accountSid = "YOUR_ACCOUNT_SID";
const char* authToken  = "YOUR_AUTH_TOKEN";
const char* fromNumber = "+YOUR_TWILIO_NUMBER";   // include +
const char* toNumber   = "+YOUR_PHONE_NUMBER;          // include +

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
  dht.begin();
WiFi.begin(ssid, password);
  Serial.print("Connecting to WiFi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nConnected to WiFi!");
}
void sendSMS(float tempC, float humidity) {
  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("WiFi not connected!");
    return;
  }
  HTTPClient http;
  String url = "https://api.twilio.com/2010-04-01/Accounts/" + String(accountSid) + "/Messages.json";
  http.begin(url);
  http.setAuthorization(accountSid, authToken);
  http.addHeader("Content-Type", "application/x-www-form-urlencoded");

  String body = "To=" + String(toNumber) + 
                "&From=" + String(fromNumber) + 
                "&Body= Warning! Temperature out of range-temperature:  " + String(tempC) + "C, Humidity: " + String(humidity) + "%";

  int httpResponseCode = http.POST(body);

  if (httpResponseCode > 0) {
    Serial.println("SMS sent!");
    Serial.println(http.getString()); 
  } else {
    Serial.print("Error sending SMS: ");
    Serial.println(httpResponseCode);
  }

  http.end();
}

void loop() {
  // put your main code here, to run repeatedly:
 humidity = dht.readHumidity();
 tempC = dht.readTemperature();

  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.print("%  Temperature: ");
  Serial.print(tempC);
  Serial.println("C");
unsigned long currentTime = millis();

if ((tempC > upperLimit || tempC < lowerLimit) && (currentTime - lastAlertTime > alertCooldown)) {
  
  sendSMS(tempC,humidity);
  Serial.println("SMS sent");
  lastAlertTime = currentTime;
}
  delay(2000);  
}

Credits

Gayathri Krishnan
4 projects • 0 followers
I’m an MBBS doctor who has always been interested in physics, which led me into Arduino projects. I’ve been building for about three months

Comments