aktaktaw aktakpower
Published © GPL3+

Simple IOT Human Counter in Premises

Simple IOT Human Counter in Premises or Shopping Complex using Raspberry Pi

IntermediateShowcase (no instructions)10 hours2,117
Simple IOT Human Counter in Premises

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×2
PIR Sensor, 7 m
PIR Sensor, 7 m
×2
Jumper wires (generic)
Jumper wires (generic)
×9
Wemos D1 Mini
Espressif Wemos D1 Mini
×1
MQ 135
×1
Resistor 220 ohm
Resistor 220 ohm
×1

Software apps and online services

Arduino IDE
Arduino IDE
Node-RED
Node-RED
MQTT
MQTT

Story

Read more

Schematics

raspberry-pi-ir-sensor-schematic

wemos-d1-mini-mq-135-sensor

Code

wemos-d1-mini-mq-135-sensor

C/C++
#include <MQ135.h>
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>

//Wifi setup
const char* ssid="YOUR_WIFI_SSID";
const char* pswd="YOUR_WIFI_PASSWORD";

//MQTT Server
const char* mqtt_server="YOUR MQTT Broker Server";
#define mqtt_port 1883

//timer
long now=millis();
long lastMeasure=0;

//define analog pin
#define ANALOGPIN A0
#define RZERO 206.85
float rzero, ppm;
MQ135 gasSensor =MQ135(ANALOGPIN);

WiFiClient espClient;
PubSubClient client(espClient);

void setup_wifi(){
  delay(10);
  WiFi.begin(ssid,pswd);
  Serial.println();
  Serial.print("Connecting to: ");
  Serial.println(ssid);

  while(WiFi.status() != WL_CONNECTED){
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi Connected");
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());
}

void mqtt_connect(){
  Serial.println("Attempting MQTT Connection");
  if(client.connect("ESP8266Client")){
    Serial.println("MQTT Server Connected");
  }
  else{
    Serial.print("MQTT Connection failed, rc=");
    Serial.print(client.state());
    Serial.println("Reconnect in 5s");
    delay(5000);
  }
}

void setup() {
  Serial.begin(115200);
  Serial.setTimeout(500);
  setup_wifi();
  client.setServer(mqtt_server, mqtt_port);
  
  rzero = gasSensor.getRZero();
  delay(3000);
  Serial.print("MQ135 RZERO Calibration Value: ");
  Serial.println(rzero);
  
}

void loop() {
  if(!client.connected()){
    mqtt_connect();
  }

  if(!client.loop()){
    client.connect("ESpClient8266");
  }

  now = millis();
  if(now-lastMeasure > 5000){
    ppm = gasSensor.getPPM();

    if(isnan(ppm)){
      Serial.println("Failed to read MQ135 Sensor");
      client.publish("/mq135/ppm","Failed to read sensor");
    }


    static char ppmTemp[7];
    dtostrf(ppm,6,2,ppmTemp);
    client.publish("/mq135/ppm",ppmTemp);

    Serial.println("CO2 ppm value: ");
    Serial.println(ppm);

    delay(3000);
  }

}

raspberry-pi-ir-sensor-entrance-gate-node-red-flow

JSON
Copy the flow, and paste at Node-Red import > Clipboard
[{"id":"67c6c2ff.061c9c","type":"debug","z":"1e50305d.a3e2f","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":550,"y":120,"wires":[]},{"id":"b489e83.a4d5018","type":"rpi-gpio in","z":"1e50305d.a3e2f","name":"","pin":"40","intype":"down","debounce":"25","read":false,"x":120,"y":80,"wires":[["5b092bfb.0532a4"]]},{"id":"5b092bfb.0532a4","type":"switch","z":"1e50305d.a3e2f","name":"payload !=motion","property":"payload","propertyType":"msg","rules":[{"t":"neq","v":"motion","vt":"msg"}],"checkall":"true","repair":false,"outputs":1,"x":330,"y":80,"wires":[["2d83afb8.42a42"]]},{"id":"2d83afb8.42a42","type":"switch","z":"1e50305d.a3e2f","name":"msg.payload == 0","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":330,"y":120,"wires":[["5b80cf68.ef7c5"]]},{"id":"5b80cf68.ef7c5","type":"change","z":"1e50305d.a3e2f","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"entrance","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":160,"wires":[["67c6c2ff.061c9c","ea9014b0.4a60f8","916a8c7e.65c47","1559b6a5.733539"]]},{"id":"a6b60375.984ca8","type":"ui_template","z":"1e50305d.a3e2f","group":"ba957629.2b4f28","name":"Counter Widget","order":1,"width":"7","height":"6","format":"<div style=\"text-align:center;\">\n<h1>No. Customer in Permise</h1>\n<br/>\n\n<font size={{msg.size}} > {{msg.payload}} </font>\n</div>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":false,"templateScope":"local","x":1020,"y":180,"wires":[[]]},{"id":"916a8c7e.65c47","type":"function","z":"1e50305d.a3e2f","name":"set log","func":"var date = Date.now()\nvar createdAt = Date.now();\nvar gate = \"entrance\"\n\nmsg.payload =date+\"/\"+gate+\"/\"+createdAt;\nreturn msg;","outputs":1,"noerr":0,"x":570,"y":380,"wires":[["4df2e94e.11aa28"]]},{"id":"a7a4631b.31f44","type":"mqtt in","z":"1e50305d.a3e2f","name":"","topic":"pi/2/exit/1","qos":"1","datatype":"auto","broker":"53ab3d41.e551b4","x":120,"y":440,"wires":[["6c2c821e.f307cc","6dd8eb5b.e78c74"]]},{"id":"6c2c821e.f307cc","type":"debug","z":"1e50305d.a3e2f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":310,"y":520,"wires":[]},{"id":"4df2e94e.11aa28","type":"file","z":"1e50305d.a3e2f","name":"","filename":"/home/pi/log/entrance-log.csv","appendNewline":true,"createDir":true,"overwriteFile":"false","encoding":"none","x":790,"y":380,"wires":[["656b9bd3.511b54"]]},{"id":"16906cdb.47ee93","type":"function","z":"1e50305d.a3e2f","name":"set count to payload","func":"msg.size = 80\nmsg.payload = msg.payload+\"/50 pax\"\nreturn msg","outputs":1,"noerr":0,"x":760,"y":180,"wires":[["e6bdc88a.c098f8","a6b60375.984ca8"]]},{"id":"e6bdc88a.c098f8","type":"debug","z":"1e50305d.a3e2f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":950,"y":140,"wires":[]},{"id":"656b9bd3.511b54","type":"debug","z":"1e50305d.a3e2f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1010,"y":380,"wires":[]},{"id":"ad500107.84ccd","type":"mqtt in","z":"1e50305d.a3e2f","name":"","topic":"/mq135/ppm/section1","qos":"1","datatype":"utf8","broker":"53ab3d41.e551b4","x":160,"y":620,"wires":[["71657a33.78df04","75fb8365.f21b7c","6349519c.149d8","c4677a7f.25ce58"]]},{"id":"71657a33.78df04","type":"debug","z":"1e50305d.a3e2f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":390,"y":620,"wires":[]},{"id":"ea9014b0.4a60f8","type":"function","z":"1e50305d.a3e2f","name":"function counter","func":"var counter = flow.get(\"counter\")||0\n\nif((msg.payload ===\"entrance\")&&(counter<50)){\n    counter++\n}\nelse if((msg.payload===\"exit\")&&(counter>0)){\n    counter--\n}\nelse if(msg.payload===\"reset\"){\n    counter=0\n}\n\nflow.set(\"counter\",counter)\nmsg.payload = counter\n\nreturn msg","outputs":1,"noerr":0,"x":560,"y":180,"wires":[["16906cdb.47ee93"]]},{"id":"75fb8365.f21b7c","type":"ui_gauge","z":"1e50305d.a3e2f","name":"","group":"6d8e7178.80df1","order":1,"width":0,"height":0,"gtype":"gage","title":"C02 Level","label":"ppm","format":"{{value}}","min":"0","max":"1000","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":390,"y":680,"wires":[]},{"id":"6349519c.149d8","type":"ui_chart","z":"1e50305d.a3e2f","name":"","group":"6d8e7178.80df1","order":3,"width":0,"height":0,"label":"CO2 PPM Chart","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"0","ymax":"1000","removeOlder":1,"removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"useUTC":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"outputs":1,"x":400,"y":740,"wires":[[]]},{"id":"8841e283.24f81","type":"file","z":"1e50305d.a3e2f","name":"","filename":"/home/pi/log/air-quality.csv","appendNewline":true,"createDir":true,"overwriteFile":"false","encoding":"none","x":540,"y":800,"wires":[[]]},{"id":"c4677a7f.25ce58","type":"function","z":"1e50305d.a3e2f","name":"set log","func":"var date = Date.now()\nmsg.payload = date+\"/mq135/ppm/section1/\"+msg.payload\nreturn msg;","outputs":1,"noerr":0,"x":350,"y":800,"wires":[["8841e283.24f81"]]},{"id":"8789b2c7.02fe","type":"inject","z":"1e50305d.a3e2f","name":"","topic":"increment","payload":"entrance","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":160,"wires":[["5b80cf68.ef7c5"]]},{"id":"66a72d5.db653d4","type":"inject","z":"1e50305d.a3e2f","name":"","topic":"decrement","payload":"exit","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":360,"wires":[["6dd8eb5b.e78c74"]]},{"id":"6dd8eb5b.e78c74","type":"change","z":"1e50305d.a3e2f","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"exit","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":400,"wires":[["ea9014b0.4a60f8"]]},{"id":"9c31fbca.8b4948","type":"inject","z":"1e50305d.a3e2f","name":"","topic":"reset","payload":"reset","payloadType":"str","repeat":"86400","crontab":"","once":false,"onceDelay":0.1,"x":310,"y":220,"wires":[["ea9014b0.4a60f8"]]},{"id":"8c4c2b02.d842f8","type":"mqtt in","z":"1e50305d.a3e2f","name":"","topic":"/mq135/ppm/section2","qos":"1","datatype":"utf8","broker":"53ab3d41.e551b4","x":120,"y":900,"wires":[["f9f8b236.a9e","47ea6a65.75a8f4","1f943d43.eddfd3","4b2406ca.28a528"]]},{"id":"f9f8b236.a9e","type":"debug","z":"1e50305d.a3e2f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":350,"y":900,"wires":[]},{"id":"47ea6a65.75a8f4","type":"ui_gauge","z":"1e50305d.a3e2f","name":"","group":"cb18c461.3ef6f8","order":1,"width":0,"height":0,"gtype":"gage","title":"C02 Level","label":"ppm","format":"{{value}}","min":"0","max":"1000","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":350,"y":960,"wires":[]},{"id":"1f943d43.eddfd3","type":"ui_chart","z":"1e50305d.a3e2f","name":"","group":"cb18c461.3ef6f8","order":3,"width":0,"height":0,"label":"CO2 PPM Chart","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"0","ymax":"1000","removeOlder":1,"removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"useUTC":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"outputs":1,"x":360,"y":1020,"wires":[[]]},{"id":"3009c8d1.bf5dc8","type":"file","z":"1e50305d.a3e2f","name":"","filename":"/home/pi/log/air-quality.csv","appendNewline":true,"createDir":true,"overwriteFile":"false","encoding":"none","x":520,"y":1080,"wires":[[]]},{"id":"4b2406ca.28a528","type":"function","z":"1e50305d.a3e2f","name":"set log","func":"var date = Date.now()\nmsg.payload = date+\"/mq135/ppm/section2/\"+msg.payload\nreturn msg;","outputs":1,"noerr":0,"x":310,"y":1080,"wires":[["3009c8d1.bf5dc8"]]},{"id":"1559b6a5.733539","type":"function","z":"1e50305d.a3e2f","name":"function total counter","func":"var total= flow.get(\"total\")||0\n\nif(msg.payload ===\"entrance\"){\n    total++\n}\nelse if(msg.payload ===\"reset\"){\n    total=0\n}\n\nflow.set(\"total\",total)\nmsg.payload = total\n\nreturn msg","outputs":1,"noerr":0,"x":600,"y":280,"wires":[["5ab2d5fc.8eb6dc"]]},{"id":"cfaf4709.1d8f28","type":"ui_template","z":"1e50305d.a3e2f","group":"ba957629.2b4f28","name":"Counter Widget","order":3,"width":"7","height":"6","format":"<div style=\"text-align:center;\">\n<h1>Total Customer Visited</h1>\n<br/>\n\n<font size={{msg.size}} > {{msg.payload}} </font>\n</div>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":false,"templateScope":"local","x":1060,"y":280,"wires":[[]]},{"id":"5ab2d5fc.8eb6dc","type":"function","z":"1e50305d.a3e2f","name":"add msg.size for font size","func":"msg.size = 80\nreturn msg","outputs":1,"noerr":0,"x":830,"y":280,"wires":[["cfaf4709.1d8f28","df6c20b5.6b4bf"]]},{"id":"df6c20b5.6b4bf","type":"debug","z":"1e50305d.a3e2f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1030,"y":320,"wires":[]},{"id":"ba957629.2b4f28","type":"ui_group","z":"","name":"Counter","tab":"82aa9dd6.c514","order":1,"disp":true,"width":"7","collapse":false},{"id":"53ab3d41.e551b4","type":"mqtt-broker","z":"","name":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"6d8e7178.80df1","type":"ui_group","z":"","name":"Vegetable Section","tab":"82aa9dd6.c514","order":2,"disp":true,"width":"6","collapse":false},{"id":"cb18c461.3ef6f8","type":"ui_group","z":"","name":"Instant Drink Section","tab":"82aa9dd6.c514","order":3,"disp":true,"width":"6","collapse":false},{"id":"82aa9dd6.c514","type":"ui_tab","z":"","name":"Shopping Mall Human Counter","icon":"Shopping Mall","disabled":false,"hidden":false}]

raspberry-pi-ir-sensor-exit-gate-node-red-flow-

JSON
Copy the flow, and paste at Node-Red import > Clipboard
[{"id":"738b8376.f759ac","type":"debug","z":"e82eeb8d.5d3038","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":765.0000419616699,"y":200.00000953674316,"wires":[]},{"id":"70cfac26.2d3314","type":"switch","z":"e82eeb8d.5d3038","name":"!=msg.motion","property":"payload","propertyType":"msg","rules":[{"t":"neq","v":"motion","vt":"msg"}],"checkall":"true","repair":false,"outputs":1,"x":298,"y":57,"wires":[["6d50a86c.57a4c8","7d338535.abea8c"]]},{"id":"6d50a86c.57a4c8","type":"switch","z":"e82eeb8d.5d3038","name":"check ir is triggered","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":540.0000762939453,"y":147.00000095367432,"wires":[["5d19a793.98b598","5aeb6975.9c5078"]]},{"id":"7d338535.abea8c","type":"function","z":"e82eeb8d.5d3038","name":"debounce message","func":"flow.set(\"motion\",msg.payload)\nreturn msg;","outputs":1,"noerr":0,"x":308.99999237060547,"y":99.00000190734863,"wires":[[]]},{"id":"5d19a793.98b598","type":"function","z":"e82eeb8d.5d3038","name":"set-message-log","func":"var date = Date.now()\nvar id = msg.payload.id\nmsg.payload = date+\"/\"+msg.topic+\"/exit/\"+msg.payload\nreturn msg;","outputs":1,"noerr":0,"x":548.5,"y":198,"wires":[["738b8376.f759ac","154e5ea0.ed7811"]]},{"id":"154e5ea0.ed7811","type":"file","z":"e82eeb8d.5d3038","name":"","filename":"/home/pi/log.csv","appendNewline":true,"createDir":false,"overwriteFile":"false","encoding":"utf8","x":737.01171875,"y":253.68359375,"wires":[["dff9f496.f48288"]]},{"id":"dff9f496.f48288","type":"debug","z":"e82eeb8d.5d3038","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":951.01953125,"y":255.00390625,"wires":[]},{"id":"75475f70.51048","type":"mqtt out","z":"e82eeb8d.5d3038","name":"","topic":"pi/2/exit/1","qos":"1","retain":"","broker":"b3f3aeb0.0845e","x":909.0117645263672,"y":36.6328125,"wires":[]},{"id":"5aeb6975.9c5078","type":"function","z":"e82eeb8d.5d3038","name":"mqtt-message","func":"msg.payload =+msg.topic+\"/exit/\"+msg.payload\nreturn msg;","outputs":1,"noerr":0,"x":678.0195388793945,"y":39.00391387939453,"wires":[["75475f70.51048"]]},{"id":"535c7e9d.66024","type":"rpi-gpio in","z":"e82eeb8d.5d3038","name":"","pin":"40","intype":"down","debounce":"25","read":false,"x":93.5,"y":80,"wires":[["70cfac26.2d3314"]]},{"id":"b3f3aeb0.0845e","type":"mqtt-broker","z":"","name":"","broker":"192.168.1.8","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

Credits

aktaktaw aktakpower

aktaktaw aktakpower

3 projects • 0 followers
Freelance R&D Developer mainly focus in AR/VR and IOT Technologies

Comments