ioT based Open-Source Cloud (Thing speak) and ESP32 Based Microcontroller DHT11 Sensor Data Acquistion
"Air humidity and Temperature Data analysis and logging at Open-source Cloud i.e., Thingspeak. "
The ESPWROOM-32 Board, DHT11 Sensor, and Arduino IDE, Thingspeak ID are must for this.
type of analysis. The Sensor is tied at PIN Number 25 Digital IO, Arduino IDE Serial Port Monitor is used display Serial Data Display because i am not using any Display module ....
check the hardware connection in detail whenever there is problem because the code is working properly.
The Code is given below:
///////////////////////////////////////Programma Started /////////////////////////////////////////////////////////
#include <math.h>
//#include <OneWire.h>
//#include <DallasTemperature.h>
#include "DHT.h"
//#include <Wire.h>
//#include <Adafruit_BMP085.h>
//#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include "ThingSpeak.h"
#include <Arduino.h>
#include <HardwareSerial.h>
///////////////////////////////Wifi setup ////////////////////////
//char ssid[] = " "; // your network SSID (name)
//char pass[] = " "; // your network password
/////////////////////////////////////////////////////////////////////////////
char ssid[] = " "; // your network SSID (name)
char pass[] = " "; // your network password
//////////////////////////////////////////////////////////////////////////
int keyIndex = 0; // your network key Index number (needed only for WEP)
WiFiClient client;
///////////////////thingspeak Channel////////////////////////////////
const char* server = "api.thingspeak.com";
unsigned long myChannelNumber = ; // you can put own channel ID
const char * myWriteAPIKey =" "; // Write own API key
/////////////////////////////////////////////////////////////////////
/ Timer variables
unsigned long lastTime = 0;
//unsigned long timerDelay = 30000;
unsigned long timerDelay = 20000;
/////////////////////////////////////////////////////////////////////////
#define DHT_SENSOR_PIN 25 // ESP32 pin GIOP21 connected to DHT11 sensor
#define DHT_SENSOR_TYPE DHT11
DHT dht_sensor(DHT_SENSOR_PIN, DHT_SENSOR_TYPE);
////////////////////Values Assigment///////////////////
#define dt_comn 1000 // Delay
#define dt_digital 1000 // Delay
///////////////////////////////////////////////////////////////////////////////
void setup()
{
///////////////////Serial Setting ////////////////////////////////
Serial.begin(115200); //Initialize serial
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo native USB port only
}
/////////////////////////////DHT11 //////////////////////////////////////////////
dht_sensor.begin(); // initialize the DHT sensor
//////////////////////////////wifi/////////////////////////////////////////////
Serial.println("\n ...WIFI setting.......");
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); // Initialize ThingSpeak
Serial.println("\n ..Thingspeak is Initialized.......");
}
/////////////////////////////////////////// Main Loop /////////////////////////
void loop()
{
//////////////////////wifi connected//////////////////////
// Connect or reconnect to WiFi
if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect to SSID: ");
//Serial.println(SECRET_SSID);
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");}
///////////////////////////////////////////////////////////////////////
//-------------------------------DHT11---------------------------///
// read humidity
float humi = dht_sensor.readHumidity();
// read temperature in Celsius
float tempC = dht_sensor.readTemperature();
// read temperature in Fahrenheit
float tempF = dht_sensor.readTemperature(true);
// check whether the reading is successful or not
if ( isnan(tempC) || isnan(tempF) || isnan(humi)) {
Serial.println("Failed to read from DHT sensor!");
} else {
Serial.print("Humidity: ");
Serial.print(humi);
Serial.print("%");
Serial.print(" | ");
Serial.print("Temperature: ");
Serial.print(tempC);
Serial.print("°C ~ ");
Serial.print(tempF);
Serial.println("°F");
}
delay(5000);
////////////////////thingspeak Values write ////////////////////////////////////////////////
// set the fields with the values
ThingSpeak.setField(1,humi); // _Air Temperature
ThingSpeak.setField(2, tempC); //DHT11 Humidity:
ThingSpeak.setField(3,tempF); //Air temp in F
// ThingSpeak.setField(4,hi); // Heat Index
// 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.");
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
lastTime = millis();
///////////////////////////////////////////////////////////////////////////////
delay(10000);
}
//------------------------------Programm code END-------------------------------------------------------//
Comments
Post a Comment