// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
// REQUIRES the following Arduino libraries:
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
// - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor
#include "DHT.h";
#include <SimpleWiFiClient.h>;
#include <WiFiUdp.h>;
SimpleWiFiClient simpleWiFiClient;
#define DHTPIN 2 // Digital pin connected to the DHT sensor
// Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
// Pin 15 can work but DHT must be disconnected during program upload.
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// ***********************************************Thingspeak Credentials*****************
String apiKey = "8G31Z8MWH77CO7HK"; //Write API key of your ThingSpeak channel
const char* server = "api.thingspeak.com"; // API for thingspeak
//****************************************************************************************
const char *ssid = "subscribe to"; // Wifi SSID of your Internet connection
const char *pass = "electronicsinnovation"; // Password
DHT dht(DHTPIN, DHT22);
void setup()
{
Serial.begin(9600); // Serial monitor Baudrate
delay(10);
//******************PowerSupply to the Sensor**********************
pinMode(D1, OUTPUT);
pinMode(D3, OUTPUT);
digitalWrite(D1,HIGH);
digitalWrite(D3,LOW);
delay(1000);
//*********************************************************
dht.begin();
Serial.println("Trying to Connect with ");
Serial.println(ssid);
simpleWiFiClient.init(ssid,pass);
simpleWiFiClient.attach(OnRecieved);
while (WiFi.status() != WL_CONNECTED)
{
// If the connection was unsuccesfull, it will try again and again
delay(500);
Serial.print(".");
}
// Connection succesfull
Serial.println("");
Serial.println("WiFi connected");
}
void loop()
{
float h = dht.readHumidity(); // Reading Temperature form DHT sensor
float t = dht.readTemperature(); // Reading Humidity form DHT sensor
if (isnan(h) || isnan(t))
{
Serial.println("Failed to read from DHT sensor!");
return;
}
// Connecting to the Thingspeak API and Posting DATA
if (client.connect(server,80)) // "184.106.153.149" or api.thingspeak.com
{
// Format of DATA Packet "Write API Key&field1=Temperature data&field2=Humidity Data"
String postStr = apiKey;
postStr +="&field1=";
postStr += String(t);
postStr +="&field2=";
postStr += String(h);
postStr += "\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
client.flush();
Serial.println(postStr);
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" degrees Celcius, Humidity: ");
Serial.print(h);
Serial.println("%.");
Serial.println("Data has been sussecfully sent to Thingspeak.");
}
client.stop();
Serial.println("Waiting to initiate next data packet...");
// thingspeak needs minimum 15 sec delay between updates
delay(60000);
}