How do you feel about this article? Help us to provide better content for you.
Thank you! Your feedback has been received.
There was a problem submitting your feedback, please try again later.
What do you think of this article?
In the first post, we have seen How to set up the FireBase account to upload data via NodeMCU and today, we will have a look at the code we need to upload in the NodeMCU and I will explain the code in detail. If you are new to Arduino, then you should have a look at How to write Arduino Code?, to get a better understanding of Arduino Code Design.
CODE:
#include <ESP8266WiFi.h>
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <FirebaseArduino.h>
#define FIREBASE_HOST "smart-parking-7f5b6.firebaseio.com" // the project name address from firebase id
#defineFIREBASE_AUTH "suAkUQ4wXRPW7nA0zJQVsx3H2LmeBDPGmfTMBHCT" // the secret key generated from firebase
#define WIFI_SSID "CircuitDigest"
// input your home or public wifi name
#define WIFI_PASSWORD "circuitdigest101" //password for Wifi
String Available = ""; //availability string
String fireAvailable = "";
LiquidCrystal_I2C lcd(0x27, 16, 2); //i2c display address 27 and 16x2 lcd display
Servo myservo; //servo as gate
Servo myservos; //servo as gate
int Empty; //available space integer
intallSpace = 90;
intcountYes = 0;
intcarEnter = D0; // entry sensor
intcarExited = D4; //exi sensor
int TRIG = D7; //ultrasonic trig pin
int ECHO = D8; // ultrasonic echo pin
int led = D3; // spot occupancy signal
intpos;
int pos1;
long duration, distance;
void setup() {
delay(1000);
Serial.begin (9600); // serial debugging
Wire.begin(D2, D1); // i2c start
myservo.attach(D6); // servo pin to D6
myservos.attach(D5); // servo pin to D5
pinMode(TRIG, OUTPUT); // trig pin as output
pinMode(ECHO, INPUT); // echo pin as input
pinMode(led, OUTPUT); // spot indication
pinMode(carExited, INPUT); // ir as input
pinMode(carEnter, INPUT); // ir as input
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); //try to connect with wifi
Serial.print("Connecting to ");
Serial.print(WIFI_SSID); // display ssid
while (WiFi.status() != WL_CONNECTED) {
Serial.print("."); // if not connected print this
delay(500);
}
Serial.println();
Serial.print("Connected to ");
Serial.println(WIFI_SSID);
Serial.print("IP Address is : ");
Serial.println(WiFi.localIP()); //print local IP address
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH); // begin firebase authentication
lcd.begin(); //begin lcd
lcd.home();
lcd.setCursor(0, 0); // 0th row and 0thh column
lcd.print("Smart Parking");
}
void loop() {
digitalWrite(TRIG, LOW); // make trig pin low
delayMicroseconds(2);
digitalWrite(TRIG, HIGH); // make trig pin high
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
duration = pulseIn(ECHO, HIGH);
distance = (duration / 2) / 29.1; // take distance in cm
Serial.print("Centimeter: ");
Serial.println(distance);
intcarEntry = digitalRead(carEnter); // read ir input
if (carEntry == HIGH) { // if high then count and send data
countYes++; //increment count
Serial.print("Car Entered = " ); Serial.println(countYes );
lcd.setCursor(0, 1);
lcd.print("Car Entered");
for (pos = 140; pos>= 45; pos -= 1) { // change servo position
myservos.write(pos);
delay(5);
}
delay(2000);
for (pos = 45; pos<= 140; pos += 1) { // change servo position
// in steps of 1 degree
myservos.write(pos);
delay(5);
}
Firebase.pushString("/Parking Status/", fireAvailable ); // send string to firebase
lcd.clear();
}
intcarExit = digitalRead(carExited); //read exit ir sensor
if (carExit == HIGH) { //if high then count and send
countYes--; //decrement count
Serial.print("Car Exited = " ); Serial.println(countYes);
lcd.setCursor(0, 1);
lcd.print("Car Exited");
for (pos1 = 140; pos1 >= 45; pos1 -= 1) { // change servo position
myservo.write(pos1);
delay(5);
}
delay(2000);
for (pos1 = 45; pos1 <= 140; pos1 += 1) { // change servo position
// in steps of 1 degree
myservo.write(pos1);
delay(5);
}
Firebase.pushString("/Parking Status/", fireAvailable ); // send string to firebase
lcd.clear();
}
if (distance < 6) { //if distance is less than 6cm then on led
Serial.println("Occupied ");
digitalWrite(led, HIGH);
}
if (distance > 6) { //if distance is greater than 6cm then off led
Serial.println("Available ");
digitalWrite(led, LOW);
}
Empty = allSpace - countYes; //calculate available data
Available = String("Available= ") + String(Empty) + String("/") + String(allSpace); // convert the int to string
fireAvailable = String("Available=") + String(Empty) + String("/") + String(allSpace);
lcd.setCursor(0, 0);
lcd.print(Available); //print available data to lcd
}
EXPLANATION OF THE PROGRAMMING CODE:
For programming, we just need to plug the Node MCU into the Computer with a Micro USB cable and with the open Arduino IDE
Some of the libraries are required for I2C Display and Servo Motor.
Here the LCD is used for the display of the availability of parking spaces and according to the instructions in the code, the servo motors will help open the Entry and Exit gates.
The Explanation of the above code is discussed in the below steps:
The library used to interface LCD in I2C protocol is Wire.h. The pins for I2C in the ESP8266 are D1 (SCL) & D2 (SDA).
- As discussed in the above steps, the database used is Firebase, so the library (FirebaseArduino.h) is to be included.
- Now, include the firebase credentials which you will get from the Google Firebase. These include the Hostname containing the project name and the secret key
- Then include the Wi-Fi Credentials such as WiFi SSID and Password
- After adding initialize I2C LCD with device address and the type of LCD, you will also need to include the servo motor for the entry and exit gate
- In the next step, start the communication for I2C LCD
- Connect the servo motors of the entry and exit gate to the D5, D6 Pins of the NodeMCU
- Now select the Trigger Pin (TRIG) as the output and the Echo pin (ECHO) as the input.
- The ultrasonic sensor will be used in detecting the parking slot availability
- Accordingly, if the car occupies the space then it will glow or else it will not glow
- Now, the two pins D0 & D4 of the NodeMCU are used for taking the IR sensor reading. This IR sensor will act as the entry and exit gates sensor and it will help in detecting the car presence
- Next, connect the Wi-Fi and wait for a while till it gets connected
- So, now the time to begin the connection with Firebase with Host and Secret key as credentials
- Begin I2C 16*2 LCD and set cursor position at the (0,0) row and the column
- By using the ultrasonic sensor take the distance and this will be used in detecting the vehicle presence in the particular arena or spot.
- Firstly, send the 2-microsecond pulse then after read the received pulse and convert it into 'cm'
- Read the IR sensor pin digitally as an entry sensor and check if it is high
- If it is high then increment the entry count and print it on the 16*2 LCD and also on the Serial Monitor
- Then move the servo motor angle to the open entry gate and can change the angle according to the use case.
- We can also use DC motor, instead of Servo Motor, but with DC Motor we also need to use Pulse Width Modulation(PWM) to control its speed.
- Now, send the reading to the Firebase using the pushString which is the function of Firebase library
- Similar steps are to be followed as above for the Exit IR sensor and also for the Exit Servo motor.
- Now check if the car is in the parking spot and if it is, then a glow led signal will be showing and a signal that the spot is full
- Else, it is shown that the spot is available
- Calculate the empty space present in the parking slot and save that in the string to send the data to the Firebase.
- Hence, the explanation of the code is completed
BUILDING THE PROJECT:
This project involves 2 IR sensors, 2 servo motors, 1 Ultrasonic sensor and one 16*2 LCD.
In this project, the ESP8266 will control the complete process, structure and also sends the parking availability information to the Google Firebase, so that this can be monitored from anywhere in the world by the internet.
Two IR sensors are used at the Entry and also at the Exit gate which will help in rotating to open or close the gate
The Ultrasonic Sensor is used in detecting if the parking slot is available or occupied and can send the data to the ESP8266 accordingly.
So by this, the parking system can be controlled. You should also have a look at this Servo Motor Control using Arduino, for understanding the working of Servo Motor.
PRACTICAL IMPLEMENTATION:
Practical Implementation-Smart Parking System Using Node MCU is shown below:
OUTPUT OF THE PROJECT:
- After uploading the code, open the database in the Firebase console.
- Connect to the internet and now you can operate the system from anywhere in the world
- This finishes the complete Smart Parking System using Node MCU ESP8266 and also with different peripherals.
- As this project will work in real time, so it's an example of real time embedded systems.
- The Smart Parking System has a vast application and different modules can be added to make it smarter
- By, this our Project is successfully done.