Coding on IOT Smart Car Parking System

Hello im doing Smart Parking system with Blynk. Im a newbie on IOT So I need help with my coding especially on the nodemcu/IOT part.

details : **
• Hardware model + communication type: Arduino Mega 2560 + NodeMCU ESP8266

Smartphone OS (Android)
• Blynk server region: LOCAL
• Blynk Library version: 0.6.1

How the project should work>> Arduino send parking sensor data status and its slot availability to Nodemcu ESP8266. Then NodeMCU ESP8266 should receive the data which then update the Blynk App. The Blynk app consist 6 Virtual LEDs (pin V1-V6) and Value Display widget for slot availbility (pin V0). The LEDs on Blynk will ON when parking is “fill” and OFF if no car detected. I have provided the original working sketch code for Arduino without the IOT part and NodeMCU code. please help I need to know how these two code can communicate/work together!

ARDUINO CODE:

#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>

#define ledred 37       // entrance LED
#define ledgreen 38    
#define ledred1 39      // exit LED
#define ledgreen1 40      
#define ledred2 41      // P1 LED
#define ledgreen2 42    
#define ledred3 43      // P2 LED
#define ledgreen3 44
#define ledred4 45      // P3 LED
#define ledgreen4 46
#define ledred5 47      // P4 LED
#define ledgreen5 48
#define ledred6 49      // P5 LED
#define ledgreen6 50
#define ledred7 51      // P6 LED
#define ledgreen7 52
#define buzzer 53

LiquidCrystal_I2C lcd(0x27, 20, 4);
Servo myservoin; // servo for in
Servo myservoout; // servo for exit

int sensor1 = 22; // entrance
int sensor2 = 23; // exit
int sensor3 = 24; // P1
int sensor4 = 25; // P2
int sensor5 = 26; // P3
int sensor6 = 27; // P4
int sensor7 = 28; // P5
int sensor8 = 29; // P6

int P1 = 0, P2 = 0, P3 = 0, P4 = 0, P5 = 0, P6 = 0;
int flag1 = 0, flag2 = 0; 
int slot = 6;

void setup() {
    Serial.begin(9600);

    myservoin.attach(8); // servo for going in connected to pin 8
    myservoout.attach(9); // servo for going out connected to pin 9
    myservoout.write(90); // set the servo(going out) in starting postion
    myservoin.write(90); // set the servo(going in) in starting position
    
    pinMode(sensor1, INPUT); // set sensor 1 as input(to open going in gate)
    pinMode(sensor2, INPUT); // set sensor 2 as input(to close going in gate)
    pinMode(sensor3, INPUT);
    pinMode(sensor4, INPUT);
    pinMode(sensor5, INPUT);
    pinMode(sensor6, INPUT);
    pinMode(sensor7, INPUT);
    pinMode(sensor8, INPUT);
    
    pinMode(ledred, OUTPUT); 
    pinMode(ledgreen, OUTPUT);
    pinMode(ledred1, OUTPUT);
    pinMode(ledgreen1, OUTPUT);
    pinMode(ledred2, OUTPUT);
    pinMode(ledgreen2, OUTPUT);
    pinMode(ledred3, OUTPUT);
    pinMode(ledgreen3, OUTPUT);
    pinMode(ledred4, OUTPUT);
    pinMode(ledgreen4, OUTPUT);
    pinMode(ledred5, OUTPUT);
    pinMode(ledgreen5, OUTPUT);
    pinMode(ledred6, OUTPUT);
    pinMode(ledgreen6, OUTPUT);
    pinMode(ledred7, OUTPUT);
    pinMode(ledgreen7, OUTPUT);
    pinMode(buzzer, OUTPUT);
    
    digitalWrite(ledred, HIGH);    // entrance
    digitalWrite(ledgreen, LOW); 
    digitalWrite(ledred1, HIGH);   // exit
    digitalWrite(ledgreen1, LOW);
    digitalWrite(ledred2, LOW);    // P1
    digitalWrite(ledgreen2, HIGH);
    digitalWrite(ledred3, LOW);    // P2
    digitalWrite(ledgreen3, HIGH);    
    digitalWrite(ledred4, LOW);    // P3
    digitalWrite(ledgreen4, HIGH);
    digitalWrite(ledred5, LOW);    // P4
    digitalWrite(ledgreen5, HIGH);
    digitalWrite(ledred6, LOW);    // P5
    digitalWrite(ledgreen6, HIGH);
    digitalWrite(ledred7, LOW);    // P6
    digitalWrite(ledgreen7, HIGH);
    
    lcd.init();
    lcd.backlight();
    lcd.begin(20, 4);  
    lcd.setCursor(0, 0);
    lcd.print("      Smart Car   ");
    lcd.setCursor(0, 1);
    lcd.print("   Parking System ");
    lcd.setCursor(0, 2);
    lcd.print("  by NAME ");
    lcd.setCursor(0, 3);
    lcd.print("       CLASS     ");
    delay(2000);
    lcd.clear();   
    
    Read_Sensor();
    
    int total = P1 + P2 + P3 + P4 + P5 + P6;
    slot = slot - total; 
}

void loop() {
    Read_Sensor();
    
    lcd.setCursor(0, 0);
    lcd.print("   Have Slot: "); 
    lcd.print(slot);
    lcd.print("    ");  
    
    lcd.setCursor(0, 1);
    if (P1 == 1) {lcd.print("P1:Fill ");}
    else {lcd.print("P1:Empty");}
    
    lcd.setCursor(10, 1);
    if (P2 == 1) {lcd.print("P2:Fill ");}
    else {lcd.print("P2:Empty");}
    
    lcd.setCursor(0, 2);
    if (P3 == 1) {lcd.print("P3:Fill ");}
    else {lcd.print("P3:Empty");}
    
    lcd.setCursor(10, 2);
    if (P4 == 1) {lcd.print("P4:Fill ");}
    else {lcd.print("P4:Empty");}
    
    lcd.setCursor(0, 3);
    if (P5 == 1) {lcd.print("P5:Fill ");}
    else {lcd.print("P5:Empty");}
    
    lcd.setCursor(10, 3);
    if (P6 == 1) {lcd.print("P6:Fill ");}
    else {lcd.print("P6:Empty");}
    
    if (digitalRead(sensor1) == 0 && flag1 == 0) { // entrance
        if (slot > 0) {
            flag1 = 1; 
            {
             digitalWrite(ledred, LOW);
             digitalWrite(ledgreen, HIGH);
             myservoin.write(180); 
             slot = slot - 1;
             }
         }
            if (flag1 == 1) {
                delay(1000);
                myservoin.write(90);
                flag1 = 0;
                digitalWrite(ledred, HIGH);
                digitalWrite(ledgreen, LOW);
            }
         else {
            lcd.setCursor(0, 0);
            lcd.print(" Sorry Parking Full "); 
            delay(1500);
            digitalWrite(buzzer, HIGH);
            delay(250);
            digitalWrite(buzzer, LOW);
            delay(250);
            digitalWrite(buzzer, HIGH);
            delay(250);
            digitalWrite(buzzer, LOW);
            delay(250); 
            }   
    }
    
    if (digitalRead(sensor2) == 0 && flag2 == 0) { //exit
        flag2 = 1; // exit
        {
        digitalWrite(ledred1, LOW);
        digitalWrite(ledgreen1, HIGH);
        myservoout.write(180); 
        slot = slot + 1;
        }
    }
        if (flag2 == 1) {
        delay(1000);
        myservoout.write(90); 
        flag2 = 0;
        digitalWrite(ledred1, HIGH);
        digitalWrite(ledgreen1, LOW);
    }

 if(digitalRead(sensor3) == HIGH)   //P1
 {
 digitalWrite(ledred2, LOW);
 digitalWrite(ledgreen2, HIGH);
 }
 else
 {
 digitalWrite(ledred2, HIGH);
 digitalWrite(ledgreen2, LOW);
 }

 if(digitalRead(sensor4) == HIGH)  //P2
 {
 digitalWrite(ledred3, LOW);
 digitalWrite(ledgreen3, HIGH);
 }
 else
 {
 digitalWrite(ledred3, HIGH);
 digitalWrite(ledgreen3, LOW);
 }
 
 if(digitalRead(sensor5) == HIGH)  //P3
 {
 digitalWrite(ledred4, LOW);
 digitalWrite(ledgreen4, HIGH);
 }
 else
 {
 digitalWrite(ledred4, HIGH);
 digitalWrite(ledgreen4, LOW);
 }
 
 if(digitalRead(sensor6) == HIGH)  //P4
 {
 digitalWrite(ledred5, LOW);
 digitalWrite(ledgreen5, HIGH);
 }
 else
 {
 digitalWrite(ledred5, HIGH);
 digitalWrite(ledgreen5, LOW);
 }

 if(digitalRead(sensor7) == HIGH)  //P5
 {
 digitalWrite(ledred6, LOW);
 digitalWrite(ledgreen6, HIGH);
 }
 else
 {
 digitalWrite(ledred6, HIGH);
 digitalWrite(ledgreen6, LOW);
 }
 if(digitalRead(sensor8) == HIGH)  //P6
 {
 digitalWrite(ledred7, LOW);
 digitalWrite(ledgreen7, HIGH);
 }
 else
 {
 digitalWrite(ledred7, HIGH);
 digitalWrite(ledgreen7, LOW);
 } 
    delay(1);
}

void Read_Sensor() {
 P1=0, P2=0, P3=0, P4=0, P5=0, P6=0;
  
if(digitalRead(sensor3) == 0){P1=1;}
if(digitalRead(sensor4) == 0){P2=1;}
if(digitalRead(sensor5) == 0){P3=1;}
if(digitalRead(sensor6) == 0){P4=1;}
if(digitalRead(sensor7) == 0){P5=1;}
if(digitalRead(sensor8) == 0){P6=1;}  
} 

NODEMCU ESP8266 CODE:

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>


char auth[] = "TOKEN"; // Blynk Auth Token
char ssid[] = "WIFI SSID"; // WiFi SSID
char pass[] = "PASSWORD"; // WiFi Password

WiFiClient client;
BlynkTimer timer;

void setup() {
  Serial.begin(9600);

  // Connect to WiFi
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");
    
  // Initialize Blynk
  Blynk.begin(auth, ssid, pass);

// Initialize serial communication with Arduino
  Serial.println("Initializing serial communication...");
  Serial.begin(9600);
}

void loop() {
  Blynk.run();

  // Receive sensor data from Arduino
  if (Serial.available() > 0) {
    String data = Serial.readStringUntil('\n');
    int P1, P2, P3, P4, P5, P6;
    sscanf(data.c_str(), "P1:%d,P2:%d,P3:%d,P4:%d,P5:%d,P6:%d", &P1, &P2, &P3, &P4, &P5, &P6);

    // Update Blynk app
    if (P1 == 1) {
      Blynk.virtualWrite(V1, 255); // Turn on LED V1
    } else {
      Blynk.virtualWrite(V1, 0); // Turn off LED V1
    }

    if (P2 == 1) {
      Blynk.virtualWrite(V2, 255); // Turn on LED V2
    } else {
      Blynk.virtualWrite(V2, 0); // Turn off LED V2
    }

    if (P3 == 1) {
      Blynk.virtualWrite(V3, 255); // Turn on LED V3
    } else {
      Blynk.virtualWrite(V3, 0); // Turn off LED V3
    }

    if (P4 == 1) {
      Blynk.virtualWrite(V4, 255); // Turn on LED V4
    } else {
      Blynk.virtualWrite(V4, 0); // Turn off LED V4
    }

    if (P5 == 1) {
      Blynk.virtualWrite(V5, 255); // Turn on LED V5
      } else {
      Blynk.virtualWrite(V5, 0); // Turn off LED V5
    }
    
    if (P6 == 1) {
      Blynk.virtualWrite(V6, 255); // Turn on LED V6
      } else {
      Blynk.virtualWrite(V6, 0); // Turn off LED V6
    }
  }
}


It’s very difficult to get this working reliably.

You’re obviously planning on using serial communication to pass data from the Arduino to the NodeMCU, but this is difficult because of timing issues. It’s obviously not going to work at the moment because your NodeMCU is listening for serial input data…

but your Arduino doesn’t have any code to send serial data to the NodeMCU.

If you’re using a Blynk Legacy Local Server then this won’t work…

because you’re not telling the device how to locate the local server on your network, or which communication port to use.

Also, this code is redundant…

because the Blynk.begin() command takes care of this, and I’m not sure why you’re creating a WiFiClient object called client or a BlynkTimer object called timer because neither of them are used in your NodeMCU code.

In my opinion, this would be better implemented using an ESP32 board rather than a combination of Arduino and NodeMCU, but this is clearly a school or college project and presumably your brief specifies the type of hardware that you’ll need to use?

There aren’t that many on this community that have experience of using Blynk Legacy with a local server, and even fewer who have experience of effective communication between an Arduino and NodeMCU via serial communication, so although people might be able to point you in the right direction, most of the effort to get this working will need to come from you.

Pete.

if you really have to use serial comm between your mega and an EP8266, then I2C is to be preferred above async communication. The mega has HW support for I2C, on the ESP8266 you will have to allocate 2 pins (D1 and D2 in the example below), one for SDA and another one for SCL. That leaves the serial com pins free for downloading and debugging.

I2C transfers bytes not integers, so if you need anything else but bytes or booleans you will have to take care of the required conversion.

On the ESP8266 (the master of the I2C comm) you need code such as (this is an example, not working code. Check the internet for further details):

byte item_to_be_send_1 = 0;
byte item_to_be_send_2 = 0;
byte item_to_be_send_3 = 0;

byte received_item_1 = 0;
byte received_item_2 = 0;

void setup(void) { 
  Wire.begin(D1, D2);

  timer.setInterval(1000L, I2Csync);
}

void I2Csync() {
  Wire.beginTransmission(8);
  Wire.write(item_to_be_send_1);
  Wire.write(item_to_be_send_2);
  Wire.write(item_to_be_send_3);

  Wire.requestFrom(8, 2);
  if (Wire.available()) {
    received_item_1 = Wire.read();             
    received_item_2 = Wire.read();            
  }
  while (Wire.available()) {
    byte dump_excessive = Wire.read();
  }
}

and call this function using a Blynk timer (e.g. every second)

on the mega you would need something like this:

byte item_to_be_send_1 = 0;
byte item_to_be_send_2 = 0;

byte received_item_1 = 0;
byte received_item_2 = 0;
byte received_item_3 = 0;

void setup(void) {
  Wire.begin(8);                /* join i2c bus with address 8 */
  Wire.onReceive(receiveEvent); /* register receive event */
  Wire.onRequest(requestEvent); /* register request event */
}

// function that executes whenever data is received from master
void receiveEvent(int howMany) {
  while (0 < Wire.available()) {
    received_item_1 = Wire.read();
    received_item_2 = Wire.read();
    received_item_3 = Wire.read();
  }
}

// function that executes whenever data is requested by master (ESP8266)
void requestEvent() {
  Wire.write(item_to_be_send_1);
  Wire.write(item_to_be_send_2);
}

good luck,

piet.

1 Like