0% found this document useful (0 votes)
27 views3 pages

Source Code:: //ex No 8

The document contains code to connect an ESP8266 to WiFi and send temperature and humidity sensor readings from a DHT11 sensor to a ThingSpeak channel via HTTP requests. The code initializes WiFi and the DHT11 sensor, takes temperature and humidity readings every 2 seconds, and sends an HTTP POST request with the readings to the ThingSpeak URL.

Uploaded by

Vinoth T
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views3 pages

Source Code:: //ex No 8

The document contains code to connect an ESP8266 to WiFi and send temperature and humidity sensor readings from a DHT11 sensor to a ThingSpeak channel via HTTP requests. The code initializes WiFi and the DHT11 sensor, takes temperature and humidity readings every 2 seconds, and sends an HTTP POST request with the readings to the ThingSpeak URL.

Uploaded by

Vinoth T
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Source Code:

//Ex no 8

#include <ESP8266WiFi.h>

#include <ESP8266HTTPClient.h>

#include <WiFiClient.h>

#include "DHT.h"

#define DHTPIN 2

#define DHTTYPE DHT11

const char* ssid = "POCO X3";

const char* password = "90809080";

String KEY = "YDV10CX50MBYXRPK";

String host = "api.thingspeak.com";

int httpPort = 80;

String url = "/apps/thingtweet/1/statuses/update";

WiFiClient wifiClient;

DHT dht(DHTPIN, DHTTYPE);

HTTPClient http;

void setup()

Serial.begin(9600);

dht.begin();

WiFi.begin(ssid, password);

while(WiFi.status() != WL_CONNECTED)

delay(200);
Serial.print(".");

Serial.println();

Serial.println("NodeMCU is connected!");

Serial.println(WiFi.localIP());

void loop() {

delay(2000);

float h = dht.readHumidity();

float t = dht.readTemperature();

float f = dht.readTemperature(true);

if (isnan(h) || isnan(t) || isnan(f)) {

Serial.println(F("Failed to read from DHT sensor!"));

return; }

Serial.print(F(" Humidity: "));

Serial.print(h);

Serial.print(F("% Temperature: "));

Serial.println(t);

http.begin(wifiClient,host,httpPort,url);

String RequestBody = "api_key="+KEY+"&status=Temperature = "+(String)t+"


Humidity"+(String)h;

int httpCode = http.POST(RequestBody);

Serial.println(httpCode);

delay(5000);

}
Output:

You might also like