Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

Real-Time Data Monitoring with ThingSpeak and Arduino

ThingSpeak is an IoT analytics platform service that allows you to aggregate, visualize, and analyze live data streams in the cloud. By integrating ThingSpeak with Arduino, you can create powerful projects that monitor and control various parameters in real-time. This integration is particularly useful for applications such as environmental monitoring, smart agriculture, and home automation. In this article, we'll walk through the process of setting up ThingSpeak with Arduino, focusing on how to send sensor data to ThingSpeak for real-time monitoring.

Project: In this project, we will create a system that monitors temperature and humidity using a DHT11 sensor connected to an Arduino. The data will be sent to ThingSpeak, where it can be visualized in real-time. The objectives of this project are:

  1. To read temperature and humidity data from the DHT11 sensor.
  2. To send the collected data to ThingSpeak at regular intervals.
  3. To visualize the data on ThingSpeak's web interface.

Components List:

  1. Arduino Uno (1)
  2. DHT11 Temperature and Humidity Sensor (1)
  3. 10kΩ Resistor (1)
  4. Breadboard (1)
  5. Jumper Wires (several)
  6. USB Cable for Arduino (1)
  7. Internet Connection (via Wi-Fi or Ethernet shield/module)

Examples:

  1. Setting up the DHT11 sensor:
#include "DHT.h"  // Include the DHT library

#define DHTPIN 2     // Define the pin where the data pin of the DHT11 is connected
#define DHTTYPE DHT11   // Define the type of DHT sensor

DHT dht(DHTPIN, DHTTYPE);  // Initialize the DHT sensor

void setup() {
  Serial.begin(9600);  // Start the serial communication
  dht.begin();  // Start the DHT sensor
}

void loop() {
  delay(2000);  // Wait a few seconds between measurements

  float humidity = dht.readHumidity();  // Read humidity
  float temperature = dht.readTemperature();  // Read temperature

  // Check if any reads failed and exit early (to try again).
  if (isnan(humidity) || isnan(temperature)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.println(" *C");
}
  1. Sending Data to ThingSpeak:
#include "DHT.h"
#include <WiFi.h>
#include <HTTPClient.h>

#define DHTPIN 2
#define DHTTYPE DHT11

const char* ssid = "your_SSID";  // Replace with your WiFi SSID
const char* password = "your_PASSWORD";  // Replace with your WiFi Password
const char* server = "http://api.thingspeak.com/update";
const char* apiKey = "your_API_KEY";  // Replace with your ThingSpeak API Key

DHT dht(DHTPIN, DHTTYPE);

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() {
  delay(2000);

  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();

  if (isnan(humidity) || isnan(temperature)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;
    String url = server;
    url += "?api_key=";
    url += apiKey;
    url += "&field1=";
    url += String(temperature);
    url += "&field2=";
    url += String(humidity);

    http.begin(url);
    int httpResponseCode = http.GET();

    if (httpResponseCode > 0) {
      String response = http.getString();
      Serial.println(httpResponseCode);
      Serial.println(response);
    } else {
      Serial.print("Error on sending POST: ");
      Serial.println(httpResponseCode);
    }

    http.end();
  } else {
    Serial.println("Error in WiFi connection");
  }
}

This code will read temperature and humidity data from the DHT11 sensor and send it to ThingSpeak every 2 seconds. Ensure you replace "your_SSID", "your_PASSWORD", and "your_API_KEY" with your actual WiFi credentials and ThingSpeak API key.

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.