Hi,
I am new here. My project are using arduino uno as microcontroller. My project consist of 3 features. First is space used calculation using ultrasound sensor. Second is activity monitoring using PIR sensor and last one is RFID system. I am full aware that arduino cannot run 3 program simultaneously but I am trying my best to make it work. So now bck to my problem related to the BLYNK, I am trying to send the result I get at arduino to BLYNK so that I can monitor the result remotely. I am using serial connection as medium between arduino and BLYNK server.
Details
Blynk V0.4.10
Arduino IDE 1.8.4
Coding
#define BLYNK_PRINT DebugSerial
#include <SoftwareSerial.h>
#include <Servo.h>
#include <math.h>
#include <SPI.h>
#include <RFID.h>
Servo myservo;
SoftwareSerial DebugSerial(2, 3); // RX, TX
#include <BlynkSimpleStream.h>
/*set pin number*/
//SCK 13
//MISO 12
//MOSI 11
#define SS_PIN 10 //RFID
#define RST_PIN 9 //RFID
#define SS_PIN2 8 //RFID2
#define RST_PIN2 7 //RFID2
int PIR_output = 6; //PIR
int servo = 5; //servo
#define trigPin 4 //sonar
#define echoPin 3 //sonar
/*common declaration*/
long duration, inches, cm = 0; //sonar
int st = 0; //servo
int empty = 0, full = 0, temp = 0;
int angle = 0;
int increment = 0;
int pos;
RFID rfidIn(SS_PIN, RST_PIN); //RFID
RFID rfidOut(SS_PIN2, RST_PIN2); //RFID2
int serNum[5];
int card1[][5] = {{111, 106, 104, 161, 204}}; //rafi
int card2[][5] = {{88, 253, 100, 57, 248}}; //hakim
bool access1 = false;
bool access2 = false;
bool card1status = false;
bool card2status = false;
/*timer*/
unsigned long prevtime = 0; //pir
unsigned long prevtime2 = 0; //servo
unsigned long prevtime3 = 0; //print data
unsigned long card1time = 0; //rfid1
unsigned long card2time = 0; //rfid2
unsigned long notallowedIn = 0;
unsigned long notallowedOut = 0;
int movements = 0;
/*declaration of constants*/
int max_distance = 30; //sonar distance in cm
int height = 30, width = 60; // size of room in cm
int timePIR = 5000; //read pir every 5 seconds
int timeSonar = 10000; //read sonar for every 10 seconds
int timePrint = 30000; //print collected data for every 30 seconds
char auth[] = "3b0fbfe2cd5241f2958742a020d4601a";
void setup()
{
// Debug console
DebugSerial.begin(9600);
//intialize
Serial.begin(9600);
Serial.begin(9600);
Blynk.begin(Serial, auth);
//setting for input and output
myservo.attach(servo);
pinMode(PIR_output, INPUT);
//Initialize RFID
SPI.begin();
rfidIn.init();
rfidOut.init();
//calculate the starting position of servo
angle = round( atan2 (height, width * 0.5) * 180 / 3.14159265 ); // initial position of servo
Serial.print("Initial angle of servo is "); Serial.println(angle);
pos = angle;
myservo.write(90);
delay(2500);
}
void loop()
{
Blynk.run();
//read from RFID card reader
rfidin();
rfidout();
//fetching data from pir sensor
pir();
//fetching data from sonar sensor
if (millis() - prevtime2 >= timeSonar)
{
servomove();
}
//Print collected data
if (millis() - prevtime3 >= timePrint)
{
printdata();
prevtime3 = millis();
}
}
void rfidin ()
{
if (rfidIn.isCard()) {
if (rfidIn.readCardSerial()) {
// Serial.print(rfidIn.serNum[0]);
// Serial.print(" ");
// Serial.print(rfidIn.serNum[1]);
// Serial.print(" ");
// Serial.print(rfidIn.serNum[2]);
// Serial.print(" ");
// Serial.print(rfidIn.serNum[3]);
// Serial.print(" ");
// Serial.print(rfidIn.serNum[4]);
// Serial.println("");
if (1)
{
for (int x = 0; x < sizeof(card1); x++) {
for (int i = 0; i < sizeof(rfidIn.serNum); i++ ) {
if (rfidIn.serNum[i] == card1[x][i]) {
access1 = true;
} else {
access1 = false;
break;
}
}
if (access1) break;
}
}
else if (access1 == false)
{
for (int x = 0; x < sizeof(card2); x++) {
for (int i = 0; i < sizeof(rfidIn.serNum); i++ ) {
if (rfidIn.serNum[i] == card2[x][i]) {
access2 = true;
}
else {
access2 = false;
break;
}
}
if (access2) break;
}
}
if (access1 && card1status == false) {
Serial.println("Welcome Rafi!");
card1time = millis();
card1status = true;
} else if (access2 && card2status == false) {
Serial.println("Welcome Hakim!");
card2time = millis();
card2status = true;
}
else {
if (millis() - notallowedIn >= 1000)
{
Serial.println("Not allowed!");
notallowedIn = millis();
}
}
}
rfidIn.halt();
}
}
void rfidout ()
{
if (rfidOut.isCard()) {
if (rfidOut.readCardSerial()) {
// Serial.print(rfidOut.serNum[0]);
// Serial.print(" ");
// Serial.print(rfidOut.serNum[1]);
// Serial.print(" ");
// Serial.print(rfidOut.serNum[2]);
// Serial.print(" ");
// Serial.print(rfidOut.serNum[3]);
// Serial.print(" ");
// Serial.print(rfidOut.serNum[4]);
// Serial.println("");
if (1)
{
for (int x = 0; x < sizeof(card1); x++) {
for (int i = 0; i < sizeof(rfidOut.serNum); i++ ) {
if (rfidOut.serNum[i] == card1[x][i]) {
access1 = true;
} else {
access1 = false;
break;
}
}
if (access1) break;
}
}
else if (access1 == false)
{
for (int x = 0; x < sizeof(card2); x++) {
for (int i = 0; i < sizeof(rfidOut.serNum); i++ ) {
if (rfidOut.serNum[i] == card2[x][i]) {
access2 = true;
} else {
access2 = false;
break;
}
}
if (access2) break;
}
}
}
if (access1 && card1status == true)
{
Serial.println("Good Bye Rafi!");
card1time = millis() - card1time;
float t1 = card1time * 0.001;
Serial.print("Your total time in room: ");
Serial.print(t1);
Serial.println(" seconds");
card1status = false;
}
else if (access2 && card2status == true)
{
Serial.println("Good Bye Hakim!");
card2time = millis() - card2time;
float t2 = card2time * 0.001;
Serial.print("Your total time in room: ");
Serial.print(t2);
Serial.println(" seconds");
card2status = false;
}
else {
if (millis() - notallowedOut >= 1000)
{
Serial.println("Not allowed!");
notallowedOut = millis();
}
}
}
rfidOut.halt();
}
void servomove()
{
//direction of servo
if (pos == angle)
st = 0;
if (pos == (180 - angle))
st = 1;
//moving the servo
if (st == 0) {
pos = pos + 1;
}
if (st == 1) {
pos = pos - 1;
}
myservo.write(pos);
//fething distance data from sonar
data();
//print servo position
Serial.print(pos); Serial.print( " "); Serial.println(cm);
delay(10);
//calculating the percentage of usage of the room
if (cm >= max_distance)
{
empty++;
}
else if (cm < max_distance)
{
full++;
}
if (pos == angle || pos == (180 - angle))
{
float value;
float y;
y = 180 - angle - angle;
value = (full / y * 100);
Blynk.virtualWrite(V5, value);
Serial.print(value); Serial.println("% space used");
Serial.print("Servo stop at "); Serial.println(pos);
prevtime2 = millis();
//print current movement index
Serial.print("Movement index: ");
Serial.println(movements);
}
if (temp != st)
{
empty = 0;
full = 0;
}
temp = st;
}
void printdata()
{
//print accumulated movement index
Serial.print("Total movement index = ");
Serial.println(movements);
movements = 0;
}
void pir()
{
if (digitalRead(PIR_output) == HIGH)
{
movements++;
}
}
void data()
{
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
if (cm > max_distance)
{
cm = max_distance;
}
}
//funtion used for sonar coding
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}