Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A Serial to Websockets device #149

Open
Koepel opened this issue Jul 16, 2021 · 23 comments
Open

A Serial to Websockets device #149

Koepel opened this issue Jul 16, 2021 · 23 comments
Assignees
Labels
enhancement New feature or request open for vote Vote at https://wokwi.com/features

Comments

@Koepel
Copy link

Koepel commented Jul 16, 2021

Feature request: A special device with RX and TX pins that connects to a websocket. A Python3 script could be the websockets server and connect to a serial port on the computer.

If that is possible then a interface to a device (for example a scale with a serial port) can be developed from within Wokwi. It would also be possible to have a real Arduino board communicate with a simulated Arduino board.

@urish urish self-assigned this Jul 16, 2021
@urish urish added the enhancement New feature or request label Jul 16, 2021
@urish
Copy link
Contributor

urish commented Jul 16, 2021

What if the Serial Monitor RX/TX were available through a web socket? Would that also do the trick?

@Koepel
Copy link
Author

Koepel commented Jul 16, 2021

A serial duplex communication to the real world would be a dream come true. I hope you don't mind, but my feature request aims higher.

It is the same as connecting a Bluetooth or GPS module to pin 0 and 1 of a Arduino Uno. It works, but then a LCD display should be added to show messages.
Developing a interface to a serial scale depends heavily on the Serial Monitor.
Suppose a real Arduino sends real sensor data to a simulated Arduino. Then I would like to see the data in the Serial Monitor.

@sutaburosu
Copy link

I think UART <-> websockets would be very useful. I would definitely look at extending my Python proxy to be bi-directional and less focussed on transporting only LED data.

I think a library such as VSync may be useful. It provides a simple mechanism to sync variables between two sketches. It only supports ints currently.

@urish
Copy link
Contributor

urish commented Aug 11, 2021

Note: there's now an experimental support for connecting to an actual Serial port. Here's an example:

https://wokwi.com/arduino/projects/306534287811805760

Only works on Chrome / Edge (other browser do not support the Serial API, and it's not very likely to change in the foreseeable future).

You can also use a virtual serial port driver (such as com0com on Windows) to make your Wokwi project speak to another software on your computer (or to a different wokwi sketch)

@urish urish added the open for vote Vote at https://wokwi.com/features label Sep 16, 2021
@urish
Copy link
Contributor

urish commented Nov 16, 2021

This one got high in the feature list, so it's a good time to start discussing the implementation.

One way would be to extend wokwi-serial-port (mentioned in the previous comment) to support web socket connection in addition to a real serial port, as a configuration option.

For instance, we could add a "url" or a "websocket" attribute that would point at the URL of the web socket. Something along the lines of:

    {
      "id": "serial",
      "type": "wokwi-serial-port",
      "attrs": { "websocket": "ws://localhost:1234", "baud": "115200" }
    }

Then, there's also the question of how we encode the data that goes into the socket. The most straightforward way would probably be to pass the binary data as-is, unmodified. We might need to introduce some buffering, as WebSocket are not stream-oriented. They break down the data into messages, and each message gets a 6 bytes header (client -> server). So sending every byte in a message would add a considerable overload, and might slow down the simulation (if we're running at 115,200, this would mean more than 10,000 messages per second).

@Koepel
Copy link
Author

Koepel commented Nov 17, 2021

A device (such as the Logic Analyzer) could be connected to SoftwareSerial for example. Is the decoding of the serial signals possible ? What if the option to use multiple Arduino boards is implemented first and the websockets device uses parts of a Arduino Uno for the serial signals ?

The existing implementation of the serial port via the Serial API of the Chrome browser has a few limitations.

The data has to be exactly the same (binary) data of course.

When Serial.println( "Hello World") is used, the Serial.write() calls follow each other very quickly. Buffering with a small timeout should be enough to capture the whole message.
If a message is split into two parts or if messages are combined, that is no problem. It is just a stream of serial data.

@urish
Copy link
Contributor

urish commented Nov 17, 2021

A device (such as the Logic Analyzer) could be connected to SoftwareSerial for example. Is the decoding of the serial signals possible ? What if the option to use multiple Arduino boards is implemented first and the websockets device uses parts of a Arduino Uno for the serial signals ?

It is possible indeed. You can also redirect the built-in Serial Monitor to different pins, and use SoftwareSerial on these pins. There's a guide explaining how set up the Serial Monitor with ATtiny85 + SoftwareSerial, but you can use the same process for the Arduino as well.

So for that part we're all set.

@yepher
Copy link

yepher commented Jan 6, 2022

This would be awesome for me to use the simulator to develop this project I am working on. if you visit that GitHub project do yourself a favor and click on the image with the TV in it (few second youtube video) :)

My vote for the "special device" would be an FTDI (or CH340 etc) board. The USB port would represent the websocket interface. You could start with just defining the websocket encapsulation protocol and let folks implement their translation in their language of choice. Or just lean on one of the many existing solutions like go-serial-websocket. This way the simulator could talk to localhost or some publically accessible webserver with the physical port.

serialPort

To test you can just connect two actual FTDIs TX->RX and RX-TX boards to your computer and have the serial console running on one and the other is the other one the server drives.

Or.... It would not be all that hard for someone to write the server to provide a virtual serial port so you can connect any old serial console to it to interact from your keyboard.

Nice thing if the reference implementation is implemented in GO there are no other dependencies a user would need on their computer to run the app and it is super easy to provide binary from many different platforms (Windows, Linux, Mac).

Please ignore that I screwed up the wiring to the FTDI interface, it should be TX->RX and RX->TX :)

@yepher
Copy link

yepher commented Jan 6, 2022

In case you want to use it I drew up the Ada Fruit FTDI Friend

30mm x 17mm SVG file:
FTDIFriends.svg.zip

The parts are grouped in the SVG so the name in the left panel in this image is the name of the ID in the SVG. This video also demonstrates the proportions to the actual physical board.

ftdiFriend

Sorry I did not add it as an element and create a PR... I am still coming up to speed on all that..... And I am not sure this is the direction you would want to go. I will be glad to fix all the spacing issues in the drawing if it would actually be useful.

@urish
Copy link
Contributor

urish commented Jan 4, 2023

Note about the com0com workaround (from @jflamy):

you need to download the previous version 2.2.20 of com0com. The later version doesn't work (signing issues). Works fine with that version.

@urish
Copy link
Contributor

urish commented Feb 17, 2023

Note: this is now supported by Wokwi for VS Code.

It uses the standard RFC2217 protocol, which is supported by the popular pySerial library. You can learn more here

@mngyuan
Copy link

mngyuan commented Apr 12, 2023

Is there a way to connect a Wokwi simulated Arduino's serial port to nodejs? Similar to the com0com suggestion, but I'm on mac so I need something different. Why did implementation of this feature use RFC2217 instead of websockets? It seems there isn't a good RFC2217 node.js library.

@urish
Copy link
Contributor

urish commented Apr 12, 2023

It seems there isn't a good RFC2217 node.js library.

If you are only interested in textual data, then you can treat the RFC2217 server as a standard TCP socket. If you need binary data, then you need to escape/unescape every occurrence of 0xff, but other than that, it's just a plain TCP socket.

@jflamy
Copy link

jflamy commented Apr 12, 2023 via email

@mngyuan
Copy link

mngyuan commented Apr 13, 2023

ah ok, it's been a while since I've used sockets but I've tried the following with the simulator running with the wokwi-serial-port part in diagram.json:

$ nc -l 4000
nc: Address already in use
$ sudo lsof -i -P | grep :4000
Code\x20H 33993             liuyue   86u  IPv6 0xd070471690e4bf31      0t0    TCP *:4000 (LISTEN)

I'm not sure the simulator is properly outputting my Serial.print() calls - how do I see serial output from my Arduino sketch in the Wokwi for VSCode simulator and why is the simulator listening on port 4000 / preventing me from listening?

@urish
Copy link
Contributor

urish commented Apr 13, 2023

The simulator is opening a TCP server. You need to connect to it to see the prints.

@mngyuan
Copy link

mngyuan commented Apr 13, 2023

is that not what nc -l 4000 does, listen on TCP port 4000?

@urish
Copy link
Contributor

urish commented Apr 13, 2023

Wokwi open a TCP server. You need to connect to it, localhost:4000 (or whatever port you set in Wokwi.toml), not listen on port 4000

@mngyuan
Copy link

mngyuan commented Apr 13, 2023

nc localhost 4000 shows nothing on my system while the wokwi simulator is running.

@urish
Copy link
Contributor

urish commented Apr 13, 2023

What does your wokwi.toml look like? Are you using a VS Code dev container or WSL?

@mngyuan
Copy link

mngyuan commented Apr 13, 2023

wokwi.toml

[wokwi]
version=1
firmware='.pio/build/uno/firmware.hex'
elf='.pio/build/uno/firmware.elf'
rfc2217ServerPort=4000

I'm on a Mac so not WSL nor a dev container.

@urish
Copy link
Contributor

urish commented Apr 13, 2023

Looks good. Does your code keep printing stuff to the serial monitor?

You'll only see what's been printed after netcat has connected.

@afifkhoury2005
Copy link

afifkhoury2005 commented Jan 4, 2024

I found the correct code to use in order to make com0com compatible with wokwi I recommend using the following code:

{
  "version": 1,
  "author": "AFIF_CODE",
  "editor": "wokwi",
  "parts": [
    {
      "type": "board-esp32-devkit-c-v4",
      "id": "esp",
      "top": 0,
      "left": 0,
      "attrs": {}
    },
    {
      "id": "serial",
      "type": "wokwi-serial-port",
      "attrs": { "baud": "115200" }
    }
  ],
  "connections": [
    ["esp:TX", "$serialMonitor:RX", ""],
    ["esp:RX", "$serialMonitor:TX", ""],
    ["$serialMonitor:RX", "serial:RX", ""],
    ["$serialMonitor:TX", "serial:TX", ""]
  ],
  "serialMonitor": {
    "display": "always"
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request open for vote Vote at https://wokwi.com/features
Projects
None yet
Development

No branches or pull requests

7 participants