localhosttcpclientesp32tcpservertoit

Messaging between apps on the same ESP32


I think I found a way to communicate between applications inside one ESP32. Anyway, it works. True, I've some doubts about the legitimacy of this approach, so I'm addressing you.

I remembered that exist magic address "127.0. 0.1" - the loopback address also referred to as the localhost. In ordinary life this address is used usially to establish an IP connection to the same computer being used by the programmer. For example, if I'm writing a client-server, then using localhost I can debug the exchange protocol inside one app without using an additional computer.

At the same time using the loopback interface bypasses any local network interface hardware.

And I thought that there is some possibility that this principle was observed in your system and I just need to try: what if it works?

And it really works. Below are texts of two apps and screenshots from parallel terminals. Receiver.toit app starts first, and then sender toit.

receiver.toit:

import net
main :
  network_interface := net.open
  serverSocket := network_interface.tcp_listen 1234
  print ("Wait...")
  socket := serverSocket.accept
  print ("Accept was done...")
  data := socket.read
  clientAddr := socket.peer_address.ip.stringify
  print ("[$clientAddr]->[$data.to_string]")
  socket.close
  print ("Socket closed...")
  serverSocket.close
  print ("ServerSocket closed...")

sender.toit:

import net
LOOPBACK := "127.0.0.1"
main :
  network_interface := net.open
  clientSocket :=  network_interface.tcp_connect LOOPBACK 1234
  print ("Connect...")
  clientSocket.write "Hello from flatmate..."
  print ("Write was done...")
  clientSocket.close
  print ("Socket closed...")

Receiver's terminal:

micrcx@micrcx-desktop:~/toit_apps/Hsm3/tests$ toit -d=dell run receiver.toit
2021-10-08T06:41:03.937058Z: <process initiated>
Wait...
Accept was done...
[127.0.0.1]->[Hello from flatmate...]
Socket closed...
ServerSocket closed...
2021-10-08T06:41:35.166388Z: <process terminated - exit code: 0>
micrcx@micrcx-desktop:~/toit_apps/Hsm3/tests$ 

Sender's terminal:

micrcx@micrcx-desktop:~/toit_apps/Hsm3/tests$ toit -d=dell run sender.toit
2021-10-08T06:41:34.641365Z: <process initiated>
Connect...
Write was done...
Socket closed...
2021-10-08T06:41:34.917420Z: <process terminated - exit code: 0>
micrcx@micrcx-desktop:~/toit_apps/Hsm3/tests$ 

Solution

  • Using TCP connection on one device is a scenario that is supported and should work. However, the server is not firewalled, and any machine on the same network can connect to the server that is running on the ESP32.

    An alternative is to use local pubsub. Have a look at the tutorial here: https://docs.toit.io/platform/tutorials/pubsub/pubsubint

    Local pubsub is more lightweight and internal, but it is a broadcast mechanism. The sender just sends out data, and any receiver that is listening gets the data. If it's important that every message is correctly received you would need to implement a small protocol on top.

    In the future there will also be a dedicated mechanism (currently called "RPC") to talk effectively between different applications.