Blynk with Python stops loop (while loop)

Hello!
I am trying to use BlynkLib with my Raspberry Pi 3 v1.2 which is running Raspbian (latest version).

My problem is, i am also trying to run SSD1306 128x64 oled screen with luma.oled library at the same time.
I can run the screen and display complex things (3 pages, and a real-time real-looking clock) on the screen, but when i inject the blynk code to my project… blynk stops my code and display just freezes. Blynk works fine, i can do whatever i want with blynk but my whole code stops working. Note this: without blynk code, my display works fine. My code: (i know theres missing functions and stuff, if i cant solve the issue, i will post full code.)


import smbus
import time
import Adafruit_SSD1306
import urllib.request, json 
import Adafruit_GPIO.SPI as SPI
import Adafruit_SSD1306
import math
import datetime
#from guizero import App, TextBox, PushButton, Text
from demo_opts import get_device
from luma.core.render import canvas
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
from subprocess import check_output
import BlynkLib

BLYNK_AUTH = 'xxxxxxxxxxxxxxxxxxxxx'
blynk = BlynkLib.Blynk(BLYNK_AUTH)

@blynk.VIRTUAL_READ(2)
def my_read_handler():
    # this widget will show some time in seconds..
    blynk.virtual_write(2, time.ticks_ms() // 1000)

RST = 24
disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST)
disp.begin()
disp.clear()
disp.display()
width = disp.width
height = disp.height
image = Image.new('1', (width, height))
draw = ImageDraw.Draw(image)

def main():
    blynk.run() #blynk runs first, i got no write or read functions in loop because display is not working currently.
    today_last_time = "Unknown"
    counter = 5
    mode = 0
    startTime = time.time()
    ipAddr = check_output(["hostname", "-I"])
    while True: #main loop
        if time.time() - startTime > 1:
            startTime = time.time()
            counter = counter - 1
            if counter <= 0:
                mode += 1
                counter += 5
                if mode >= 4:
                    mode = 1
        if mode == 0: #the page 0, which shows the ip of my rpi on the ssd1306 128x64 oled
            with canvas(device) as draw:
                ipAddr = check_output(["hostname", "-I"])
                draw.text((0,0), str(ipAddr), font=fontTempHumd, fill=255)
        elif mode == 1:  #now it displays a clock on screen. also displays date, hour and such stuff.
            now = datetime.datetime.now()
            today_date = now.strftime("%d %b %y")
            today_time = now.strftime("%H:%M:%S")
            if today_time != today_last_time:
                today_last_time = today_time
                with canvas(device) as draw:
                    now = datetime.datetime.now()
                    today_date = now.strftime("%d %b %y")
                    margin = 4
                    cx = 30
                    cy = min(device.height, 64) / 2
                    left = cx - cy
                    right = cx + cy
                    hrs_angle = 270 + (30 * (now.hour + (now.minute / 60.0)))
                    hrs = posn(hrs_angle, cy - margin - 7)
                    min_angle = 270 + (6 * now.minute)
                    mins = posn(min_angle, cy - margin - 2)
                    sec_angle = 270 + (6 * now.second)
                    secs = posn(sec_angle, cy - margin - 2)
                    draw.ellipse((left + margin, margin, right - margin, min(device.height, 64) - margin), outline="white")
                    draw.line((cx, cy, cx + hrs[0], cy + hrs[1]), fill="white")
                    draw.line((cx, cy, cx + mins[0], cy + mins[1]), fill="white")
                    draw.line((cx, cy, cx + secs[0], cy + secs[1]), fill="red")
                    draw.ellipse((cx - 2, cy - 2, cx + 2, cy + 2), fill="white", outline="white")
                    draw.text((65,25), getDate()['tarih'], fill=255)
                    draw.text((63,1), getDate()['saat'] + ":" + getDate()['dakika'], font=fontTimeSaat, fill=255)
                    draw.text((57,38), "'", font=fontWeather, fill=255)
                    draw.text((70,40), str(format(getTempSHT()['1'], '.1f')) + "C" + "  " + str(format(getTempSHT()['2'], '.0f')) + "%", font=fontTempHumd, fill=255)
        elif mode == 2: #now, lets display weather of my state.
            with canvas(device) as draw:
               if getWeather()['1'] == "Clear":
                   weathertranslate = "Az bulutlu"
                   fontCode = "5"
               elif getWeather()['1'] == "Partly Cloudy":
                   weathertranslate = "Parçalı bulutlu"
                   fontCode = "%"
               elif getWeather()['1'] == "Thunderstorm":
                   weathertranslate = "Sağanak Yağmur" #this is turkish, lol
                   fontCode = "8"
               elif getWeather()['1'] == "Mostly Cloudy":
                   weathertranslate = "Çoğunlukla bulutlu"
                   fontCode = "%"
               elif getWeather()['1'] == "Sunny":
                   weathertranslate = "Güneşli"
                   fontCode = "3"
               elif getWeather()['1'] == "Fair":
                   weathertranslate = "Açık"
                   fontCode = "1"
               elif getWeather()['1'] == "Rain":
                   weathertranslate = "Yağmurlu"
                   fontCode = "8"
               else:
                   weathertranslate = getWeather()['1']
                   fontCode = ")"
                
    
               draw.text((0,0), fontCode, font=fontWeather, fill=255)
               draw.text((23,2), weathertranslate, font=fontWeatherTranslate, fill=255)
               draw.text((0,20), "'", font=fontWeather, fill=255)
               draw.text((23,22), str(getWeather()['2']), font=fontWeatherTranslate, fill=255)
               draw.text((40,16), "*", font=fontWeatherLogos, fill=255)
               draw.text((65,22), " " + str(getWeather()['3']), font=fontWeatherTranslate, fill=255)
               draw.text((-3,35), "G", font=fontWeatherLogos, fill=255)
               draw.text((23,42), str(getWeather()['4']) + " mb", font=fontWeatherTranslate, fill=255)      
        elif mode == 3:
            with canvas(device) as draw:
                if float(getDoviz()['dolar_change_rate']) >= 0:
                    changeDirection_dolar = "c"
                else:
                    changeDirection_dolar = "g"
                 
                if float(getDoviz()['euro_change_rate']) >= 0:
                    changeDirection_euro = "c"
                else:
                    changeDirection_euro = "g"
                    
                if float(getDoviz()['sterlin_change_rate']) >= 0:
                    changeDirection_sterlin = "c"
                else:
                    changeDirection_sterlin = "g"
                 
                 
                draw.text((7,0), "$ "+ str(format(getDoviz()['dolar'], '.2f')), font=fontDoviz, fill=255)
                draw.text((7,20), "€ "+ str(format(getDoviz()['euro'], '.2f')), font=fontDoviz, fill=255)
                draw.text((7,40), "£ "+ str(format(getDoviz()['sterlin'], '.2f')), font=fontDoviz, fill=255)
                draw.text((56,0), changeDirection_dolar, font=fontUpDown, fill=255)
                draw.text((56,20), changeDirection_euro, font=fontUpDown, fill=255)
                draw.text((56,40), changeDirection_sterlin, font=fontUpDown, fill=255)
                draw.text((74,-2), "%" + format(getDoviz()['dolar_change_rate']), font=fontDoviz, fill=255)
                draw.text((74,18), "%" + format(getDoviz()['euro_change_rate']), font=fontDoviz, fill=255)
                draw.text((74,38), "%" + format(getDoviz()['sterlin_change_rate']), font=fontDoviz, fill=255)
                

if __name__ == "__main__":
    try:
        device = get_device()
        main()
    except KeyboardInterrupt:
        pass
 

i did changed blynk.run position to here:

if name == “main”:
try:
device = get_device()
main()
blynk.run()
except KeyboardInterrupt:
pass

now, display works just fine! blynk looks fine, and everthing looks fine. But blynk does not connecting to its server. Or, its not trying to connect to the server. This is the debug output:

%Run clockitself.py


/ _ )/ /_ _____ / /__
/ _ / / // / _ / '/
/
//_, /////_
/
__/

Give Blynk a Github star! => GitHub - vshymanskyy/blynk-library-python: Blynk library for Python. Works with Python 2, Python 3, MicroPython.

Version: luma.oled 2.5.0 (luma.core 1.8.0)
Display: ssd1306
Interface: i2c
Dimensions: 128 x 64

But before (the blynk position is after def main() ) the output is like this: (which is blynk can connect to server but display is not displaying anything)

%Run clockitself.py


/ _ )/ /_ _____ / /__
/ _ / / // / _ / '/
/
//_, /////_
/
__/

Give Blynk a Github star! => GitHub - vshymanskyy/blynk-library-python: Blynk library for Python. Works with Python 2, Python 3, MicroPython.

Version: luma.oled 2.5.0 (luma.core 1.8.0)
Display: ssd1306
Interface: i2c
Dimensions: 128 x 64

TCP: Connecting to blynk-cloud.com:80
Blynk connection successful, authenticating…
Access granted, happy Blynking!

problem is not solved yet, i am trying to solve it for days. If i cant solve it more than 1 or 2 days i will publish full code so everyone can access to the code and try to find a solution.

I was successful in getting Blynk and another real-time process to work together (in one script) by using threads. Google threads for Python and read/experiment as I did.

Sorry I do not have the code to show… my RPi is presently being used as a 24/7 camera server.

Yeah! thats right, rpi has quad-core cpu in it. I will research for multi-threading.

Hi, i tried multi-threading. I injected threading code inside my code, everything looks fine, even blynk connects its server and display shows what i want. But theres while loop, everything updates. With threading that while loop is now broken and my whole code is not working. What do you mean ‘broken’ i mean, the while loop is not working, my code works ONCE like c language’s void setup().

This is what i injected to my code:

blynkStartupThread = threading.Thread(name=‘blynk_thrd’, target=blynkStartup)
mainThread = threading.Thread(name=‘main_thrd’, target=main)

device = get_device()
blynkStartupThread.start()
mainThread.start()

Not too many other users of Blynk and Python… and I was only puttering around anyhow… but without my RPi available I am unable to further experiment, so hopefully someone else will be able to assist.

I know, arduino with blynk is super easy but not everyone likes easy things. I want people use to RPi with blynk on the future. I actually got more 2 raspberries in my box, i can use them for experimenting but this is my main project, and i am learning python these days.

I made some experiments with my code. I have runned same code, now blynk runs, display is not refreshing its like 0,2 frames per second, clock text updates itself but the clock not, this is weirder than i expected. Weird things happening with my code now.

I dug my dusty old RPi Model B out of oblivion… but turns out I didn’t keep my thread experiment. However, it seems to me I needed to put the Blynk.run() in the separate thread process.

Post your current code.

So, what i did was move Blynk to the MQTT. Yeah i know, blynk is very useful when used with basic projects but sometimes i cant get what i want from blynk. With blynk, my code just corrupts and makes weird things. Now, i optimized code, i got nice performance out of rpi now.

I supported Blynk for 2 whole years, with arduino mega 2556 + esp8266 but with rpi and python i needed to use mqtt. Its easier, yeah theres a difference but i got what i want now. I even bought energies to support you guys, so i am really happy with blynk, but blynk needs to be little bit upgraded for linux-use. I cant post my current code, because theres no much than functions and stuff. And i added mqtt so my old code is now gone.

Thanks for you support, you digged out your rpi for me :smiley: but i tried many things for days (not hours, days!!) So yeah. I solved problem with MQTT. If you guys really do some work on blynk, i believe you guys can do it with python BlynkLib.

You can install Node-Red on your Pi and then install the Blynk plug-in for Node-Red. This works very nicely with Mosquitto for MQTT.

This is how I use Blynk for both of my home automation setups (one in the UK and the other at our holiday Home in Spain. My devices (mostly Wemos D1 Mini and Sonoff based systems) don’t run any Blynk code, just MQTT code to talk to Node-Red on the Pi, which in turn handles the two-way communication with Blynk.
It’s also very easy to add other plug-in to Node-Red to give additional functionality. I use plugins for Amazon Alexa and Ikea TRÅDFRI that work very nicely alongside Blynk.

Node-Red is a graphical programming environment, but is very flexible because you can write your own functions in NodeJS. It makes it very quick and easy to get up and running with, but allows you to develop extended functionality (or add nodes that have been pre-written) to take it to the next level.

Pete.

Sorry I couldn’t be more assistance… I am NOT a developer, just an average shmoo and barely learning python myself :blush: Besides, I needed an excuse to dredge out the old Pi anyhow… my Blynk projects no longer look quite like I want anymore, so perhaps now is the time to look into this node-red and MQTT stuff…

Hi @Gunner, I’m not sure that Node-Red would help with your font size issue.
The Node-Red dashboard is probably its weakest feature and is actually why I turned to Blynk as both a way to get a better UI and a proper mobile app rather than simply a web browser control panel

That’s not to say you shouldn’t take a trip over to the dark side and see what you’ve been missing with all the other stuff!

Pete.

I know other methods won’t help the Blynk GUI side… IMHO a good thing was ruined and the resulting justifications where enough to frustrate and alienate.

But on a good note, it encouraged me to break away from “brand loyalty” and to look closer at other IoT and programming alternatives, even if not necessarily beneficial on the GUI side :stuck_out_tongue_winking_eye:

I am visually oriented, so perhaps Node-Red might be easier to pick up… although I admit to not really caring for the graphical programming methods… Node-Red, LEGO, Edison Robot’s Ed.Py, etc… they are all just a bit different enough to make multi-fluency frustrating in its own way.

1 Like

Hi guys! i got some news, i installed node-red to my own server and i did installed mqtt plug-ins and i tried mqtt with a simple ui. It worked well, but i also want a mobile app, so i need Blynk with MQTT support. So i installed blynk plugin for node-red but i dont know what to do because even multi-threading code on rpi is not properly working.

don’t run any Blynk code, just MQTT code to talk to Node-Red on the Pi, which in turn handles the two-way communication with Blynk.
It’s also very easy to add other plug-in to Node-Red to give additional functionality. I use plugins for Amazon Alexa and Ikea TRÅDFRI that work very nicely alongside Blynk.

Thanks for your support! i added blynk plugin for nodered but theres no gui things like slider, chart for node-red dashboard. I will try blynk with node-red tomorrow, i am little bit busy these days. I am sorry for my bad english, i am trying to communicate with my old english skills but i think it works. Have a good day!

I am little bit idiot today, node-red has default slider ui’s in it. Lol. I will add blynk to my project soon and send you guys the news

As I said earlier to @Gunner, I don’t use the Node-Red dashboard and the UI controls at all. I did try them initially, but the layout tools are so poor that they aren’t worth using.

Blynk is my only UI for most projects (I have a Nextion touch screen that I use with one project, but I won’t go into that here).

Let’s say that you want to have a Blynk slider that controls the brightness of an LED. Add the slider to Blynk and link it to a virtual pin. Add a Blynk write event node and link that to the same virtual pin.
Then ad an MQTT output node and specify the MQTT topic that you want to write the slider value to.
Link the two nodes together and deploy the project.

When you move the Blynk slider, the value will be received by Node-Red and sent out as an MQTT message.

Pete.

Here is an example on how to create a multi-threaded python application where Blynk runs in its own thread and you are free to put your code in the main() routine without needing to call blynk.run(), but you are able to call Blynk functions like blynk.virtual_write. The example also includes the my_user_task and write_handler functions.