I am trying to refactor the quickstart guide into a separate class with a header file and cpp.
My goal is to simplify my main.cpp so that the logic for the Blynk interactions is hidden in a separate header and implementation file.
Please see the below code:
// BlynkCredentials.h
#define BLYNK_TEMPLATE_ID "myTemplate"
#define BLYNK_TEMPLATE_NAME "Quickstart Template"
#define BLYNK_AUTH_TOKEN "myToken"
//BlynkHandler.h
#ifndef BLYNK_HANDLER_H
#define BLYNK_HANDLER_H
#include "BlynkCredentials.h"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h> // Include the Blynk ESP32 library
class BlynkHandler
{
public:
BlynkHandler(const char* ssid, const char* pass, const char* authToken);
void begin();
void run();
void sendUptime();
private:
BlynkTimer timer;
const char* ssid;
const char* pass;
const char* authToken;
void setupTimer();
};
#endif // BLYNK_HANDLER_H
// BlynkHandler.cpp
#include "BlynkHandler.h"
// Constructor initializes WiFi credentials and auth token
BlynkHandler::BlynkHandler(const char* ssid, const char* pass, const char* authToken)
: ssid(ssid), pass(pass), authToken(authToken)
{
}
void BlynkHandler::begin()
{
Serial.begin(115200);
Blynk.begin(authToken, ssid, pass); // Use Blynk instance provided by the library
setupTimer(); // Setup timer event
}
void BlynkHandler::run()
{
Blynk.run(); // Use Blynk instance provided by the library
timer.run();
}
void BlynkHandler::sendUptime()
{
Blynk.virtualWrite(V2, millis() / 1000);
}
void BlynkHandler::setupTimer()
{
// Call sendUptime every second
timer.setInterval(1000L, [this]() { this->sendUptime(); });
}
// Define the BLYNK_WRITE and BLYNK_CONNECTED functions for handling Blynk virtual pin events
BLYNK_WRITE(V0)
{
int value = param.asInt();
Blynk.virtualWrite(V1, value);
}
BLYNK_CONNECTED()
{
Blynk.setProperty(V3, "offImageUrl", "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations.png");
Blynk.setProperty(V3, "onImageUrl", "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations_pressed.png");
Blynk.setProperty(V3, "url", "https://docs.blynk.io/en/getting-started/what-do-i-need-to-blynk/how-quickstart-device-was-made");
}
And this is how my main.cpp should look
// main.cpp
#include "BlynkCredentials.h"
#include <Arduino.h>
#include "BlynkHandler.h"
// WiFi credentials
const char* ssid = "user1tech-1stFloor";
const char* pass = "user1982";
// Instantiate BlynkHandler with ssid, password, and authToken
BlynkHandler blynkHandler(ssid, pass, BLYNK_AUTH_TOKEN);
void setup()
{
blynkHandler.begin();
}
void loop()
{
blynkHandler.run();
}
Not sure but I am encountering this error of multiple Blynk definitions
c:/users/user1/.platformio/packages/toolchain-xtensa-esp32/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: .pio\build\esp32dev\src\main.cpp.o:C:\git-repo\smart-circuit-breaker/.pio/libdeps/esp32dev/Blynk/src/BlynkSimpleEsp32.h:98: multiple definition of `Blynk'; .pio\build\esp32dev\src\BlynkHandler.cpp.o:C:\git-repo\smart-circuit-breaker/.pio/libdeps/esp32dev/Blynk/src/BlynkSimpleEsp32.h:98: first defined here
Any thoughts what is wrong?