BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

The ultimate esp32 project *plz help*

I tried to combined some of the project i made in single board (esp32) and after hours and hours i uploaded the code and it didn’t work it spouses to control 8 channel relay and 4 physical switch to control 4 from the 8 manually and with the blynk app and a dht sensor for the humidity and temperature and 12v dc to cool the system and reed switch for the door and finally a rc522 for the door lock but unfortunately it didn’t work and i don’t know why the relays are work with huge delay and i don’t get any reads from the sensors

#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
#include <SPI.h>
#include <MFRC522.h>
#include <EEPROM.h>
BlynkTimer timer;


#define DEBUG_SW 0


#define S1 36
#define R1 13

#define S2 35
#define R2 14

#define S3 34
#define R3 27

#define FAN 16

#define SS_PIN 21
#define RST_PIN 22
#define BTN_PIN 39
#define SLN_PIN 15

MFRC522 mfrc522(SS_PIN, RST_PIN);
unsigned long uidDec, uidDecTemp;
int ARRAYindexUIDcard;
int EEPROMstartAddr;
long adminID = 1122539531;
bool beginCard = 0;
bool addCard = 1;
bool skipCard = 0;
int LockSwitch;
unsigned long CardUIDeEPROMread[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int PiezoPin = 17;




int MODE = 0;


// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "......";
char pass[] = ".....";

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = ".......";

WidgetLCD lcd(V10);

void myTimerEvent()
{
 // You can send any value at any time.
 // Please don't send more that 10 values per second.
 Blynk.virtualWrite(V0, millis() / 1000);
 
}

int reed_s = 17; // Reed sensor

int flag = 0; 

#define DHTPIN 4        
#define DHTTYPE DHT11     // DHT 11
DHT dht(DHTPIN, DHTTYPE);


void sendSensor()
{
 float h = dht.readHumidity();
 float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

 if (isnan(h) || isnan(t)) {
   Serial.println("Failed to read from DHT sensor!");
   return;
 }


 // You can send any value at any time.
 // Please don't send more that 10 values per second.
 Blynk.virtualWrite(V5, t);
 Blynk.virtualWrite(V6, h);
}




int switch_ON_Flag1_previous_I = 0;
int switch_ON_Flag2_previous_I = 0;
int switch_ON_Flag3_previous_I = 0;


BLYNK_WRITE(V12)
{
 int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
 digitalWrite(R1, pinValue);
 // process received value
}

BLYNK_WRITE(V13)
{
 int pinValue = param.asInt(); // assigning incoming value from pin V2 to a variable
 digitalWrite(R2, pinValue);
 // process received value
}

BLYNK_WRITE(V14)
{
 int pinValue = param.asInt(); // assigning incoming value from pin V3 to a variable
 digitalWrite(R3, pinValue);
 // process received value
}

BLYNK_WRITE(V15)
{
 int pinValue = param.asInt(); // assigning incoming value from pin V3 to a variable
 digitalWrite(FAN, pinValue);
 // process received value
}


void with_internet()
{
 if (digitalRead(S1) == LOW)
 {
   if (switch_ON_Flag1_previous_I == 0 )
   {
     digitalWrite(R1, LOW);
     if (DEBUG_SW) Serial.println("Relay1- ON");
     Blynk.virtualWrite(V12, 0);
     switch_ON_Flag1_previous_I = 1;
   }
   if (DEBUG_SW) Serial.println("Switch1 -ON");

 }
 if (digitalRead(S1) == HIGH )
 {
   if (switch_ON_Flag1_previous_I == 1)
   {
     digitalWrite(R1, HIGH);
     if (DEBUG_SW) Serial.println("Relay1 OFF");
     Blynk.virtualWrite(V12, 1);
     switch_ON_Flag1_previous_I = 0;
   }
   if (DEBUG_SW)Serial.println("Switch1 OFF");
 }


 if (digitalRead(S2) == LOW)
 {
   if (switch_ON_Flag2_previous_I == 0 )
   {
     digitalWrite(R2, LOW);
     if (DEBUG_SW)  Serial.println("Relay2- ON");
     Blynk.virtualWrite(V13, 0);
     switch_ON_Flag2_previous_I = 1;
   }
   if (DEBUG_SW) Serial.println("Switch2 -ON");

 }
 if (digitalRead(S2) == HIGH )
 {
   if (switch_ON_Flag2_previous_I == 1)
   {
     digitalWrite(R2, HIGH);
     if (DEBUG_SW) Serial.println("Relay2 OFF");
     Blynk.virtualWrite(V13, 1);
     switch_ON_Flag2_previous_I = 0;
   }
   if (DEBUG_SW)Serial.println("Switch2 OFF");
   //delay(200);
 }

 if (digitalRead(S3) == LOW)
 {
   if (switch_ON_Flag3_previous_I == 0 )
   {
     digitalWrite(R3, LOW);
     if (DEBUG_SW) Serial.println("Relay3- ON");
     Blynk.virtualWrite(V14, 0);
     switch_ON_Flag3_previous_I = 1;
   }
   if (DEBUG_SW) Serial.println("Switch3 -ON");

 }
 if (digitalRead(S3) == HIGH )
 {
   if (switch_ON_Flag3_previous_I == 1)
   {
     digitalWrite(R3, HIGH);
     if (DEBUG_SW) Serial.println("Relay3 OFF");
     Blynk.virtualWrite(V14, 1);
     switch_ON_Flag3_previous_I = 0;
   }
   if (DEBUG_SW)Serial.println("Switch3 OFF");
   //delay(200);
 }





}

void without_internet()
{

 digitalWrite(R1, digitalRead(S1));
 digitalWrite(R2, digitalRead(S2));
 digitalWrite(R3, digitalRead(S3));

}




void setup()
{
 // Debug console
 if (DEBUG_SW) Serial.begin(9600);
pinMode(SLN_PIN, OUTPUT); digitalWrite(SLN_PIN, LOW);
 pinMode(BTN_PIN, INPUT_PULLUP);
 pinMode(PiezoPin, OUTPUT);

SPI.begin();
 mfrc522.PCD_Init();

 
 pinMode(S1, INPUT);
 pinMode(R1, OUTPUT);

 pinMode(S2, INPUT);
 pinMode(R2, OUTPUT);

 pinMode(S3, INPUT);
 pinMode(R3, OUTPUT);
 pinMode(FAN, OUTPUT);

dht.begin();
timer.setInterval(1000L, sendSensor);
pinMode(reed_s, INPUT_PULLUP); 
 timer.setInterval(1000L,sensorvalue1);

 
 //pinMode(MODE, INPUT);
 WiFi.begin(ssid, pass);
 Blynk.config(auth);//, ssid, pass);

  lcd.clear(); //Use it to clear the LCD Widget
 EEPROM.begin(512);
 DisplayWAiT_CARD();
 EEPROMreadUIDcard();

 digitalWrite(PiezoPin, HIGH), delay(100), digitalWrite(PiezoPin, LOW);

}

void loop()
{

digitalWrite(SLN_PIN, LOW);

 if (digitalRead(BTN_PIN) == LOW) {
   digitalWrite(SLN_PIN, HIGH); //unlock
   lcd.print(0, 0, " BUTTON UNLOCK ");
   lcd.print(0, 1, "   DOOR OPEN   ");
   digitalWrite(PiezoPin, HIGH), delay(200), digitalWrite(PiezoPin, LOW);
   delay(3000);
   DisplayWAiT_CARD();
 }

 if (beginCard == 0) {
   if ( ! mfrc522.PICC_IsNewCardPresent()) {  //Look for new cards.
     Blynk.run();
     return;
   }

   if ( ! mfrc522.PICC_ReadCardSerial()) {  //Select one of the cards.
     Blynk.run();
     return;
   }
 }

 //Read "UID".
 for (byte i = 0; i < mfrc522.uid.size; i++) {
   uidDecTemp = mfrc522.uid.uidByte[i];
   uidDec = uidDec * 256 + uidDecTemp;
 }

 if (beginCard == 1 || LockSwitch > 0)EEPROMwriteUIDcard();  //uidDec == adminID

 if (LockSwitch == 0) {
   //CardUIDeEPROMread.
   for (ARRAYindexUIDcard = 0; ARRAYindexUIDcard <= 9; ARRAYindexUIDcard++) {
     if (CardUIDeEPROMread[ARRAYindexUIDcard] > 0) {
       if (CardUIDeEPROMread[ARRAYindexUIDcard] == uidDec) {
         lcd.print(0, 0, "CARD ACCESS OPEN");
         lcd.print(3, 1, uidDec);
         digitalWrite(SLN_PIN, HIGH); //unlock
         digitalWrite(PiezoPin, HIGH), delay(200), digitalWrite(PiezoPin, LOW);
         delay(3000);
         break;
       }
     }
   }

   if (ARRAYindexUIDcard == 10) {
     lcd.print(0, 0, " Card not Found ");
     lcd.print(0, 1, "                ");
     lcd.print(0, 1, "ID : ");
     lcd.print(5, 1, uidDec);
     for (int i = 0; i <= 2; i++)delay(100), digitalWrite(PiezoPin, HIGH), delay(100), digitalWrite(PiezoPin, LOW);
     digitalWrite(SLN_PIN, LOW);  //lock();
     delay(3000);
   }

   ARRAYindexUIDcard = 0;
   DisplayWAiT_CARD();
 }


 
 if (WiFi.status() != WL_CONNECTED)
 {
   if (DEBUG_SW) Serial.println("Not Connected");
 }
 else
 {
   if (DEBUG_SW) Serial.println(" Connected");
   Blynk.run();
 }

 timer.run(); // Initiates SimpleTimer
 if (MODE == 0)
   with_internet();
 else
   without_internet();
}

BLYNK_WRITE(V1) {
 int a = param.asInt();
 if (a == 1) beginCard = 1; else beginCard = 0;
}

BLYNK_WRITE(V2) {
 int a = param.asInt();
 if (a == 1) {
   skipCard = 1;
   if (EEPROMstartAddr / 5 < 10) EEPROMwriteUIDcard();
 } else {
   skipCard = 0;
 }
}

BLYNK_WRITE(V3) {
 int a = param.asInt();
 if (a == 1) {
   digitalWrite(SLN_PIN, HIGH); //unlock
   lcd.print(0, 0, " APP UNLOCK OK ");
   lcd.print(0, 1, "   DOOR OPEN   ");
   digitalWrite(PiezoPin, HIGH), delay(200), digitalWrite(PiezoPin, LOW);
   delay(3000);
   DisplayWAiT_CARD();
 } 
}


void sensorvalue1()
{

if((digitalRead(reed_s) == LOW) && ( flag == 0  ))
{
 Blynk.virtualWrite(V7,0 ); //turns off the led
 Blynk.virtualWrite(V8,255 ); // turns on the led
Blynk.notify("Door Closed!!!"); 
 flag = 1; 
}



if((digitalRead(reed_s) == HIGH) && ( flag == 1) )
{
 Blynk.virtualWrite(V7,255 ); // turns on the led
 Blynk.virtualWrite(V8,0 ); // turns off the led 
 Blynk.notify("Door Opened!!!"); 
 flag = 0; 
 
}

}

void EEPROMwriteUIDcard() {

 if (LockSwitch == 0) {
   lcd.print(0, 0, " START REC CARD ");
   lcd.print(0, 1, "PLEASE TAG CARDS");
   delay(500);
 }

 if (LockSwitch > 0) {
   if (skipCard == 1) {  //uidDec == adminID
     lcd.print(0, 0, "   SKIP RECORD   ");
     lcd.print(0, 1, "                ");
     lcd.print(0, 1, "   label : ");
     lcd.print(11, 1, EEPROMstartAddr / 5);
     EEPROMstartAddr += 5;
     skipCard = 0;
   } else {
     Serial.println("writeCard");
     EEPROM.write(EEPROMstartAddr, uidDec & 0xFF);
     EEPROM.write(EEPROMstartAddr + 1, (uidDec & 0xFF00) >> 8);
     EEPROM.write(EEPROMstartAddr + 2, (uidDec & 0xFF0000) >> 16);
     EEPROM.write(EEPROMstartAddr + 3, (uidDec & 0xFF000000) >> 24);
     EEPROM.commit();
     delay(10);
     lcd.print(0, 1, "                ");
     lcd.print(0, 0, "RECORD OK! IN   ");
     lcd.print(0, 1, "MEMORY : ");
     lcd.print(9, 1, EEPROMstartAddr / 5);
     EEPROMstartAddr += 5;
     delay(500);
   }
 }

 LockSwitch++;

 if (EEPROMstartAddr / 5 == 10) {
   lcd.clear();
   lcd.print(0, 0, "RECORD FINISH");
   delay(2000);
   EEPROMstartAddr = 0;
   uidDec = 0;
   ARRAYindexUIDcard = 0;
   EEPROMreadUIDcard();
 }
}

void EEPROMreadUIDcard() {
 for (int i = 0; i <= 9; i++) {
   byte val = EEPROM.read(EEPROMstartAddr + 3);
   CardUIDeEPROMread[ARRAYindexUIDcard] = (CardUIDeEPROMread[ARRAYindexUIDcard] << 8) | val;
   val = EEPROM.read(EEPROMstartAddr + 2);
   CardUIDeEPROMread[ARRAYindexUIDcard] = (CardUIDeEPROMread[ARRAYindexUIDcard] << 8) | val;
   val = EEPROM.read(EEPROMstartAddr + 1);
   CardUIDeEPROMread[ARRAYindexUIDcard] = (CardUIDeEPROMread[ARRAYindexUIDcard] << 8) | val;
   val = EEPROM.read(EEPROMstartAddr);
   CardUIDeEPROMread[ARRAYindexUIDcard] = (CardUIDeEPROMread[ARRAYindexUIDcard] << 8) | val;

   ARRAYindexUIDcard++;
   EEPROMstartAddr += 5;
 }

 ARRAYindexUIDcard = 0;
 EEPROMstartAddr = 0;
 uidDec = 0;
 LockSwitch = 0;
 DisplayWAiT_CARD();
}

void DisplayWAiT_CARD() {
 lcd.print(0, 0, "   ATTACH THE   ");
 lcd.print(0, 1, "      CARD      ");
}```

and its my first time here so sorry for the mess

@killerQueen please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Pete.

1 Like

Your void loop() is a disaster and it’s totally incompatible with Blynk.
Instead of the expected 2 lines of code it has around 65 lines of code, which includes delays.
Other areas of your code also include delays, which isn’t the way to code for Blynk.

You need to read this and implement it:

Pete.

First thank your for help second i divided this code into 3 part (door lock system with rfid) (relay and buttons) (some sensors) and put every one on nodemcu and it works

1 Like