/*
Adapted from WriteSingleField Example from ThingSpeak Library (Mathworks)
Rui Santos
Complete project details at https://RandomNerdTutorials.com/esp32-thingspeak-publish-arduino/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
#include <WiFi.h>
//#include "ThingSpeak.h"
#include "DHT.h"
#include <Adafruit_Sensor.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define DHTPIN 4
#define DHTTYPE DHT11 // DHT11
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
DHT dht(DHTPIN, DHTTYPE);
const char* ssid = "Wokwi-GUEST"; // your network SSID (name)
const char* password = ""; // your network password
WiFiClient client;
unsigned long myChannelNumber = 2393815;
const char* myWriteAPIKey = "VTGG8UR61R56VJYV";
// Timer variables
unsigned long lastTime = 0;
unsigned long timerDelay = 30000; //30000 = 30 seconden
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
/*
// Variable to hold temperature readings
float temperatureC;
//uncomment if you want to get temperature in Fahrenheit
//float temperatureF;
*/
/*
// Create a sensor object
Adafruit_BME280 bme; //BME280 connect to ESP32 I2C (GPIO 21 = SDA, GPIO 22 = SCL)
*/
/*
void initBME(){
if (!bme.begin(0x76)) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
}
*/
void setup() {
Serial.begin(115200); //Initialize serial
//initBME();
Serial.println(F("DHT11 test!"));
dht.begin();
WiFi.mode(WIFI_STA);
//ThingSpeak.begin(client); // Initialize ThingSpeak
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
}
void loop() {
if ((millis() - lastTime) > timerDelay) {
// Connect or reconnect to WiFi
if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect");
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, password);
delay(5000);
}
Serial.println("\nConnected!");
}
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float humidity = dht.readHumidity();
// Read temperature as Celsius (the default)
float temperatureC = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
// float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temperatureC)/* || isnan(f)*/) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// http://www.wpc.ncep.noaa.gov/html/heatindex_equation.shtml
// Compute heat index in Fahrenheit (the default)
// float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float heatIndexC = dht.computeHeatIndex(temperatureC, humidity, false);
Serial.print(F("Luchtvochtigheid: ")); /*Humidity:*/
Serial.print(humidity);
Serial.print(F("%, Temperatuur: "));
Serial.print(temperatureC);
Serial.print(F("°C, Gevoelstemperatuur: "));
Serial.print(heatIndexC);
Serial.println(F("°C"));
// Serial.print(f);
// Serial.print(F("°F Heat index: "));
// Serial.print(hif);
// Serial.println(F("°F"));
// set the fields with the values
/* ThingSpeak.setField(1, humidity);
ThingSpeak.setField(2, temperatureC);
ThingSpeak.setField(3, heatIndexC);
// ThingSpeak.setField(4, f);
// ThingSpeak.setField(5, hif);
/*
// Get a new temperature reading
temperatureC = bme.readTemperature();
Serial.print("Temperature (ºC): ");
Serial.println(temperatureC);
//uncomment if you want to get temperature in Fahrenheit
/*temperatureF = 1.8 * bme.readTemperature() + 32;
Serial.print("Temperature (ºC): ");
Serial.println(temperatureF);
*/
/*
// Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
// pieces of information in a channel. Here, we write to field 1.
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if(x == 200){
Serial.println("Channel update successful."); //200 is de waarde die thingspeak terug geeft!
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
*/
display.clearDisplay();
display.setCursor(0,8);
display.println("INFORMATICA");
display.setCursor(0,25);
display.print("Humidity: ");
display.print(humidity);
display.println(" %");
display.setCursor(0,35);
display.print("Temp.: ");
display.print(temperatureC);
display.print(" "); //spatie na getal
display.print((char)247);
display.println("C"); //display.print((char)247); // degree symbol
display.setCursor(0,45);
display.print(millis()/1000 ); //prints to OLED
display.display(); //SHOW on OLED
lastTime = millis();
}
}