Section outline

  • IoT-Based Weather Bot

    In this hands-on project, you will build an Internet-connected weather bot using an ESP32 or NodeMCU. The bot will measure environmental data (like temperature and humidity) and send it to the cloud in real time for monitoring and visualization.

    • 🎯 Project Goals

      • Use a DHT11 or DHT22 sensor to measure temperature and humidity
      • Connect your ESP32/NodeMCU to Wi-Fi
      • Publish sensor data to the cloud using MQTT or ThingSpeak
      • Visualize the live data on a dashboard

      🔌 Components Needed

      • ESP32 or NodeMCU board
      • DHT11 or DHT22 sensor
      • Jumper wires and breadboard
      • USB cable
      • Wi-Fi access

      🧠 Circuit Diagram

      Connect the DHT sensor to your ESP32 as follows:

      DHT Sensor ESP32
      VCC 3.3V
      GND GND
      DATA D4 (GPIO4)

       

    • 💻 Sample Code (Sending Data to MQTT Broker)

      #include <WiFi.h>
      #include <PubSubClient.h>
      #include <DHT.h>
      
      #define DHTPIN 4
      #define DHTTYPE DHT11
      DHT dht(DHTPIN, DHTTYPE);
      
      const char* ssid = "your_SSID";
      const char* password = "your_PASSWORD";
      const char* mqtt_server = "broker.hivemq.com";
      
      WiFiClient espClient;
      PubSubClient client(espClient);
      
      void setup() {
        Serial.begin(115200);
        WiFi.begin(ssid, password);
        dht.begin();
      
        while (WiFi.status() != WL_CONNECTED) {
          delay(500);
          Serial.print(".");
        }
      
        client.setServer(mqtt_server, 1883);
      }
      
      void loop() {
        if (!client.connected()) {
          reconnect();
        }
        client.loop();
      
        float temp = dht.readTemperature();
        float hum = dht.readHumidity();
      
        if (!isnan(temp) && !isnan(hum)) {
          char tempStr[8];
          char humStr[8];
          dtostrf(temp, 1, 2, tempStr);
          dtostrf(hum, 1, 2, humStr);
      
          client.publish("/weatherbot/temp", tempStr);
          client.publish("/weatherbot/humidity", humStr);
        }
      
        delay(5000);
      }
      
      void reconnect() {
        while (!client.connected()) {
          if (client.connect("WeatherBotClient")) {
            client.subscribe("/weatherbot/commands");
          } else {
            delay(2000);
          }
        }
      }
      
    • 📊 Visualizing Data

      • Using ThingSpeak: Create a free account and channel, then use HTTP POST to send data from ESP32
      • Using MQTT Dashboard App: Subscribe to topics like /weatherbot/temp to view real-time data on your phone

      🛠️ Optional Enhancements

      • Add an OLED display to show live data on the robot
      • Use IFTTT to trigger alerts if temperature goes above a limit
      • Send data to Google Sheets via webhook

      ✅ Key Takeaways

      • You can build real-time, connected devices with very basic components
      • MQTT and cloud services like ThingSpeak make data sharing simple
      • This weather bot is a great foundation for more advanced IoT automation