User avatar
alexanderre
Posts: 19
Joined: Fri Mar 04, 2016 4:51 pm
Location: Kyiv, Ukraine
Contact: Facebook

Script crashes randomly

Fri Dec 08, 2017 12:04 pm

Hi all. I'am a Python and GPIO newbie and therefore I might do something badly wrong, that's why I need your help :)

I have a script here that gets different data from GPIO connected sensors (temperature, humidity, pressure & also shows date & time):

Code: Select all

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

#general modules
import os
import glob
import time
import datetime
import gc

#measuring modules
import spidev as SPI
import SSD1306
import Adafruit_DHT as dht
from BMP180 import BMP180

#displaying
import Image
import ImageDraw
import ImageFont

#Reading temp via DS18B20
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
def read_rom():
	name_file=device_folder+'/name'
	f = open(name_file,'r')
	return f.readline()

def read_temp_raw():
	f = open(device_file, 'r')
	lines = f.readlines()
	f.close()
	return lines

def read_temp():
	lines = read_temp_raw()
	while lines[0].strip()[-3:] != 'YES':
		time.sleep(0.2)
		lines = read_temp_raw()
	equals_pos = lines[1].find('t=')
	if equals_pos != -1:
		temp_string = lines[1][equals_pos+2:]
		temp_c = float(temp_string) / 1000.0
		return temp_c    
#---------------------------------------------------------------

#DISPLAY
# Raspberry Pi pin configuration:
RST = 19
# Note the following are only used with SPI:
DC = 16
bus = 0
device = 0

# 128x64 display with hardware SPI:
disp = SSD1306.SSD1306(RST, DC, SPI.SpiDev(bus,device))
# Initialize library.
disp.begin()
# Clear display.
disp.clear()
disp.display()
# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
width = disp.width
height = disp.height
image = Image.new('1', (width, height))
# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)
# Draw a black filled box to clear the image.
def displayClean():
        clean = draw.rectangle((0,0,width,height), outline=0, fill=0)
        return clean

# Draw some shapes.
# First define some constants to allow easy resizing of shapes.
padding = 1
top = padding
x = padding
# Load default font.
#font = ImageFont.load_default()
# Alternatively load a TTF font.
# Some other nice fonts to try: http://www.dafont.com/bitmap.php
font = ImageFont.truetype('mono.ttf', 18)
# Write two lines of text.

while True:

        #what is now?
        now=datetime.datetime.now() 
        
        #1 - Date
        draw.text((x, top), 'Date: ', font=font, fill=255)
        draw.text((x, top+20), str(now.day)+'/'+str(now.month)+'/'+str(now.year), font=font, fill=255)
        disp.image(image)
        disp.display()
        displayClean()
        time.sleep(6)

        #2 - Time
        draw.text((x, top), 'Time: ', font=font, fill=255)
        draw.text((x, top+20), str(now.hour) + ':' + str(now.minute), font=font, fill=255)
        disp.image(image)
        disp.display()
        displayClean()
        time.sleep(6)

        #3 Temperature via DS18B20
        draw.text((x, top), 'Temp DS: ', font=font, fill=255)
        draw.text((x, top+20),str(read_temp())+ 'C' , font=font, fill=255)
        disp.image(image)
        disp.display()
        time.sleep(6)
        displayClean()

        #4 - Temperature and...

        h,t = dht.read_retry(dht.DHT22, 18)  # Poll DHT-22
        
        draw.text((x, top), 'Temp DHT: ', font=font, fill=255)
        draw.text((x, top+20),str('{0:0.1f}'.format(t))+ 'C' , font=font, fill=255)
        disp.image(image)
        disp.display()
        displayClean()
        time.sleep(6)

        #5 - Humidity
        draw.text((x, top), 'Humidity: ', font=font, fill=255)
        draw.text((x, top+20),str('{0:0.1f}%'.format(h)) , font=font, fill=255)
        disp.image(image)
        disp.display()
        displayClean()
        time.sleep(6)

        #6 - Pressure
        bmp = BMP180()
        draw.text((x, top), 'Pressure: ', font=font, fill=255)
        draw.text((x, top+20), str(bmp.read_pressure() / 100.0) + ' hPa', font=font, fill=255)
        disp.image(image)
        disp.display()
        displayClean()
        time.sleep(6)

#cleaning
        tempdslist = [base_dir, device_folder, device_file]
        del tempdslist
        superlist = [RST, DC, bus, device, disp, width, height, image, draw, padding, top, x, font, h, t]
        del superlist
        gc.enable()
        gc.collect()
        time.sleep(1)
        #print ('It works!')
It works with no error messages for some time and then randomly crashes with errors like it at some point can't find some sensors and so on (will add exact error messages below).

Why is that happening? How do I avoid it? As you can see I tried to manually delete all the variables in the #cleaning section to free some memory which seems kinda dumb for year 2017 and the computers of nowadays and the code I use, but you never know :). Also I used gc (garbage collector) for the same reason. It seems to help a tad but didn't solve the problem completely - I still get error messages from cron after random intervals (from 30 minutes to 10 hours) and the script crashes...

User avatar
alexanderre
Posts: 19
Joined: Fri Mar 04, 2016 4:51 pm
Location: Kyiv, Ukraine
Contact: Facebook

Re: Script crashes randomly

Fri Dec 08, 2017 12:12 pm

Error example:

Code: Select all

sh: 1: modprobe: not found
sh: 1: modprobe: not found
Traceback (most recent call last):
 File "myApp.py", line 125, in <module>
   draw.text((x, top+20),str('{0:0.1f}'.format(t))+ 'C' , font=font, fill=255)
ValueError: Unknown format code 'f' for object of type 'str'

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Script crashes randomly

Fri Dec 08, 2017 12:37 pm

I suspect you're not getting valid values for humidity and temperature each loop so you need to put in some error handling before trying to format the data.

As an example:

Code: Select all

h,t = dht.read_retry(dht.DHT22, 18)  # Poll DHT-22

try:
    # Let's see if we can format the text
    temp_str = '{0:0.1f}'.format(t)
     
except ValueError:
    # Got a bad temp value so let's skip and not update the text
    pass
    
else:
    # It's a bit unusual to have "else" in "try...except..." blocks but it's very useful as it stops you catching errors
    # that you weren't trying to catch
    # This section only runs if you didn't trigger the above ValueError        
    draw.text((x, top), 'Temp DHT: ', font=font, fill=255)
    draw.text((x, top+20),temp_str + 'C' , font=font, fill=255)
    disp.image(image)
    disp.display()
    displayClean()
    
time.sleep(6)
There will be other (simpler) ways of doing this no doubt and people here who actually have your sensor may post better examples. I'm just trying to show a generic way of catching errors. Hope it helps.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
alexanderre
Posts: 19
Joined: Fri Mar 04, 2016 4:51 pm
Location: Kyiv, Ukraine
Contact: Facebook

Re: Script crashes randomly

Fri Dec 08, 2017 12:59 pm

I see, thanks, will try that out, error handling was one of the I things I thought about.

Still it seems weird for me that sometimes I get the values right and sometimes I don't with the same code... :roll:

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Script crashes randomly

Fri Dec 08, 2017 1:09 pm

I don't think it's uncommon to get the occasional bad result from that sensor. I think I've seen numerous posts saying that over the years!
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
OutoftheBOTS
Posts: 711
Joined: Tue Aug 01, 2017 10:06 am

Re: Script crashes randomly

Fri Dec 08, 2017 9:24 pm

It is common to get an occasional bad reading. I think 1 of the most common reasons for this can be we all tend to use the cheapest connector wire we can find. Connectors r always a place errors in transmission of data can occur, that's why high end equipment will use gold platted connectors to reduce the problem.

Simple error checking will weed out the problem

User avatar
alexanderre
Posts: 19
Joined: Fri Mar 04, 2016 4:51 pm
Location: Kyiv, Ukraine
Contact: Facebook

Re: Script crashes randomly

Sat Dec 09, 2017 2:17 pm

Well I made a really short sh script to start my py file and added a cron job to launch it @reboot and one thing I changed yesterday was I added sudo in front of the path to the sh script in crontab. Stopped getting a modprobe related error, but the one with temperature showed up after almost 24 hours, so now I added the code you offered and will see what's gonna happen :)
Thanks again, will keep you updated on this one :)

User avatar
alexanderre
Posts: 19
Joined: Fri Mar 04, 2016 4:51 pm
Location: Kyiv, Ukraine
Contact: Facebook

Re: Script crashes randomly

Sat Dec 09, 2017 5:31 pm

OutoftheBOTS wrote:
Fri Dec 08, 2017 9:24 pm
It is common to get an occasional bad reading. I think 1 of the most common reasons for this can be we all tend to use the cheapest connector wire we can find. Connectors r always a place errors in transmission of data can occur, that's why high end equipment will use gold platted connectors to reduce the problem.

Simple error checking will weed out the problem
Well you never know for sure why this or that may happen but in my case I doubt it's the connectors - I use pretty good quality dedicated jumpers and an extension board Pioneer600...

pcmanbob
Posts: 7247
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Script crashes randomly

Sun Dec 10, 2017 11:42 am

regarding your modprobe error if you are using raspbian jessie or stretch you no longer need these lines

Code: Select all

os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
you just need to activate 1-wire interface in raspi-config or add dtoverlay=w1-gpio to /boot/config.txt
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
alexanderre
Posts: 19
Joined: Fri Mar 04, 2016 4:51 pm
Location: Kyiv, Ukraine
Contact: Facebook

Re: Script crashes randomly

Mon Dec 11, 2017 7:26 pm

pcmanbob wrote:
Sun Dec 10, 2017 11:42 am
regarding your modprobe error if you are using raspbian jessie or stretch you no longer need these lines

Code: Select all

os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
you just need to activate 1-wire interface in raspi-config or add dtoverlay=w1-gpio to /boot/config.txt
Oh, I see, thanks, will do :)


Update: looked into my config file and I already had that line there, so I guess I'll just comment those lines of code in my script :)

User avatar
alexanderre
Posts: 19
Joined: Fri Mar 04, 2016 4:51 pm
Location: Kyiv, Ukraine
Contact: Facebook

Re: Script crashes randomly

Tue Dec 12, 2017 6:17 pm

So I've followed your advices and so far so good, so thanks again for your time and help :)

Return to “Python”