marco
Published © LGPL

ESP8266-01 PIR and DHT on Thingspeak

I use a ESP8266-01 connected to AC (ACDC converter) to check presence in the corridor and measuring temperature and humidity.

BeginnerShowcase (no instructions)1 hour8,100
ESP8266-01 PIR and DHT on Thingspeak

Things used in this project

Hardware components

ESP8266 ESP-01
Espressif ESP8266 ESP-01
×1
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
DHT22 Temperature Sensor
DHT22 Temperature Sensor
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

ThingSpeak API
ThingSpeak API
Arduino IDE
Arduino IDE

Story

Read more

Schematics

ESP8266-01 pins

wiring diagram

Code

Code for ESP

C/C++
//esp01 with pir, t, h,ACpowered
//connect all to 5V, no need V shifter
//DHT to pin 2 - digital
//pir to pin 0 - digital


#include "DHT.h"

#define DHTPIN 2     
#define DHTTYPE DHT11   // DHT 11



#include <ESP8266WiFi.h>


//change this with the pin that you use
int t=20;

int pir;
int pinpir=0;
int pirMax;


// thingspeak
String apiKey = "xxxxxxxxxxxx";  // write key
const char* server = "api.thingspeak.com";

//  wifi 
const char* ssid = "xxxxxxxxxxxxx";
const char* password = "xxxxxxxxxxx";


DHT dht(DHTPIN, DHTTYPE,20);
WiFiClient client;

void setup() {
Serial.begin(115200);
delay(10);
dht.begin();

WiFi.begin(ssid, password);

}


void loop() {

pirMax=0;
for (int i=0; i < t; i++){
      
      pir= digitalRead(pinpir);
     
      if(pir>pirMax)pirMax=pir;
      delay(1000);
   } 

 
pir=pirMax;

float h = dht.readHumidity();
float t = dht.readTemperature();


if (client.connect(server,80)) { // "184.106.153.149" or api.thingspeak.com
String postStr = apiKey;
postStr +="&field1=";
postStr += String(t);
postStr +="&field2=";
postStr += String(h);
postStr +="&field3=";
postStr += String(pir);
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.stop();


// thingspeak needs minimum 15 sec delay between updates
delay(8000);
}

Credits

marco

marco

1 project • 2 followers

Comments