"Problem Reading Temperature from DHT11 Sensor via RS485 Between Raspberry Pi 4 and Raspberry Pi Pic

OP #7498208
Rate this post
• ▲ useful
▼ not useful

I want to read the temperature data from the DHT11 sensor connected to the Pico using RS485 between Raspberry Pi 4 and Raspberry Pi Pico. I use the following code for this, but when I use it like this, I can send data from Raspberry Pi without any problems. However, when I send data from Raspberry Pi Pico, if I assume the temperature is 25, sometimes I can read this value from Raspberry Pi and sometimes I can't. I tried to adjust the correct time with Time.sleep() but I couldn't succeed. How can I solve this?

Raspberry Pi Code

1
import serial
2
import time
3

4
RS485 = serial.Serial('/dev/serial0', baudrate=9600, timeout=1)
5

6

7
def send_data(data):
8
    ser = serial.Serial('/dev/serial0', baudrate=9600, timeout=1)
9
    ser.write(data.encode())
10
    print("data sent")
11

12

13
temperature = 0
14
while True:
15
    data_to_send = "xtemperature"
16
    time.sleep(1)
17
    send_data(data_to_send)
18
    time.sleep(1)
19
    
20
    while True:
21
        
22
        received_data = RS485.readline().rstrip()
23
        received_data = received_data.decode('utf-8')
24
        print(received_data)
25
        time.sleep(1)
26
        received_value = int(received_data)  
27
        if 0 <= received_value <= 70:
28
            temperature=received_value
29
            break
30
    
31
    break
32
print("TEMPERATURE : ",temperature)

Raspberry Pi Pico Code

1
from machine import Pin
2

3
import time
4
from dht import  DHT11
5

6
uart_tx = machine.Pin(4)  # TX pin: GPIO 0
7
uart_rx = machine.Pin(5)  # RX pin: GPIO 1
8

9

10
uart = machine.UART(1, baudrate=9600, tx=uart_tx, rx=uart_rx)
11

12

13

14
def response():
15
    received_data = uart.read()
16
    try:
17
        received_string = received_data.decode('utf-8').strip()
18
        print("DATA :")
19
        print(received_string)
20
        if received_string:
21
            if received_string[0] == 'x':
22
                pin = Pin(15,Pin.IN,Pin.PULL_UP)
23
                d = DHT11(Pin(15))
24
                d.measure()
25
                T = d.temperature()
26
                x=0
27
                while x<150:
28
                    x=x+1
29
                    T=d.temperature()
30
                    send_data = str(T)
31
                    data = (send_data + "\r\n")
32
                    time.sleep(1)
33
                    uart.write(data.encode('utf-8'))
34
                    print("data sent")
35
                    print(data)
36
                    
37
                return received_string
38
            
39
    except UnicodeError:
40
        pass
41
    return ""
42

43

44
while True:
45
    if uart.any():
46
        incoming_data = response()
47
        print(incoming_data)

Reply

Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.

Formatting
  • [c]C-Code[/c]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.