【发布时间】:2019-06-23 02:12:50
【问题描述】:
我用 Raspbian 在 PI3b 上运行 domoticz,为了提高效率,PI3b 现在有一个 7 英寸的屏幕来显示 domoticz 行为 + 气象站信息 + 互联网预报...... 为了展示所有这些,我编写了一个带有温度/压力图形的 C++/WXWidget 应用程序...... 温度/压力图是用 Python3/matplotlib 绘制的,保存为 3 个 png 文件。 Python 脚本读取文件中的数据并绘制/保存图形。
它在终端上运行良好....但无法使用 crontab 或来自 domoticz 的事件 lua 脚本的“os.execute()”...
我已经提升了脚本和 png 文件的所有权限/访问权限(读/写)
一个python脚本从bme280传感器读取数据,crontab 2分钟,没问题,它可以从终端,crontab,lua事件...
第二个脚本读取数据、绘制图表并发送 http json 命令以更新 domoticz 中的设备。这在终端上可以正常工作,但不能从 domoticz (lua os.execute()) 或 crontab 中工作。
在 crontab 中调用: sudo /usr/bin/python3 /home/pi/Desktop/graph.py
来自 domoticz lua 脚本的调用: os.execute('sudo /usr/bin/python3 /home/pi/Desktop/graph.py')
从终端它可以很好地与 python、/usr/bin/python 或 /usr/bin/python3 (--> matplotlib) 配合使用
python --version = 3.7.0
在许多 python 版本之间看起来像一个问题,好的一个不是从 crontab 和 lua-scripts 调用的......如何修复它?
默认 python 版本为 2.7.0、3.0、3.6、3.7 已测试以检查 env/bin/path 的问题... urllib.urlopen 发现的唯一问题已更改为“urlopen from urllib.request”
#!/usr/bin/python3
import matplotlib.lines as lines
import matplotlib.pyplot as plt
#import urllib ---> urllib.urlopen() works with /usr/bin/python
from urllib.request import urlopen #---> to work with /usr/bin/python3
fig = plt.figure()
ax = fig.add_subplot(111)
ax.grid(which='major', axis='x', color=(0.6, 0.6, 0.6), linewidth=1)
ax.patch.set_color('black')
fig.set_facecolor((0.0, 0.0, 0.0))
fig.set_size_inches(26.25, 7.42)
fig.patch.set_facecolor('black')
ax.spines["top"].set_visible(False)
ax.spines["bottom"].set_visible(False)
ax.spines["right"].set_visible(False)
ax.spines["left"].set_visible(False)
plt.ylim(0, 63)
plt.xlim(0, 210)
plt.style.use('dark_background')
list = []
d1 = 0
d2 = 0
d3 = 0
def readf( str ): #open/read file, fill the list
list[:] = []
with open(str) as infile: #parsing to float
numbers = infile.read()
numbers = numbers.replace(" ","").replace("\r","").replace("\n","")
for num in numbers.split(";"):
n = float(num)
list.append(n)
infile.close()
if str == "/home/pi/Dev/PI-Weather_Station/pressval.txt":
t = len(list) - 1
t1 = t - 6
t2 = t - 12
t3 = t - 24
d1 = list[t] - list[t1]
d2 = list[t] - list[t2]
d3 = list[t] - list[t3]
if d1 < 0 and d2 < 0:
httpresponse = urlopen("http://192.168.x.xxx:xxxx/json.htm?type=command¶m=updateuservariable&vname=tendance&vtype=2&vvalue=baisse")
elif d1 < 0 and d2 > 0:
httpresponse = urlopen("http://192.168.x.xxx:xxxx/json.htm?type=command¶m=updateuservariable&vname=tendance&vtype=2&vvalue=stable")
elif d1 > 0 and d2 > 0:
httpresponse = urlopen("http://192.168.x.xxx:xxxx/json.htm?type=command¶m=updateuservariable&vname=tendance&vtype=2&vvalue=hausse")
elif d1 > 0 and d2 < 0:
httpresponse = urlopen("http://192.168.x.xxx:xxxx/json.htm?type=command¶m=updateuservariable&vname=tendance&vtype=2&vvalue=stable")
return;
def chart( stg ): #plot/save the charts
o = len(list)
omin = 1400
omax = -50
i = 0
n = 0
line = lines.Line2D([i, i+1], [list[i], list[i+1]], lw=1, color='blue', axes=ax)
if o > 210:
i = o - 210
n = i
while i < o-1:
if stg == "/home/pi/Dev/PI-Weather_Station/tex.png":
line = lines.Line2D([i, i+1], [list[i], list[i+1]], lw=3, color=(0.0, 0.7, 1.0), axes=ax)
ax.add_line(line)
elif stg == "/home/pi/Dev/PI-Weather_Station/tin.png":
line = lines.Line2D([i, i+1], [list[i], list[i+1]], lw=3, color='yellow', axes=ax)
ax.add_line(line)
elif stg == "/home/pi/Dev/PI-Weather_Station/press.png":
line = lines.Line2D([i, i+1], [list[i], list[i+1]], lw=3, color='red', axes=ax)
ax.add_line(line)
if list[i] < omin:
omin = list[i]
if list[i] > omax:
omax = list[i]
i += 1
ax.axis([n, o, omin - 0.1, omax + 0.1])
ax.axhline((omax+omin)/2, 0, 1)
ax.axvline(n+30, 0, 1)
ax.axvline(n+60, 0, 1)
ax.axvline(n+90, 0, 1)
ax.axvline(n+120, 0, 1)
ax.axvline(n+150, 0, 1)
ax.axvline(n+180, 0, 1)
fig.savefig(stg, dpi = 10, bbox_inches = 'tight')
return;
readf("/home/pi/Dev/PI-Weather_Station/texval.txt")
chart("/home/pi/Dev/PI-Weather_Station/tex.png")
readf("/home/pi/Dev/PI-Weather_Station/tinval.txt")
chart("/home/pi/Dev/PI-Weather_Station/tin.png")
readf("/home/pi/Dev/PI-Weather_Station/pressval.txt")
chart("/home/pi/Dev/PI-Weather_Station/press.png")
plt.close()
【问题讨论】:
-
OK:cron 的调试:Traceback(最近一次调用最后):文件“/home/pi/Desktop/graph.py”,第 6 行,在
from urllib.request import urlopen ImportError : 没有名为 request 的模块
标签: python-3.x matplotlib cron raspbian domoticz