0

I have a script in python3 which executes and shows information in an 8x8 MAX7219 matrix and works perfectly. When I run the script directly from the .py file, the matrix does not turn off (and that's what I need).

The detail is in that I need this script to run every 5 minutes, I set a cronjob to execute the script but the problem is that the matrix is turned off when I finish running the .py file

I'm missing some command so that does not happen at the time of executing the .py file, I attach the cron job configuration

# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
*/5   *    *    *    * /usr/bin/python3 /home/pi/Estacion.py

photo of the matrix and I need it does not turn off! :)

Matrix

Estacion.py

import sys
import time
import datetime
import requests
import Adafruit_DHT
import os

#Librerias para la matriz LCD

import re
import argparse

from luma.led_matrix.device import max7219
from luma.core.interface.serial import spi, noop
from luma.core.render import canvas
from luma.core.virtual import viewport
from luma.core.legacy import text, show_message
from luma.core.legacy.font import proportional, CP437_FONT, TINY_FONT, SINCLAIR_FONT, LCD_FONT

#Libreria Camara
from picamera import PiCamera
from time import sleep

#libreria ftp
import ftplib

#SE INICIA PANEL LCD
# create matrix device
serial = spi(port=0, device=0, gpio=noop())
device = max7219(serial, cascaded=8, block_orientation=-90)
print("Created device")

#Fecha con formato DATETIME y con segundos en 00
fecha = str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M"))
nombre_foto = str(datetime.datetime.now().strftime("%Y%m%d%H%M"))

# mensaje en LCD
show_message(device, fecha, fill="white", font=proportional(CP437_FONT))

#Inicio, temperatura y humedad 1ro DHT22
humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.AM2302, 4)

temperatura = str('{0:0.1f}'.format(temperature)) + "C"

with canvas(device) as draw:
        text(draw, (11, 0), temperatura, fill="white")

#URL donde se reciben los datos por el medoto POST Yo uso codeigniter
url = 'lalalal my website here'
#Variables y valores enviadas por post
payload = {'fecha': fecha, 'temp': temperature, 'humedad': humidity, 'nombre_foto' : nombre_foto + ".jpg"}
#Ejecucion de la url y las variables
r = requests.post(url, data=payload)

if r.status_code == 500:
    print ('Vale pico el hosting error 500')
else:
    print("Subido Correctamente " + str(r.status_code))

print("sacando fotooo")

with canvas(device) as draw:
        text(draw, (0, 0), "FOTO! :)", fill="white")

camera = PiCamera()

camera.start_preview
camera.annotate_text = fecha
sleep(3)
camera.capture('/home/pi/Pictures/' + nombre_foto + '.jpg',resize=(1280,720))
camera.stop_preview()
camera.close()

with canvas(device) as draw:
        text(draw, (11, 0), temperatura, fill="white")

#se sube la foto

session = ftplib.FTP('lalalala my ftp url here','[email protected]','hueheuheumypass')
file = open('/home/pi/Pictures/' + nombre_foto + '.jpg','rb')                  # file to send
session.storbinary('STOR ' + nombre_foto + '.jpg', file)     # send the file
file.close()                                    # close file and FTP

#se borra la foto
os.remove('/home/pi/Pictures/' + nombre_foto + '.jpg')

session.quit()
9
  • perhaps the display is turned on by Estacion.py and therefore, when it stops running, the display turns off? Jul 22, 2018 at 3:54
  • the detail is that when I run the script using the python idle, even if I close the idle and everything, the matrix does not turn off.
    – Oscar Cid
    Jul 22, 2018 at 4:02
  • oh, ok - wasn't sure ... so the cron job turns the display on when it executes, then the display turns off? Jul 22, 2018 at 4:03
  • When I run the script using the idle or console, the script runs completely and the array does not shut down When executed by the cron job, it is executed in the same way, but when finished it turns off the matrix
    – Oscar Cid
    Jul 22, 2018 at 4:10
  • 1
    sorry is the python 3 (Idle), the editor included in the raspbian
    – Oscar Cid
    Jul 22, 2018 at 4:18

1 Answer 1

0

I have solved this in a fairly simple and really a bit obvious way.

As the script runs every 5 minutes, I added a 4 minute sleep at the end of the code

sleep (240)

This allows only the matrix to be turned off for a short period of time until it is restarted again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.