
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!')
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
