How to code beaglebone black to read the data from DHT11 sensor?

I’m trying to connect my BeagleBone Black with DHT11 sensor. I’m following the Adafruit guide they provide, but i can’ t able to resolve it.

The code returns me errors.

import board
import adafruit_dht
import time
    stdin, stdout, stderr = bbb.exec_command('echo out > /sys/class/gpio/gpio45/direction')
    print("Connessione riuscita!!!!")
    # Sensor should be set to Adafruit_DHT.DHT11,
    # Adafruit_DHT.DHT22, or Adafruit_DHT.AM2302.
    dht_device = adafruit_dht.DHT11(board.D18)

    temperature = dht_device.temperature
    humidity = dht_device.humidity
    print(
    "Temp: {:.1f} , C Humidity: {}% ".format(
    temperature, humidity
    )
    )
    if humidity is not None and temperature is not None:
        print('Temp={0:0.1f}*C  Humidity={1:0.1f}%'.format(temperature, humidity))
    else:
        print('Failed to get reading. Try again!')
    # Fai suonare il buzzer per 10 secondi

It gives me the error: AttributeError: module 'board' has no attribute 'D18' . I also tried with the P8_11 pin which would be the actual one of the BeagleBone, but still it doesn’t work.

Does anyone know how to fix this error, or alternatively give me a way to connect the two devices in Python? Thanks in advance.

Pretty sure you have to enable a device tree overlay for one-wire. I don’t have a BBB close by at the moment to verify that. Some very good folks on this forum and they might jump on this and get you up and running.

Also, highly recommend getting Derek Malloy’s book on BeagleBoneBlack. Do believe he has a working example for one-wire communications.

1 Like

I didn’t find anything in the book, maybe I saw a different book

Have you tried the following as indicated by the tutorial:

If you’re using a BeagleBone Black with a DHT22 (or an AM2302) sensor connected to Pin P8_11, change the following line from

dhtDevice = adafruit_dht.DHT22(board.D18)

to

`dhtDevice = adafruit_dht.DHT22(board.P8_11)

If you’re using a DHT11 sensor, you can change the sensor type by renaming the DHT22 class to DHT11:

`dhtDevice = adafruit_dht.DHT11(board.D18)``

yes, i tried everything. The error is always the same.
Browsing the github repo of the Adafruit library, I noticed that the functions of the board object are activated only if the board.id is equal to “BEAGLEBONE_BLACK” , but in my case it is set as “GENERIC_LINUX” and I don’t know why.

My code runs on vscode via WSL, I wouldn’t want the adafruit library that I import into the code to take the WSL system and not my BBB… How can I make the library take as the board.id what it needs to myself?

That might be your problem, you should try running it locally on the BBB.

Adafruit uses a Platform Detect sub-library to figure out what you’re running on, so if it’s running on the beagle, it looks at the EEPROM memory to figure out it’s a Black.

Also make sure you’re running the python script with as root (sudo python …)

As another debug step try running this script (as root again) to see what it repors:

thank you so much, i’ll report you the result as soon as possible.

This is the result of the script:

Board Detection Test

Check that the Chip and Board IDs match your board and that this it is
correctly detecting whether or not it is a Linux board.

Chip id: GENERIC_X86
Board id: GENERIC_LINUX_PC

Linux Detection

Is this an embedded Linux system? True

Raspberry Pi Boards

Is this a Pi 3B+? False
Is this a Pi 4B? False
Is this a 40-pin Raspberry Pi? False
Is this a Raspberry Pi Compute Module? False

Other Boards

Is this a Siemens Simatic IOT2000 Gateway? False
Is this a 96boards board? False
Is this a BeagleBone board? False
Is this a Giant board? False
Is this a Coral Dev board? False
Is this a MaaXBoard? False
Is this a SiFive board? False
Is this a PYNQ board? False
Is this a Rock Pi board? False
Is this a NanoPi board? False
Is this a Khadas VIM3 board? False
Is this a Clockwork Pi board? False
Is this a Seeed Board? False
Is this a UDOO board? False
Is this an ASUS Tinker board? False
Is this an STM32MP1 board? False
Is this a generic Linux PC? True
Is this an OS environment variable special case? False

Apparently the problem is as I said in the answer I provided earlier. At this point, is there a way from WSL to make adafruit detect the beaglebone black (which is connected via USB to my pc). Through the code, I connect to the BBB via an SSH connection.

I think your problem is you’re trying to execute the Adafruit Library on your local WSL instance and it won’t work like that, the python code needs to run directly on the Beagle.

If you need to talk back and forth You probably need to write some code so your host PC sends commands or packets that tell the beagle what to do and have IT invoke the Blika library. You won’t be able to invoke Blinka on your X86 machine and expect it to run on the Beagle.

You could have the Python sketch on the Beagle send UDP packets to your main computer with the sensor values, but the app would need to run on the Beagle.

Yes, I’m writing a class dedicated to the connection to the Beaglebone in which I copy the system requirements.txt via SSH communication and install them on the BeagleBone.

Once this is done, I forward my main python file with the various operations for collecting environmental data with DHT11. Since my Beaglebone in the project I’m developing has the MASTER function in a Modbus RTU architecture that should send data to the Blockchain, I think the only part I’m missing is to send the requests back somehow, maybe as you said you with UDP packets.
Does this seem doable to you?
Do you have any recommendations in particular?

Anyway, thanks for the replies and I’d love to have more discussions with you.

Or, alternatively, I could avoid all these vicissitudes by using sensors that don’t use AdaFruit and this particular intrinsic mechanism.

Yeah probably doable. I used UDP as an example, for sensor applications you should look at MQTT .

What do you think if i avoid all these vicissitudes by using sensors that don’t use AdaFruit and this particular intrinsic mechanism for a easier way of communication???

My apologies for giving you bad information, I looked in that book and did not find it. We have several different boards so it must have been one of them and not the BBB.

1 Like

no problem, thanks anyway