Im trying to write/run a program that logs temperature and outputs the results to a png image.
I think the program is running fine (i get the desired .log file) up until the grpah part, then i get the following error:
Im running the program through ssh, putty, windows 7.Traceback (most recent call last):
File "temp_record_all_in_one.py", line 81, in <module>
plot(time, temp, color='red', linewidth=2.0, linestyle="-")
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 2460, in plot
ax = gca()
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 701, in gca
ax = gcf().gca(**kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 369, in gcf
return figure()
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 343, in figure
**kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 80, in new_figure_manager
window = Tk.Tk()
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1712, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable
the code for the program is:
Code: Select all
#import all neccessary modules
import RPi.GPIO as GPIO
import time
from pylab import *
#state pin setup
LED1_GPIO_PIN = 18
LED2_GPIO_PIN = 25
BUTTON_GPIO_PIN = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(BUTTON_GPIO_PIN, GPIO.IN)
GPIO.setup(LED1_GPIO_PIN, GPIO.OUT)
GPIO.setup(LED2_GPIO_PIN, GPIO.OUT)
#turn on LED
GPIO.output(LED1_GPIO_PIN, GPIO.HIGH)
#Search for button press
while True:
if GPIO.input(BUTTON_GPIO_PIN):
break
while GPIO.input(BUTTON_GPIO_PIN):
pass
#Turn down led, light up 'processing' led
GPIO.output(LED1_GPIO_PIN, GPIO.LOW)
GPIO.output(LED2_GPIO_PIN, GPIO.HIGH)
#create file with timestamp and write to it
timestamp = time.strftime("%Y-%m-%d--%H-%M-%S")
prog_start = time.time()
filename = "".join(["temperaturedata", timestamp, ".log"])
graphname = "".join(["temperaturedata", timestamp, ".png"])
datafile = open(filename, "w", 1)
#measure every 15 seconds
measurement_wait = 2
button_pressed = False
#CREATE & GATHER DATAFILE
#check for end button press and gather data
while True:
time_1 = time.time()
tfile = open("/sys/bus/w1/devices/28-000003ea1c1e/w1_slave")
text = tfile.read()
tfile.close()
temperature_data = text.split()[-1]
temperature = float(temperature_data[2:])
temperature = temperature / 1000
datafile.write(str(time.time()-prog_start) + "\t" + str(temperature) + "\n")
time_2 = time.time()
if (time_2 - time_1) < measurement_wait:
no_of_sleeps = int(round((measurement_wait - (time_2 - time_1)) / 0.1))
for i in range(no_of_sleeps):
time.sleep(0.1)
if GPIO.input(BUTTON_GPIO_PIN):
button_pressed = True
break
if button_pressed:
break
#close and save datafile
datafile.close()
#CREATE GRAPH
#create time and temp arrays
time = {}
temp = {}
#open file created above
with open(filename, 'r') as f:
lines = f.readlines()
#state what data is from which tab
for line in lines:
time = line.split('\t')[0].strip()
temp = line.split('\t')[1].strip()
#plot line graph of time (x) vs temp (y)
plot(time, temp, color='red', linewidth=2.0, linestyle="-")
title('Time taken to boil 500 ml water', verticalalignment='top', fontsize=12, weight='bold')
xticks
xlabel('Time (s)', fontsize=10)
ylim(0, 110)
yticks
ylabel('Temperature (C)', fontsize=10)
savefig(graphname)
GPIO.output(LED2_GPIO_PIN, GPIO.LOW)
I dont want to view the resultant graph through putty, rather save it direct to a png which i will then print/view on my windows PC.
Any help would be amazing.
Cheers,
Steve