Please help in adapting Blynk legacy code to Blynk IOT

How do I convert the code so that it works with new Blynk IOT


#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <Blynk.h>
BlynkTimer Mytimer;

char auth[] = "xxxxx";

WidgetLCD lcd(V3);

int calibrationTime = 10;

long unsigned int lowIn;

long unsigned int pause = 100;

boolean lockLow = true;

boolean takeLowTime;

int pirPin = 13;  //d7 pin on wemos d1 mini

unsigned long lastMillis = 0;

#define BLYNK_GREEN     "#23C48E"
#define BLYNK_RED       "#D3435C"


void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, "xxxxxx", "xxxxx");
  Mytimer.setInterval(1000L, switchstate);
  pinMode(pirPin, INPUT_PULLUP);
  digitalWrite(pirPin, LOW);

  Blynk.virtualWrite(V1, 255);
  Blynk.setProperty(1, "color", "#23C48E");

  Serial.println("Booting");
  WiFi.mode(WIFI_STA);
  WiFi.begin("xxxxx", "xxxx");
  while (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("Connection Failed! Rebooting...");
    delay(5000);
    ESP.restart();
  }


  ArduinoOTA.setHostname("fire sensor with chirp led status");


  ArduinoOTA.onStart([]() {
    String type;
    if (ArduinoOTA.getCommand() == U_FLASH) {
      type = "sketch";
    } else { // U_FS
      type = "filesystem";
    }

    // NOTE: if updating FS this would be the place to unmount FS using FS.end()
    Serial.println("Start updating " + type);
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("\nEnd");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  });
  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) {
      Serial.println("Auth Failed");
    } else if (error == OTA_BEGIN_ERROR) {
      Serial.println("Begin Failed");
    } else if (error == OTA_CONNECT_ERROR) {
      Serial.println("Connect Failed");
    } else if (error == OTA_RECEIVE_ERROR) {
      Serial.println("Receive Failed");
    } else if (error == OTA_END_ERROR) {
      Serial.println("End Failed");
    }
  });
  ArduinoOTA.begin();
  Serial.println("Ready");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void switchstate()
{
  int pinStatus = digitalRead(pirPin);
  if (pinStatus)
  {
    if (lockLow)
      Serial.println("loop1 start");
    {
      lockLow = false;
      Serial.println("loop1 begins");
      lcd.print(0, 0, " Fire Status OK                   ");
      Serial.println("No Fire serial Detected");
      delay(50);
    }
    takeLowTime = true;
    Serial.println("loop1 end");
  }


  if (!pinStatus)
  {
    lockLow = false;
    if (takeLowTime)
    {
      lowIn = millis();
      takeLowTime = false;
    }
    if (!lockLow && millis() - lowIn > pause)
    {
      lockLow = true;
      Serial.println("loop 2 started");
      Blynk.notify(" ♨♨♨Fire Detected in the Home! ♨ ♨ ♨");
      Blynk.setProperty(1, "color", "#D3435C");  //red color
      lcd.print(0, 0, " Fire Detected! ♨ ♨ ♨ ♨ ♨ ♨ ♨ ♨ ♨ ♨");
      Serial.println("Fire Detector Chirping Started");
      Serial.println("Fireeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee Detected");

      Mytimer.setTimeout(20000L, []()
      {
        Blynk.setProperty(V1, "color", "#23C48E");
        Serial.println("Fire Detector Chirping Ended");
        lockLow = false;
      });

      Serial.println("loop 2 ends");
    }
  }

}

void loop()
{
  ArduinoOTA.handle();
  Blynk.run();
  Mytimer.run();
}

If you wnat to keep the same functionality (Arduino OTA as opposed to Blynk.Air) then you don’t need to change much.

Create a template and virtual datastreams in the Blynk IoT web console. When you create the datastreams that are being used for LEDs then set the m8n/max values to 0-255 (or use 0-1 and change your sketch where it writes 255 to the LED widget.

I don’t think you should be including this line anyway…

Once you’ve done that then you create a device from your template and that will give you three lines of code that MUST be pasted at tge very top of your sketch.

Bl6nk notify…

Isn’t used anymore. Instead you need to create an event in your template screen and add a notification to that, then use Blynk.logEvent
Your existing sketch looks messy in this respect, sending multiple notifications for the same fire event, although it’s difficult to tell as you haven’t used meaningful variable names or added in-code comments to explain the logic, and I can’t be bothered to unpick it.

Either way, you should read this…

Once you’ve done that then you’ll need to install the latest Blynk library and re-compile your code.
You’ll need to create a new mobile dashboard, and also create a web dashboard if you want to use that functionality too.

More info on all of this in the documentation…

Pete.

I’m curious – what type of sensor are using with this interesting sketch?

This is what I got so far. I have compiled & uploaded to my device.

My chip is showing online in the web console. the lcd & chirping led status is properly working on the app.

But i am not getting any mobile notification like i did previously.I have replaced the blynk notify with Blynk.logEvent

Also I noticed when i first open the app everytime, the lcd is always blank, once its triggers it show any string there. until I exit out of the application & restart the application at which is back to been blank.


#define BLYNK_TEMPLATE_ID "xxxxxxxxx"
#define BLYNK_DEVICE_NAME "Fire Detector"
#define BLYNK_AUTH_TOKEN "xxxxxx"

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
BlynkTimer timer;

char ssid[] = "xxxxx";
char pass[] = "xxxx";
char auth[] = BLYNK_AUTH_TOKEN;

WidgetLCD lcd(V3);

int calibrationTime = 10;

long unsigned int lowIn;

long unsigned int pause = 100;

boolean lockLow = true;

boolean takeLowTime;

int pirPin = 13;  //d7 pin on wemos d1 mini

unsigned long lastMillis = 0;

#define BLYNK_GREEN     "#23C48E"
#define BLYNK_RED       "#D3435C"


void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(1000L, switchstate);
  pinMode(pirPin, INPUT_PULLUP);
  digitalWrite(pirPin, LOW);

  Blynk.virtualWrite(V1, 255);
  Blynk.setProperty(1, "color", "#23C48E");

  // for Arudino OTA code start below  -------------------------------------------------

  Serial.println("Booting");
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, pass);
  while (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("Connection Failed! Rebooting...");
    delay(5000);
    ESP.restart();

  }

  ArduinoOTA.setHostname("fire sensor with chirp led status");


  ArduinoOTA.onStart([]() {
    String type;
    if (ArduinoOTA.getCommand() == U_FLASH) {
      type = "sketch";
    } else { // U_FS
      type = "filesystem";
    }

    // NOTE: if updating FS this would be the place to unmount FS using FS.end()
    Serial.println("Start updating " + type);
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("\nEnd");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  });
  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) {
      Serial.println("Auth Failed");
    } else if (error == OTA_BEGIN_ERROR) {
      Serial.println("Begin Failed");
    } else if (error == OTA_CONNECT_ERROR) {
      Serial.println("Connect Failed");
    } else if (error == OTA_RECEIVE_ERROR) {
      Serial.println("Receive Failed");
    } else if (error == OTA_END_ERROR) {
      Serial.println("End Failed");
    }
  });
  ArduinoOTA.begin();
  Serial.println("Ready");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

// Arduino OTA code end -------------------------------------------------

void switchstate()
{
  int pinStatus = digitalRead(pirPin);
  if (pinStatus)
  {
    if (lockLow)
    {
      lockLow = false;
      lcd.print(0, 0, " Fire Status OK                   ");
      delay(50);
    }
    takeLowTime = true;
  }


  if (!pinStatus)
  {
    lockLow = false;
    if (takeLowTime)
    { 
      lowIn = millis();
      takeLowTime = false;
    }
    if (!lockLow && millis() - lowIn > pause)
    {
      lockLow = true;
      Blynk.logEvent(" ♨♨♨Fire Detected in the Home! ♨ ♨ ♨");
      Blynk.setProperty(1, "color", "#D3435C");  //red color
      lcd.print(0, 0, " Fire Detected! ♨ ♨ ♨ ♨ ♨ ♨ ♨ ♨ ♨ ♨");

      timer.setTimeout(20000L, []()
      {
        Blynk.setProperty(V1, "color", "#23C48E");
        //  Serial.println("Fire Detector Chirping Ended");
        lockLow = false;
      });
    }
  }

}

void loop()
{
  ArduinoOTA.handle();
  Blynk.run();
  timer.run();
}


The right syntax is

Blynk.logEvent("event_code");

You can also add custom description

Blynk.logEvent("event_code", "custom message");

Read the documentation for more details

1 Like

I have updated the code to reflect right syntax for blynk log event but even then the in app mobile notifications are not coming.

Also the status on the status on the lcd widget on the mobile app, seems to be working but not as quite as I would like.

now atleast its not blank until the sensor is triggered, but it seems to say p 0 0 before display the string i want it to display

see screenshot below.


#define BLYNK_TEMPLATE_ID "xxx"
#define BLYNK_DEVICE_NAME "Fire Detector"
#define BLYNK_AUTH_TOKEN "xxx"

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
BlynkTimer timer;

char ssid[] = "xxxxx";
char pass[] = "xxxx";
char auth[] = BLYNK_AUTH_TOKEN;

WidgetLCD lcd(V3);

int calibrationTime = 10;

long unsigned int lowIn;

long unsigned int pause = 100;

boolean lockLow = true;

boolean takeLowTime;

int pirPin = 13;  //d7 pin on wemos d1 mini

unsigned long lastMillis = 0;

#define BLYNK_GREEN     "#23C48E"
#define BLYNK_RED       "#D3435C"


void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(1000L, switchstate);
  pinMode(pirPin, INPUT_PULLUP);
  digitalWrite(pirPin, LOW);

  Blynk.virtualWrite(V1, 255);
  Blynk.setProperty(1, "color", "#23C48E");

  // for Arudino OTA code start below

  Serial.println("Booting");
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, pass);
  while (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("Connection Failed! Rebooting...");
    delay(5000);
    ESP.restart();

  }

  ArduinoOTA.setHostname("fire sensor with chirp led status_new");


  ArduinoOTA.onStart([]() {
    String type;
    if (ArduinoOTA.getCommand() == U_FLASH) {
      type = "sketch";
    } else { // U_FS
      type = "filesystem";
    }

    // NOTE: if updating FS this would be the place to unmount FS using FS.end()
    Serial.println("Start updating " + type);
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("\nEnd");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  });
  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) {
      Serial.println("Auth Failed");
    } else if (error == OTA_BEGIN_ERROR) {
      Serial.println("Begin Failed");
    } else if (error == OTA_CONNECT_ERROR) {
      Serial.println("Connect Failed");
    } else if (error == OTA_RECEIVE_ERROR) {
      Serial.println("Receive Failed");
    } else if (error == OTA_END_ERROR) {
      Serial.println("End Failed");
    }
  });
  ArduinoOTA.begin();
  Serial.println("Ready");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

// Arduino OTA code end

void switchstate()
{
  int pinStatus = digitalRead(pirPin);
  if (pinStatus)
  {
    if (lockLow)
    {
      lockLow = false;
      lcd.clear();
      lcd.print(0,0,"Status OK                    ");
      delay(50);
    }
    takeLowTime = true;
  }


  if (!pinStatus)
  {
    lockLow = false;
    if (takeLowTime)
    { 
      lowIn = millis();
      takeLowTime = false;
    }
    if (!lockLow && millis() - lowIn > pause)
    {
      lockLow = true;
      Blynk.logEvent("SmokeSensed");
      Blynk.setProperty(1, "color", "#D3435C");  //red color
      lcd.print(0,0," Fire Detected! ♨ ♨ ♨ ♨ ");

      timer.setTimeout(20000L, []()
      {
        Blynk.setProperty(V1, "color", "#23C48E");
        //  Serial.println("Fire Detector Chirping Ended");
        lockLow = false;
      });
    }
  }

}

void loop()
{
  ArduinoOTA.handle();
  Blynk.run();
  timer.run();
}

Your earlier screenshot shows the event code as “fire_detected_in_the_home”

Have you bow edited the event, as well as your sketch, to make the event code “SmokeSensed” ?
If so, then posting an updated screenshot of the event screen would help.

Is your LCD widget in simple or advanced mode?

Pete.

Yes i did change the event name to make it simple.

Also the lcd widget is currently in simple mode. In simple it doesnt have any issues where the lcd shows blank every time app is restarted but it shows p 0 0 for some reason

If i am on advanced it display nothing until something is triggered. If the screen were to time out or app closed and re open the app OR phone screen unlocked to go to app, the lcd would be blank again until the sensor is triggered to display lol

In simple mode, your LCD widget performs as a normal widget, so there’s no need to use the advanced mode commands to send data to the LCD widget. You can simply use

Blynk.virtualWrite(V3, "custom message");

The most likely explanation for this is that you are using the advanced mode command with simple mode.

The Blynk IoT platform has not yet integrated data retention for the Terminal and LCD widgets.

Your screenshots are showing part of the options screens, but not whether you’ve selected the “send event to notifications tab” and “send event to timeline” options. Neither have you included the timeline screen.

Have you read the Notifications and flag variables link I provided regarding this?

Pete.

The Blynk virtual write solved the LCD widget status problem. I was already on normal rather than on advanced widget. Thank you.

Hi Pete,

Yes I read the FAQ you send, thats how I got to know about sent event to notification tab as well as send event to timeline needs to active for those features. I see the alert showing up on the timeline, but like legacy blynk it doesnt send push notification on android while the phone is sleep or phone screen is on or while blynk IOT is already open.

Update*************
Just Realized I had the deliver push notification as alerts option active so disabled that push notificaiton are showing up on Android.

Thanks for your help. I just got to figure out now why Arudino OTA is not working.