How to fix crontab issue or 'os.execute()' issue with python script?

174 Views Asked by At

I run domoticz on a PI3b with Raspbian, for better effiency the PI3b has now a 7" screen to show domotics behaviour + weather-station information + internet forecast... To show all of this I've coded a C++/WXWidget app with graphics for temp/pressure... Temp/pressures graphs are with Python3/matplotlib plotted, saved as 3 png files. The Python script read datas in files and plot/save the graphs.

It works good from terminal.... but no way to work with crontab, or with an "os.execute()" from events lua-scripts of domoticz...

I've pushed Up all permissions/access for scripts and png files (read/write)

One python script read data from bme280 sensor, crontab 2 minutes, no problem, it works from terminal, crontab, lua events...

Second script read datas, plot graphs and send http json command in order to update a device in domoticz. This works fine from terminal, but not from domoticz (lua os.execute()) or from crontab.

call in crontab: sudo /usr/bin/python3 /home/pi/Desktop/graph.py

call from domoticz lua script: os.execute('sudo /usr/bin/python3 /home/pi/Desktop/graph.py')

from terminal it works fine with python, /usr/bin/python, or /usr/bin/python3 (--> matplotlib)

python --version = 3.7.0

It looks like a problem beetween many python versions, the good one is not called from crontab and lua-scripts.... How to fix-it ?

default python version to 2.7.0, 3.0, 3.6, 3.7 tested to check problem with env/bin/path... only problem found with urllib.urlopen changed with "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&param=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&param=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&param=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&param=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()
1

There are 1 best solutions below

0
On BEST ANSWER

Oky,

source: Generating a PNG with matplotlib when DISPLAY is undefined

that was a pb with matplotlib, the script runs fine in terminal but for others it needs more codes:

#!/usr/bin/python

import matplotlib
matplotlib.use('Agg')