Section outline

  • Sending Sensor Data to the Cloud (e.g., ThingSpeak)

    One of the most exciting capabilities of IoT in robotics is sending live sensor data to the cloud, where it can be viewed, stored, and analyzed remotely. In this section, we will explore how to transmit data from ESP32 or NodeMCU to a cloud service like ThingSpeak.

    • ๐ŸŒ What is ThingSpeak?

      ThingSpeak is a cloud-based platform provided by MathWorks that allows devices to send data using HTTP protocols. It offers easy graphing, public/private channels, and MATLAB-based data analysis tools.

      • Free for personal use with limited updates per day
      • Supports HTTP and MQTT protocols
      • Real-time chart plotting and analytics
      • Used widely for temperature monitoring, environment sensing, etc.

      ๐Ÿ› ๏ธ What You Need

      • ESP32 or NodeMCU microcontroller
      • Wi-Fi connection
      • Sensor (e.g., DHT11 for temperature/humidity)
      • ThingSpeak account (https://thingspeak.com)

      ๐Ÿ“ถ Setting Up a ThingSpeak Channel

      1. Create a free ThingSpeak account and log in.
      2. Click on "Channels" > "New Channel".
      3. Give a name (e.g., WeatherData), and enable the fields you want (e.g., Field1: Temperature, Field2: Humidity).
      4. Save the channel and note down your Write API Key from the API Keys tab.

      ๐Ÿ”Œ Circuit Setup

      Example: DHT11 sensor with ESP32

      • VCC โ†’ 3.3V
      • GND โ†’ GND
      • Data โ†’ GPIO 4
    • ๐Ÿง‘โ€๐Ÿ’ป Sample Code to Send Data to ThingSpeak

      
      // Required Libraries
      #include <WiFi.h>
      #include <HTTPClient.h>
      #include "DHT.h"
      
      // Wi-Fi Credentials
      const char* ssid = "YourWiFiSSID";
      const char* password = "YourWiFiPassword";
      
      // ThingSpeak Settings
      const char* server = "http://api.thingspeak.com/update";
      String apiKey = "YOUR_WRITE_API_KEY";
      
      // DHT Sensor Settings
      #define DHTPIN 4
      #define DHTTYPE DHT11
      DHT dht(DHTPIN, DHTTYPE);
      
      void setup() {
        Serial.begin(115200);
        WiFi.begin(ssid, password);
        dht.begin();
      
        while (WiFi.status() != WL_CONNECTED) {
          delay(1000);
          Serial.println("Connecting to WiFi...");
        }
        Serial.println("Connected to WiFi");
      }
      
      void loop() {
        float temp = dht.readTemperature();
        float hum = dht.readHumidity();
      
        if (isnan(temp) || isnan(hum)) {
          Serial.println("Failed to read from DHT sensor!");
          return;
        }
      
        if (WiFi.status() == WL_CONNECTED) {
          HTTPClient http;
          String url = server + String("?api_key=") + apiKey +
                       "&field1=" + String(temp) +
                       "&field2=" + String(hum);
      
          http.begin(url);
          int httpCode = http.GET();
          if (httpCode > 0) {
            Serial.println("Data sent successfully");
          } else {
            Serial.println("Error sending data");
          }
          http.end();
        }
      
        delay(15000); // Wait 15 seconds before next update
      }
      

      ๐Ÿ“Š Viewing Your Data

      After running the sketch, open your ThingSpeak channel. You will see real-time graphs under each field that you've updated from your ESP board. These are auto-generated and updated every 15 seconds (or based on your delay).

    • ๐Ÿ’ก Use Cases

      • Environment monitoring robots that upload data every few minutes
      • Health robots that log temperature/humidity in a remote room
      • Weather bots that stream live updates to a dashboard

      โš ๏ธ Tips & Troubleshooting

      ย 

      • Ensure correct Wi-Fi SSID and password
      • Double-check your ThingSpeak Write API Key
      • Use serial monitor for debugging network or sensor errors
      • Observe ThingSpeak update limits: 1 update every 15 seconds for free accounts