#define SECRET_SSID " " //Wifi credentials to connect to network
#define SECRET_PASS " "
#define CHANNEL_ID //Your channel id
#define WRITE_APIKEY " " //Api key
#include <WiFiClient.h>
WiFiClient wifiClient;
#include <ESP8266HTTPClient.h>
#include "ThingSpeak.h"
//pin declarartions and global variables declarations
int irPin=2;
int irPin1=13;
int led = 4;
int count=0;
int count1=0,lft=0;
int val1=0,val2=0;
int mag = 0;
//Tweet credential declarations
String host = "api.thingspeak.com";
int httpPort = 80;
String url = "/apps/thingtweet/1/statuses/update";
HTTPClient http;
unsigned long myChannelNumber = CHANNEL_ID;
const char * myWriteAPIKey = WRITE_APIKEY;
#include <ESP8266WiFi.h>
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password
int keyIndex = 0; // your network key index number (needed only for WEP)
WiFiClient client;
void setup() {
pinMode(irPin, INPUT);
pinMode(irPin1, INPUT);
pinMode(led,OUTPUT);
Serial.begin(115200);
delay(100);
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
}
void loop() {
// Connect or reconnect to WiFi
if (WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(SECRET_SSID);
while (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
}
val1=digitalRead(irPin);
//Entry counter
if(val1 == LOW)
{
count++;
Serial.print("People Entering: ");
Serial.println(count);
lft=count-count1;
Serial.print("People in room: ");
Serial.println(lft);
ThingSpeak.writeField(myChannelNumber, 1, count, myWriteAPIKey);
delay(500);
// digitalWrite(led,LOW);
}
//Exit counter
val2=digitalRead(irPin1);
if(val2 == LOW)
{
count1++;
Serial.print("People Leaving: ");
Serial.println(count1);
lft=count-count1;
Serial.print("People in room: ");
Serial.println(lft);
ThingSpeak.writeField(myChannelNumber, 2, count1, myWriteAPIKey);
delay(500);
// digitalWrite(led,LOW);
}
//digitalWrite(led,LOW);
delay(50);
ThingSpeak.writeField(myChannelNumber, 3, lft, myWriteAPIKey);
delay(100);
//Tweet if 10 people have entered
if(lft == 10)
{
http.begin(wifiClient,host,httpPort,url);
String RequestBody = "api_key=6R3LW6MNGC3MSAC2&status=10 people have entered the room!";
int httpCode = http.POST(RequestBody);
//Serial.println(httpCode);
delay(2000);
}
//Glow led and serial monitor print if more than 20 people have entered.
if(lft > 20)
{
digitalWrite(led,HIGH);
}
else
{
digitalWrite(led,LOW);
}
delay(500);
}
Comments