Author Topic: "Problem Reading Temperature from DHT11 Sensor via RS485 Between Raspberry Pi 4  (Read 791 times)

0 Members and 1 Guest are viewing this topic.

Offline rdvncrtTopic starter

  • Newbie
  • Posts: 7
  • Country: tr
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


import serial
import time

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


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


temperature = 0
while True:
    data_to_send = "xtemperature"
    time.sleep(1)
    send_data(data_to_send)
    time.sleep(1)
   
    while True:
       
        received_data = RS485.readline().rstrip()
        received_data = received_data.decode('utf-8')
        print(received_data)
        time.sleep(1)
        received_value = int(received_data) 
        if 0 <= received_value <= 70:
            temperature=received_value
            break
   
    break
print("TEMPERATURE : ",temperature)


Raspberry Pico Code


from machine import Pin

import time
from dht import  DHT11

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

# UART nesnesi oluşturma
uart = machine.UART(1, baudrate=9600, tx=uart_tx, rx=uart_rx)



def response():
    received_data = uart.read()
    try:
        received_string = received_data.decode('utf-8').strip()
        print("DATA :")
        print(received_string)
        if received_string:
            if received_string[0] == 'x':
                pin = Pin(15,Pin.IN,Pin.PULL_UP)
                d = DHT11(Pin(15))
                d.measure()
                T = d.temperature()
                x=0
                while x<150:
                    x=x+1
                    T=d.temperature()
                    send_data = str(T)
                    data = (send_data + "\r\n")
                    time.sleep(1)
                    uart.write(data.encode('utf-8'))
                    print("data sent")
                    print(data)
                   
                return received_string
           
    except UnicodeError:
        pass
    return ""


while True:
    if uart.any():
        incoming_data = response()
        print(incoming_data)
 

Offline AndrewNorman

  • Contributor
  • Posts: 21
  • Country: au
Not sure on what the exact solution to your problem is, but a couple of observations.

1.   Some of the printed DHT11 datasheets I have seen to indicate that they are able to be read every 1 second but depending on the quality I have found some to be unstable reading more than once every 2 seconds.

2.   You are creating two instances of the connection for the serial device in the Raspberry PI code. Not sure if the library supports this usage, but in the past I use the same instance for both sending and receiving

Quote
RS485 = serial.Serial('/dev/serial0', baudrate=9600, timeout=1)   <---- Instance 1


def send_data(data):
    ser = serial.Serial('/dev/serial0', baudrate=9600, timeout=1)    <------ Instance 2
    ser.write(data.encode())
    print("data sent")

That might be causing some of the strange problems you are seeing.

Hope this helps narrow down the cause.
- Andrew
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf