Dimmer with ESP32 – Blynk IoT Platform. Arduino code, settings, Web and mobile APP.

Blynk IoT  is a full suite of software required to prototype, deploy, and remotely manage connected electronic devices at any scale: from personal IoT projects to millions of commercially connected products.

With Blynk anyone can connect their hardware to the cloud and build a no-code iOS, Android, and web applications to analyze real-time and historical data coming from devices, control them remotely from anywhere in the world, receive important notifications, and much more…

We’ve prepared a quick and easy way to connect your Dimmer project. Whether you are using Blynk.Console or Blynk.Apps, on your first login, it will start automatically. We highly recommend to follow these guides to get the first results and move forward.

Quickstart

Code Overview

#define BLYNK_PRINT Serial

/* Fill-in your Template ID (only if using Blynk.Cloud) */
#define BLYNK_TEMPLATE_ID "MyTemplateID"
#define BLYNK_DEVICE_NAME "MyDeviceName"
#define BLYNK_AUTH_TOKEN "MyAuthToken"
#define APP_DEBUG

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

#include <RBDdimmer.h>   //Dimmer connection to microcontroller and Arduino library. https://rocketcontroller.com/dimmer-connection-to-microcontroller-and-arduino-library-examples/

static int gpio_dimmer = 23;
static int gpio_zerocross = 5; // seting of pins

dimmerLamp dimmer(gpio_dimmer, gpio_zerocross);

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


BLYNK_WRITE(V0) // Executes when the value of virtual pin changes
{
  int value = param.asInt();
  Serial.print("DIMMING = ");
  Serial.println(value);
  dimmer.setPower(value);   // DIMMING
}

void setup()
{
  // Debug console
  Serial.begin(115200);
 dimmer.begin(NORMAL_MODE, ON);
  
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
}

void loop()
{
  Blynk.run();
}

Definitions

In the beginning, we define three main parameters.

#define BLYNK_TEMPLATE_ID “MyTemplateID”

#define BLYNK_DEVICE_NAME “MyDeviceName”

#define BLYNK_AUTH_TOKEN “MyAuthToken”

You already know what a TemplateID is. Device Name can be any.

WiFi Credentials

char ssid[] = "YourNetworkName"; 
char pass[] = “YourPassword”;

You will only see these lines when you work with a WiFi-enabled device. Replace it with your home or office WiFi network name and password.

If your network doesn’t have a password – leave it empty: char pass[] = “”

Dimming

The code below listens for actions from Blynk to V0 Datastream, and then records the value to a variable. We use BLYNK_WRITE(V0) for that.

BLYNK_WRITE(V0) 
{
  int value = param.asInt();
  Serial.print("DIMMING = ");
  Serial.println(value);
  dimmer.setPower(value);   // DIMMING
}

Set Up Datastreams

Add Datastream

For the goal of this tutorial skip the Metadata Tab and go directly to the Datastreams tab.

Datastreams are channels that are used to send data between the device and Blynk.Cloud. We will be using this Datastream to send random values from your device.

Click on Add Datastream button. Choose Virtual Pin in the dropdown menu:

Set up the Datastream like this:

  • Virtual Pin: V0
  • Data Type: Integer
  • Min/Max: 0/100

Skip all the other settings. When done, press Create and the new Datastream will be created.

These settings mean that all the devices that inherit this Template will process integers in the range of 0 to 100 through a Virtual Pin V0.

For setup Web Dashboard and Mobile App follow this instruction. https://docs.blynk.io/en/getting-started/template-quick-setup/set-up-web-dashboard