#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>
// WiFi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Beeceptor endpoint
const char* serverUrl = "https://api.thingspeak.com/channels/2320565/feeds.json?api_key=JGAXZ2BZWGRW8VBN&results=1";
// DHT sensor configuration
#define DHTPIN 4 // Define the GPIO pin to which the DHT22 is connected
#define DHTTYPE DHT22 // Define the sensor type (DHT11 or DHT22)
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.print("Connecting to WiFi");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
// Initialize the DHT sensor
dht.begin();
}
void loop() {
// Read temperature and humidity
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (!isnan(temperature) && !isnan(humidity)) {
// Create an HTTP client
HTTPClient http;
// Send temperature and humidity data to Beeceptor as form parameters
String postData = "temperature=" + String(temperature) + "&humidity=" + String(humidity);
http.begin(serverUrl);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpResponseCode = http.POST(postData);
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
Serial.println("Data sent to Beeceptor.");
} else {
Serial.print("Error in HTTP request. HTTP Response code: ");
Serial.println(httpResponseCode);
}
http.end();
} else {
Serial.println("Failed to read from DHT sensor!");
}
delay(60000); // Send data every 1 minute (adjust as needed)
}