0

I am running a setup of 4 ESP32 which are connected to a PI through serial connections. Is there a way to statically bind all of the ESPs, so the port order after every boot doesn't matter? It also has to be ID unspecific, since I'm running several similar setups and the code should fit all. Also, in case of a replacement, I just want the system to know that there is the same device on the same port.

I'm sorry if I wrote it confusing, but generally, I want the ESPs to be replaceable, while the port describes the function of the device.

Thanks guys and happy coding

sawdust
  • 16,103
  • 3
  • 40
  • 50
Achim
  • 11
  • 3
  • This seems off-topic for this site. Also the OS does not really care what is on the remote end of a serial link. That is an application program's problem to solve. – sawdust Jul 12 '21 at 07:34
  • You're (mis)using *"serial ports"* and *"serial connections"* to mean some kind of USB adapter??? – sawdust Jul 12 '21 at 18:38
  • The ESPs are connected via USB to the Pi since they are sitting in the same box – Achim Jul 13 '21 at 08:05
  • For your information, USB is a bus, and adds its own protocol layer(s) to the connection. A USB connection using adapters or using CDC ADM may in effect seem like a *"serial connection"*, but is actually much more complicated (both HW & SW) than using UARTs. You completely conceal what you have by neglecting to mention USB anywhere. – sawdust Jul 13 '21 at 17:58

3 Answers3

3

The only practical approach is ID based, sorry. USB devices are created in the same order they are initialized, so no guarantees whatsoever. If it helps, here's an udev rule which creates symlinks from the serial number of the Silabs USB-serial chip, e.g. /dev/EDK-d80d38b81e. Tested with DevKitC and LOLIN32 boards.

# udev rules for the Espressif ESP32-DevKitC and Wemos LOLIN32 dev boards to
# allocate symlinks with fixed names for each device using its serial number.

# CP2102N/CP2104N with idVendor=10c4, idProduct=ea60
#
# Instructions:
# 1. Copy udev rules and restart daemon:
#   $ sudo ln -s "$PWD/70-cp210xn_ESP32-DevKitC.rules" /etc/udev/rules.d/ && sudo systemctl restart udev
# 2. Disconnect and connect the board to USB

# Espressif ESP32-DevKitC with CP2102N.
# Sample serial in chip is "7063b99e4b74ea11b6f52208cf25bb41" where only first
# 10 chars seem to be unique. We cut the serial down to those and create a
# symlink from the result, e.g. "/dev/EDK-7063b99e4b"
SUBSYSTEMS=="usb", KERNEL=="ttyUSB*", ATTRS{idVendor}=="10c4", ATTRS{idProduct}=="ea60", ATTRS{manufacturer}=="Silicon Labs", ATTRS{product}=="CP2102N USB to UART Bridge Controller",  PROGRAM="/usr/bin/awk -- 'BEGIN { print substr(\"$attr{serial}\",1,10) }'" SYMLINK+="EDK-%c", GROUP="dialout", MODE="0660"

# Wemos LOLIN32 with CP2104N, sample serial in chip is "01DFA32C".
# We create a symlink "EDK-01DFA32C" (cropping to first 10 chars just in case).
SUBSYSTEMS=="usb", KERNEL=="ttyUSB*", ATTRS{idVendor}=="10c4", ATTRS{idProduct}=="ea60", ATTRS{manufacturer}=="Silicon Labs", ATTRS{product}=="CP2104 USB to UART Bridge Controller",  PROGRAM="/usr/bin/awk -- 'BEGIN { print substr(\"$attr{serial}\",1,10) }'" SYMLINK+="EDK-%c", GROUP="dialout", MODE="0660"

Tarmo
  • 3,728
  • 1
  • 8
  • 25
1

I had a similar issue, where I needed to statically bind physical USB ports to devices, in my case they are cheap serial/USB converters and they don't have a unique ID, so the only way to identify them was using physical port mapping. I solved creating udev rules with aliases, like this:

SUBSYSTEM=="tty",KERNELS=="2-2:1.0",SYMLINK+="device1"
SUBSYSTEM=="tty",KERNELS=="2-3:1.0",SYMLINK+="device2"

Now I open the connection with the devices using the symlinks. In this way as long as you don't swap connectors, each device will always have the same symlink.

rok
  • 2,574
  • 3
  • 23
  • 44
0

Unfortunately, the Raspberry Pi (don't know about linux in general) is reassigning the USB ports after each boot or when connecting/disconnecting devices. So the serial port numbers change more or less randomly. You need some other way of identifying which device is which. Probably the best method is to use unused pins for identifying the device and query the device id from the host at startup, so you know which ESP32 is connected on which port.

PMF
  • 14,535
  • 3
  • 23
  • 49