LCD Widget for Blynk Library for Python

Hi all,

I have been working on a project for a Blynk project using a Raspberry Pi Zero W and Python 3. I noticed that there is no support for the LCD Widget. I decided to take a look at the Blynk Library and created a class that can control the LCD Widget.

# Controls the LCD Widget in the Blynk App
class WidgetLCD():
    # Constructor
    # Constructor for the Blynk LCD Widget
    # Parameters:
    #     blynk - Blynk object
    #     vPin  - The Virtual pin the LCD is connected to
    def __init__(self, blynk, vPin):
        self.__blynk = blynk
        self.__vPin = vPin

    # clear()
    # Clears the LCD widget
    # Parameters: None
    def clear(self):
        self.__blynk.virtual_write(self.__vPin, 'clr')

    # printlcd(x, y, s)
    # Prints a message to the LCD
    # Parameters:
    #     x - x-position of the lcd cursor | range [0, 15]
    #     y - y-position of the lcd cursor | range [0, 1]
    #     s - messages to print to the LCD
    def printlcd(self, x, y, s):
        self.__blynk.virtual_write(self.__vPin, '\0'.join(map(str, ('p', x, y, s))))

To use it, make sure to

import BlynkLib
import WidgetLCD

BLYNK_AUTH = "Your Auth Code Here"

blynk = BlynkLib.Blynk(BLYNK_AUTH)
lcd = WidgetLCD.WidgetLCD(blynk, 0)

In the Blynk App, set the LCD Widget to Advanced Mode and attach it to Virtual Pin V0.

Then call it using

lcd.printlcd(0, 0, "Hello, World")

Hope this helps!

1 Like

Nice… I am going to try this out ASAP :smiley:

In case everything works, please ping @vshymanskyy and make a pull request.

Thanks for doing that!

Wupps… This uses Python 3 and so far I am barely getting Python 2.7 to work… oh well… later I guess.

Is there any way this could still be contributed to blynk-library-python? To install it I used pip3 install blynk-library-python instead of pip install blynk-library-python in order to make it work with python 3.

Supposedly there is mainly syntax differences between versions?? I think? But what they are I don’t know off hand…

Using Python 2.7 I just added your file into the same folder that my is in, and it seems to import without error, but I get this when I run it…

Traceback (most recent call last):
  File "/home/gunner/Blynk-Test-Py/", line 10, in <module>
    lcd = WidgetLCD.WidgetLCD(blynk, 5)
NameError: name 'blynk' is not defined

Judging by the traceback message, it looks like you didn’t create a BlynkLib.Blynk object named blynk that the WidgetLCD class needs to function. To do that, all you have to do is declare

BLYNK_AUTH = 'Your Auth Code Here'

blynk = BlynkLib.Blynk(BLYNK_AUTH)

before you declare

lcd = WidgetLCD.WidgetLCD(blynk, 0)

Of course you could always name you BlynkLib.Blynk object whatever you want (within python variable name rules). All you would have to do is change the name of the object you pass to WidgetLCD.WidgetLCD().

Here is some example code that I have got to work for both Python 2 and Python 3. This assumes a button attached to Virtual Pin V1 and an LCD in Advanced mode attached to Virtual Pin V0.

import BlynkLib
import WidgetLCD
import datetime

BLYNK_AUTH = 'Your Auth Code Here'

blynk = BlynkLib.Blynk(BLYNK_AUTH)
lcd = WidgetLCD.WidgetLCD(blynk, 0)

def printTime(value):
    if value == '1':
        lcd.printlcd(0, 0,"%m/%d/%Y"))
        lcd.printlcd(0, 1,"%I:%M:%S %p"))

if __name__ == '__main__':

Yup… I missed the obvious and added it into existing code, but out of order :blush:

Works beautifly now :smiley: Thanks!

@vshymanskyy @Gunner
I have sent a pull request to vshymanskyy/blynk-library-python that contains the code and the example. Unfortunately, I do not know how to use setuptools and could not modify to accommodate the new file.

Hope this can be added soon.

It might take awhile as the developers are busy… there is an addon for NodeJS that is still pending after months.

But meanwhile, this topic will work for those that inquire :slight_smile:

It works with micropython as well with a minor change:

#import WidgetLCD
lcd = BlynkLib.WidgetLCD(blynk, 0) #added lib name

    def display(value):
        if value[0] == '1': # added arry
            lcd.printlcd(0, 0, str(my_ip))
            #lcd.printlcd(0, 1,"%I:%M:%S %p"))