Page 1 of 1

Connection between an esp8266 and a rs485 to get sensor values

Posted: Wed Jul 27, 2022 11:16 am
by AntonioBrito
Hello guys, im currently working on a project where I need to get an array of hex decimal values, to do it I need to send via uart an array to ask for the values needed( temperature),
my esp8266 is connected to max485 ( rs485 to TTL converter) , im gonna post the code below so I can figure out what my problem is:

Code: Select all

from machine import Pin
from machine import UART
import utime
import uos
led= machine.Pin(14,machine.Pin.OUT)
rts= machine.Pin ( 12, machine.Pin.OUT)

modbus = UART(0)
modbus.init(baudrate=9600, parity=0, bits=8, stop=1, timeout=500, timeout_char=2, tx=Pin(1), rx=Pin(3))

def leer_sensor():
    global result
    print("modbus")
    uos.dupterm(None, 1)
    rts.on()
    modbus.write(b'\xFF\x03\x00\x09\x00\x01\x41\xD6')
    utime.sleep(0.1)
    rst.off()
    result = modbus.readline()
    print(result)
    utime.sleep(0.5)
    uart = UART(0, 115200)
    uos.dupterm(uart, 1)
    led.on()
    utime.sleep(5)
   
    return()

while True: 
    leer_sensor()

Re: Connection between an esp8266 and a rs485 to get sensor values

Posted: Wed Jul 27, 2022 12:18 pm
by karfas
AntonioBrito wrote:
Wed Jul 27, 2022 11:16 am

Code: Select all

from machine import Pin
    rts.on()
    modbus.write(b'\xFF\x03\x00\x09\x00\x01\x41\xD6')
    utime.sleep(0.1)
    rst.off()
First, there is a typo in the code you posted (it shoud be rts.off(), isn't it?).
Second, a RS485 requires precise timing when switching the sender on and off. The attached device is free to send immediate after it receives the last character of the poll message.
I don't know whether the proposed uart_wait_tx_done() - see viewtopic.php?f=18&t=9116&p=51626&hilit ... ifo#p51402 - is implemented or if the ESP8266 can switch the RTS pin by itself. For the ESP32, there is a half-duplex option (at least on ESP-IDF level) to do this.

Re: Connection between an esp8266 and a rs485 to get sensor values

Posted: Wed Jul 27, 2022 2:55 pm
by AntonioBrito
i appreciate your answer im going to try to implement your idea, but how can I see what I am receiving from the sensor if uart0 is the repl ?
Do you think esp32 is a better option for my project?
with esp 32 can I use 2 uart at the same time, for example one for the repl and one for the communication with the sensor?

Re: Connection between an esp8266 and a rs485 to get sensor values

Posted: Wed Jul 27, 2022 7:04 pm
by karfas
AntonioBrito wrote:
Wed Jul 27, 2022 2:55 pm
i appreciate your answer im going to try to implement your idea, but how can I see what I am receiving from the sensor if uart0 is the repl ?
Do you think esp32 is a better option for my project?
with esp 32 can I use 2 uart at the same time, for example one for the repl and one for the communication with the sensor?
20 years ago we attached a second RS485/RS232 converter to the RS485 bus and looked at the data stream from a VT220. If I remember correctly, this had a special mode to show control characters like STX/ETX and the like.
Yes, you can use both UARTs on the ESP32. In my opinion, the ESP32 is the better choice for any hobby project. Of course it looks different when you are going to produce thousands of devices and then the price difference matters.

Re: Connection between an esp8266 and a rs485 to get sensor values

Posted: Wed Jul 27, 2022 11:06 pm
by AntonioBrito
thank you for your reply :)