Implementing the Blynk protocol in Lua/NodeMCU/ESP8266

Hi,

I’m working to write a Lua library to implement the Blynk protocol directly on the ESP8266 / NodeMCU firmware.

Working

  • login
  • receive response from blynk-cloud.com
  • receive and process HARDWARE command to set virtual pins

Not working

I receive a message with type 20 (HARDWARE) id 29602 and length 4. After decoding I get vr 5 which means the ESP should read virtual pin 5 and return the value.

If I want to return a value of 512, the length of my message would be:

  • 1 byte for the command HARDWARE
  • 2 bytes for the message id
  • 2 bytes for the message length (message is vw\05\0512\0 length is 9, \0 is a embedded null)
  • 9 bytes for the message

14 bytes total.

But this doesn’t work. The Blynk app does not receive the 512 for pin 5. Can I get some pointers as to what I’m doing wrong?

Question: Should my message include the trailing \0? When the message is sent to the ESP, there are no trailing \0

Question: Should I generate a new unique message id, or return the same one from the vr 5?

Thanks for your help!

Hi. Thanks for your interest. Here are some hints:

  1. You should skip trailing \0, but it has to work anyway.
  2. Length in the header is only for the payload (data). So it should be 8 in this case.
  3. You should return same message id for all requests.
    When sending messages, do not use message id = 0, as this is reserved and invalid.
  4. Don’t forget to send periodic PING message (each 10 seconds in simplest case).

You can contact me directly to discuss any additional questions.
Cheers!

1 Like

@vshymanskyy many thanks for the hints! A hex dump showed that I was not putting \0 in my responses correctly (they were literal "\\0" strings! :scream:). That is fixed and the ESP is talking to Blynk properly! Doing some more testing to make sure I have the protocol working for Notifications and email.

I am only making the Lua library fairly flexible, so the user adds the features he wants, mainly because the ESP8266 does not have a lot of heap space. Perhaps you can comment on the API? Also, please notify anyone you know if that might be interested in testing. I plan to clean up the code over the next few days (weeks?) and publish on GitHub.


-- setup up callback functions
function set_callbacks(b)
  -- on a "virtual write" to a pin, write the value using pulse width modulation
   b:on ( 'vw', function(cmd)
             local pin = cmd[2]
             local value = tonumber(cmd[3])
             pwm.setup(pin, 500, value)
             pwm.start(pin)
   end)
end

local token = '1234567890'
-- Create a new blynk objects, setup the callbacks and connect to the blynk server
b = blynk.new ( token, 4, set_callbacks ):connect()
1 Like

I’m not very familiar with Lua API guidelines, so unfortunately can’t advice…
I can join testing in few days actually.

Thanks for your involvement. It’s a great contribution to the ESP community :pray:

@vshymanskyy

Quick question. When sending multiple messages back to blynk-cloud.com, is it necessary to wait for a RESPONSE? I’m guessing it is, when I send multiple messages at once, only one goes through.

Thanks!

@Pavel, @vshymanskyy Thanks for all the help!

Here is an initial implementation of Blynk for the ESP8266 / NodeMCU

Please post issues via GitHub Issues for the project.

1 Like

We need to promote it to ESP community now! Great job, hope you’ll get some recognition.

@blezek @blavery I have created a new Lua library, which can run on OpenWrt, regular desktop OS, and Lua NodeMCU as well.
I would be glad if you could try it and review it: https://github.com/vshymanskyy/blynk-library-lua
Thanks!

3 Likes

Good, the unofficial Blezek Lua library has been serving well, but an official Lua version should be a good thing.

Unfortunately I won’t be able to try it out for some little while, as I am away from home for a month, and I haven’t touched an 8266 for a very long while.

And then I looked today at NodeMcu website, and I see that the 8266 landscape has recently changed dramatically. There is a new run-code-from-flash mode that no longer cramps the lua code into tiniest achievable memory.

One thing I did notice: the new library is structured with some files in a (blynk) folder. I don’t recall the NodeMcu supporting folders in its simple file system, but maybe that’s been a new feature in the meantime too?

@blavery you’re right, the file system is flat, but it handles the / separator correctly.
Check out the readme! Thx