What You’ll Need for the Project
To build this simple IoT project, you’ll need the following components:
- Hardware:
- Microcontroller: A popular choice is the ESP8266 or ESP32. These are affordable and have built-in Wi-Fi capabilities, making them ideal for IoT projects.
- Sensors: For this project, you can use a DHT11 temperature and humidity sensor.
- Breadboard and Jumper Wires: To set up your components.
- Power Supply: A USB cable to power the microcontroller.
- Software:
- Arduino IDE: To write and upload code to your microcontroller.
- Blynk or ThingSpeak: Cloud platforms to visualize your IoT data.
Step 1: Setting Up the Hardware
- Connecting the DHT11 Sensor to ESP8266/ESP32:
- DHT11 Pinout: Connect the VCC pin of the DHT11 sensor to the 5V pin of the ESP8266. Connect GND to the ground pin of the ESP8266.
- The Data pin of the DHT11 sensor should be connected to a digital I/O pin of the microcontroller (e.g., D2 pin on the ESP8266).
- Circuit Overview:
- Ensure all connections are secure before proceeding to the next step.
Step 2: Writing the Code
We’ll use the Arduino IDE to write the code for this IoT project. The code will collect temperature and humidity data from the DHT11 sensor and send it to the cloud.
Install Required Libraries:
- Open the Arduino IDE and go to Sketch > Include Library > Manage Libraries.
- Search for DHT sensor library and install it.
- Install the ESP8266/ESP32 board manager if you haven’t done so already. (Go to File > Preferences, add the following URL in the Additional Boards Manager URLs field:
http://arduino.esp8266.com/stable/package_esp8266com_index.json
).
Code for the Project:
#include <ESP8266WiFi.h>
#include <DHT.h>
#define DHTPIN 2 // Pin connected to DHT11 sensor
#define DHTTYPE DHT11 // Define sensor type
DHT dht(DHTPIN, DHTTYPE);
const char* ssid = "your_wifi_ssid"; // WiFi SSID
const char* password = "your_wifi_password"; // WiFi Password
void setup() {
Serial.begin(9600);
dht.begin();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from sensor!");
return;
}
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print("°C, Humidity: ");
Serial.print(humidity);
Serial.println("%");
// Add code here to send the data to the cloud platform (e.g., Blynk or ThingSpeak)
delay(2000); // Wait for 2 seconds before reading again
}
Explanation of the Code:
- The code first connects to the Wi-Fi network using the WiFi.begin() function.
- Then, it initializes the DHT11 sensor and continuously reads the temperature and humidity values in the loop() function.
- If the readings are valid, the values are displayed on the serial monitor.
Step 3: Sending Data to the Cloud
To make the IoT project useful, you’ll send the data (temperature and humidity) to a cloud platform for visualization.
You can use platforms like Blynk or ThingSpeak to visualize your sensor data in real-time. We’ll use ThingSpeak in this example.
Step 1: Create a ThingSpeak Account
- Go to ThingSpeak and sign up for a free account.
- Once logged in, create a new channel to store the sensor data.
- Add fields for temperature and humidity.
- Note the Channel API Key for use in your code.
Step 2: Modify the Code to Send Data to ThingSpeak
#include <ESP8266WiFi.h>
#include <DHT.h>
#include <WiFiClient.h>
#include <ThingSpeak.h>
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
const char* ssid = "your_wifi_ssid";
const char* password = "your_wifi_password";
const char* server = "api.thingspeak.com"; // ThingSpeak server
unsigned long myChannelNumber = YOUR_CHANNEL_NUMBER;
const char* myWriteAPIKey = "YOUR_API_KEY"; // Replace with your API Key
WiFiClient client;
void setup() {
Serial.begin(9600);
dht.begin();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
ThingSpeak.begin(client); // Initialize ThingSpeak
}
void loop() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from sensor!");
return;
}
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print("°C, Humidity: ");
Serial.print(humidity);
Serial.println("%");
// Write data to ThingSpeak
ThingSpeak.setField(1, temperature); // Field 1 is for temperature
ThingSpeak.setField(2, humidity); // Field 2 is for humidity
// Update ThingSpeak channel
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
delay(2000); // Wait for 2 seconds
}
This code sends temperature and humidity data to ThingSpeak. It uses the ThingSpeak.writeFields() method to update the cloud platform with the latest sensor readings.
Step 4: Visualizing the Data
- Go to your ThingSpeak channel to view the real-time graph of temperature and humidity data.
- As your ESP8266 device sends data, you’ll see live updates on the ThingSpeak dashboard.