0% found this document useful (0 votes)
6 views2 pages

ESP8266 DHT11 PIR Sensor Setup Guide

The document provides a code example for programming an ESP8266 microcontroller with a DHT11 temperature and humidity sensor and a PIR motion sensor. It includes setup instructions for Wi-Fi and Adafruit IO credentials, as well as definitions for MQTT feeds. Users are advised to change the credentials and feed keys for each new deployment of the project.

Uploaded by

hukabar347
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)
6 views2 pages

ESP8266 DHT11 PIR Sensor Setup Guide

The document provides a code example for programming an ESP8266 microcontroller with a DHT11 temperature and humidity sensor and a PIR motion sensor. It includes setup instructions for Wi-Fi and Adafruit IO credentials, as well as definitions for MQTT feeds. Users are advised to change the credentials and feed keys for each new deployment of the project.

Uploaded by

hukabar347
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

Programming (ESP8266 with DHT11 and PIR):

See below for the main annotated code. Key setup options (Wi-Fi SSID, password, Adafruit IO
credentials) must be changed for each new deployment.
// Include libraries for DHT, MQTT, Wi-Fi
#include <ESP8266WiFi.h>
#include <DHT.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#define PIR_PIN D1 // PIR Sensor
#define DHT_PIN D2 // DHT11 Sensor
#define LED_GREEN_PIN D5 // Green LED
#define LED_RED_PIN D6 // Red LED
#define DHT_TYPE DHT11 // Sensor type
DHT dht(DHT_PIN, DHT_TYPE);
// Wi-Fi and Adafruit IO setup — change credentials below before use
#define WLAN_SSID "YourWiFiName"
#define WLAN_PASS "YourWiFiPassword"
#define AIO_SERVER "[Link]"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "YourAIOUsername”
#define AIO_KEY "YourAIOKey"
// MQTT Feed definitions — update keys for each project
#define FEED_TEMP_KEY "[Link]"
#define FEED_HUMIDITY_KEY "[Link]"
#define FEED_MOTION_KEY "[Link]"
WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME,
AIO_KEY);
Adafruit_MQTT_Publish temperatureFeed = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME
"/feeds/"
FEED_TEMP_KEY);
Adafruit_MQTT_Publish humidityFeed = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME
"/feeds/"
FEED_HUMIDITY_KEY);
Adafruit_MQTT_Publish motionFeed = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME
"/feeds/"
FEED_MOTION_KEY);
void setup() {
[Link](115200);
pinMode(PIR_PIN, INPUT);
pinMode(LED_GREEN_PIN, OUTPUT);
pinMode(LED_RED_PIN, OUTPUT);
[Link]();
// WiFi setup (change to your details)
[Link](WLAN_SSID, WLAN_PASS);
while ([Link]() != WL_CONNECTED) {
delay(500);
}
}
void loop() {
// Read PIR Sensor
int motionState = digitalRead(PIR_PIN);
digitalWrite(LED_GREEN_PIN, motionState);
digitalWrite(LED_RED_PIN, !motionState);
// Publish motion status (1 for detected, 0 for idle)
[Link](motionState);
// Read DHT11 Data every 2s, publish every 2mins (can adjust timing)
float humidity = [Link]();
float temperature = [Link]();
if (!isnan(humidity) && !isnan(temperature)) {
[Link](temperature);
[Link](humidity);
}
delay(2000); // Adjust delay as per need
}
// --- Change credentials, feed keys, and delay times for each new project setup.

You might also like