Need help converting code to Blynk IoT

hi friend…
wiring arduino mega with ESP8266 wifi module it same like old blynk?
and coding to get online also same or just litter bit difference?.
i check example code totally difference.
here my coding and it work fine with old blynk. which part need to adjust for getting online with new blynk 2.0?
device : arduino mega + esp8266


#include <SPI.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h> // include Blynk ESP8266

#define BLYNK_PRINT Serial  // Comment this out to disable prints and save space
LiquidCrystal_I2C lcd(0x27,16,2);  // 0x27 or 39 or 0x3F

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";  // awe token here
char ssid[] = "xxxxxxxxxxx"; //wifi id change here
char pass[] = "xxxxxxxxxxxxx"; // wifi password change here


// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial1  // // #19 TX1, #18 RX1  on MEGA
#define ESP8266_BAUD 9600  // make sure 9600, 115220 not stable
ESP8266 wifi(&EspSerial);

#define sensor1 2  // sensor 1 at pin 2
#define sensor2 3  // sensor 2 at pin 3
#define sensor3 4  // sensor 3 at pin 4
#define sensor4 5  // sensor 4 at pin 5
#define bz 6 // buzzer at pin 6
#define ledGreen1 7  // LED green at pin 7
#define ledYellow1 8  // LED yellow at pin 8
#define ledRed1 9  // LED red at pin9
#define ledGreen2 10  // LED green at pin 7
#define ledYellow2 11  // LED yellow at pin 8
#define ledRed2 12  // LED red at pin9

int levelup1;
int leveldown1;
int levelup2;
int leveldown2;

WidgetLED led1(V0);
WidgetLED led2(V1);

#define BLYNK_GREEN     "#23C48E"
#define BLYNK_YELLOW    "#ED9D00"
#define BLYNK_RED       "#D3435C"
BlynkTimer timer;
//BlynkTimer timer1;


void setup()
{

pinMode(sensor1, INPUT);
pinMode(sensor2, INPUT);
pinMode(sensor3, INPUT);
pinMode(sensor4, INPUT);
pinMode(bz, OUTPUT);
pinMode(ledGreen1, OUTPUT);
pinMode(ledYellow1, OUTPUT);
pinMode(ledRed1, OUTPUT);
pinMode(ledGreen2, OUTPUT);
pinMode(ledYellow2, OUTPUT);
pinMode(ledRed2, OUTPUT);

digitalWrite(bz,HIGH);
delay(100);
digitalWrite(bz,LOW);  
lcd.init();
lcd.backlight();            
lcd.begin(16,2);
Serial.begin(9600);
lcd.setCursor(0,0);
lcd.print("MEDICAL LIQUID");
lcd.setCursor(0,1);
lcd.print("LEVEL MONITORING"); 
 delay(5000);
digitalWrite(bz,HIGH);
delay(100);
digitalWrite(bz,LOW);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("PLS WAIT...");
lcd.setCursor(0,1);
lcd.print("SETTING UP..");
EspSerial.begin(ESP8266_BAUD);  // Set ESP8266 baud rate
delay(10);
Blynk.begin(auth, wifi, ssid, pass);
timer.setInterval(500L, sensor ); // every 500ms sent this data
// timer1.setInterval(1000L, blinkLed); // every 10sec sent this data
delay(100);
lcd.clear();  
digitalWrite(bz,HIGH);
delay(100);
digitalWrite(bz,LOW);
led1.on();  // turn on virtual LED1
led2.on();  // turn on virtual LED2
}

void loop()
{
levelup1 = digitalRead(sensor1);
leveldown1 = digitalRead(sensor2);
levelup2 = digitalRead(sensor3);
leveldown2 = digitalRead(sensor4);
Blynk.run();
timer.run();

}
void sensor()
{
 

if(levelup1==HIGH && leveldown1==HIGH) // if water full > 75%
{
led1.setColor(BLYNK_GREEN); // set led color green
Blynk.virtualWrite(V2,100);
  lcd.setCursor(0,0);
  lcd.print("KATIL 1:");
  lcd.setCursor(8,0);
 lcd.print(">75%"); 
 digitalWrite(ledGreen1,HIGH);
 digitalWrite(ledYellow1,LOW);
 digitalWrite(ledRed1,LOW);
 digitalWrite(bz,LOW);
}
if(levelup1==LOW && leveldown1==HIGH) // if water half 25 - 75%
{
 led1.setColor(BLYNK_YELLOW); // set led color yellow
 Blynk.virtualWrite(V2,50);
 lcd.setCursor(0,0);
  lcd.print("KATIL 1:");
  lcd.setCursor(8,0);
 lcd.print("*50%"); 
 digitalWrite(ledGreen1,LOW);
 digitalWrite(ledYellow1,HIGH);
 digitalWrite(ledRed1,LOW);
 digitalWrite(bz,LOW);
}  
if(levelup1==LOW && leveldown1==LOW) // if water half <25%
{
 led1.setColor(BLYNK_RED);// set led color red
 Blynk.virtualWrite(V2,25); 
 lcd.setCursor(0,0);
  lcd.print("KATIL 1:");
  lcd.setCursor(8,0);
 lcd.print("<25%");
 digitalWrite(ledGreen1,LOW);
 digitalWrite(ledYellow1,LOW);
 digitalWrite(ledRed1,HIGH);
 digitalWrite(bz,HIGH);
 delay(50);
 digitalWrite(bz,LOW); 
 delay(1000);
 Blynk.notify("PARAS AIR KATIL 1 KURANG DARI 20%");  // sent notification to hp 
}  
// sensor botol 2
if(levelup2==HIGH && leveldown2==HIGH) // if water full > 75%
{
led2.setColor(BLYNK_GREEN); // set led color green
Blynk.virtualWrite(V3,100);
  lcd.setCursor(0,1);
  lcd.print("KATIL 2:");
  lcd.setCursor(8,1);
 lcd.print(">75%"); 
 digitalWrite(ledGreen2,HIGH);
 digitalWrite(ledYellow2,LOW);
 digitalWrite(ledRed2,LOW);
 digitalWrite(bz,LOW);
}
if(levelup2==LOW && leveldown2==HIGH) // if water half 25 - 75%
{
 led2.setColor(BLYNK_YELLOW); // set led color yellow
 Blynk.virtualWrite(V3,50);
 lcd.setCursor(0,1);
  lcd.print("KATIL 2:");
  lcd.setCursor(8,1);
 lcd.print("*50%"); 
 digitalWrite(ledGreen2,LOW);
 digitalWrite(ledYellow2,HIGH);
 digitalWrite(ledRed2,LOW);
 digitalWrite(bz,LOW);
}  
if(levelup2==LOW && leveldown2==LOW) // if water half <25%
{
 led2.setColor(BLYNK_RED);// set led color red
 Blynk.virtualWrite(V3,25); 
 lcd.setCursor(0,1);
  lcd.print("KATIL 2:");
  lcd.setCursor(8,1);
 lcd.print("<25%");
 digitalWrite(ledGreen2,LOW);
 digitalWrite(ledYellow2,LOW);
 digitalWrite(ledRed2,HIGH);
 digitalWrite(bz,HIGH);
 delay(50);
 digitalWrite(bz,LOW); 
 delay(1000);
 Blynk.notify("PARAS AIR KATIL 2 KURANG DARI 20%");  // sent notification to hp 
}  
else 
{

} 

}

@awegadget I’ve moved your post to a new topic, as it was unrelated to the topic that you posted in. Please don’t try to hijack other people’s topics in this way.

Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Pete.

Thanks and sorry…hope someone can help.

Hey there,

Check this
https://docs.blynk.io/en/blynk-1.0-and-2.0-comparison/migrate-from-1.0-to-2.0

And this
https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

1 Like

Ok thanks…let me check and try…:+1:t2:

1 Like

I still get stuck😰…try find example not found arduino mega + esp8266 with new blynk…almost use nodemcu board huhu

1 Like

Thanks john👍🏻

1 Like

You’re welcome buddy :+1:

Will the wifi provisioning work with this example sketch ?
I know OTA and few stuff will not work. So i am wondering how about provisioning!

https://docs.blynk.io/en/blynk.edgent/supported-boards

No no… my question was just by adding

This is the easiest way to migrate legacy Blynk projects. Just add BLYNK_TEMPLATE_ID and BLYNK_DEVICE_NAME on top of your existing firmware (before any Blynk includes), set new Auth Token and that’s it.

Will the sketch be able to give wifi provisioning capabilities?

I am using the edgent example that is already in the IDE… and those has the required header files to get all the features.

Adding the Template ID etc to the top of an existing sketch simply tells the 1.x.x library to use the Blynk IoT servers rather than the Legacy servers. Presumably the Template ID also does some other stuff in the background, but it certainly doesn’t give WiFi provisioning.

And, WiFi provisioning doesn’t work with the Arduino Uno/Nano/Mega boards + ESP01 anyway.

Pete.

hi all friend thanks for helping… now i able get online now.
just minor edit on my coding.
just add in BLYNK TEMPLATE ID , BLYNK DEVICE NAME & BLYNK AUTH TOKEN
i just make simple led on/off coding and add all info above and it work…
device : arduino mega + esp8266
here coding:

/* Fill-in your Template ID (only if using Blynk.Cloud) */
// device info at blynk.console.
#define BLYNK_TEMPLATE_ID "xxxxx"
#define BLYNK_DEVICE_NAME "LED ON OFF"
#define BLYNK_AUTH_TOKEN "xxxxx"

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

//#include <SPI.h>
//#include <Ethernet.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h> // include Blynk ESP8266

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "xxx"; //wifi id change here
char pass[] = "xxxxxx"; // wifi password change here


// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial1  // // #19 TX1, #18 RX1  on MEGA
#define ESP8266_BAUD 9600  // make sure 9600, 115220 not stable
ESP8266 wifi(&EspSerial);
#define LEDPIN 12          // What digital pin we're connected to
#define sw1 7
int suislampu = 0; 
//BlynkTimer timer;
WidgetLED ledlamp(V1);
// 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 setup()
{
  // Debug console
  Serial.begin(9600);
pinMode(LEDPIN, OUTPUT);
pinMode(sw1, INPUT_PULLUP);
  EspSerial.begin(ESP8266_BAUD);  // Set ESP8266 baud rate
  delay(10);
 Blynk.begin(auth, wifi, ssid, pass);

 
}

void loop()
{
  Blynk.run();
//  timer.run();
suislampu = digitalRead(sw1);
if (suislampu == LOW )
{
  digitalWrite(LEDPIN,HIGH);
  ledlamp.on();
  delay(3000);
  digitalWrite(LEDPIN,LOW);
}
if (suislampu == HIGH )
{
  digitalWrite(LEDPIN,LOW);
  ledlamp.off(); 
}
}
BLYNK_WRITE(V0)  // blynk temp setpoint sw

{
  int swStatus2 = param.asInt();
  if (swStatus2 == 1) {
      digitalWrite(LEDPIN, HIGH);
      ledlamp.on();  
 delay(3000);
   }
  else {
    digitalWrite(LEDPIN, LOW);
     ledlamp.off(); 
  }
}

You clearly didn’t read the “Keep your void loop clean” document though!

Pete.

hahaha… yes i just rush do testing… just to get arduino and blyk app respond only. :grinning:

So to get the full potential of the BlynkIoT we need to start with edgent example with all the header files right ? I mean to have all the functionalities.

Yes, along with Edgent supported hardware.
This topic is about a Mega + ESP01, which isn’t supported by Edgent.

Pete.

1 Like