strange phenomen Raspberry /DHT 22 /RF 433 mhz /Python

harbarwurz

New Member
Joined
Apr 19, 2020
Messages
2
Reaction score
0
Credits
0
Hello
I have a strange problem:

On my Raspberry Pi 4 I have a Humi/temp sensor "DHT 22" (working) and a RF 433 Mhz transmitter to switch a heating mat based on the measured temperatures.
Measuring and sending from the IDE (Thonny) or the shell works fine. (messurement and then heat ON or OFF)

So I created the script as a crontab:

Code:
* * * * * /usr/bin/python3 /home/pi/script.py

Inside the python script i write a logfile to see if the script is working:

Python:
f = open("out.txt", "w")
f.write(tnow)
f.write('\n')
f.write(f'Temperatur: {temperature:0.1f}C\n')
f.write(f'Feuchtigkeit: {humidity:0.1f}%')
f.close()

The script writes the logfile every Minute, but it wont switch the heatmap.
IF i run the script from Thonny, the script switches the Power adapters. In bash too, but not when just running as cronjob
The command to send the code is "send 11111 1 1", the script "send" is in $PATH:

pi@raspberrypi:~ $ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games:/home/pi/raspberry-remote

and works directly in shell, no matter from where i call it:

pi@raspberrypi:~ $ send
This is rasperry remote, an application to control remote plugs with the
Raspberry Pi.
Based on RCSwitch and wiringPi. See github.com/xkonni/raspberry-remote for
further reference......

The python script is owned by user "pi" (and the same group) and is chmod +x, like i wrote before it works perfektly when running
in Thonny or Bash directly.

This is my Python script, sure it could be wrote better, but im a newbie in Python
Python:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import Adafruit_DHT
import time
import subprocess

humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.DHT22, 14)
tnow = time.strftime('%H:%M')

f = open("out.txt", "w")
f.write(tnow)
f.write('\n')
f.write(f'Temperatur: {temperature:0.1f}C\n')
f.write(f'Feuchtigkeit: {humidity:0.1f}%')
f.close()


#Temperature output bash
print(tnow, '\n')
print(f'Temperatur: {temperature:0.1f}C')
print(f'Feuchtigkeit: {humidity:0.1f}%')

#Temperatur Function
#heater on

if (temperature <= 23):
    FNULL = open(os.devnull, 'w')
    retcode = subprocess.call(['send 11111 1 1'], stdout=FNULL, stderr=subprocess.STDOUT, shell=True)
  
#heater off
elif (temperature >= 25):
    FNULL = open(os.devnull, 'w')
    retcode = subprocess.call(['send 11111 1 0'], stdout=FNULL, stderr=subprocess.STDOUT, shell=True)

SYSLOG:
Apr 19 07:10:01 raspberrypi CRON[30011]: (pi) CMD (/usr/bin/python3 /home/pi/script.py)
Apr 19 07:11:01 raspberrypi CRON[30156]: (pi) CMD (/usr/bin/python3 /home/pi/script.py)
Apr 19 07:12:01 raspberrypi CRON[30321]: (pi) CMD (/usr/bin/python3 /home/pi/script.py)
Apr 19 07:13:01 raspberrypi CRON[30479]: (pi) CMD (/usr/bin/python3 /home/pi/script.py)
Apr 19 07:14:01 raspberrypi CRON[30633]: (pi) CMD (/usr/bin/python3 /home/pi/script.py)
Apr 19 07:15:01 raspberrypi CRON[30793]: (pi) CMD (/usr/bin/python3 /home/pi/script.py)
Apr 19 07:16:01 raspberrypi CRON[30940]: (pi) CMD (/usr/bin/python3 /home/pi/script.py)
Apr 19 07:17:01 raspberrypi CRON[31098]: (pi) CMD (/usr/bin/python3 /home/pi/script.py)
......

Do you have an Idea what the problem could be ? I had it running perfectly before, but then someone spilled a drink on my raspberry and i had to start over with a new raspy and the code from the backup and i can't get the code to work this time. Please help me !!
 
Last edited:




Top