Idahowalker
Posts: 555
Joined: Wed Jan 03, 2018 5:43 pm

Re: Implement temperature and humidity(DHT11) using Python

Sat Sep 19, 2020 4:35 pm

jayleong1992 wrote:
Sat Sep 19, 2020 3:00 pm
Idahowalker wrote:
Sat Sep 19, 2020 2:43 pm
jayleong1992 wrote:
Sat Sep 19, 2020 2:38 pm


Is that I just follow the code provided and follow the pin I use ?
makes sense.
Follow the code and my pin getting this
pi@raspberrypi:~ $ python3 test.py
Usage: sudo ./Adafruit_DHT.py [11|22|2302] <GPIO pin number>
Example: sudo ./Adafruit_DHT.py 2302 4 - Read from an AM2302 connected to GPIO pin #4
My opinion, use another library for it seems the one you are using is not doing the thing.
Without knowing why you are deleting my postings, I will not know how...

pcmanbob
Posts: 9878
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Implement temperature and humidity(DHT11) using Python

Sat Sep 19, 2020 5:41 pm

I think the ImportError: cannot import name 'Beaglebone_Black_Driver' from 'Adafruit_DHT' is due to the adafruit code being written before the pi4 was release so it does not have the pi 4 in the code so defaults to Beaglebone_Black.

I seem to remember that someone posted a fix for this showing the line you needed to add to the adafruit code, but I cant find.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

jayleong1992
Posts: 50
Joined: Sat Aug 29, 2020 10:53 am

Re: Implement temperature and humidity(DHT11) using Python

Sun Sep 20, 2020 4:17 am

Idahowalker wrote:
Sat Sep 19, 2020 4:35 pm
jayleong1992 wrote:
Sat Sep 19, 2020 3:00 pm
Idahowalker wrote:
Sat Sep 19, 2020 2:43 pm


makes sense.
Follow the code and my pin getting this
pi@raspberrypi:~ $ python3 test.py
Usage: sudo ./Adafruit_DHT.py [11|22|2302] <GPIO pin number>
Example: sudo ./Adafruit_DHT.py 2302 4 - Read from an AM2302 connected to GPIO pin #4
My opinion, use another library for it seems the one you are using is not doing the thing.
Noted and Thanks.
I have look for other library related for DHT11 but there is without LCD 16x2 component. It was able to function as well.

Code: Select all

import RPi.GPIO as GPIO
import dht11
import time
import datetime

# initialize GPIO
GPIO.setwarnings(True)
GPIO.setmode(GPIO.BCM)

# read data using pin 4
instance = dht11.DHT11(pin=4)

try:
        while True:
            result = instance.read()
            if result.is_valid():
                print("Last valid input: " + str(datetime.datetime.now()))
                print("Temperature: %-3.1f C" % result.temperature)
                print("Humidity: %-3.1f %%" % result.humidity)

            time.sleep(6)

except KeyboardInterrupt:
    print("Cleanup")
    GPIO.cleanup()
pi@raspberrypi:~/DHT11_Python $ python3 temperature.py
Last valid input: 2020-09-20 12:05:39.492301
Temperature: 31.2 C
Humidity: 73.0 %
Last valid input: 2020-09-20 12:05:45.576771
Temperature: 31.7 C
Humidity: 71.0 %
^CCleanup
But for my study final project are require to install LCD 16x2 component to display the temperature and humidity.
Below code was after I added LCD code, but I facing error which I dont know is because my coding problem or I miss out some library ?

Code: Select all

import RPi.GPIO as GPIO
import dht11
import time
import datetime
import board
import digitalio
import Adafruit_CharLCD as LCD

# initialize GPIO
GPIO.setwarnings(True)
GPIO.setmode(GPIO.BCM)

# read data using pin 4
instance = dht11.DHT11(pin=4)

# Modify this if you have a different sized character LCD
lcd_columns = 16
lcd_rows = 2

GPIO.setwarnings(True)
GPIO.setmode(GPIO.BCM)

# read data using pin 4
instance = dht11.DHT11(pin=4)

# Modify this if you have a different sized character LCD
lcd_columns = 16
lcd_rows = 2
 
# compatible with all versions of RPI as of Jan. 2019
# v1 - v3B+

lcd_rs = 37 #RS of LCD is connected to GPIO 37 on PI
lcd_en = 35 #EN of LCD is connected to GPIO 35 on PI 
lcd_d4 = 33 #D4 of LCD is connected to GPIO 33 on PI
lcd_d5 = 31 #D5 of LCD is connected to GPIO 31 on PI
lcd_d6 = 29 #D6 of LCD is connected to GPIO 29 on PI
lcd_d7 = 23 #D7 of LCD is connected to GPIO 23 on PI

lcd = LCD.Adafruit_CharLCD(str(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, 
                                lcd_columns, lcd_rows))

try:
        while True:
            result = instance.read()
            if result.is_valid():
                print("Last valid input: " + str(datetime.datetime.now()))
                print("Temperature: %-3.1f C" % result.temperature)
                print("Humidity: %-3.1f %%" % result.humidity)
		lcd_line_1 = ("Temperature: %-3.1f C" % result.temperature)
		lcd_line_2 = ("Humidity: %-3.1f %%" % result.humidity)
		lcd.message = lcd_line_1 + lcd_line_2
		
            time.sleep(6)

except KeyboardInterrupt:
    print("Cleanup")
    GPIO.cleanup()
Below the error I faced.
pi@raspberrypi:~/DHT11_Python $ python3 temperature.py
Traceback (most recent call last):
File "test3.py", line 35, in <module>
lcd_columns, lcd_rows))
TypeError: str() takes at most 3 arguments (8 given)
pi@raspberrypi:~/DHT11_Python $ python3 temperature.py
File "test3.py", line 44
lcd_line_1 = ("Temperature: %-3.1f C" % result.temperature)
^
TabError: inconsistent use of tabs and spaces in indentation
pi@raspberrypi:~/DHT11_Python $

jayleong1992
Posts: 50
Joined: Sat Aug 29, 2020 10:53 am

Re: Implement temperature and humidity(DHT11) using Python

Sun Sep 20, 2020 4:25 am

pcmanbob wrote:
Sat Sep 19, 2020 5:41 pm
I think the ImportError: cannot import name 'Beaglebone_Black_Driver' from 'Adafruit_DHT' is due to the adafruit code being written before the pi4 was release so it does not have the pi 4 in the code so defaults to Beaglebone_Black.

I seem to remember that someone posted a fix for this showing the line you needed to add to the adafruit code, but I cant find.

Thanks pcmanbob. I tried to search for solution from google, but I didn't find it.
I have look for other DHT11 related library like Idahowalker advised.
It's work, but without the LCD 16x2 display code.

Idahowalker
Posts: 555
Joined: Wed Jan 03, 2018 5:43 pm

Re: Implement temperature and humidity(DHT11) using Python

Sun Sep 20, 2020 11:58 am

Did you fix this error
pi@raspberrypi:~/DHT11_Python $ python3 temperature.py
Traceback (most recent call last):
File "test3.py", line 35, in <module>
lcd_columns, lcd_rows))
TypeError: str() takes at most 3 arguments (8 given)
Without knowing why you are deleting my postings, I will not know how...

jayleong1992
Posts: 50
Joined: Sat Aug 29, 2020 10:53 am

Re: Implement temperature and humidity(DHT11) using Python

Sun Sep 20, 2020 1:04 pm

Idahowalker wrote:
Sun Sep 20, 2020 11:58 am
Did you fix this error
pi@raspberrypi:~/DHT11_Python $ python3 temperature.py
Traceback (most recent call last):
File "test3.py", line 35, in <module>
lcd_columns, lcd_rows))
TypeError: str() takes at most 3 arguments (8 given)
Sry Idahowalker, please forgive me...I really no idea how to fix this error. Is that like this? lcd = LCD.Adafruit_CharLCD(str(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows))

Idahowalker
Posts: 555
Joined: Wed Jan 03, 2018 5:43 pm

Re: Implement temperature and humidity(DHT11) using Python

Sun Sep 20, 2020 1:14 pm

jayleong1992 wrote:
Sun Sep 20, 2020 1:04 pm
Idahowalker wrote:
Sun Sep 20, 2020 11:58 am
Did you fix this error
pi@raspberrypi:~/DHT11_Python $ python3 temperature.py
Traceback (most recent call last):
File "test3.py", line 35, in <module>
lcd_columns, lcd_rows))
TypeError: str() takes at most 3 arguments (8 given)
Sry Idahowalker, please forgive me...I really no idea how to fix this error. Is that like this? lcd = LCD.Adafruit_CharLCD(str(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows))
Really? Go to https://www.w3schools.com/python/ref_fu ... %20More%20 and read up on the str(), I'm not going to give lessons on using the Python str() method.

lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows) <<<which one of those input parameters needs to be a string?
Without knowing why you are deleting my postings, I will not know how...

jayleong1992
Posts: 50
Joined: Sat Aug 29, 2020 10:53 am

Re: Implement temperature and humidity(DHT11) using Python

Sun Sep 20, 2020 1:44 pm

Idahowalker wrote:
Sun Sep 20, 2020 1:14 pm
jayleong1992 wrote:
Sun Sep 20, 2020 1:04 pm
Idahowalker wrote:
Sun Sep 20, 2020 11:58 am
Did you fix this error
Sry Idahowalker, please forgive me...I really no idea how to fix this error. Is that like this? lcd = LCD.Adafruit_CharLCD(str(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows))
Really? Go to https://www.w3schools.com/python/ref_fu ... %20More%20 and read up on the str(), I'm not going to give lessons on using the Python str() method.

lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows) <<<which one of those input parameters needs to be a string?
Noted, let me go thru the link you provided. Thanks

Idahowalker
Posts: 555
Joined: Wed Jan 03, 2018 5:43 pm

Re: Implement temperature and humidity(DHT11) using Python

Sun Sep 20, 2020 2:33 pm

str(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows) <<< Do you see what's wrong with the string conversion function and why there is an indication of this
TypeError: str() takes at most 3 arguments (8 given)
error?
Without knowing why you are deleting my postings, I will not know how...

pcmanbob
Posts: 9878
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Implement temperature and humidity(DHT11) using Python

Sun Sep 20, 2020 7:57 pm

jayleong1992

Why are you constantly chopping and changing which LCD drive you are using ?

pick one get it working without the temperature sensor reading bit so that you can just put 2 lines of simple text on the LCD,

then take your now working example of the temp sensor reading code and add that to your working LCD drive code.

So for example does this code using RPLCD write to your LCD ?

Code: Select all

#!/usr/bin/python3

import time  
from RPLCD.gpio import CharLCD
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)

lcd = CharLCD(numbering_mode=GPIO.BOARD, cols=16, rows=2, pin_rs=37, pin_e=35, pins_data=[33, 31, 29, 23],charmap='A02', auto_linebreaks=True, compat_mode=True)

while True:

    lcd.clear()
    lcd.cursor_pos = (0, 0)
    lcd.write_string("This is line 1")
    lcd.cursor_pos = (1, 0)
    lcd.write_string("This is line 2)
    time.sleep(3)

    
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

jayleong1992
Posts: 50
Joined: Sat Aug 29, 2020 10:53 am

Re: Implement temperature and humidity(DHT11) using Python

Mon Sep 21, 2020 1:26 pm

pcmanbob wrote:
Sun Sep 20, 2020 7:57 pm
jayleong1992

Why are you constantly chopping and changing which LCD drive you are using ?

pick one get it working without the temperature sensor reading bit so that you can just put 2 lines of simple text on the LCD,

then take your now working example of the temp sensor reading code and add that to your working LCD drive code.

So for example does this code using RPLCD write to your LCD ?

wahhhhhh....Many thanks for advise. I will follow your step to look for LCD library first which is working then I only add on temp reading code

Code: Select all

#!/usr/bin/python3

import time  
from RPLCD.gpio import CharLCD
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)

lcd = CharLCD(numbering_mode=GPIO.BOARD, cols=16, rows=2, pin_rs=37, pin_e=35, pins_data=[33, 31, 29, 23],charmap='A02', auto_linebreaks=True, compat_mode=True)

while True:

    lcd.clear()
    lcd.cursor_pos = (0, 0)
    lcd.write_string("This is line 1")
    lcd.cursor_pos = (1, 0)
    lcd.write_string("This is line 2)
    time.sleep(3)

    
I tried run the code you provide, getting error as below
pi@raspberrypi:~ $ python3 myproject.py
Traceback (most recent call last):
File "myproject.py", line 9, in <module>
lcd = CharLCD(numbering_mode=GPIO.BOARD, cols=16, rows=2, pin_rs=37, pin_e=35, pins_data=[33, 31, 29, 23],charmap='A02', auto_linebreaks=True, compat_mode=True)
TypeError: __init__() got an unexpected keyword argument 'compat_mode'

pcmanbob
Posts: 9878
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Implement temperature and humidity(DHT11) using Python

Mon Sep 21, 2020 6:18 pm

Well I have just installed RPLCD 1.3.0 on one of my test pi4's and the code only had one error which was a missing set of quoters on line 17

corrected line lcd.write_string("This is line 2")

So how did you indtall RPLCD 1.3.0 on your pi ?

Recommended way is to use pip3

Code: Select all

sudo apt update
sudo apt install python3-pip
sudo pip3 install RPLCD
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

jayleong1992
Posts: 50
Joined: Sat Aug 29, 2020 10:53 am

Re: Implement temperature and humidity(DHT11) using Python

Wed Sep 23, 2020 1:50 pm

pcmanbob wrote:
Mon Sep 21, 2020 6:18 pm
Well I have just installed RPLCD 1.3.0 on one of my test pi4's and the code only had one error which was a missing set of quoters on line 17

corrected line lcd.write_string("This is line 2")

So how did you indtall RPLCD 1.3.0 on your pi ?

Recommended way is to use pip3

Code: Select all

sudo apt update
sudo apt install python3-pip
sudo pip3 install RPLCD
Hi pcmanbob, many thanks for guide and advise, its work now...
But I can't use "python3 test.py" to run the file, it was showing error as below
Traceback (most recent call last):
File "test2.py", line 9, in <module>
lcd = CharLCD(numbering_mode=GPIO.BOARD, cols=16, rows=2, pin_rs=37, pin_e=35, pins_data=[33, 31, 29, 23],charmap='A02', auto_linebreaks=True, compat_mode=True)
If I was changed it to "sudo python test.py", it's work and display on LCD.

pcmanbob
Posts: 9878
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Implement temperature and humidity(DHT11) using Python

Wed Sep 23, 2020 1:59 pm

It should work without sudo ,

Please post the full error message you are missing the error from the bottom of the lines you posted.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

jayleong1992
Posts: 50
Joined: Sat Aug 29, 2020 10:53 am

Re: Implement temperature and humidity(DHT11) using Python

Sat Sep 26, 2020 4:31 am

pcmanbob wrote:
Wed Sep 23, 2020 1:59 pm
It should work without sudo ,

Please post the full error message you are missing the error from the bottom of the lines you posted.
Noted.
I have go thru the code which you provided

Code: Select all

sudo apt update
sudo apt install python3-pip
sudo pip3 install RPLCD
This is the error when I used "python3 test2.py" to execute it.
pi@raspberrypi:~ $ python3 test2.py
Traceback (most recent call last):
File "test2.py", line 9, in <module>
lcd = CharLCD(numbering_mode=GPIO.BOARD, cols=16, rows=2, pin_rs=37, pin_e=35, pins_data=[33, 31, 29, 23],charmap='A02', auto_linebreaks=True, compat_mode=True)
TypeError: __init__() got an unexpected keyword argument 'compat_mode'
Below I used with sudo, it successful to execute and the lcd display worked as well
pi@raspberrypi:~ $ sudo python test2.py
Attachment was after I tried both method. Thanks
Attachments
Capture.JPG
Capture.JPG (70.18 KiB) Viewed 304 times

pcmanbob
Posts: 9878
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Implement temperature and humidity(DHT11) using Python

Sat Sep 26, 2020 8:30 am

My be with all the different LCD drivers you have tried there is a conflict occurring,
I would suggest you try flashing a fresh copy of raspberry pi OS on to another SD card then once updated you try installing RPLCD again and see if it works correctly then ,
as I said it works fine for me using raspberry pi os lite , running the python code without sudo.

The only time you need to use sudo is when doing the RPLCD install
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

jayleong1992
Posts: 50
Joined: Sat Aug 29, 2020 10:53 am

Re: Implement temperature and humidity(DHT11) using Python

Sat Sep 26, 2020 10:08 am

pcmanbob wrote:
Sat Sep 26, 2020 8:30 am
My be with all the different LCD drivers you have tried there is a conflict occurring,
I would suggest you try flashing a fresh copy of raspberry pi OS on to another SD card then once updated you try installing RPLCD again and see if it works correctly then ,
as I said it works fine for me using raspberry pi os lite , running the python code without sudo.

The only time you need to use sudo is when doing the RPLCD install
I was using Raspberry Pi OS Full (32 bits) thru NOOBS
During the NOOBS installation, there was 2 selection which is OS Lite/OS full.
I have no idea what different on OS Lite/OS full.

pcmanbob
Posts: 9878
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Implement temperature and humidity(DHT11) using Python

Sat Sep 26, 2020 10:13 am

It should work correctly with the full ( IE with desktop ) or lite versions ( command line only ).
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

jayleong1992
Posts: 50
Joined: Sat Aug 29, 2020 10:53 am

Re: Implement temperature and humidity(DHT11) using Python

Sat Sep 26, 2020 10:27 am

pcmanbob wrote:
Sat Sep 26, 2020 10:13 am
It should work correctly with the full ( IE with desktop ) or lite versions ( command line only ).
I download from official website https://www.raspberrypi.org/downloads/noobs/
But I have no idea why I'm facing this problem
I was think the previous my posted Adafruit facing the error is that related also?
Now I tried both ( DHT11 + LCD 16x2) put it together

This is DHT11 which is working

Code: Select all

import RPi.GPIO as GPIO
import dht11
import time
import datetime

# initialize GPIO
GPIO.setwarnings(True)
GPIO.setmode(GPIO.BCM)

# read data using pin 4
instance = dht11.DHT11(pin=4)

try:
        while True:
            result = instance.read()
            if result.is_valid():
                print("Last valid input: " + str(datetime.datetime.now()))
                print("Temperature: %-3.1f C" % result.temperature)
                print("Humidity: %-3.1f %%" % result.humidity)

            time.sleep(6)

except KeyboardInterrupt:
    print("Cleanup")
    GPIO.cleanup()


LCD work as well (with sudo only able to run)

Code: Select all

#!/usr/bin/python3

import time  
from RPLCD.gpio import CharLCD
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)

lcd = CharLCD(numbering_mode=GPIO.BOARD, cols=16, rows=2, pin_rs=37, pin_e=35, pins_data=[33, 31, 29, 23],charmap='A02', auto_linebreaks=True, compat_mode=True)

while True:

    lcd.clear()
    lcd.cursor_pos = (0, 0)
    lcd.write_string("This is line 1")
    lcd.cursor_pos = (1, 0)
    lcd.write_string("This is line 2)
    time.sleep(3)


This is after I put it together, but getting error.

Code: Select all

!/usr/bin/python3

import time
import dht11
import datetime  
from RPLCD.gpio import CharLCD
import RPi.GPIO as GPIO


# initialize GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)

# read data using pin 4
instance = dht11.DHT11(pin=4)

lcd = CharLCD(numbering_mode=GPIO.BOARD, cols=16, rows=2,
 pin_rs=37, pin_e=35,pin_e=35, pins_data=[33, 31, 29, 23],
 charmap='A02', auto_linebreaks=True, compat_mode=True)


while True:


        lcd.clear()
        lcd.cursor_pos = (0, 0)
        lcd.write_string(""Temperature: %-3.1f C" % result.temperature")
        lcd.cursor_pos = (1, 0)
        lcd.write_string("Humidity: %-3.1f %%" % result.humidity)
        time.sleep(3)


try:
    while True:

        result = instance.read()
        if result.is_valid():
        print("Last valid input: " + str(datetime.datetime.now()))
        print("Temperature: %-3.1f C" % result.temperature)
        print("Humidity: %-3.1f %%" % result.humidity)



except KeyboardInterrupt:
    print("Cleanup")
    GPIO.cleanup()

pcmanbob
Posts: 9878
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Implement temperature and humidity(DHT11) using Python

Sat Sep 26, 2020 11:18 am

jayleong1992 wrote:
Sat Sep 26, 2020 10:27 am

I download from official website https://www.raspberrypi.org/downloads/noobs/
But I have no idea why I'm facing this problem
I was think the previous my posted Adafruit facing the error is that related also?
did you do as I suggested and start over with a fresh install of the OS ?

Now I tried both ( DHT11 + LCD 16x2) put it together

This is after I put it together, but getting error.
Though you would have learned by now that you need to post the error message.............. ;)
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

jayleong1992
Posts: 50
Joined: Sat Aug 29, 2020 10:53 am

Re: Implement temperature and humidity(DHT11) using Python

Sat Sep 26, 2020 11:29 am

pcmanbob wrote:
Sat Sep 26, 2020 11:18 am
jayleong1992 wrote:
Sat Sep 26, 2020 10:27 am

I download from official website https://www.raspberrypi.org/downloads/noobs/
But I have no idea why I'm facing this problem
I was think the previous my posted Adafruit facing the error is that related also?
did you do as I suggested and start over with a fresh install of the OS ?

:cry: I only have one SD card which current in used

Now I tried both ( DHT11 + LCD 16x2) put it together

This is after I put it together, but getting error.
Though you would have learned by now that you need to post the error message.............. ;)
sry...sry...I was forgot
below is the error message I faced
pi@raspberrypi:~/DHT11_Python $ sudo python test3.py
File "test3.py", line 1
!/usr/bin/python3
^
SyntaxError: invalid syntax

pcmanbob
Posts: 9878
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Implement temperature and humidity(DHT11) using Python

Sat Sep 26, 2020 12:36 pm

Your shebang line is missing the first character which should be #,

but if you are calling the program with " python3 script.py ", you don't actually need the shebang line anyway.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

jayleong1992
Posts: 50
Joined: Sat Aug 29, 2020 10:53 am

Re: Implement temperature and humidity(DHT11) using Python

Sun Sep 27, 2020 3:01 am

pcmanbob wrote:
Sat Sep 26, 2020 12:36 pm
Your shebang line is missing the first character which should be #,

but if you are calling the program with " python3 script.py ", you don't actually need the shebang line anyway.
Noted and thanks.
I have face another error which is
pi@raspberrypi:~/DHT11_Python $ sudo python test3.py
File "test3.py", line 38
print("Last valid input: " + str(datetime.datetime.now()))
^
IndentationError: expected an indented block
but I was follow back the exact code, it should be work ?

jayleong1992
Posts: 50
Joined: Sat Aug 29, 2020 10:53 am

Re: Implement temperature and humidity(DHT11) using Python

Sun Sep 27, 2020 3:28 am

jayleong1992 wrote:
Sun Sep 27, 2020 3:01 am
pcmanbob wrote:
Sat Sep 26, 2020 12:36 pm
Your shebang line is missing the first character which should be #,

but if you are calling the program with " python3 script.py ", you don't actually need the shebang line anyway.
Noted and thanks.
I have face another error which is
pi@raspberrypi:~/DHT11_Python $ sudo python test3.py
File "test3.py", line 38
print("Last valid input: " + str(datetime.datetime.now()))
^
IndentationError: expected an indented block
but I was follow back the exact code, it should be work ?
I was solved the problem....
now I facing
pi@raspberrypi:~/DHT11_Python $ sudo python test3.py
Traceback (most recent call last):
File "test3.py", line 26, in <module>
lcd.write_string("Temperature: %-3.1f C" % result.temperature)
NameError: name 'result' is not defined
but "result" I already defined
result = instance.read()
if result.is_valid():

pcmanbob
Posts: 9878
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Implement temperature and humidity(DHT11) using Python

Sun Sep 27, 2020 8:06 am

You cant have 2 while true loops in your program as you have because once the program enters the first loop it will never run the second loop.

Also you are attempting to put the result on the LCD before it has been read.

try this version of your code with only one loop

Code: Select all

#!/usr/bin/python3

import time
import dht11
import datetime  
from RPLCD.gpio import CharLCD
import RPi.GPIO as GPIO


# initialize GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)

# read data using pin 4
instance = dht11.DHT11(pin=4)

lcd = CharLCD(numbering_mode=GPIO.BOARD, cols=16, rows=2,
 pin_rs=37, pin_e=35,pin_e=35, pins_data=[33, 31, 29, 23],
 charmap='A02', auto_linebreaks=True, compat_mode=True)


try:
    while True:

        result = instance.read()
        if result.is_valid():
        print("Last valid input: " + str(datetime.datetime.now()))
        print("Temperature: %-3.1f C" % result.temperature)
        print("Humidity: %-3.1f %%" % result.humidity)

        lcd.clear()
        lcd.cursor_pos = (0, 0)
        lcd.write_string(""Temperature: %-3.1f C" % result.temperature")
        lcd.cursor_pos = (1, 0)
        lcd.write_string("Humidity: %-3.1f %%" % result.humidity)
        time.sleep(3)

except KeyboardInterrupt:
    print("Cleanup")
    GPIO.cleanup()
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Python”