Send message from the cloud to the arduino using esp8266 wifi module

I am a college undergraduate currently doing my term paper about a iot parking system using an ultrasonic sensor and wifi module. The ultrasonic sensor is used to determine the vacancy of the parking slot and a rgb module is used to indicate the the status (green - vacant, red - occupied). The esp8266 is used to communicate between the prototype and the cloud/app.

I am currently using blynk app to test the connectivity of my prototype and application. I have put 3 leds in the template of the app to indicate also the status of the 3 slots.

Now, I want to add another feature. If i press a button in the blynk app, a yellow led (reserved) will light up and when the ultrasonic detects that the slot is already occupied, the yellow led will turn off and the red led will turn on. Can anyone help me?

//arduino code//

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

SoftwareSerial nodemcu (2,3);
LiquidCrystal_I2C lcd (0x27, 16, 2);

const int trig1 = 13;
const int echo1 = 12;
const int trig2 = 11;
const int echo2 = 10;
const int trig3 = 9;
const int echo3 = 8;

const int r1 = 7;
const int g1 = 6;
const int r2 = 5;
const int g2 = 4;
const int r3 = 3;
const int g3 = 2;

int duration1 = 0;
int distance1 = 0;
int duration2 = 0;
int distance2 = 0;
int duration3 = 0;
int distance3 = 0;

int count = 0;
int freeslot = 0;
int s1 = 0;
int s2 = 0;
int s3 = 0;

String sensor1;
String sensor2;
String sensor3;
String cdata = "";

void setup() 
{
  pinMode(trig1 , OUTPUT);
  pinMode(echo1 , INPUT);
  pinMode(trig2 , OUTPUT);
  pinMode(echo2 , INPUT);
  pinMode(trig3 , OUTPUT);
  pinMode(echo3 , INPUT);

  pinMode(r1, OUTPUT);
  pinMode(g1, OUTPUT);
  pinMode(r2, OUTPUT);
  pinMode(g2, OUTPUT);
  
  lcd.begin();
  lcd.backlight();

  Serial.begin(9600); 
  nodemcu.begin(9600);  

}

void loop()
{
cdata = cdata + sensor1+" ,"+sensor2+" ,"+sensor3;
Serial.println(cdata);
nodemcu.println(cdata);
digitalWrite(trig1, HIGH);
delayMicroseconds(1000);
digitalWrite(trig1, LOW);
duration1 = pulseIn(echo1, HIGH);
distance1 = (duration1/2) / 28.5;
delay(1000);
cdata = "";

if (distance1 <= 50) {
s1 = 1;
sensor1 = "0";
digitalWrite(r1, HIGH);
digitalWrite(g1, LOW);
Serial.print ("s1: OCCUPIED");
lcd.print("INTRAMUROADS");
lcd.setCursor(0,1);
lcd.print("SLOT1: OCCUPIED");
delay(1000);
lcd.clear();
} else {
  s1 = 0;
  sensor1 = "255";
  digitalWrite(g1, HIGH);
  digitalWrite(r1, LOW);  
  Serial.print ("s1: VACANT");
  lcd.print("INTRAMUROADS");
  lcd.setCursor(0,1);
  lcd.print("SLOT1: VACANT");
  delay(1000);
  lcd.clear();
}


digitalWrite(trig2, HIGH);
delayMicroseconds(1000);
digitalWrite(trig2, LOW);
duration2 = pulseIn(echo2, HIGH);
distance2 = (duration2/2) / 28.5;
delay(1000);

if (distance2 <=50) {
  s2 = 1;
  sensor2 = "0";
  digitalWrite(r2, HIGH);
  digitalWrite(g2, LOW);
  Serial.print (" ,s2: OCCUPIED");
  lcd.print("INTRAMUROADS");
  lcd.setCursor(0,1);
  lcd.print("SLOT2: OCCUPIED");
  delay(1000);
  lcd.clear();

}  else {
    s2 = 0;
    sensor2 = "255";
    digitalWrite(r2, LOW);
    digitalWrite(g2, HIGH);
    Serial.print (" ,s2: VACANT");
    lcd.print("INTRAMUROADS");
    lcd.setCursor(0,1);
    lcd.print("SLOT2: VACANT");
    delay(1000);
    lcd.clear();
  }



digitalWrite(trig3, HIGH);
delayMicroseconds(1000);
digitalWrite(trig3, LOW);
duration3 = pulseIn(echo3, HIGH);
distance3 = (duration3/2) / 28.5;
delay(1000);

if (distance3 <=50) {
  s3 = 1;
  sensor3 = "0"; 
  digitalWrite(r3, HIGH);
  digitalWrite(g3, LOW);
  Serial.print (" ,s3: OCCUPIED");
  lcd.print("INTRAMUROADS");
  lcd.setCursor(0,1);
  lcd.println("SLOT3: OCCUPIED");
  delay(1000);
  lcd.clear();

} else {
  s3 = 0;
  sensor3 = "255";
  digitalWrite(r3, LOW);
  digitalWrite(g3, HIGH);
  Serial.println(" ,s3: VACANT");
  lcd.print("INTRAMUROADS");
  lcd.setCursor(0,1);
  lcd.println("SLOT3: VACANT");
  delay(1000);
  lcd.clear();
}
count = s1 + s2 + s3;
freeslot = 3 - count;
Serial.print("Free slot :");
Serial.println(freeslot);

}

//esp8266 code//

#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SoftwareSerial.h>

char auth [] = "authkey";
char ssid [] = "wifiname";
char pass [] = "wifipass";

SimpleTimer timer;

String myString;
char rdata;

int s1, s2, s3;
int led1, led2, led3;

void myTimerEvent () {
  Blynk.virtualWrite(V1, millis()/1000);
}

void setup () {
  Serial.begin(9600);
  Blynk.begin(auth,ssid,pass);
    timer.setInterval(100L,sr1);
    timer.setInterval(100L,sr2);
    timer.setInterval(100L,sr3);
    
}

void loop () {

  if(Serial.available() == 0) {
    Blynk.run();
    timer.run();
  }
  if (Serial.available() > 0) {
    rdata = Serial.read();
    myString = myString+ rdata;
   // Serial.println(rdata);
    if(rdata == '\n') {
      Serial.println(myString);

      String l = getValue(myString, ',', 0);
      String m = getValue(myString, ',', 1);
      String n = getValue(myString, ',', 2);

      led1 = l.toInt();
      led2 = m.toInt();
      led3 = n.toInt();

      myString = "";
    }
  }
}

void sr1() {
  int sdata = led1;
  Blynk.virtualWrite(V10, sdata);
}
void sr2() {
  int sdata = led2;
  Blynk.virtualWrite(V11, sdata);
}
void sr3() {
  int sdata = led3;
  Blynk.virtualWrite(V12, sdata);
}

String getValue(String data, char separator, int index) {
  int found = 0;
  int strIndex[] = {0,-1};
  int maxIndex = data.length() -1;

  for (int i = 0; i<= maxIndex && found <= index; i++) {
    if (data.charAt(i) == separator || i == maxIndex) {
      found++;
      strIndex[0] = strIndex[1] +1;
      strIndex[1] = (i == maxIndex) ? i+1 : i;
    }
  }
  return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}

Hi!

For this very simple project why you don't use only a NodeMCU?

Best regards.

Hi, becuase i would like to install more than one sensor in the project. The nodemcu has only limited pins which cannot entertain the other sensors :<

Which type of sensors?

An arduino Uno has 15 GPIO excluding serial (0 -1), I2C (A4-A5) and pin 13 due the builtin LED.

A nodeMCU has 13 GPIO.

A ESP32 has 18 GPIO.

So consireding your needs for pins ESP32 is more suitable.

4 more ultrasonic sensors and 12 more leds (might use rgb module). So i am considering using an arduino mega instead to host the needs for pins and using the esp module for iot communication purpose only.

Also, does my arduino code work if i upload it to the esp module?

You don't need of this amount of pins to control LEDs. You can use addressable led strip like WS2812 that will take a single pin.

Look this:

thank you for your answer, i have tried your suggestion to use the esp8266 as the microcontroller and have achieved my goal. Thank you so much for your help. as for the LEDs, i will dig more into how to use addressable LEDs to lessen the pins needed for my project :>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.