#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <DHTesp.h>
const String SSID = "Wokwi-GUEST";
const String PW = "";
const String BASE_URL = "http://api.thingspeak.com/update.json";
const String WRITE_KEY = "";
const String KEY_FIELD = "api_key";
const String TEMP_FIELD = "field1";
const String HUMIDITY_FIELD = "field2";
const String RESISTOR_FIELD = "field3";
const String OTHER_FIELD = "field4";
const int DHT_PIN = 15;
const int ANALOG_PIN = 34;
WiFiClient wifi;
HTTPClient http;
DHTesp dht;
void setup() {
Serial.begin(115200);
Serial.println();
Serial.print("Started. Connecting...");
WiFi.begin(SSID, PW);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println(" Connected. Finishing setup...");
dht.setup(DHT_PIN, DHTesp::DHT22);
http.begin(BASE_URL);
Serial.println("Setup done.");
}
void loop() {
float temperature = dht.getTemperature();
float humidity = dht.getHumidity();
int resistor = analogRead(ANALOG_PIN);
int other = random(100);
String request_data = "{\"" + KEY_FIELD + "\":\"" + WRITE_KEY + "\", \"" +
TEMP_FIELD + "\":\"" + String(temperature) + "\", \"" +
HUMIDITY_FIELD + "\":\"" + String(humidity) + "\", \"" +
RESISTOR_FIELD + "\":\"" + String(resistor) + "\", \"" +
OTHER_FIELD + "\":\"" + String(other) + "\"}";
Serial.println();
Serial.println("Posting... " + request_data + " to " + BASE_URL);
http.addHeader("Content-Type", "application/json");
int status = http.POST(request_data);
if (status > 0) {
Serial.println("Status: " + String(status) + ". Payload: " + http.getString());
} else {
Serial.println("Error: " + String(status));
}
delay(5000);
}