Convert legacy blynk code to blynk iOT

I’m an old blynk legacy user but I don’t understand the current blynk iot format. how do I convert it?
I have code with header and footer format like this.my board code is Nodemcu with wifi manager


#include <FS.h>
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager
 
#include <ArduinoJson.h>          //https://github.com/bblanchon/ArduinoJson
 
#include <SimpleTimer.h>
#include <DHT.h>
 
char blynk_token[34] = "BLYNK_TOKEN";
//flag for saving data
bool shouldSaveConfig = false;
 
//callback notifying us of the need to save config
void saveConfigCallback () {  
  Serial.println("Should save config");
  shouldSaveConfig = true;
}
bool pirEnabled = false;
BLYNK_WRITE(V1){
  int control4DigitalPins = param.asInt();
  if(control4DigitalPins == 1){
    digitalWrite(D1, HIGH);
    digitalWrite(D2, HIGH);
    digitalWrite(D3, HIGH);
    digitalWrite(D7, HIGH);
  }
  else{
    digitalWrite(D1, LOW);
    digitalWrite(D2, LOW);
    digitalWrite(D3, LOW);
    digitalWrite(D7, LOW);
  }
}
.....................
.......................
....................
.................
dht.begin();
 
  // Setup a function to be called every second
  timer.setInterval(10000L, sendWifi);
  timer.setInterval(1000L, sendSensor);
}
void sendWifi()
{
  Blynk.virtualWrite(V2, map(WiFi.RSSI(), -105, -40, 0, 100) );
}
void loop()
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer
}

and I also took a sample program from the new blynk iot like this


// Fill-in information from your Blynk Template here
//#define BLYNK_TEMPLATE_ID           "TMPLxxxxxx"
//#define BLYNK_DEVICE_NAME           "Device"

#define BLYNK_FIRMWARE_VERSION        "0.1.0"

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG

#define APP_DEBUG

// Uncomment your board, or configure a custom board in Settings.h
//#define USE_SPARKFUN_BLYNK_BOARD
//#define USE_NODE_MCU_BOARD
//#define USE_WITTY_CLOUD_BOARD
//#define USE_WEMOS_D1_MINI

#include "BlynkEdgent.h"

void setup()
{
  Serial.begin(115200);
  delay(100);

  BlynkEdgent.begin();
}

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


how do i convert it? thank you for helping because only now i want to switch to the new blynk iot

There’s two options

One:
You can make some modifications to the blynk legacy sketch to make it work with the new blynk like adding BLYNK_TEMPLATE_ID and BLYNK_DEVICE_NAME at the top of your sketch, the first lines. For more details you can check the documentation
https://docs.blynk.io/en/blynk-1.0-and-2.0-comparison/migrate-from-1.0-to-2.0

Two:
You can use blynk edgent, which will give you the ability to use WiFi provisioning and OTA firmware update.

Using the Edgent example (your second piece of code) would make your WiFi manager code redundant, so you’d need to delete that part of your existing sketch (or most likely just copy/paste the bits of your existing sketch that you wat to keep).

In the Edgent sketch you’d need to un-comment these lines of code:

and put the values that you obtain from the template screen in Blynk IoT in there.

You’d also need to un-comment this line of code:

so that the Settings.h tab of the sketch uses the appropriate LED and Blynk reset pins. These are used during the provisioning process (the part you did previously with WiFi Manager).
Unlike WiFiManager where you used a captive portal to enter the WiFi credentials (and possibly auth token), you do this via the new Blynk app, and an auth token is assigned dynamically as part of this process.

If you don’t want to use Edgent then it’s simply a case of creating a Template and Device in the web console then pasting the snippet of code that is then generated at the top of your existing sketch (and maybe tweaking the auth token variable name).

Pete.

So like this ?
im not understand with new blynk iot format…can anyone helpme?
im used wifi manager before on blynk legacy…
should i delete it because it is no longer needed?because i want the token to be replaceable like the old blynklegacy

#define BLYNK_TEMPLATE_ID "xxxxx"
#define BLYNK_DEVICE_NAME "RUMAH UNGARAN"
#define BLYNK_AUTH_TOKEN "xxxxxxxxxxxxxxxxxxxxxxxxx"
#include <FS.h>
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager
 
#include <ArduinoJson.h>          //https://github.com/bblanchon/ArduinoJson
 
#include <SimpleTimer.h>
#include <DHT.h>
 
char blynk_token[34] = "BLYNK_TOKEN";
//flag for saving data
bool shouldSaveConfig = false;
 
//callback notifying us of the need to save config
void saveConfigCallback () {  
  Serial.println("Should save config");
  shouldSaveConfig = true;
}
bool pirEnabled = false;
BLYNK_WRITE(V1){
  int control4DigitalPins = param.asInt();
  if(control4DigitalPins == 1){
    digitalWrite(D1, HIGH);
    digitalWrite(D2, HIGH);
    digitalWrite(D3, HIGH);
    digitalWrite(D7, HIGH);
  }
  else{
    digitalWrite(D1, LOW);
    digitalWrite(D2, LOW);
    digitalWrite(D3, LOW);
    digitalWrite(D7, LOW);
  }
}
.....................
.......................
....................
.................
dht.begin();
 
  // Setup a function to be called every second
  timer.setInterval(10000L, sendWifi);
  timer.setInterval(1000L, sendSensor);
}
void sendWifi()
{
  Blynk.virtualWrite(V2, map(WiFi.RSSI(), -105, -40, 0, 100) );
}
void loop()
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer
}

and how to write footer format blynk run replace ?


Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer
}

replace to?


BlynkEdgent.run();
}

You can check the documentation for more details
https://docs.blynk.io/en/getting-started/activating-devices/blynk-edgent-wifi-provisioning

Do you mean that you want the auth token to be entered via the WiFi Manager configurations screen?
If so, that wasn’t a Blynk Legacy feature, it was a WiFi Manager custom field feature, and there’s nothing to stop you doing the same thing with WiFi Manager and Blynk IoT.

I’d suggest that you re-read what I wrote in post #3

Pete.

this is my code without using wifi manager. how do i change to new blynk iot version?
because in the code there is still a code
char auth[] = “xxxx”;
Thank you for help


#define BLYNK_TEMPLATE_ID "xxxxxx"
#define BLYNK_DEVICE_NAME "RUMAH UNGARAN"
#define BLYNK_AUTH_TOKEN "BTjAVwdSwUT2dsz_pTxxxxxxxxxx"
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>
 
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxx";
 
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "PULPSTONE";
char pass[] = "internet";
bool pirEnabled = false;
BLYNK_WRITE(V1){
  int control4DigitalPins = param.asInt();
  if(control4DigitalPins == 1){
    digitalWrite(D1, HIGH);
    digitalWrite(D2, HIGH);
    digitalWrite(D3, HIGH);
    digitalWrite(D7, HIGH);
  }
  else{
    digitalWrite(D1, LOW);
    digitalWrite(D2, LOW);
    digitalWrite(D3, LOW);
    digitalWrite(D7, LOW);
  }
}
}
 
BLYNK_WRITE(V3){  // button in switch mode
  if(param.asInt()){
    pirEnabled = true;  
  }
  else{
    pirEnabled = false;    
  }
}
 
#define DHTPIN 2          // What digital pin we're connected to
#define ledPower 16
 
// Uncomment whatever type you're using!
#define DHTTYPE DHT11     // DHT 11
//#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301
 
DHT dht(DHTPIN, DHTTYPE);
SimpleTimer timer;
 
// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{
  int h = dht.readHumidity();
  int t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
 
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  int p=digitalRead(14);
 
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
 if((p==HIGH) && (pirEnabled == true)){
 Blynk.notify("Gerakan terdeteksi");
}
}
void setup()
{
  pinMode (ledPower,OUTPUT);
  digitalWrite (ledPower, HIGH);
  pinMode(14,INPUT);
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth, ssid, pass);
 
  dht.begin();
 
  // Setup a function to be called every second
  timer.setInterval(10000L, sendWifi);
  timer.setInterval(1000L, sendSensor);
}
void sendWifi()
{
  Blynk.virtualWrite(V2, map(WiFi.RSSI(), -105, -40, 0, 100) );
}
 
void loop()
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer
}

Start by opening the Blynk Edgent ESP8266 example from the Arduino IDE.
Add your Template ID and Device name, and un-comment the NodeMCU board type in the sketch.
You’ve created a device based on that sketch - delete that device in the web console.

Read this, especially the section about defining your switch and physical LED. You’ll then understand why this line of your code will be an issue…

Copy the relevant lines of code from your sketch into the Edgent example. DO NOT copy these lines over…

Replace this line:

With:
BlynkTimer timer;

You do not need…

in your void loop, the BlynkEdgent.run line that’s already there does the same thing.

I’d also suggest that you switch from referencing your I/O pins using a mixture of GPIO and “D” numbers, and stick with GPIO numbers throughout and add a comment which tells you which D number this is to simplify wiring.

I’d also stop trying to read your DHT sensor every 1 second and move to 5 second intervals. If you need 1 second temperature/humidity endings then switch to a faster sensor. The DCT sensors extremely slow and inaccurate and aren’t worth using for most applications, but if you do use them it’s best to read them no more than once every 5 seconds.

When you’ve uploaded the sketch you provision the board with WiFi credentials and a dynamically assigned Auth token with the “+ New Device” option in the app.

Pete.

So like this?

#define BLYNK_TEMPLATE_ID "TMPLYTcvzJ-K"
#define BLYNK_DEVICE_NAME "RUMAH UNGARAN"
#define BLYNK_FIRMWARE_VERSION        "0.1.0"

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG

#define APP_DEBUG

// Uncomment your board, or configure a custom board in Settings.h
//#define USE_SPARKFUN_BLYNK_BOARD
#define USE_NODE_MCU_BOARD
//#define USE_WITTY_CLOUD_BOARD
//#define USE_WEMOS_D1_MINI

#include "BlynkEdgent.h"
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <SPI.h>
#include <DHT.h>
 
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
//char auth[] = "xxxx";
 
// Your WiFi credentials.
// Set password to "" for open networks.
//char ssid[] = "PULPSTONE";
//char pass[] = "internet";
bool pirEnabled = false;
BLYNK_WRITE(V1){
  int control4DigitalPins = param.asInt();
  if(control4DigitalPins == 1){
    digitalWrite(GPIO5, HIGH);
    digitalWrite(GPIO4, HIGH);
    digitalWrite(GPIO0, HIGH);
    digitalWrite(GPIO13, HIGH);
  }
  else{
    digitalWrite(GPIO5, LOW);
    digitalWrite(GPIO4, LOW);
    digitalWrite(GPIO0, LOW);
    digitalWrite(GPIO13, LOW);
  }
}
}
 
BLYNK_WRITE(V3){  // button in switch mode
  if(param.asInt()){
    pirEnabled = true;  
  }
  else{
    pirEnabled = false;    
  }
}
 
#define DHTPIN 2          // What digital pin we're connected to
#define ledPower 16
 
// Uncomment whatever type you're using!
#define DHTTYPE DHT11     // DHT 11
//#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301
 
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
 
// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{
  int h = dht.readHumidity();
  int t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
 
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  int p=digitalRead(14);
 
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
 if((p==HIGH) && (pirEnabled == true)){
 Blynk.notify("Gerakan terdeteksi");
}
}
void setup()
{
  pinMode (ledPower,OUTPUT);
  digitalWrite (ledPower, HIGH);
  pinMode(14,INPUT);
  Serial.begin(9600); // See the connection status in Serial Monitor
  Serial.begin(115200);
  delay(100);

  BlynkEdgent.begin();
}
dht.begin();
 
  // Setup a function to be called every second
  timer.setInterval(50000L, sendWifi);
  timer.setInterval(5000L, sendSensor);
}
void sendWifi()
{
  Blynk.virtualWrite(V2, map(WiFi.RSSI(), -105, -40, 0, 100) );
}
 
void loop() {
  BlynkEdgent.run();
}

please for guide…

If you go to the Settings.h file, you will notice that pins 0 and 2 are already in use, so using them for a different purpose may cause a conflict. I’d suggest that you check the Settings.h file and choose your pins wisily.

Blynk.notify has been replaced with Blynk.logEvent. read the documentation for more details

Only one Serial.begin is required.

You should add timer.run();

Duplicated lines of code!

Delete this, it’s misleading.

You need to remove “GPIO” and simply have the pin numbers…

    digitalWrite(5, HIGH);
    digitalWrite(4, HIGH);
    digitalWrite(0, HIGH);
    digitalWrite(13, HIGH);

But a better wat woiuld be like this:
Near the top of your code, after the #include "BlynkEdgent.h" line of code, define aliases for your pins, like you have here, where you give pin 16 the alias “ledPower”…

then use this alias when you reference the pin in your code, like you do here…

This makes the code easier to understand and debug, but also makes it easier to change which pins you are using because you only need to make one change at the top of your code rather than doing search and replace operations on the sketch.

More duplication, you only ned one of these. If you’re using a NodeMCU then a baud rate of 74880 is usually best, as this is usually the native baud rate of the board and it allows you to view both the boot messages from the board and the serial debug messages without changing baud rates in your serial monitor.
For an ESP32 a baud rate of 115200 is best.

As @John93 mentioned, this now needs to be changed so that it logs an event that has a notification attached to it. There is a limit of how many times each event type and notification can be triggered each day (100 times in a 24 hour period), and your code would lof an event each time sendSensor is called and this ‘if’ statement is true…

This could lead yo you quickly running out of events/notifications for the day. The way around this is to use a “flag” variable as described here…

But, your sendWifi and sendSensor functions aren’t being called at the moment, because you missed-out these lines in your void setup:

  timer.setInterval(10000L, sendWifi);
  timer.setInterval(1000L, sendSensor); <<< Change to at least 5000L

Pete.

Thanks Mr Pete & Mr John93…I made this revision…is it correct?


#define BLYNK_TEMPLATE_ID "XXXXXXXXX"
#define BLYNK_DEVICE_NAME "RUMAH UNGARAN"
#define BLYNK_FIRMWARE_VERSION        "0.1.0"

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG

#define APP_DEBUG

// Uncomment your board, or configure a custom board in Settings.h
//#define USE_SPARKFUN_BLYNK_BOARD
#define USE_NODE_MCU_BOARD
//#define USE_WITTY_CLOUD_BOARD
//#define USE_WEMOS_D1_MINI

#include "BlynkEdgent.h"
#define ledPower 16
#include <SPI.h>
#include <DHT.h>
 

bool pirEnabled = false;
BLYNK_WRITE(V1){
  int control4DigitalPins = param.asInt();
  if(control4DigitalPins == 1){
    digitalWrite(5, HIGH);
    digitalWrite(4, HIGH);
    digitalWrite(0, HIGH);
    digitalWrite(13, HIGH);
  }
  else{
    digitalWrite(5, LOW);
    digitalWrite(4, LOW);
    digitalWrite(0, LOW);
    digitalWrite(13, LOW);
  }
}
}
 
BLYNK_WRITE(V3){  // button in switch mode
  if(param.asInt()){
    pirEnabled = true;  
  }
  else{
    pirEnabled = false;    
  }
}
 
#define DHTPIN 2          // What digital pin we're connected to
#define ledPower 16
 
// Uncomment whatever type you're using!
#define DHTTYPE DHT11     // DHT 11
//#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301
 
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
 
// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{
  int h = dht.readHumidity();
  int t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
 
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  int p=digitalRead(14);
 
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
 if((p==HIGH) && (pirEnabled == true)){
 Blynk.logEvent("Gerakan terdeteksi");
}
}
void setup()
{
  pinMode (ledPower,OUTPUT);
  
  pinMode(14,INPUT);
  Serial.begin(74880); // See the connection status in Serial Monitor
  
  delay(100);

  BlynkEdgent.begin();
}
dht.begin();
 
  // Setup a function to be called every second
  timer.setInterval(50000L, sendWifi);
  timer.setInterval(5000L, sendSensor);
}
void sendWifi()
{
  Blynk.virtualWrite(V2, map(WiFi.RSSI(), -105, -40, 0, 100) );
}
 
void loop() {
  BlynkEdgent.run();
}
timer.run();

Thank you for your attention

timer.run should be in the void loop.

void loop() {
  BlynkEdgent.run();
  timer.run();
}

More duplicates…

and this code, which should be inside your void setup, is stranded in No Man’s Land and has a stray closing curly bracket at the end…

You’ve also ignored my advice about adding a flag variable to prevent your Blynk.logEvent code from exceeding the notifications limit.

Pete.