Write data to ESP32 over USB connection with MicroPython - uart

I have an ESP32 connected to a computer via USB port. I can use the ESP32 to send data over the serial connection using the print statement, I need to periodically write commands into the ESP32. How do I read what is coming over the COM port on the ESP32 in MicroPython? I unsuccessfully tried many variations of the following:
from machine import UART
uart = UART(115200)
while 1:
if uart.any():
msg = uart.read()
print(msg)

Print is for printing in REPL only. If you want to communicate with MCU via serial port you have to write to it.
The simplest example would be:
# your imports and initialization
msg = uart.read()
uart.write(msg)
And on your computer you have to run some serial console e.g. picocom or if you're Windows user then Putty. After connection just type something in terminal and hit enter. This is basically all you need to start echoing messages. You can use Python serial library on your machine but I suggest to stick with simplest tools until you connect successfully for the first time.
Two more things though:
Your init is incomplete imho. It should contain port and timeout options.
You can't use the same USB port for programming and communication (like in Arduino). REPL is going to blow your connection up. You need USB to serial adapter for $2 from China and connect to other UART pins (there are 3 UART interfaces on ESP32).

Related

Emulate CP210x USB-FTDI chip using SAMD21

The dev boards for the ESP32 family of MCU use CP210x (or similar) "FTDI" chips to communicate with the MCU when flashing. CP210x presents a COM port to the host computer which runs esptool.py, a script which implements the Esressif communication protocol. Rather then use a CP210x, I would like to utilize a SAMD21 in its place.
I have managed to receive logs from my ESP32, to a serial monitor on my computer, via the SAMD21, over USB. The SAMD21 connects to the ESP32 via UART, with the standard ESP configuration of 115200 8N1. I can manually enter bootloader mode by holding the correct pins low at boot, and I get the log back confirming the correct bootloader mode.
When I run esptool.py, however, the connection fails, and I get a timeout. Likewise the esptool "monitor" fails to acknowledge the same logs which my terminal emulator easily detects.
What is the communication protocol between the host computer running esptool.py, the CP210x FTDI chip, and the ESP32, and how can I emulate the CP210x with a SAMD21? The definition of "FTDI", "TTL" and "RS232" are all a bit fuzzy, as far as I can determine with research online, so if anyone has experience in this arena, I would be very curious to hear your advice.
The code I have on my SAMD21 is just:
#include <Arduino.h>
void setup()
{
Serial.begin(115200);
Serial1.begin(115200);
}
void loop()
{
if(Serial.available())
{
Serial1.write(Serial.read());
}
if(Serial1.available())
{
Serial.write(Serial1.read());
}
}
Where Serial1 is the UART and Serial is the USB Serial connection.
esptool.py expects a serial port (known as COM port on Windows) to communicate with the ESP32. It doesn't care if the computer has an old-style serial port, is using a USB-to-serial bridge with a proprietary protocol (FTDI, CP210x and the like) or is using the standardized UBS protocol for serial communication (USB CDC ACM). This is left to the operating system and the installed drivers.
However, esptool.py modifies the baud rate and uses the RTS and DTR signals to reset the ESP32. It is also rather susceptible to timing issues with regards to the reset. If you set the ESP32 in boot mode manually, you should be able to get away without these.
However, the most likely cause is that the Arduino CDC implementation does not implement flow control. If esptool.py sends more data than fit into the internal buffer, it is likely discarded, instead of sending a NAK back so the host computer can retry later.
I don't fully understand the SAMD21 Arduino core to verify it. If so, I don't see how you can make it work with an Arduino program. You would need to resort to some other framework for programming the SAMD21.
Update
After more tests, it turns out the USB CDC implementation of the SAMD21 Arduino core correctly implements flow control. So no data will be lost.
In fact, I was able to successfully upload code to an ESP32 module. The setup was:
Arduino code built with PlatformIO. Instead of Serial, I've used SerialUSB as I'm unsure how to control the project settings available in the regular Arduino IDE.
For the ESP32, I've used a ESP32-WROOM-32 module on a minimal board (reset and boot button, 2 pull-up resistors).
I've connected the board via GND, 3.3V, TX, RX directly to the SAMD21 dev board.
I've verified that I can see the ESP32 log output in normal run mode and the "waiting for download" prompt in bootloader mode (after pressing BOOT and RESET).
Arduino has multiple boards where the esp32 is on-board as WiFi adapter. To flash the esp32, there is a tool sketch called SerialNINAPassthrough in examples of the WiFi library, which should be uploaded into the main MCU of the board (SAMD21 on two of the official boards).
The SerialNINAPassthrough sketch handles the DTR and RTS signals sent by the esptool to reset the board into the flashing mode.

Node-red "serial port temporarily unavailable, cannot lock serial port" problem

I am trying to control some sensors and a hydraulic system and a vane with a servo via raspberry pi and arduino. I use serial node to communicate with arduino. One serial node for incoming data from arduino and one for sending data to arduino over node-red. The problem is node-red cannot lock the usb serial port and cannot communicate with arduino while arduino ide can do it. In Node-red forum They have advised me to closed other programs that can use the serial port. I have closed arduino ide and reset Pi 4 but nothing changed. I have restart them but problem still stands.
Is there a way to solve this locking problem?
How can I see the serial ports and the programs that use the ports?
Can I prevent them to use these ports with a javascript or a python function code?
I have found the problem and fix it. There is no other program or software was using serial port /dev/ttyACM0 was being used by another flow in a passive node-red window. I have to delete that page and nodes.

How can i emulate streaming data from a serial port?

I have a text file containing NMEA (GPS) data that I' like to replay to a COM port so the application I'm building can read it.
I considered writing a quick C# console app to loop over the file but I don't have any COM ports. This appraoch would require a virtual COM port and I'm not sure what challenges that would entail.
I also looked at GPSGate. Nice product but it doesn't seem to replay NMEA files over one of its virtual com ports.
How can I stream an existing text file to simulate GPS over a serial connection? I prefer a serial port, rather than mocking, to avoid reworking the application I'm developing and to facilitate use with other applications that use GPS over serial ports for which we don't have source code.
Update 1 - I downloaded a trial version of Virtual Serial Port Driver by Eltima and within a few minutes had some C# code writing to a serial port and appearing in Termite. Yes, it works but $100 seems excessive for what I'm trying to accomplish. Open source or free commercial would be preferred.
gpsfake is part of the gpsd project
gpsfake is a test harness for gpsd and its clients. It opens a pty (pseudo-TTY), launches a gpsd instance that thinks the slave side of the pty is its GPS device, and repeatedly feeds the contents of one or more test logfiles through the master side to the GPS. If there are multiple logfiles, sentences from them are interleaved in the order the files are specified.
You can port the output to a virtual serial via /usr/bin/gpspipe -r | socat - PTY,link=/tmp/gpsd.pty,raw using gpspipe, also from the gpsd project, to be picked up however you choose.
It is POSIX compliant.
You need a virtual serial port such as:
http://sourceforge.net/projects/com0com/
and a hyper terminal replacement.. in XP and earlier it was very easy with the shipped copy of hyperterminal as mentioned bellow..
You set up a virtual null modem connection
Null modem has a COM4 to COM5 Connection established as Null modem std gps is 4800Baud
Hyperterminal to COM5
Application to COM4
Copy paste your NMEA Values into Hyperterminal this isn't 1Hz like GPS though.. to enable 1Hz(or 1 line per second) you will need to find a different application than hyperterminal to send the messages at 1 second increments.

how to transmit serial data from GPS device to computer through IP?

Well..I have found some third party application regarding sending data from com port to IP. but I have not found any basic tutorial regarding them. so can anyone help me with this? I have a GPS device which I will connect to my laptop through usb to serial adapter.Now I need to send that data from a laptop to another laptop on same network. Can I use putty to view that data in another laptop(receiver)? Is virtual serial port driver meant for this kind of application?
If you do not want to write your own tool for it, you can simply use ncat and set up a daemon that reads piped data from one process and broadcasts it to all connected clients.
If you want something that reads the data from the serial port and then transmits it to clients, you'll need to write a server application that accepts connections and sends data around, but there's entire books on this. It should be easy to do for your purposes as written here, but it depends on the amount of control you need.
Alternatively you can use a virtual serial port application as you had mentioned, which might be the easiest route. The two devices will need to be on the same network unless the application supports TCP based virtualization instead of the common Ethernet based implementation.
This Python script works very well as a free "device server". Just enter the serial port configuration and the IP address and port information.
https://github.com/jaredly/pydbgp/blob/master/symbian/serial_tcp_redirect.py
This can work on both Windows and Linux.
You need pyserial.
You can always try using stand alone hardware such as the SENA LS100 device server.

AT command with 3g modem while it is connected

I would like to query a modem using AT commands using hyperterminal while it is connected, but whenever I try to establish a hyperterm connection, it says that it is in use. I saw a similar post How can I communicate with a 3G modem via pySerial while it is connected? that has the same problem, but my system in running on windows xp, therefore i am unable to switch to a different tty.
Hence I would like to ask if there are any solutions or alternatives to query the modem. Thank you!
3G modems typically (always?) provides two serial interfaces that can be used for AT command communication. Those serial interfaces ends up as /dev/ttyACM0 + /dev/ttyACM1 or /dev/ttyUSB0 + /dev/ttyUSB1 on Linux, /dev/cuaU0 + /dev/cuaU1 on FreeBSD, and on windows machines they should end up as COM<N> + COM<M> for some N and M number (not necessarily next to each other).
So open the device manager and try to find the other serial interface which is not used by hyper terminal and use that one.