Measure temperature π‘οΈ and humidity π§ using an ESP8266 microcontroller and an HTU21D sensor, then send the data to ThingSpeak every 15 minutes while conserving power using deep sleep mode π΄.
- Introduction
- Components Required
- Circuit Diagram
- Setup Instructions
- Code Explanation
- Usage
- Troubleshooting
- License
This project demonstrates how to build a low-power weather station using the ESP8266 microcontroller and the HTU21D temperature and humidity sensor. The ESP8266 reads data from the sensor and uploads it to ThingSpeak every 15 minutes. Between readings, the ESP8266 enters deep sleep mode to conserve power.
- π§ ESP8266 Development Board (e.g., NodeMCU or WeMos D1 Mini)
- π‘οΈ HTU21D Temperature and Humidity Sensor
- π Jumper Wires
- πΆ Wi-Fi Network with Internet Access
- π Power Supply (e.g., USB cable or battery pack)
π Connections:
HTU21D Pin | ESP8266 Pin |
---|---|
VCC | 3.3V |
GND | GND |
SDA | GPIO4 (D2) |
SCL | GPIO5 (D1) |
π Deep Sleep Connection:
- GPIO16 (D0) connected to RST
Connect the HTU21D sensor to the ESP8266 as per the connections above.
- Use jumper wires to make the connections.
- Ensure that GPIO16 (D0) is connected to RST to enable wake-up from deep sleep.
- Install the Arduino IDE if you haven't already.
- Add the ESP8266 board to the Arduino IDE:
- Go to File > Preferences.
- In Additional Boards Manager URLs, add:
http://arduino.esp8266.com/stable/package_esp8266com_index.json
- Go to Tools > Board > Boards Manager, search for ESP8266, and install it.
- Sign up for a free account on ThingSpeak.
- Create a new Channel.
- Add two fields:
- Field 1: Temperature π‘οΈ
- Field 2: Humidity π§
- Note down your Channel ID and Write API Key.
- You'll need these for the code.
Make sure you have the following libraries installed:
- ESP8266WiFi.h (comes with ESP8266 core)
- ThingSpeak.h
- Wire.h (comes with Arduino IDE)
- SparkFunHTU21D.h
- ThingSpeak Library:
- Go to Sketch > Include Library > Manage Libraries.
- Search for ThingSpeak and install it.
- SparkFun HTU21D Library:
- Search for SparkFun HTU21D and install it.
Replace the placeholders in the code with your Wi-Fi credentials, ThingSpeak Channel ID, and Write API Key.
#include <ESP8266WiFi.h>
#include <ThingSpeak.h>
#include <Wire.h>
#include <SparkFunHTU21D.h>
HTU21D myHTU21D;
const char* ssid = "YOUR_SSID"; // π Your Wi-Fi SSID
const char* password = "YOUR_PASSWORD"; // π Your Wi-Fi Password
unsigned long myChannelNumber = YOUR_CHANNEL_NUMBER; // π Your ThingSpeak Channel ID
const char* myWriteAPIKey = "YOUR_WRITE_API_KEY"; // π Your ThingSpeak Write API Key
WiFiClient client;
void setup() {
Serial.begin(115200);
delay(100);
// π οΈ Initialize the sensor
Wire.begin();
myHTU21D.begin();
// πΆ Connect to Wi-Fi
WiFi.begin(ssid, password);
Serial.println("Connecting to Wi-Fi...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to Wi-Fi");
// π Initialize ThingSpeak
ThingSpeak.begin(client);
// π‘οΈπ§ Read data from the sensor
float humidity = myHTU21D.readHumidity();
float temperature = myHTU21D.readTemperature();
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" *C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
// π€ Send data to ThingSpeak
ThingSpeak.setField(1, temperature);
ThingSpeak.setField(2, humidity);
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if (x == 200) {
Serial.println("Data successfully sent to ThingSpeak");
} else {
Serial.println("Error sending data: " + String(x));
}
// π΄ Disconnect Wi-Fi to save power
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
// π΄ Enter deep sleep for 15 minutes
Serial.println("Entering deep sleep for 15 minutes");
ESP.deepSleep(15 * 60 * 1000000); // Time in microseconds
}
void loop() {
// The ESP8266 will restart after deep sleep
}
- Connect your ESP8266 to your computer via USB.
- Select the correct Board and Port from the Tools menu.
- Click the Upload button.
-
Libraries Included:
ESP8266WiFi.h
: Handles Wi-Fi connectivity.ThingSpeak.h
: Interfaces with ThingSpeak API.Wire.h
: Enables I2C communication.SparkFunHTU21D.h
: Communicates with the HTU21D sensor.
-
Global Variables:
- Wi-Fi credentials (
ssid
,password
). - ThingSpeak configuration (
myChannelNumber
,myWriteAPIKey
). HTU21D
object for sensor interaction.
- Wi-Fi credentials (
-
setup()
Function:- Initializes serial communication for debugging.
- Initializes the HTU21D sensor.
- Connects to Wi-Fi.
- Reads temperature and humidity data.
- Sends data to ThingSpeak.
- Disconnects Wi-Fi to save power.
- Puts the ESP8266 into deep sleep for 15 minutes.
-
loop()
Function:- Left empty because the ESP8266 will reset after waking up from deep sleep, and the code in
setup()
will run again.
- Left empty because the ESP8266 will reset after waking up from deep sleep, and the code in
-
Once uploaded, the ESP8266 will:
- Wake up from deep sleep.
- Connect to Wi-Fi.
- Read sensor data.
- Send data to ThingSpeak.
- Enter deep sleep for 15 minutes.
- Repeat the cycle.
-
π View your data on ThingSpeak:
- Log in to your ThingSpeak account.
- Navigate to your channel to see real-time updates.
-
Cannot Connect to Wi-Fi:
- Double-check your SSID and password.
- Ensure your Wi-Fi network is 2.4 GHz (ESP8266 does not support 5 GHz).
-
No Data on ThingSpeak:
- Verify your Channel ID and Write API Key are correct.
- Ensure your ThingSpeak channel is set up properly with the correct fields.
-
ESP8266 Not Waking Up:
- Make sure GPIO16 (D0) is connected to RST.
- Check your power supply.
-
Error Codes from ThingSpeak:
- -301: Failed to connect to ThingSpeak.
- -302: Failed to update data.
- -401: Unauthorized (check your API Key).
This project is open-source and available under the MIT License.
Enjoy your new low-power weather station! π€οΈ If you have any questions or need further assistance, feel free to ask. Happy coding! π€