/*
* This sketch sends data via HTTP GET requests to api.thingspeak.com service.
*
* You need to get API write Key at api.thingspeak.com and paste them
* below. Or just customize this script to talk to other HTTP servers.
*
*/
#include <WiFi.h>
#include <DHT.h>
#define DHTPIN 15
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
const char* host = "api.thingspeak.com";
const String channelsAPIKey = "KW96VC6FRD90CZS2";
const unsigned int updateChannelsInterval = 15 * 1000;
// capteurs
float temperature;
float humidity;
float Illumination;
WiFiClient client;
long lastConnectionTimeChannels = 0;
boolean lastConnectedChannels = false;
int failedCounterChannels = 0;
void setup() {
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
startWiFi();
}
void loop()
{
humidity =dht.readHumidity();
temperature =dht.readTemperature();
Serial.println(humidity);
Serial.println(temperature);
Serial.println(Illumination);
Serial.print("connecting to ");
Serial.println(host);
//client.stop();
delay(1000);
updateChannels();
}
void updateChannels()
{
String tsData;
tsData="field1="+String(temperature, DEC)+"&field2="+String(humidity, DEC);
Serial.println(tsData);
if(!client.connected() && (millis() - lastConnectionTimeChannels > updateChannelsInterval))
{
if (client.connect("api.thingspeak.com", 80))
{
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+channelsAPIKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(tsData.length());
client.print("\n\n");
client.print(tsData);
lastConnectionTimeChannels = millis();
if (client.connected())
{
Serial.println("****************************************");
Serial.println("Update channels");
Serial.println();
Serial.println("Connecting to ThingSpeak...");
Serial.println();
Serial.println("Server response ->");
Serial.println();
failedCounterChannels = 0;
while(client.connected() && !client.available()) delay(1); //waits for data
while (client.connected() || client.available())
{
char charIn = client.read();
Serial.print(charIn);
//client.stop();
}
Serial.println();
Serial.println();
Serial.println("...disconnected");
Serial.println();
}
else
{
failedCounterChannels++;
Serial.println("Connection to ThingSpeak failed ("+String(failedCounterChannels, DEC)+")");
Serial.println();
lastConnectionTimeChannels = millis();
}
}
else
{
failedCounterChannels++;
Serial.println("Connection to ThingSpeak Failed ("+String(failedCounterChannels, DEC)+")");
Serial.println();
lastConnectionTimeChannels = millis();
}
}
if (failedCounterChannels > 3 ) {startWiFi();}
client.stop();
Serial.flush();
}
void startWiFi()
{
client.stop();
Serial.println();
Serial.println("Connecting Arduino to network...");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
delay(1000);
}