SunFounder ESP32 Starter Kit
The loop function continuously runs Blynk and the Blynk timer functions.
void loop() {
Blynk.run();
timer.run();
}
6. Blynk App Interaction
These functions are called when the device connects to Blynk and when there’s a change in the state of the virtual
pin V0 on the Blynk app.
• Every time the device connects to the Blynk server, or reconnects due to poor network conditions, the
BLYNK_CONNECTED() function is called. The Blynk.syncVirtual() command request a single Virtual
Pin value. The specified Virtual Pin will perform BLYNK_WRITE() call.
• Whenever the value of a virtual pin on the BLYNK server changes, it will trigger BLYNK_WRITE().
// This function is called every time the device is connected to the Blynk.Cloud
BLYNK_CONNECTED() {
Blynk.syncVirtual(V0);
}
// This function is called every time the Virtual Pin 0 state changes
BLYNK_WRITE(V0) {
awayHomeMode = param.asInt();
// additional logic
}
7. Data Handling
Every second, the myTimerEvent() function calls sendData(). If the away mode is enabled on Blynk, it
checks the PIR sensor and sends a notification to Blynk if motion is detected.
• We use Blynk.virtualWrite(V1, "Somebody in your house! Please check!"); to change the
text of a label.
• Use Blynk.logEvent("intrusion_detected"); to log event to Blynk.
void myTimerEvent() {
sendData();
}
void sendData() {
if (awayHomeMode == 1) {
state = digitalRead(sensorPin); // Read the state of the PIR sensor
Serial.print("state:");
Serial.println(state);
// If the sensor detects movement, send an alert to the Blynk app
if (state == HIGH) {
Serial.println("Somebody here!");
Blynk.virtualWrite(V1, "Somebody in your house! Please check!");
Blynk.logEvent("intrusion_detected");
}
(continues on next page)
1.52. 8.9 Blynk-based Intrusion Notification System 237