#include <WiFi.h>
#include "DHTesp.h"
#include "ThingSpeak.h"
#include <Adafruit_Sensor.h>
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
#define SERIAL_DEBUG_BAUD 115200
#define DHT 15
// the Wifi radio's status
int status = WL_IDLE_STATUS;
String request_string;
unsigned long myChannelNumber = 1;
String myAPIkey = "T9KAS7RSS4SGODFO";
String thingSpeakAddress = "api.thingspeak.com";
WiFiClient client;
void InitWiFi()
{
Serial.println("Connecting to AP ...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to AP");
}
void reconnect() {
status = WiFi.status();
if ( status != WL_CONNECTED) {
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to AP");
}
}
const int DHT_Pin = 15;
void setup() {
// initialize serial for debugging
Serial.begin(SERIAL_DEBUG_BAUD);
Serial.println();
InitWiFi();
pinMode(DHT_Pin, OUTPUT);
}
void loop() {
delay(10000);
float t=random(27,42);
float h=random(10,100);
if (WiFi.status() != WL_CONNECTED) {
reconnect();
}
else {
if (client.connect("api.thingspeak.com", 80)) {
request_string = "/update?";
request_string += "api_key=";
request_string += myAPIkey;
request_string += "&";
request_string += "field1";
request_string += "=";
request_string += t;
request_string += "&";
request_string += "field2";
request_string += "=";
request_string += h;
Serial.println(String("GET ") + request_string + " HTTP/1.1\r\n" +
"Host: " + thingSpeakAddress + "\r\n" +
"Connection: close\r\n\r\n");
client.print(String("GET ") + request_string + " HTTP/1.1\r\n" +
"Host: " + thingSpeakAddress + "\r\n" +
"Connection: close\r\n\r\n");
}
}
}