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

THINGSPEAK CODE

This document contains an Arduino sketch for an ESP8266 microcontroller that reads temperature and humidity data from a DHT11 sensor. It connects to a specified WiFi network and sends the sensor data to a ThingSpeak server using HTTP POST requests. The code includes error handling for sensor readings and HTTP communication.

Uploaded by

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

THINGSPEAK CODE

This document contains an Arduino sketch for an ESP8266 microcontroller that reads temperature and humidity data from a DHT11 sensor. It connects to a specified WiFi network and sends the sensor data to a ThingSpeak server using HTTP POST requests. The code includes error handling for sensor readings and HTTP communication.

Uploaded by

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

#include <ESP8266WiFi.

h>

#include <ESP8266HTTPClient.h>

#include <DHT.h>

#define DHTPIN D4

#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

const char* ssid = "PRJ CABINWIFI";

const char* password = "123456789";

const char* apiKey = "1NHTL148ZWRJ6UCR";

const char* serverAddress = "http://api.thingspeak.com/update";

void setup() {

dht.begin();

Serial.begin(115200);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {

delay(1000);

Serial.println("Connecting to WiFi...");

}
void loop() {

float humidity = dht.readHumidity();

float temperature = dht.readTemperature();

if (isnan(humidity) || isnan(temperature)) {

Serial.println("Failed to read DHT sensor data");

} else {

Serial.print("Humidity: ");

Serial.print(humidity);

Serial.print("%\tTemperature: ");

Serial.print(temperature);

Serial.println("°C");

WiFiClient client;

HTTPClient http;

http.begin(client, serverAddress);

http.addHeader("Content-Type", "application/x-www-form-urlencoded");

String postData = "api_key=" + String(apiKey) + "&field1=" + String(temperature) + "&field2=" +


String(humidity);

int httpCode = http.POST(postData);


if (httpCode > 0) {

Serial.print("HTTP Response Code: ");

Serial.println(httpCode);

} else {

Serial.println("HTTP POST failed.");

http.end();

delay(3000);

You might also like