#include <Adafruit_NeoPixel.h>
#include <WiFi.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// channel id : 2308227
//channel api key : 900QW6WMUMXQBZ0N
#include <WiFi.h>
#include "ThingSpeak.h"
//#include <HTTPClient.h>
// initialize wirelelss connection to communicate to thingspeak
const char* WIFI_NAME = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
const int myChannelNumber = 2308227 ;
const char* myApiKey = "900QW6WMUMXQBZ0N";
const char* server = "http://api.thingspeak.com/update";
WiFiClient client;
// initialize the lcd display
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 4);
// initializes time server
#define NTP_SERVER "africa.pool.ntp.org"
#define UTC_OFFSET 3
#define UTC_OFFSET_DST 0
//const long utcOffsetInSeconds = 3 * 3600; // Set to 3 hours ahead of UTC
// defines the pins used in esp32
#define PIN 13
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 16 // Popular NeoPixel ring size
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
#define DELAYVAL 500 // Time (in milliseconds) to pause between pixels
//PIR SENSOR
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 30;
//the time when the sensor outputs a low impulse
long unsigned int lowIn;
//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int tpause = 5000;
boolean lockLow = true;
boolean takeLowTime;
int pirPin = 16; //the digital pin connected to the PIR sensor's output
//int ledPin = 13;
//LDR
const int ldrPin = 2; // Analog input pin for the LDR
const int led= 18; // Digital output pin for the LED
//people counter
int peopleCount = 0;
bool motionDetected = false;
bool prevMotionState = false;
/////////////////////////////
void spinner() // function for displaying time
{
static int8_t counter = 0;
const char* glyphs = "\xa1\xa5\xdb";
LCD.setCursor(15, 1);
LCD.print(glyphs[counter++]);
if (counter == strlen(glyphs)) {
counter = 0;
}
}
//SETUP
void setup()
{
Serial.begin(9600);//initializes serial monitor
pinMode(pirPin, INPUT); //defines pins
pinMode(led, OUTPUT);
digitalWrite(pirPin, LOW);
pinMode(ldrPin, INPUT);
Serial.println("People Counter");
//give the sensor some time to calibrate
//Serial.print("calibrating sensor ");
//for(int i = 0; i < calibrationTime; i++){
//Serial.print(".");
//delay(1000);
// }
//Serial.println(" done");
//Serial.println("SENSOR ACTIVE");
//delay(50);
// Time server
LCD.init();
LCD.backlight();
LCD.setCursor(0, 0);
LCD.print("Connecting to ");
LCD.setCursor(0, 1);
LCD.print("WiFi ");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED)
{
delay(250);
spinner();
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
LCD.clear();
LCD.setCursor(0, 0);
LCD.println("Online");
LCD.setCursor(0, 1);
LCD.println("Updating time...");
configTime(UTC_OFFSET, UTC_OFFSET_DST, NTP_SERVER);
//thingspeak
WiFi.begin(WIFI_NAME, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.println("Wifi not connected");
}
Serial.println("Wifi connected !");
Serial.println("Local IP: " + String(WiFi.localIP()));
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
}
///////////////////////////
//LOOP
void loop()
{
//neopixel();
printLocalTime();
pir();
//LDR();
Thingspeak();
}
void neopixel()
{
//pixels.clear(); // Set all pixel colors to 'off'
// The first NeoPixel in a strand is #0, second is 1, all the way up
// to the count of pixels minus one.
for(int i=0; i<NUMPIXELS; i++) { // For each pixel...
// pixels.Color() takes RGB values, from 0,0,0 up to 255,255,255
// Here we're using a moderately bright green color:
pixels.setPixelColor(i, pixels.Color(255, 255, 255));
pixels.show(); // Send the updated pixel colors to the hardware.
delay(DELAYVAL); // Pause before next pass through loop
}
}
void turnOffNeoPixels()
{
for (int i = NUMPIXELS - 1; i >= 0; i--) {
// Set the color of the current pixel to black
pixels.setPixelColor(i, pixels.Color(0, 0, 0));
pixels.show(); // Send the updated pixel colors to the hardware.
delay(DELAYVAL); // Optional delay to create a sequential turn-off effect
}
}
void pir()
{
// Read the PIR sensor's output state
motionDetected = digitalRead(pirPin);
// If motion is detected and the previous state was not motion
if (motionDetected && !prevMotionState) {
// Increment the people count
peopleCount++;
prevMotionState = true;
Serial.print("People Count: ");
Serial.println(peopleCount);
LCD.setCursor(0, 2);
LCD.println("People Count: ");
LCD.setCursor(13, 2);
LCD.println(peopleCount);
}
else if (!motionDetected) {
// If no motion is detected, set the previous state to false
prevMotionState = false;
}
// Check if the LDR threshold is met
bool ldrMet = ldrThresholdMet();
// If both PIR motion and LDR threshold are met
if (digitalRead(pirPin) == HIGH && ldrMet) {
// Turn on the LED and NeoPixel ring
digitalWrite(led, HIGH);
neopixel();
if (lockLow) {
// If transitioning to a motion state, set lockLow to false
lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis() / 1000);
Serial.println(" sec");
delay(50);
}
takeLowTime = true;
} else {
// If either PIR motion or LDR threshold is not met, turn off LED and NeoPixel
digitalWrite(led, LOW);
turnOffNeoPixels();
}
}
bool ldrThresholdMet() {
// Read the LDR value
int ldrValue = analogRead(ldrPin);
// Set the LDR threshold value
int threshold = 500; // Adjust this value according to your needs
// Check if the LDR value is below the threshold
return ldrValue < threshold;
}
void printLocalTime()
{
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
LCD.setCursor(0, 1);
LCD.println("Connection Err");
return;
}
LCD.setCursor(8, 0);
LCD.println(&timeinfo, "%H:%M:%S");
LCD.setCursor(0, 1);
LCD.println(&timeinfo, "%d/%m/%Y %Z");
}
void Thingspeak()
{
int x = ThingSpeak.writeFields(myChannelNumber,myApiKey);
//Serial.println("Temp: " + String(data.temperature, 2) + "°C");
//Serial.println("Humidity: " + String(data.humidity, 1) + "%");
if(x == 200)
{
Serial.println("Data pushed successfull");
}
else
{
Serial.println("Push error" + String(x));
}
Serial.println("---");
ThingSpeak.setField(1, peopleCount);
}