1

So, I'm pretty new to this. Want to play around with temperature & humidity sensors.

I have a pi with 4x DHT11 sensors, on wires of ranging lengths hosted in different situations. I've written a few short python scripts, one of which reads the DHT11 & writes to a text file. This ends up in a SQL DB to be graphed.

Scripts are stored in /var/scripts/production/, and launched on boot via /etc/rc.local, xargs allows parallel execution.

ls /var/scripts/production/*.py|xargs -n 1 -P 15 sudo python

Everything works, but when I place the DHT11 script into production & reboot, my pi will last anywhere from minutes to hours, but will eventually crash.

I thought it was something with multiprocessing / pool in my script, so hashed that out & replaced with a simple for loop. The pi still crashes though.

/var/log/kern.log shows some interesting behavior, the moment the pi crashes, the clock is knocked back anywhere from a few seconds to a few minutes, and it appears to reboot (however I don't believe this reboot ever to be a success).

I'm a little bit stumped. Is there something wrong with my python script (botched together using other people's scripts & a bit of my own ingenuity). Or is it something to do with the physical environment. Can the 3v3 rail become overloaded with too many sensors? Too much power for it to read / handle? Can people get external 3.3v PSUs for this purpose?

I work in IT so have a good knowledge of computers & code, just not so much Linux or raspberry pis. Any help would be greatly appreciated!

.

EDIT:

After trying joan's suggestion, I changed DHT11.py so I'm not constantly executing "sensor = Adafruit_DHT.DHT11" on each loop.

The pi still crashed, at about 10:43. I had a top session open: https://imgur.com/a/IAddAWJ

Here's an updated kern.log: https://pastebin.com/HHSAvdUY

It looks like w1 is causing the crashes, but I don't actually have any w1 devices plugged in. I'm not trying to query them either. Does Adafruit look for w1 devices? .

.

Edit 2:

w1 isn't causing the crash, I commented out dtoverlay in /boot/config.txt and got another crash.

Just rebuilt the pi's OS (iteration 7).. I'm under the suspicion a DHT11 on a long cable might be the culprit now. I've unplugged this and will see what it does.

.

EDIT 3:

So it's not the sensor on the long wire... I've replaced the whole pi now.

DHT11.py:

import os
import time
#from multiprocessing import Pool
import Adafruit_DHT

path_temperature = "/var/scripts/temperature"

# Create temperature directory
if not os.path.exists(path_temperature):
    os.makedirs(path_temperature)

path_humidity = "/var/scripts/humidity"

# Create humidity directory
if not os.path.exists(path_humidity):
    os.makedirs(path_humidity)

# Define the script that will read the sensor, & write to a text file
def readsensor(gpio):
    sensor = Adafruit_DHT.DHT11
    th, tt = Adafruit_DHT.read_retry(sensor, gpio)

    if th < 100:
        if tt > 0:
            os.chdir(path_humidity)
            file = open("DHT11H-" + str(gpio),"w")
            file.write("%s" % (th))
            file.close()
            print("Sensor %s has humidity %f" % ("DHT11H-" + str(gpio), th))

            os.chdir(path_temperature)
            file = open("DHT11T-" + str(gpio),"w")
            file.write("%s" % (tt))
            file.close()
            print("Sensor %s has temperature %f" % ("DHT11T-" + str(gpio), tt))

# sensor GPIO pins
sensorgpios = [
  '19',
  '12',
  '16',
  '13',
  ]

while True:
    #pool = Pool()
    #pool.map(readsensor, sensorgpios)
    #pool.close()
    #pool.join()

    for sensor in sensorgpios:
        readsensor(sensor)

    time.sleep(10)

kern.log:

Mar 16 14:02:44 raspicontroller kernel: [ 7991.186942] w1_master_driver w1_bus_master1: Family 7a for 7a.0025e7a1ce6b.46 is not registered.
Mar 16 14:03:11 raspicontroller kernel: [ 8017.782410] w1_master_driver w1_bus_master1: Attaching one wire slave 7a.0025e721ce6b crc 9f
Mar 16 14:03:11 raspicontroller kernel: [ 8017.786981] w1_master_driver w1_bus_master1: Family 7a for 7a.0025e721ce6b.9f is not registered.
Mar 16 14:05:32 raspicontroller kernel: [ 8159.133401] w1_master_driver w1_bus_master1: Attaching one wire slave 1a.c025c7614a6b crc dd
Mar 16 14:05:32 raspicontroller kernel: [ 8159.137977] w1_master_driver w1_bus_master1: Family 1a for 1a.c025c7614a6b.dd is not registered.
Mar 16 14:05:57 raspicontroller kernel: [ 8184.183538] w1_master_driver w1_bus_master1: Attaching one wire slave 1a.2025c7614a6b crc 34
Mar 16 14:05:57 raspicontroller kernel: [ 8184.188049] w1_master_driver w1_bus_master1: Family 1a for 1a.2025c7614a6b.34 is not registered.
Mar 16 14:06:23 raspicontroller kernel: [ 8210.483798] w1_master_driver w1_bus_master1: Attaching one wire slave 1a.2025c7610a6b crc dd
Mar 16 14:06:23 raspicontroller kernel: [ 8210.488509] w1_master_driver w1_bus_master1: Family 1a for 1a.2025c7610a6b.dd is not registered.
Mar 16 14:06:11 raspicontroller kernel: [    0.000000] Booting Linux on physical CPU 0x0
Mar 16 14:06:11 raspicontroller kernel: [    0.000000] Linux version 4.14.98-v7+ (dom@dom-XPS-13-9370) (gcc version 4.9.3 (crosstool-NG crosstool-ng-1.22.0-88-g8460611)) #1200 SMP Tue Feb 12 20:27:48 GMT 2019
Mar 16 14:06:11 raspicontroller kernel: [    0.000000] CPU: ARMv7 Processor [410fd034] revision 4 (ARMv7), cr=10c5383d
Mar 16 14:06:11 raspicontroller kernel: [    0.000000] CPU: div instructions available: patching division code
Mar 16 14:06:11 raspicontroller kernel: [    0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
Mar 16 14:06:11 raspicontroller kernel: [    0.000000] OF: fdt: Machine model: Raspberry Pi 3 Model B Rev 1.2
Mar 16 14:06:11 raspicontroller kernel: [    0.000000] Memory policy: Data cache writealloc
Mar 16 14:06:11 raspicontroller kernel: [    0.000000] cma: Reserved 8 MiB at 0x3ac00000
Mar 16 14:06:11 raspicontroller kernel: [    0.000000] On node 0 totalpages: 242688
Mar 16 14:06:11 raspicontroller kernel: [    0.000000] free_area_init_node: node 0, pgdat 80c85780, node_mem_map ba39f000
  • 2
    I am not sure I have an answer. One observation DO NOT attempt to connect external 3.3V to the Pi; it is unnecessary (and dangerous) - the Pi can supply 800mA and the sensors use negligible power. I use the Adafruit code and it usually works, but the timing is critical, and readings occasionally fail (or produce nonsense). There is little point in attempting to use Pool - the code is undemanding of processor resources. Are you attempting to use a w1 driver? – Milliways Mar 21 at 0:22
  • I don't like the way the sensor is opened repeatedly in the loop. I wonder if it is not releasing old resources and running out of memory. Perhaps run top in a separate terminal window and check memory usage over time. My preferred style would be to do sensor = Adafruit_DHT.DHT11 outside the loop. – joan Mar 21 at 8:27
  • Thanks for the info Milliways, I won't use an external PSU! Yeah I sometimes get nonsense back from Adafruit, which is why I have if humidity < 100 & temperature > 0. Yes I've also had w1 sensors on the pi. in my process of elimination, trying to find the culprit causing the crash, I've unplugged them and disabled the script. – Dan Mar 21 at 9:33
  • Thankyou Joan, I'll see what top is telling me. Just noticed I'm declaring "sensor = Adafruit_DHT.DHT11" on every loop. I'll declare it at script start and pass it through to the function! Thanks for your advice, I'll let you know – Dan Mar 21 at 9:37
  • I suggest running it without logging. Read the temperature, but don't write it to disk. See if it still crashes. That will help you distinguish between problems related to the sensor and problems related to the logging itself. – Brick Mar 22 at 19:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.