Python Forum
My first script does not work anymore
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My first script does not work anymore
#1
Hello,
I began in python few month ago and wrote a script for a raspberry pi to display on a ssd1306 oled screen some informations.
Everyhting was fine but since few weeks the code does not run anymore.

I have this error each time I try to launch the script(but it used to work):

Error:
pi@domoticz:~/scripts $ python /home/pi/scripts/simpletime01.py & [1] 24405 pi@domoticz:~/scripts $ Traceback (most recent call last): File "/home/pi/scripts/simpletime01.py", line 192, in <module> display_info() File "/home/pi/scripts/simpletime01.py", line 140, in display_info disp.display() File "build/bdist.linux-armv7l/egg/Adafruit_SSD1306/SSD1306.py", line 167, in display File "build/bdist.linux-armv7l/egg/Adafruit_SSD1306/SSD1306.py", line 129, in command File "build/bdist.linux-armv7l/egg/Adafruit_GPIO/I2C.py", line 116, in write8 File "build/bdist.linux-armv7l/egg/Adafruit_PureIO/smbus.py", line 256, in write_byte_data IOError: [Errno 121] Remote I/O error python /home/pi/scripts/nano time01.py^C [1]+ Termine 1 python /home/pi/scripts/simpletime01.py
Coils someone help me to resolve this / understand why it suddenly stopped to work please

My code is

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import datetime,time
import subprocess
import Adafruit_GPIO.SPI as SPI
import Adafruit_SSD1306
import os
import locale
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import RPi.GPIO as GPIO

# Set up GPIO with internal pull-up
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)

# Set et initialize OLED display
RST = None
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)

draw.rectangle((0,0,width,height), outline=0, fill=0)

padding = -2
top = padding
bottom = height-padding
x = 0

# Set fonts
font1 = ImageFont.truetype("/home/pi/scripts/Tahoma.ttf", 12)
font2 = ImageFont.truetype("/home/pi/scripts/Tahoma.ttf", 24)
font3 = ImageFont.truetype("/home/pi/scripts/Tahoma.ttf", 10)

prev_millis = 0
display = 0

# Define refresh delay
now = datetime.datetime.now()
nextSecond = now+datetime.timedelta(seconds=1)
nextSecondHalf = now+datetime.timedelta(seconds=.5)

# Define temperature
temp = os.popen("vcgencmd measure_temp").readline()
temperature = temp.replace("temp=","")

# Define WIFI signal
signal = os.popen("iwconfig wlan0|awk '/Link Quality/{split($2,a,\"=|/\");print int((a[2]/a[3])*100)\"%\"}'").readline()
signal_int = int(signal.replace("%",""))

#signal_int = os.popen("iwconfig wlan0|awk '/Link Quality/{split($2,a,\"=|/\");print int((a[2]/a[3])*100)}'").readline()

def display_time(temperature,signal,signal_int):

	global font1, font2, font3

	locale.setlocale(locale.LC_TIME, 'fr_FR.utf8')
	current_time = time.strftime("%H:%M:%S")
	current_date = time.strftime("%a %d %b %Y")
	date = current_date.decode('utf-8')
	draw.rectangle((0,0,width,height), outline=0, fill=0)

	x = 0
	draw.ellipse((x, 0 , x+8, 8), outline=255, fill=0)
	x = 11
	draw.ellipse((x, 0 , x+8, 8), outline=255, fill=0)
	x = 22
	draw.ellipse((x, 0 , x+8, 8), outline=255, fill=0)
	x = 33
	draw.ellipse((x, 0 , x+8, 8), outline=255, fill=0)

	if int(signal_int) > 25:
		x = 0
		draw.ellipse((x+2, 2 , x+6, 6), outline=255, fill=255)

	if int(signal_int) > 50:
		x = 11
		draw.ellipse((x+2, 2 , x+6, 6), outline=255, fill=255)

	if int(signal_int) > 75:
		x = 22
		draw.ellipse((x+2, 2 , x+6, 6), outline=255, fill=255)

	if int(signal_int) > 99:
		x = 33
		draw.ellipse((x+2, 2 , x+6, 6), outline=255, fill=255)

	# font = font3
	# x = (disp.width)-64-(string_width(font,signal))
	# draw.text((x, top+12), signal, font=font3, fill=255)

	font = font1
	x = (disp.width)-(string_width(font,temperature))
	draw.text((x, top), temperature, font=font1, fill=255)

	font = font2
	x = (disp.width/2)-(string_width(font,current_time)/2)
	draw.text((x, top+16), current_time, font=font2, fill=255)

	font = font1
	x = (disp.width/2)-(string_width(font,date)/2)
	draw.text((x, top+40),date, font=font1, fill=255)

	disp.image(image)
	disp.display()

def display_blank():

	draw.rectangle((0,0,width,height), outline=0, fill=0)
	disp.image(image)
	disp.display()

def display_info():

	global font1, font2, font3
	draw.rectangle((0,0,width,height), outline=0, fill=0)

	cmd = "hostname -I | cut -d\' \' -f1"
	IP = subprocess.check_output(cmd, shell = True )
	essid = os.popen("iwconfig wlan0|grep 'ESSID'|awk -F ' ' '{print $4}'").readline()
	link_quality = os.popen("iwconfig wlan0 | grep 'Link' | awk -F ' ' '{print $1,$2}'").readline()
	signal_level = os.popen("iwconfig wlan0 | grep 'Signal' | awk -F ' ' '{print $3,$4,$5}'").readline()

	draw.text((x, top),		str(essid),					font=font1, fill=255)
	draw.text((x, top+16),	"IP: " + str(IP),			font=font1, fill=255)
	draw.text((x, top+30),	str(link_quality),			font=font1, fill=255)
	draw.text((x, top+44),	str(signal_level),			font=font1, fill=255)

	disp.image(image)
	disp.display()

def display_info2():

	global font1, font2, font3
	draw.rectangle((0,0,width,height), outline=0, fill=0)

	cmd = "hostname -I | cut -d\' \' -f1"
	IP = subprocess.check_output(cmd, shell = True )
	essid = os.popen("iwconfig wlan0|grep 'ESSID'|awk -F ' ' '{print $4}'").readline()
	link_quality = os.popen("iwconfig wlan0 | grep 'Link' | awk -F ' ' '{print $1,$2}'").readline()
	signal_level = os.popen("iwconfig wlan0 | grep 'Signal' | awk -F ' ' '{print $3,$4,$5}'").readline()

	draw.text((x, top),		str(essid),					font=font1, fill=255)
	draw.text((x, top+16),	"IP: " + str(IP),			font=font1, fill=255)
	draw.text((x, top+30),	str(link_quality),			font=font1, fill=255)
	draw.text((x, top+44),	str(signal_level),			font=font1, fill=255)

	disp.image(image)
	disp.display()

def string_width(fontType,string):

	string_width = 0
	for i, c in enumerate(string):
		char_width, char_height = draw.textsize(c, font=fontType)
		string_width += char_width
	return string_width

while True:

	now = datetime.datetime.now()
	millis = int(round(time.time() * 1000))

	if now > nextSecond:
		temp = os.popen("vcgencmd measure_temp").readline()
		temperature = temp.replace("temp=","")
		signal = os.popen("iwconfig wlan0|awk '/Link Quality/{split($2,a,\"=|/\");print int((a[2]/a[3])*100)\"%\"}'").readline()
		signal_int = int(signal.replace("%",""))
		nextSecond = now+datetime.timedelta(seconds=1)

	if((millis - prev_millis) > 300):
		# Cycle through different displays
		if(not GPIO.input(18)):
			display += 1
			if(display > 2):
				display = 0
			prev_millis = int(round(time.time() * 1000))

	if(display == 0):
		display_time(temperature,signal,signal_int)
	elif(display == 1):
		display_info()
	elif(display == 2):
		display_blank()

	# Turn off the display after a delay
	if((millis - prev_millis) > 600000):
		display = 2
		prev_millis = int(round(time.time() * 1000))

	time.sleep(0.1)
Reply
#2
I see you used ctrl-c to exit program.

So it's getting hung up somewhere (on surface it looks like the while loop starting at line 169).

Besides looking at code step by step, you can do a couple of things to isolate the problem.
  1. run in debugger (your IDE should have one) step through to see where hang-up is occurring.
  2. Please a print statement at the start of each function that prints name of function.
    This will isolate to a single function.
likes this post
Reply
#3
hello, thanks for your help
for some reasons I tried to reinstall all my libraries and it solved the problem... it is working for more than 24h now without any failure...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Make my py script work only on 1 compter tomtom 14 3,738 Feb-20-2022, 06:19 PM
Last Post: DPaul
  readline.parse_and_bind() does not work in start-up script of Python interpreter zzzhhh 0 1,476 Jan-18-2022, 11:05 AM
Last Post: zzzhhh
  Script stop work after 3 actioins - PLEASE WHERE IS THE PROBLEM? rondon442 0 1,534 Sep-27-2021, 05:40 PM
Last Post: rondon442
  string function doesn't work in script ClockPillow 3 2,333 Jul-13-2021, 02:47 PM
Last Post: deanhystad
  Curses script doesn't work wavic 1 4,066 Jan-08-2021, 09:11 PM
Last Post: wavic
  Pyinstaller - I can't drag and drop into the CMD terminal anymore.. aquerci 4 5,778 Jan-25-2020, 12:24 PM
Last Post: aquerci
  Code used to work, not anymore for no apparent reason? chicagoxjl 1 1,850 Jan-08-2020, 05:05 PM
Last Post: jefsummers
  Python code suddenly not working anymore mike_b 5 5,684 Oct-02-2019, 03:38 PM
Last Post: ichabod801
  All of a sudden Pycharm doesn't run anymore (select valid interpreter) Luke_Drillbrain 1 5,223 May-04-2017, 07:30 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020