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