cyberman54 / ESP32-Paxcounter

Wifi & BLE driven passenger flow metering with cheap ESP32 boards
https://cyberman54.github.io/ESP32-Paxcounter/
Other
1.76k stars 409 forks source link

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

Open heaversm opened 2 years ago

heaversm commented 2 years ago

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!

cyberman54 commented 2 years ago

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

cyberman54 commented 2 years ago

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

cyberman54 commented 2 years ago

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

heaversm commented 2 years ago

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!

cyberman54 commented 2 years ago

Do you have a concept for No. 3?

heaversm commented 2 years ago

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"]);
  }
}
cyberman54 commented 2 years ago

@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:

1) in paxcounter.conf: #define VERBOSE 0

in platformio.ini: debug_level = 0

2) 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);

3) 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 commented 2 years ago

reopened as enhancement idea with new titel

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

heaversm commented 2 years ago

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.

cyberman54 commented 2 years ago

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 commented 2 years ago

@heaversm so, could you proceed with you project?