#include "DHT.h"
#define DHTPIN 27
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* serverName = "http://api.thingspeak.com/update";
String apiKey = "VOPRROG6GLVMQ8L0";
unsigned long lastTime = 0;
unsigned long timerDelay = 10000;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println(F("DHTxx test!"));
dht.begin();
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
Serial.println("Timer set to 10 seconds (timerDelay variable), it will take 10 seconds before publishing the first reading.");
randomSeed(analogRead(27));
}
void loop() {
float n = dht.readHumidity();
float t = dht.readTemperature();
if ((millis() - lastTime) > timerDelay) {
//Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
WiFiClient client;
HTTPClient http;
http.begin(client, serverName);
// Specify content-type header
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Data to send with HTTP POST
String httpRequestData = "api_key=" + apiKey + "&field2=" + "xlabel" + float(t);
String httpRequestData1 = "api_key=" + apiKey + "&field2=" + "ylabel" + float(n);
int httpResponseCode = http.POST(httpRequestData + httpRequestData1); // Send HTTP POST request
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
lastTime = millis();
}
delay(1000); // this speeds up the simulation
}
esp:0
esp:2
esp:4
esp:5
esp:12
esp:13
esp:14
esp:15
esp:16
esp:17
esp:18
esp:19
esp:21
esp:22
esp:23
esp:25
esp:26
esp:27
esp:32
esp:33
esp:34
esp:35
esp:3V3
esp:EN
esp:VP
esp:VN
esp:GND.1
esp:D2
esp:D3
esp:CMD
esp:5V
esp:GND.2
esp:TX
esp:RX
esp:GND.3
esp:D1
esp:D0
esp:CLK
dht1:VCC
dht1:SDA
dht1:NC
dht1:GND