Skip to content

πŸŒ‘οΈπŸ’§ A low-power weather station project using ESP8266 and HTU21D sensor to measure temperature and humidity and send data to ThingSpeak every 15 minutes.

License

Notifications You must be signed in to change notification settings

CyberScopeToday/ESP8266-HTU21D-Weather-Station

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“‘ ESP8266 & HTU21D Weather Station with ThingSpeak Integration

CodeFactor

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 😴.

πŸ“‹ Table of Contents

Introduction

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.

Components Required

  • 🧠 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)

Circuit Diagram

πŸ”— Connections:

HTU21D Pin ESP8266 Pin
VCC 3.3V
GND GND
SDA GPIO4 (D2)
SCL GPIO5 (D1)

πŸ”Œ Deep Sleep Connection:

  • GPIO16 (D0) connected to RST

Setup Instructions

1. Wiring

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.

2. Preparing the ESP8266

  • 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.

3. Setting Up ThingSpeak

  1. Sign up for a free account on ThingSpeak.
  2. Create a new Channel.
  3. Add two fields:
    • Field 1: Temperature 🌑️
    • Field 2: Humidity πŸ’§
  4. Note down your Channel ID and Write API Key.
    • You'll need these for the code.

4. Programming the ESP8266

πŸ“¦ Libraries Required

Make sure you have the following libraries installed:

  • ESP8266WiFi.h (comes with ESP8266 core)
  • ThingSpeak.h
  • Wire.h (comes with Arduino IDE)
  • SparkFunHTU21D.h

πŸ”§ Installing Libraries

  • 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.

πŸ“ Code

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
}

πŸš€ Uploading the Code

  1. Connect your ESP8266 to your computer via USB.
  2. Select the correct Board and Port from the Tools menu.
  3. Click the Upload button.

Code Explanation

  • 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.
  • 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.

Usage

  • Once uploaded, the ESP8266 will:

    1. Wake up from deep sleep.
    2. Connect to Wi-Fi.
    3. Read sensor data.
    4. Send data to ThingSpeak.
    5. Enter deep sleep for 15 minutes.
    6. Repeat the cycle.
  • πŸ“ˆ View your data on ThingSpeak:

    • Log in to your ThingSpeak account.
    • Navigate to your channel to see real-time updates.

Troubleshooting

  • 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).

License

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! πŸ€–

About

πŸŒ‘οΈπŸ’§ A low-power weather station project using ESP8266 and HTU21D sensor to measure temperature and humidity and send data to ThingSpeak every 15 minutes.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages