DHT11 stopped working (fix suggested) #368
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Describe the bug
My DHT11 was working… then it wasn't. I note that https://github.com/adafruit/Adafruit_Python_DHT is now marked as deprecated, and they point to CircuitPython as the successor. I hacked together a fix, which is documented below and working for me.
A start for a fix
This is hacked together, but pretty close to what's going to be required as the successor (handling the AM2302 previously handled by getDHTTemp.py).
CircuitPython-DHT
Replace
oprint/lib/python3.7/site-packages/octoprint_enclosure/getDHTTemp.pyNote
My fix only works for DHT11 & DHT22, where the old
getDHTTemp.pyworked (I assume from reading the code — I don't have the other devices to test them) for DHT11, DHT22 & AM2302.And for the further avoidance of doubt, I'm gratified that this has been helpful to some folks… and for the love of Sanity & Efficiency, I hope that someone has the time to turn this into a proper Pull Request that makes the obvious changes properly! The code above is a nasty hack that's pointing in the direction of doing this properly, but it's totally not doing this properly yet.
Is it working now with this fix ?
My fix is working for me.
(I've updated my comment. See the note I added — my fix works only for DHT11, since I don't have DHT22 or AM2302 to test, and I didn't duplicate the previous handling of those devices. This is totally not the "responsible contribution to an open source project I use and love" I'd like to make if I had more time; it's instead a completely half-done "ohh I hacked together a totally back yard patch that got me working, and someone [more responsible than me | with more time] might find useful as a start to doing this properly"… which is why I filed it as an Issue rather than a Pull request.)
You're awesome

btw, is it possible to add second sensor ? I saw that you put "dhtDevice = adafruit_dht.DHT11(board.D23)" in the script means we cannot change GPIO pin from the plugin. Is there a way so to change the script so we can change port from plugin setting ?
Thanks so much
also the same error yet i cannot get this "fix" to work
Got mine to work with my DT22, just had to change it to
dhtDevice = adafruit_dht.DHT22(board.D4)
in oprint/lib/python2.7/site-packages/octoprint_enclosure/getDHTTemp.py
Just changed to DHT22 and work like charm too
So I went through this entire process (had to install libgpiod2 manually using dpkg) and when I reload the octoprint page, it displays the proper temp/humidity for about 2 seconds, then when the site is finished loading, it goes back to 32*/0% not working like before.
Any ideas?
I had high hopes for this fix. Since I can get a reading from the script in terminal.
My problem is that i run python3, maybe I just have to edit init.py from:
cmd = sudo_str + "python " + script + str(sensor) + " " + str(pin)to
cmd = sudo_str + "python3 " + script + str(sensor) + " " + str(pin)I am mid print atm. so I cannot reboot octorpint to test. and have none result just editing the init file.
python3 getDHTTemp.py 26.1 | 33.4This plugin does not support python 3 without some changes!
It’s all over other issues, I’m working on a new version for python 3 with
major code refactoring.
Yeah, I know it doesn't support python3 yet, was only asking for a temporary fix. I tried changing init.py and had no success. I'm right on the doorstep of a solution for my issue. I've managed to get the values in cli, after a lot of hurdles. I'm running pi4 and also had issues with wiringpi not supporting my board layout. And a few other issues.
I still have hope to find a solution that will work until you finish porting over to python3. I admire the people with skills to actually make this work. Me on the other hand has no knowledge about python language, and only trying to reverse engineer on a noob level
You would definitely need to change python to python3 if that is the python
that you installed the libraries on....
Also there is the issue with getting the variable from the terminal that
needs to be applied for python3...
Changed to python3, and restarted octoprint service. but then octoprint gui would not load. Service was running, no error, so I had to reverse the changes to get it back up and running. I think I need to read up on python before I continue.
2020-11-24 15:39:34,319 - octoprint.plugins.enclosure - DEBUG - Dht result: b'22.1 | 51.6\n'2020-11-24 15:39:44,382 - octoprint.plugins.enclosure - WARNING - An exception of type TypeError occurred on log_error. Arguments:'("a bytes-like object is required, not 'str'",)I have followed all procedure and using DHT22 and it works. I also put it in dashboard for easier to see.
i finally got it working:
edited init.py with:
cmd = sudo_str + "python3 " + script + str(sensor) + " " + str(pin)temp, hum = stdout.decode().split("|")finally results:
2020-11-25 07:46:45,930 - octoprint.plugins.enclosure - DEBUG - Sensor Input 1 Temperature: 21.8 humidity 38.1temp and humidity now showing in plugin and on dashboard.
Not sure what was the issue when trying to edit init.py earlier.
maybe some formating error through terminal. i got script errors on a simple thing as =
so i updated my locale, and tried again.
2 lines edited in init.py, and using OP's script modified for DHT22 and another pin.
I could not get it working since I upgrade to python 3. I get the sensor working by console, but not by the plugin. I've tryed what was shown on this thread but not success. I did something wrong I guess, my best chance is to wait for the native compatibility with python 3 of the plugin.
This is my last log output:
2020-11-26 23:55:16,456 - octoprint.plugins.enclosure - WARNING - An exception of type NameError occurred on log_error. Arguments:
("name 'stdout' is not defined",)
I've done the process shown by the OP and this one later:
https://github.com/vitormhenrique/OctoPrint-Enclosure/issues/368#issuecomment-733504482
P.D. N00bie question. In the OP, for the pin lane, (board.DXX) means the Board or BCM number? I get confused since I've seen in this post people leaving that as "board.D4"), but in the raspberry pinout, the "board pin n4" is a 5V Power pin. My sensor's attached to GPIO4 (Board pin 7). I guess that I should leave that as "board.D7", should I?
(board.DXX) is gpio pin number..
GPIO4 should then be (board.D4)
Your error seems to be different. Stdout variable is not defined.
Did you do changes to init.py or only getDHTtemp.py?
@kincaid-egotrip
I did the change on both. Now I'm printing so I'm not sure if I can modify those on the way. However, here's two screenshots of my plugin config:
I noticed that I'm not able to change the BOARD or BCM reference for the GPIO pins. That checkbox sticks activated despite of the changes I'm appliying to it. Not a big deal, since I'm also using successfully a GPIO output for controling a LED strip, and works flawessly choosing the BOARD pin reference. I assume that it's the same for the DHT22. The sensor is attached to GPIO4, board pin number 7, isn't it? (Raspberry Pi 3B+). Anyways, I'm not getting any read in "4" (GPIO BCM), neither in "7" (GPIO BOARD).
Another issue that I notice is that my path is slightly different from the seen on this post. I'm not sure what difference does it make: (.../python2.7/... -> .../python3.7/...)
oprint/lib/python3.7/site-packages/octoprint_enclosure/getDHTTemp.py
oprint/lib/python3.7/site-packages/octoprint_enclosure/init.py
Here's another few screenshots of both archives:

While I was writing this post I figured out that maybe my error came from change the "temp, hum" lane where I shouldn't. I change it from whatever it was writted to "temp, hum = stdout.decode().split("|")" in "check_enclosure_temp" and "read_dht_temp". I didn't change "get_sensor_data".
There's no doubt with the another lane, cmd = sudo_str + "python3 " + script + str(sensor) + " " + str(pin), since I can only find it at one place on the file, located on "read_dht_temp", the last screenshot from above. I think that maybe I should only change lanes on the "read_dht_temp" function. If that's true, maybe somebody could provide me the original lane in "check_enclosure_temp" to avoid reinstall all the plugin?
As you can see, the sensor is working fine:

I think that I gave all the needed information that I'm able to recognise. Sorry for this text brick UwU.
Cheers! Thanks in advance!
P.D. OctoPrint version 1.4.2
OctoPi version 0.17.0
@Wyzed17
Try running getDHTtemp.py with python3 in CLI, and see what the output of that script is. Just to se if you get any output.
you shold see something like:
21.3 | 39.6And I think you have done more changes in init.py than needed.
There are 2 lines under read_dht_temp
cmd = sudo_str + "python " + script + str(sensor) + " " + str(pin)temp, hum = stdout.split("|")change those two lines to:
cmd = sudo_str + "python3 " + script + str(sensor) + " " + str(pin)temp, hum = stdout.decode().split("|")The reason for the changes are, Running getDHTtemp.py as python3
and: since script is executed as python3, you need to decode the result back in, or else it tries to read bytes instead of string, (or was it the other way around?)
anywho: only 2 changes needed in init.py
The script OP has posted fixes depriciated libraries,
my change, fixes python3 with new script.
I made some edits to the original post to clarify some of the things identified in the discussion above, and also…
… while I'm gratified that my terrible hack has been helpful to some folks, I do hope that someone has the time to turn this into a proper Pull Request that makes the obvious changes properly… and thereby permanently adds their name to (or adds another entry beside their name on) the rolls of the Beloved and Adored Contributors to Open Source Software. If you have the time, that could be You!
Had the same Problem with my DHT22s, changing
stdout.split("|")tostdout.decode().split("|")worked for me.I run OctoPrint in a Python3 Venv, so
pythonandpython3point to the same binary.By the way, installing the Adafruit plug-in with pip inside the Venv didn't work , installing from source did. So with these obstacles out of the way, my two DHT22 both work as expected, although sometimes reporting strange values (e.g. 3000% humidity). I guess that's caused by interference and not by software.
The changes did not work for me still all 0.
from python shell looks good in interface 0
I did
cmd = sudo_str + "python " + script + str(sensor) + " " + str(pin)
temp, hum = stdout.split("|")
change those two lines to:
cmd = sudo_str + "python3 " + script + str(sensor) + " " + str(pin)
temp, hum = stdout.decode().split("|")
Enable sensor debugging in settings and watch the OctoPrint log, should be some information what's going on there. The values are zero when an error happens while reading, and that should be printed to the log.
This is what I get
pi@octopi:~/OctoPrint/venv/lib/python3.7/site-packages/octoprint_enclosure $ python getDHTTemp.py 20
23.0 | 43
2020-12-16 19:29:05,573 - octoprint.plugins.enclosure - WARNING - An exception of type ValueError occurred on log_error. Arguments:
('not enough values to unpack (expected 2, got 1)',)
its come from temp, hum = stdout.decode().split("|")
@NA7KR you are using python 3, you need to apply this patch to get it working:
16f3ec8178Still the same error
init.txt
getDHTTemp.txt
Does that look correct had to rename to upload
just removed and install with https://github.com/vitormhenrique/OctoPrint-Enclosure/archive/master.zip
Still same error.
2020-12-16 23:41:16,824 - octoprint.plugins.enclosure - INFO - Failed to execute python scripts, try disabling use SUDO on advanced section of the plugin.
2020-12-16 23:41:16,826 - octoprint.plugins.enclosure - WARNING - An exception of type ValueError occurred on log_error. Arguments:
('not enough values to unpack (expected 2, got 1)',)
now working with above code getDHTTemp.ph but only one sensor as looks to be hard coded:
fix it
anyone use 2 sensor ? I want to put another sensor on filadryer if possible.
I added second sensor in plugin setting, it show some number but number remain showed even I take the sensor out. Seem it's not working then.
I have 2 sensors working. Did you specify different pins for each sensor? Does it work if you call the script directly?
Haven't used script yet but I did specify other pin for 2nd sensor in plugin setting.
Did you set pin in plugin setting or change it in script?
I specified everything in plugin settings. Only modification I made is described here: https://github.com/vitormhenrique/OctoPrint-Enclosure/issues/368#issuecomment-737412289

My settings menu looks like this:
Thanks so much.
Will give it a try again.
I added 2nd sensor in plugin setting and haven't put it on yet but temp dan humidity already showed in dashboard. Looks like it just copy the data from enclosure temp and humidity.
How did you this? changed stdout.split("|") to stdout.decode().split("|")
Hi.. could you post complete script for getDHTTemp.py ?
Currently I only can use 1 sensor and want to add another 1.
Thanks
full code
Thanks so much.
I have edited getDHTTemp.py and seems still only 1 sensor works.
Is there any GPIO restriction? I use GPIO 5 and 26 but both did not work.
Also, since you said that you reinstall it, is that mean that only file getDHTTemp.py was changed ?
Did you change init.py as well ?
thanks
AM2302 perfectly working with the fix for DHT22. THX!
I've one again combed through this entire thread and have tried every single suggestion mentioned, including wiping out the plugin entirely and reloading it then making changes again.
Thus far the only thing I have managed to accomplish is before tonight, while the octoprint page was loading, the sensor plugin showed the correct temp values..... now, while it loads, after it loads, ten years later............. it shows 0/0%. Why is something that should be simple this difficult to accomplish????
If you are using new releases of python 3, this plugin DOES NOT SUPPORT it, i said this many times, it is possible to make it work, but for more advanced users, the new release will support python 3 but it will take few months to be released!
Top comment updated with fixes suggested by others in this thread, and now working for me on python3 (with my super flakey DHT11, for values of "working" that return
-1 | -1about four times in five… but that does appear to be enough to get a value).I also updated the code to actually parse the command line arguments and choose DHT11 vs DHT22… which might make multiple sensors work?
(And I note @robertomilan commented that he had "AM2302 perfectly working with the fix for DHT22". I'm not sure what that means, but it might mean that if you use an AM2302 but configure it as a DHT22 it works?)
Tried the steps in first post but getting an error outside enclosure plugin
python3.7 ./getDHTTemp.py 11 4
Traceback (most recent call last):
File "./getDHTTemp.py", line 28, in
raise error
File "./getDHTTemp.py", line 18, in
temperature_c = dhtDevice.temperature
File "/home/pi/.local/lib/python3.7/site-packages/adafruit_dht.py", line 243, in temperature
self.measure()
File "/home/pi/.local/lib/python3.7/site-packages/adafruit_dht.py", line 190, in measure
pulses = self._get_pulses_bitbang()
File "/home/pi/.local/lib/python3.7/site-packages/adafruit_dht.py", line 133, in _get_pulses_bitbang
with DigitalInOut(self._pin) as dhtpin:
File "/home/pi/.local/lib/python3.7/site-packages/digitalio.py", line 118, in init
self._pin = Pin(pin.id)
AttributeError: 'str' object has no attribute 'id'
Hi
Sorry to that question, but my understanding isn`t that well. I also tried to fix according your suggestions to get DHT22 Sensor working, but without success. in terminal it works.
Did I understand it right, that you are working on new version that will take some time up to Adafruit Lib works properly on Oct enclosure plug in?
Thanks for your great job.
This fix worked for me (with a DHT22), thanks very much!
Hello
Can somebody give me a hint on what am I doing wrong:
Code in simpletest2. *.py works, so wiring is ok.
simpletest2.py looks like:
`import time
import board
import adafruit_dht
dhtDevice = adafruit_dht.DHT22(board.D4, use_pulseio=False)
while True:
try:
temperature_c = dhtDevice.temperature
humidity = dhtDevice.humidity
print(
"Temp: {:.1f} C Humidity: {}% ".format(
temperature_c, humidity
)
)
except RuntimeError as error:
print(error.args[0])
time.sleep(2.0)
continue
except Exception as error:
dhtDevice.exit()
raise error
`
OctoPrint version : 1.5.3
OctoPi version : 0.18.0
pi@octopi:~ $ python3 simpletest2.py
Temp: 22..0)4 C Humidity: 37.8%
Temp: 24.5 C Humidity: 37.4%
Temp: 24.5 C Humidity: 37.4%
Checksum did not validate. Try again.
Temp: 24.5 C Humidity: 37.4%
Temp: 24.5 C Humidity: 37.5%
KeyboardInterrupt
pi@octopi:~ $
`
I tought to adapt the getDHTtemp.py script it might work. but it isn`t.
#modif code : getDHTtemp.py looks like:
importtimeimport board
import sys
import adafruit_dht
dhtDevice = adafruit_dht.DHT22(board.D4)
dhtDevice = adafruit_dht.DHT22(board.D4, use_pulseio=False) #I also tried that version of code :(
try:
temperature_c = dhtDevice.temperature
humidity = dhtDevice.humidity
print("{:.1f} | {} ".format(temperature_c, humidity))
except RuntimeError as error:
except Exception as error:
dhtDevice.exit()
raise error
`
thanks a lot
did you have found the Issue ?
Hello
Let
s say it that way: No i didnt find an Issue but a intermediate temporary turn arround until enclosure is raised up by vitormhenrique to python v3. There are several reason why I gave up modfiying the enclosure plugin.I didn`t finished yet the coding but first looked promising: I out sourced all sensor and actors to an arduino and connected over I2C to the Raspberrypi where Octopi is running. See: Add i2c gpio support #390
Hope I could help, and you get the temp into octopi.
Here the arduino file but BE AWARE not finished and tested Yet!!
`// Copyright: Raoul Hecky
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
//
//Libraries used:
// https://github.com/milesburton/Arduino-Temperature-Control-Library.git
// https://github.com/PaulStoffregen/OneWire
// extended and modified by Francis Hartmann
#include <Wire.h>
#include <DHT.h>
#include <DHT_U.h>
const byte I2C_ADDR = 0x08;
#define DHTTYPE DHT22
//In Out
const byte PIN_TEMP_HUM_ENCLOSURE_1 =2;
const byte PIN_TEMP_CTRL_ENCLOSURE_1PWM =3;
const byte PIN_TEMP_HUM_ENCLOSURE_2 =4;
const byte PIN_Temp_CTRL_ENCLOSURE_2=5;
const byte PIN_PRINTER =12;
const byte PIN_LIGHT =13;
// library
DHT dht_ENC_1(PIN_TEMP_HUM_ENCLOSURE_1, DHTTYPE);
DHT dht_ENC_2(PIN_TEMP_HUM_ENCLOSURE_2, DHTTYPE);
float temp_ENC_1=0,hum_ENC_1=0,temp_ENC_2=0,hum_ENC_2=0;
unsigned long lastTempRequest = 0;
int delayInMillis = 0;
uint8_t lightState = HIGH;
uint8_t printerState = HIGH;
void setup()
{
Serial.begin(115200);
/// sensors.begin();
// //disable blocking code
// sensors.setWaitForConversion(false);
// sensors.requestTemperatures();
// delayInMillis = 750;
// lastTempRequest = millis();
}
void loop()
{
BootUpDelay();
requestTemperatureEnclosure_1();
digitalWrite(PIN_LIGHT, lightState);
digitalWrite(PIN_PRINTER, printerState);
}
void BootUpDelay()
{
}
void requestTemperatureEnclosure_1()
{
if (millis() - lastTempRequest >= delayInMillis) // waited long enough??
{
}
/* Action | Operation | Register | Data to write
*/
const byte REG_LIGHT = 0x01;
const byte REG_TEMP = 0x02;
const byte REG_PRINTER = 0x03;
uint8_t opcode; // register
void receiveEvent(int numBytes)
{
//read register
opcode = Wire.read();
}
union floatBytes
{
uint8_t bytes[4];
float value;
} floatContainer;
void requestEvent()
{
if (opcode == REG_LIGHT)
{
Wire.write(!lightState);
}
else if (opcode == REG_PRINTER)
{
Wire.write(!printerState);
}
else if (opcode == REG_TEMP)
{
Serial.print("Read temp: ");
Serial.print(temp_ENC_1);
Serial.println("");
}
`
Hello...
I have tried everything here....
And nothing works for me whit a dht22 :(
Hope it will be fix with the next release...
I followed the OP but sensors still read 0 in octoprint? The sensor itself worked if I manually ran the example script on the main page, before doing all of this, so the sensor is hooked up properly. Now that example script no longer works though?
If the DHT22 sensor is not going to work, what sensor is recommended to use?
Well, I needed to upgrade the SD card anyways, so took the time to reinstall everything from scratch and followed the OP exactly.
Yet it still just gives me a 0 reading on the sensor.
So any suggestions to get this working? Or better off moving to another sensor? What sensor is recommended?
Hello
The only sensor i succed to work is the DS18B20... see the readme...
But it's only for température
All the other i tried don't works with the New raspiOS...
Sad, I was wanting the humidity reading so I could keep track of the filament / humidity relationship in my garage. Temp is better then nothing though.
Any idea why the fix seems to work for most others but not for me? I know the sensor is working fine as manually getting the temp from it worked fine.
You have to know that humidity is not pertinent in the print chamber...
Because you get the humidity in air... not inside plastic...
For that i have made a DIY drybox / spooler with heating élément and a cheap température/humidity controler that i start 4 or 5 hours before printing for dry my filaments....
https://www.lesimprimantes3d.fr/forum/topic/40753-fabriquer-un-d%C3%A9shumidificateur-de-filaments-cest-possible/
Yeah, I am aware of that, more so that I can see what the ambient conditions are before/after printing to get an idea of how things react at different humidity levels. Not super important, just sucks to waste the money on these expensive sensors.
So wait for a release of octoprint-enclosure...
I hope me too... but there is the DS18B20 Who works great for this moment...
Or connect an arduino over I2C and there you got DHT11/22 sensor working. There you can also hack other interesting things.
By the way, I also didn
t got it running as it was originally planed, but thats life. honestly I absolutely understand frustration :( but honestly victorhenrique did a great job with the enclosure plugin, and I am sure once he gots time and master done, there are going to be a working plug in.Oh, I am not blaming anyone or upset, just bummed lol.
You have any quick documentation on this particular workaround? It would be nice to have my octopi display the environmental sensors on my print enclosure again before the Raspberry Pi 9 is released.
Yes and no. here I wrote a trial, but I didn`t have time to fisnish the code. But assume it gives you an idee.
https://github.com/vitormhenrique/OctoPrint-Enclosure/issues/368#issuecomment-828725508
The electrical wiring I cheked on the net for examble:
https://www.youtube.com/watch?v=me7mhrRbspk --> with or without level shift.
Crazy these hoop jumps still work. thanks.
got my DHT11 working
Im on OctoPi 1.0.0 with Python 3.9 - tried everything and the usual pip installs etc
Unable to set line 4 to input
The adafruit_dht example worked fine but I had to edit
https://github.com/vitormhenrique/OctoPrint-Enclosure/blob/master/octoprint_enclosure/getDHTTemp.py#L19
dht_dev = sensor(board.D4, use_pulseio=False)Also adding import board at the top of the file