SunFounder ESP32 Starter Kit
1.52.4 4. Code explanation
1. Configuration & Libraries
Here, you set up the Blynk constants and credentials. You also include the necessary libraries for the ESP32 and
Blynk.
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "xxxxxxxxxxx"
#define BLYNK_TEMPLATE_NAME "Intrusion Alert System"
#define BLYNK_AUTH_TOKEN "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
2. WiFi Setup
Enter your WiFi credentials.
char ssid[] = "your_ssid";
char pass[] = "your_password";
3. PIR Sensor Configuration
Set the pin where the PIR sensor is connected and initialize the state variables.
const int sensorPin = 14;
int state = 0;
int awayHomeMode = 0;
BlynkTimer timer;
4. setup() Function
This function initializes the PIR sensor as an input, sets up serial communication, connects to WiFi, and config-
ures Blynk.
• We use timer.setInterval(1000L, myTimerEvent) to set the timer interval in setup(), here we set
to execute the myTimerEvent() function every 1000ms. You can modify the first parameter of timer.
setInterval(1000L, myTimerEvent) to change the interval between myTimerEvent executions.
void setup() {
pinMode(sensorPin, INPUT); // Set PIR sensor pin as input
Serial.begin(115200); // Start serial communication at 115200 baud␣
˓→rate for debugging
// Configure Blynk and connect to WiFi
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
timer.setInterval(1000L, myTimerEvent); // Setup a function to be called every␣
˓→second
}
5. loop() Function
236 Chapter 1. For Arduino User