Last active
March 16, 2021 14:36
-
-
Save elktros/76a1ce8c3c99bc940ea2e453306b0c80 to your computer and use it in GitHub Desktop.
Code for Interfacing DHT11 Humidity Sensor with ESP8266 and posting the information on ThingSpeak API. Code is uploaded directly to ESP8266 Module.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <DHT.h> | |
#include <ESP8266WiFi.h> | |
#define DHTPIN 2 //DHT11 is connected to GPIO Pin 2 | |
String apiKey = "XXXXXXXXXXXXXXXX"; // Enter your Write API key from ThingSpeak | |
const char* ssid = "SSID"; // Enter your WiFi Network's SSID | |
const char* pass = "PASSWORD"; // Enter your WiFi Network's Password | |
const char* server = "api.thingspeak.com"; | |
float humi; | |
float temp; | |
DHT dht(DHTPIN, DHT11); | |
WiFiClient client; | |
void setup() | |
{ | |
Serial.begin(115200); | |
delay(10); | |
dht.begin(); | |
Serial.println("Connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, pass); | |
while (WiFi.status() != WL_CONNECTED) | |
{ | |
delay(100); | |
Serial.print("*"); | |
} | |
Serial.println(""); | |
Serial.println("***WiFi connected***"); | |
} | |
void loop() | |
{ | |
humi = dht.readHumidity(); | |
temp = dht.readTemperature(); | |
if (client.connect(server,80)) // "184.106.153.149" or api.thingspeak.com | |
{ | |
String sendData = apiKey+"&field1="+String(temp)+"&field2="+String(humi)+"\r\n\r\n"; | |
//Serial.println(sendData); | |
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(sendData.length()); | |
client.print("\n\n"); | |
client.print(sendData); | |
Serial.print("Temperature: "); | |
Serial.print(temp); | |
Serial.print("deg C. Humidity: "); | |
Serial.print(humi); | |
Serial.println("%. Connecting to Thingspeak."); | |
} | |
client.stop(); | |
Serial.println("Sending...."); | |
delay(10000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment