Connection between an esp8266 and a rs485 to get sensor values

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
AntonioBrito
Posts: 7
Joined: Wed Jul 27, 2022 10:39 am

Connection between an esp8266 and a rs485 to get sensor values

Post by AntonioBrito » Wed Jul 27, 2022 11:16 am

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()

User avatar
karfas
Posts: 193
Joined: Sat Jan 16, 2021 12:53 pm
Location: Vienna, Austria

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

Post by karfas » Wed Jul 27, 2022 12:18 pm

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.
A few hours of debugging might save you from minutes of reading the documentation! :D
My repositories: https://github.com/karfas

AntonioBrito
Posts: 7
Joined: Wed Jul 27, 2022 10:39 am

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

Post by AntonioBrito » 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?

User avatar
karfas
Posts: 193
Joined: Sat Jan 16, 2021 12:53 pm
Location: Vienna, Austria

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

Post by karfas » Wed Jul 27, 2022 7:04 pm

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.
A few hours of debugging might save you from minutes of reading the documentation! :D
My repositories: https://github.com/karfas

AntonioBrito
Posts: 7
Joined: Wed Jul 27, 2022 10:39 am

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

Post by AntonioBrito » Wed Jul 27, 2022 11:06 pm

thank you for your reply :)

Post Reply