Hi, New to Blynk and haven’t found answers to what I’m looking for yet, so I’m hoping someone can help me with this.
Is it possible to use Blynk for shared control of esp32? I’ve read that we should not use the loop function, but, I need to do a few things, I need to control a number of sensors and have hardware react accordingly, I need to use NFC, etc, but I would also need Blynk’s help to simply open/close locks through motor driver. I would also want to push a notification to Blynk app if one particular sensor was activated… So basically, I want to have my code as is, but interact with Blynk occasionally, so I want to share control of the hardware between the normal code, NFC and user intervention through Blynk. Would I be able to do that? or do I have to clear out all of void loop?
The code is not ready; it’s still work in progress and trying to decide exactly what I want to do, but I wanted to see if at least, in theory, I can do what I was thinking of doing.
Having said that, here’s the current, no where near final void loop. I had to adjust it just now to get you an idea of what I’m trying to do.
Under comment // 1, I want to be able to unlock using both Blynk and NFC.
Under comment // 2, I want the code to do the work. No Blynk involvement.
Under comment // 3, I want to get a notification through Blynk - Through the app and sms (is sms an option?!)
So would I be able to maintain something like this in the loop while using Blynk in the scenarios described above?
Thank you
void loop(void) {
////// NFC ///////
boolean success;
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);
String hex_value = "";
if (success) {
Serial.println("Found a card!");
Serial.print("UID Length: "); Serial.print(uidLength, DEC); Serial.println(" bytes");
Serial.print("UID Value: ");
for (uint8_t i = 0; i < uidLength; i++)
{
Serial.print(" 0x"); Serial.print(uid[i], HEX);
hex_value += (String)uid[i];
Serial.println(hex_value);
}
Serial.println("");
Serial.println(", NFC HEX value= ");
Serial.println(hex_value);
Serial.println("");
Serial.println( hex_value);
}
else
{
Serial.println("Timed out waiting for a card");
delay(1000);
}
// Reed Sensor
reed_sensor_status = digitalRead(reed_sensor);
Serial.print("Reed Sensor status ");
Serial.print(reed_sensor_status);
Serial.println("");
// Microwave Sensor
microwave_sensor_status = digitalRead(microwave_sensor);
Serial.print("Microwave Sensor status ");
Serial.print(microwave_sensor_status);
Serial.println("");
// Ultrasonic Sensors
duration_ultrasonic_1 = ultrasonic_duration(trig_pin_1, echo_pin_1);
distance_ultrasonic_1 = duration_ultrasonic_1 / 58.00;
duration_ultrasonic_2 = ultrasonic_duration(trig_pin_2, echo_pin_2);
distance_ultrasonic_2 = duration_ultrasonic_2 / 58.00;
duration_ultrasonic_3 = ultrasonic_duration(trig_pin_3, echo_pin_3);
distance_ultrasonic_3 = duration_ultrasonic_3 / 58.00;
// Calculate the Ultrasonic distance:
Serial.print("Ultrasonic sensor 1 Distance = ");
Serial.print(distance_ultrasonic_1);
Serial.println(" cm");
Serial.print("Ultrasonic sensor 2 Distance = ");
Serial.print(distance_ultrasonic_2);
Serial.println(" cm");
Serial.print("Ultrasonic sensor 3 Distance = ");
Serial.print(distance_ultrasonic_3);
Serial.println(" cm");
//////////////// Checks
// 1
if ( (hex_value == "123456789") and reed_sensor_status == 0 and locked_flag == true)
{
activate_lock(lock_2_in1); // control motor driver - unlock
locked_flag = false;
}
// 2
else if ( (distance_ultrasonic_1 > 200 and distance_ultrasonic_2 > 200 and distance_ultrasonic_3 > 200) and reed_sensor_status == 0 and locked_flag == false) {
activate_lock(lock_2_in2); // control motor driver
locked_flag = true;
}
// 3
else if (microwave_sensor_status > 0) {
Serial.print("Motion Detected ! ");
}
}
Thanks for the link, and not spamming Blynk makes sense! The question I have now is would I be able to still have my code do it’s thing, while only sending Blynk a notification when the microwave sensor is triggered while respecting the timer? So can I have something like
So I really don’t need to get sensor readings even every second. Only when it detects motion. Can I do the above and use the timer at the same time instead of calling it through the setup function every second?
Can the timer be defined once for everything, and using the timer.run in the loop regulate everything in the loop?(I really don’t need anything to run more than once per second) or would I need multiple timer.setInterval statements in the void setup? and would that be the only way to call the sensorDataSend function and other functions?
I suppose I can put everything in a universal function outside the loop like the following - would that work?
void setup()
{
timer.setInterval(1000L, runAll); //timer will run every sec
}
void loop() {
Blynk.run();
timer.run();
}
void runAll ()
// code
sensorValue = digitalRead(microwave_sensor);
If sensorValue > 0) {
sensorDataSend()
}
}
void sensorDataSend()
{
Blynk.virtualWrite(V1, sensorValue); // sending sensor value to Blynk app
}
In general though, if I have a bunch of code, does the Blynk.run() and timer.run() seek out only Blynk components while leaving the other code alone?
I hope my questions made sense… and thanks again for your help
The Blynk library does a lot of work in the background each time it’s called, and it needs to be called as frequently as possible, which is why Blynk.run needs to be in the void loop, and the void loop kept clear of other time-consuming processes.
Ideally, timers should be used to call functions which read GPIO values and do similar tasks which would otherwise slow-down the execution of the void loop.
When it comes to checking the status of microwave sensors and reed switches, you have several options…
Poll the GPIO pins that these sensors re connected to on a regulator basis - say every 100ms - using a timer
Attach interrupts to the pins that the sensors are connected to, so that the interrupt handler only triggers when the pin changes to the alert state. This will probably require some debounce code on any contacts that don’t give a clean transition from one state to another (which is most mechanical contacts such as switches, relays etc).
When using the Blynk notify widget you have to take certain precautions. The maximum notification frequency is one message every 5 seconds. Ideally, you don’t want your phone to be spammed with dozens of messages each time an alert status is detected, so you’ll need to add some code to ensure that this doesn’t happen. Exactly how you do that will depend on your sensors and the scenario. It’s up to you to decide what defines an “all clear” status, after which and subsequent triggers will result in a new notification being sent.
Your biggest problem, from a void loop point of view, will be your NFC/RFID reader. with many devices, you have no choice but to put this code in your void loop, so you’ll just have to do some experimentation.
You’ll probably want to avoid the use of Blynk.begin, and instead manage your own WiFi connection then use Blynk.config and Blynk.connect, assuming that you want your NFC/RFID reader to continue working even when there is no connection to the Blynk server (i.e your WiFi is down or the Blynk server is unreachable).
The ESP32 dos give the possibility to use both cores of the CPU, running the “messy” code in one thread and the Blynk code in another. This might be something worth exploring if you can’t get your NFC/RFID library to play nicely with Blynk.
Blynk.begin is a blocking function. If there is no connection to WiFi or the Blynk server then code execution won’t progress beyond the Blynk.begin command. Any functionality that you want to continue working in these circumstances, including the ability to unlock doors using NFC, ought to be written using Blynk.confiig/Blynk.connect again, as Blynk.connect does not block the code execution in the same way.
It’s not a straight forward drop-in replacement. The syntax is different and as I said, you need to manage your own WiFi connection as well.
Yes, lots of examples of using a flag to prevent multiple notifications being sent. Just search the forum and you’ll find what you want.
That’s one way to do it, assuming you don’t use interrupts.
However, it depends how long the code in your runall function takes to execute. If it takes too long then you may have Blynk problems and your NFC sensors may less responsive than you’d like.