ESP32-Paxcounter icon indicating copy to clipboard operation
ESP32-Paxcounter copied to clipboard

Make a USB stick version (without Lora) of paxcounter using new ESP32 S3 USB Device API

Open heaversm opened this issue 3 years ago • 11 comments

I have a node.js web app that can read from the serial port of the computer using this library.

I want to be able to plug in my Lilygo LORA32 (TTGO v3 v1.6.1) to USB on the computer and ingest the pax value to use in my code to drive a web visualization.

I can see that this is being logged in the serial monitor of VSCODE via : senddata.cpp:91 :

sendData() Sending count results pax=7 wifi=1 ble=6

how would I set something up such that I could listen expressly for the pax value coming from the serial monitor and store that to a variable in node.js? I don't need any actual node.js code, just need to know how to set up the serial output in the esp32-paxcounter code (presumably in senddata.cpp?

Thanks for the help!

heaversm avatar Jul 08 '22 23:07 heaversm

don't do it in senddata.cpp. Make a new sendserial.cpp using spislave.cpp as template.

cyberman54 avatar Jul 09 '22 06:07 cyberman54

https://github.com/cyberman54/ESP32-Paxcounter/blob/master/src/spislave.cpp

cyberman54 avatar Jul 09 '22 16:07 cyberman54

Here is a How To:

You need

  1. a messagebuffer, to store the payload
  2. a queue, as buffer for the serial data
  3. a protocol, suitable for your application
  4. a free or freeable serial port

1+2: see spislave.cpp (change the SPI transmit calls by serial port calls) 3: consider overhead and checksum, e.g. transfer the payload as byte array or UTF8 string, e.g. comma separated string with checksum, as used in NMEA 4: consider if you want to use the USB port, then you probably want to mute/reroute all console output; or using other GPIO

cyberman54 avatar Jul 09 '22 16:07 cyberman54

Really wish I had the knowledge and expertise to understand how to do this - most of this sounds pretty outside of my wheelhouse (web development / basic arduino / pi). Think I'll need to find a way to hire someone to figure this out for me. Appreciate your help!

heaversm avatar Jul 10 '22 23:07 heaversm

Do you have a concept for No. 3?

cyberman54 avatar Jul 12 '22 06:07 cyberman54

Well - all my application needs to do is ingest the total number of devices detected (ble + wifi). If I can get to the point where I can read that value over serial, I am good to go. I tried using the packed decoder from your library that gets put into The Things Network. When I put it into my own code and passing the serial output, it returns a wifi / bluetooth count - but I'm not convinced that it is returning accurate values.

This is what I have:

const parser = serialPort.pipe(
  new ReadlineParser({
    delimiter: "\n",
  })
);

const decodedData = Decoder(data);

where Decoder uses:

function Decoder(bytes) {
  var decoded = {};

  if (bytes.length === 0) {
    return {};
  }

  // only wifi counter data, no gps
  if (bytes.length === 2) {
    return decode(bytes, [uint16], ["wifi"]);
  }
  // wifi + ble counter data, no gps
  if (bytes.length === 4) {
    return decode(bytes, [uint16, uint16], ["wifi", "ble"]);
  }
}

heaversm avatar Jul 13 '22 01:07 heaversm

@heaversm

A simple approach:

TTGO boards have ESP32 without USB, but U0RXD/U0TXD of ESP32 connected to USB chip on board, which is normally used to flash the board and for serial console. For your approach you could "misuse" the serial console. That's simple, try this:

  1. Mute device output on the serial console (= USB port of TTGO board), as far as possible
  2. Add prints of payload to serial console, as needed
  3. Switch off Lora, as it's not needed in this use case

How To:

in paxcounter.conf: #define VERBOSE 0

in platformio.ini: debug_level = 0

in senddata.cpp: add this line ets_printf("$PAX,%d,%d,%d\r\n", count.pax, count.wifi_count, count.ble_count); after this line ESP_LOGD(TAG, "Sending count results: pax=%d / wifi=%d / ble=%d", count.pax, count.wifi_count, count.ble_count);

in halfile, e.g. ttgov21new.h: #define HAS_LORA 0

Then you should get output like this on USB:

$PAX,22,5,17
$PAX,21,1,20
$PAX,21,1,20
$PAX,21,0,21

cyberman54 avatar Jul 14 '22 07:07 cyberman54

reopened as enhancement idea with new titel

https://docs.espressif.com/projects/esp-idf/en/latest/esp32s2/api-reference/peripherals/usb_device.html

cyberman54 avatar Jul 14 '22 15:07 cyberman54

It worked! pax

Thank you.

I'm not entirely sure why ets_printf works and not ESP_LOGD - I tried searching the ESP-IDF docs for that function and it just says

See ets_printf with no link.

But I did find this rust documentation, which indicates it prints out to "UART and other devices".

At any rate - very grateful for your help, thank you.

heaversm avatar Jul 15 '22 03:07 heaversm

ESP_LOGD is based on ets_printf, but considers log level. Which we set to NONE. To mute all console output for your use case.

cyberman54 avatar Jul 16 '22 10:07 cyberman54

@heaversm so, could you proceed with you project?

cyberman54 avatar Jul 20 '22 11:07 cyberman54