Mechatronics LAB
Published © GPL3+

ESP32 Weather Data Logger with DHT Sensor (ThingSpeak + Goog

ESP32 Weather Data Logger with DHT Sensor (ThingSpeak + Google Sheets)

BeginnerProtip1 hour174
ESP32 Weather Data Logger with DHT Sensor (ThingSpeak + Goog

Things used in this project

Hardware components

dht2
×1

Story

Read more

Schematics

ESP32 Weather Data Logger with DHT Sensor (ThingSpeak + Google Sheets)

Code

ESP32 Weather Data Logger with DHT Sensor (ThingSpeak + Google Sheets)

Arduino
#include <WiFi.h>
#include <DHT.h>
#include <ThingSpeak.h>

// Wi-Fi credentials
const char* ssid = "your-SSID";
const char* password = "your-password";

// ThingSpeak credentials
#define THINGSPEAK_API_KEY "your-Write-API-Key"
#define THINGSPEAK_CHANNEL_ID "your-Channel-ID"

// DHT22 sensor setup
#define DHTPIN 4         // Pin D4 connected to DHT22
#define DHTTYPE DHT22    // Define the sensor type

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

WiFiClient client;

void setup() {
  // Start Serial Monitor
  Serial.begin(115200);

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");

  // Initialize ThingSpeak
  ThingSpeak.begin(client);

  // Initialize DHT sensor
  dht.begin();
}

void loop() {
  // Read temperature and humidity from DHT22 sensor
  float temperature = dht.readTemperature(); // Temperature in Celsius
  float humidity = dht.readHumidity();       // Humidity in percentage

  // Check if the readings are valid
  if (isnan(temperature) || isnan(humidity)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Print the values to Serial Monitor
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.print(" °C  ");
  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.println(" %");

  // Upload data to ThingSpeak
  ThingSpeak.setField(1, temperature);
  ThingSpeak.setField(2, humidity);
  
  // Write to ThingSpeak every 15 seconds
  int responseCode = ThingSpeak.writeFields(THINGSPEAK_CHANNEL_ID, THINGSPEAK_API_KEY);
  
  if (responseCode == 200) {
    Serial.println("Data sent to ThingSpeak successfully");
  } else {
    Serial.println("Error sending data to ThingSpeak");
  }

  // Wait for 15 seconds before sending new data
  delay(15000);
}

Apps Script

JavaScript
Apps Script
function logWeatherData() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var url = "https://api.thingspeak.com/channels/YOUR_CHANNEL_ID/feeds/last.json?api_key=YOUR_API_KEY";
  
  var response = UrlFetchApp.fetch(url);
  var json = JSON.parse(response.getContentText());
  
  var timestamp = json.created_at;
  var temperature = json.field1;
  var humidity = json.field2;
  
  sheet.appendRow([timestamp, temperature, humidity]);
}

Credits

Mechatronics LAB
75 projects • 47 followers
I am Sarful , I am a Mechatronics Engineer & also a teacher I am Interested in the evolution of technology in the automation industry .
Contact

Comments

Please log in or sign up to comment.