Starting with Blynk, correct structure

Hi,

Just created some project using Blynk, but I would like to ask if the used structure is right. I’m using a Lolin D1 mini, running on battery power and sleeping between the measuring points for 60 minutes.

[Unformatted code removed by moderator]

My questions:

  1. comments on used structure?
  2. should I use Blynk.begin() or Blynk.connect() and why?
  3. shoul I use Blynk.run() or Blynk.connect() and why?
  4. is the possition of Blynk.run() right, or should it be located after the writes?
  5. do I need to use the syncVirtual(), or is that covered by the BLYNK_WRITE( V7 )?

Hope to get some answers from you. Thanks!

@Mullemj 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.

Pete, you crack me up. If Zelenskyy posted that he had found a solution to the Ukrainian invasion…you would tell him to put backticks in the post before reading.

I do understand what you are trying to do.

1 Like

first of all you should read this

Should be in the void setup.

Should be in the void loop.

Check this

You should add a triple backticks before and after your sketch or else it will be deleted, the sketch should be correctly formatted.
also, it will be more useful if you post the sketch you’re using.

@ScottB I use the triple backticks thing as a basic test, it tells me a lot :wink:

Pete.

@Mullemj I’ve removed your code because although you edited your post, you didn’t use the correct triple backtick characters, despite me giving you some to copy/paste, and despite there being an example of how to correctly format code in the text that appeared when you created this topic.

Blynk.begin is a blocking function, and if your device fails to connect to either WiFi or the Blynk server it will never get to the deepsleep command, and there flatten your battery.
As a result, you should manage your own WiFi connection, then use Blynk.config and Blynk.begin when using deepsleep.

Blynk requires the Blynk.run command to be executed in order for the Blynk magic to work.
If you use Blynk.config then Blynk.connect will be performed the first time that a Blynk.run is encountered. You could therefore argue that Blynk.connect is redundant, but I like to use it anyway - it makes the program logic easier to follow.

Blynk.syncVirtual, located inside a BLYNK_CONNECTED function will cause the corresponding BLYNK_WRITE function to trigger once the device wakes up and has established a connection to the Blynk server.
Without this in place, the BLYNK_WRITE function will only trigger if you happen to change the value of the corresponding widget at the exact time that the device wakes up.

As far as program structure is concerned, you have to throw all of the normal Blynk coding rules out of the window. But, it depends on what you want to achieve with your project.

The connection to Blynk takes a while, and if you put a Blynk.syncVirtual immediately after Blynk.connect in your void loop then the sync won’t work - which is why you put it in the BLYNK_CONNECTED function.

If you want to check the state of one widget then branch your program depending on what that widget value is, then you could potentially put virtually all of your code in the BLYNK_WRITE function. Or, you could set a global flag variable within your BLYNK_WRITE and do your code branching within your void loop.
I’d always recommend that you avoid the use of BlynkTimer in deep sleep sketches.

Also, it’s a good idea to use a static IP address, as most routers give a quicker connection this way (provided you set-up a static device in your router) but it depends how critical it is to minimise your wake time.
If wake time is really critical then you’re better using HTTP(S) API calls to Blynk rather than going through the Blynk connect process.

Assuming that you don’t go down the API route, this topic shows a good foundation sketch…

The examples in that topic are for Blynk Legacy, but if you’re using Blynk IoT then it’s fairy easy to modify the sketch.

This is an example of how…

but I’d suggest starting with the Beehive Connected version of the code and making the necessary changes, as you’ll be starting from a better place with that code.

Pete.

Hi John,

Thanks for your feedback. Perhaps I’m a nitwit, but how do I add the triple backticks to format the source?

Joop

Hi John,

Thanks for your tips. The sketch works like a charm now. I can read the temperature and switch things on and off.
What I’m curious about, how much traffic does the blynk.run() statement in the loop() when I do not a write action?

Joop

1 Like

Like this
triple backticks
Triple backticks look like this ( ``` )

Ah, that’s clear now! I tried using dots…

Both backticks ( ``` ) or tildes ( ~~~ ) works fine.

You should read this:

But, as your device will be asleep most of the time I don’t think that traffic will be much of an issue, especially as far as the Heartbeat settings are concerned.

Pete.