BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

LCD Widget for Blynk Library for Python

rpi
python
#1

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.

WidgetLCD.py

# 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.clear()
lcd.printlcd(0, 0, "Hello, World")

Hope this helps!

1 Like

#2

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

0 Likes

#3

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

Thanks for doing that!

0 Likes

#4

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

0 Likes

#5

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.

0 Likes

#6

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 BlynkLib.py 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/PythonTest.py", line 10, in <module>
    lcd = WidgetLCD.WidgetLCD(blynk, 5)
NameError: name 'blynk' is not defined
0 Likes

#7

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.

LCD_Test.py

import BlynkLib
import WidgetLCD
import datetime

BLYNK_AUTH = 'Your Auth Code Here'

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

@blynk.VIRTUAL_WRITE(1)
def printTime(value):
    if value == '1':
        lcd.clear()
        lcd.printlcd(0, 0, datetime.datetime.now().strftime("%m/%d/%Y"))
        lcd.printlcd(0, 1, datetime.datetime.now().strftime("%I:%M:%S %p"))

if __name__ == '__main__':
    blynk.run()
0 Likes

#8

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

Works beautifly now :smiley: Thanks!

0 Likes

#9

@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 setup.py to accommodate the new file.

Hope this can be added soon.

0 Likes

#10

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:

0 Likes

#11

It works with micropython as well with a minor change:

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

    @blynk.VIRTUAL_WRITE(4)
    def display(value):
        if value[0] == '1': # added arry
            lcd.clear()
            lcd.printlcd(0, 0, str(my_ip))
            #lcd.printlcd(0, 1, datetime.datetime.now().strftime("%I:%M:%S %p"))
0 Likes