igrr / libnmea-esp32

ESP-IDF component for libnmea, NMEA parser
MIT License
40 stars 10 forks source link

I2C example #15

Closed beriberikix closed 1 year ago

beriberikix commented 2 years ago

Hi Ivan! Thanks for making this port to the new component system :)

Would it be possible for someone to create an I2C example? I'm interested in a specific use case to support the PA1010D which is used in the open source Ribbit Network project (see more here.)

igrr commented 2 years ago

Hi @beriberikix, I don't have this particular GPS module but i'll try to order one. It might be a while before I get my hands on it, though!

From a brief look, you'd have to replace this part: https://github.com/igrr/libnmea-esp32/blob/7a2ab38e477af82807f8159d3c71ecfbcc9b57b6/example/main/nmea_example_main.c#L59-L64

with something like

int read_bytes = 0;
while(read_bytes < UART_RX_BUF_SIZE - total_bytes) {
    char c;
    // i2c_master_read_slave is from esp-idf i2c_example_main.c
    if (i2c_master_read_slave(i2c_num, (uint8_t*) &c, 1) != ESP_OK) {
        break;
    }
    if (c == 0) { // no more bytes to read
        break;
    }
    buffer[total_bytes + read_bytes] = c;
    read_bytes++;
}

if (read_bytes == 0) {
    vTaskDelay(pdMS_TO_TICKS(10));
    continue;
}

plus the initialization of I2C at the top of app_main, similar to the i2c_self_test example.

igrr commented 2 years ago

I got the GPS in question and will try to add an example in the next couple of weeks.

igrr commented 2 years ago

@beriberikix you are welcome to try Ribbit-Network/ribbit-network-frog-hardware#16.

I haven't had a chance to test if UART still works after these changes, but I2C seems to work.

akhilgupta1093 commented 2 years ago

Hi @igrr ! Thanks so much for this implementation. I'm trying it out for Ribbit Network, but it seems like the only data coming from the PA1010D GPS is in the form of GPGSA and GPGSV sentences, which don't contain position data (reference).

It seems like the device is sending other sentence types, although, weirdly, I can't find too much information about these other sentence types (GLGSA, GNRMC, GNVTG, GNGGA...)

You can see the code I'm using here

Did you manage to receive other sentence types from your PA1010D GPS? If so, do you have an idea about what I'm doing wrong?

akhilgupta1093 commented 2 years ago

Seems like GNRMC data may be what I want from the device (GNRMC reference) -- I guess this isn't currently supported in the NMEA package. Do you have a suggestion for a workaround/another package to use?

igrr commented 2 years ago

@akhilgupta1093 Do you specifically need GNRMC (i.e. GPS+GLONAS) and not GPRMC (GPS)?

If you want to configure the receiver to send particular sentences or enable/disable particular talkers (GPS/GPS+GLONAS) you need to send some commands to it. The commands are vendor-specific. Adafruit has made the command reference for this module available here. You can also check their tutorial, it has a few sample PMTK commands.

igrr commented 2 years ago

I've opened an upstream PR to add support for GN* sentences in libnmea: https://github.com/jacketizer/libnmea/pull/58.

akhilgupta1093 commented 2 years ago

Thanks for the references, I'll take a look at enabling/disabling particular talkers. We only need location data, not satellite data, so GP* would be good enough for us. In any case, I look forward to your PR being merged. Thanks a ton for your help!

akhilgupta1093 commented 2 years ago

@igrr I'm having a little trouble configuring the receiver like you mentioned above (the examples are in circuitpython, I'm using esp-idf and c). I've decided to wait for your GN* support to be merged into libnmea-esp32 as the solution to my issue. No time rush, of course, just let me know when this is done, thanks

akhilgupta1093 commented 1 year ago

@igrr I see that your PR has now been merged into the jacketizer/libnmea repo! If you update your fork of the repo, I think I should be good to go, if I'm not mistaken

igrr commented 1 year ago

@akhilgupta1093 I have updated https://github.com/igrr/libnmea-esp32/pull/16, please give it a try.

akhilgupta1093 commented 1 year ago

@igrr Sorry, I'm currently running idf.py add-dependency igrr/libnmea to add the dependency to my project, like the readme mentions. How can I use this specific branch of igrr/libnmea-esp32?

igrr commented 1 year ago

You can modify the idf_component.yml in your project to install the component from a git repository, until a new version is released:

dependencies:
  libnmea-esp32:
    git: https://github.com/igrr/libnmea-esp32.git
    version: feature/i2c

(or version: <git commit id>)

akhilgupta1093 commented 1 year ago

@igrr Thanks for the support! I have it working on my side, but the sentences are all giving zero values, like below. Any idea why this might be?

GPGGA sentence
Number of satellites: 0
Altitude: 0.000000 M
Longitude:
  Degrees: 0
  Cardinal: .000000
Latitude:
  Degrees: 0
  Minutes: 0.000000
  Cardinal: 

GPRMC sentence
Longitude:
  Degrees: 0  Minutes: 0.000000
  Cardinal: 
Latitude:
  Degrees: 0
  Minutes: 0.000000
igrr commented 1 year ago

Are you sure the module actually has a fix on the satellites?

I have put my module next to the window and it took about 5 minutes for the module to acquire a fix. (I have a relatively clear view of the sky outside; if you have other buildings nearby it may take even more time.)

akhilgupta1093 commented 1 year ago

Thanks for the tip, seems like there's data being ingested now, but for some reason it's facing an error trying to read the data bytes. (here's my branch for reference)

Getting this error log repeatedly:

E (26673) frog_sensor: Failed to read byte over I2C: ESP_FAIL (0xffffffff)
igrr commented 1 year ago

Does it fail all the time, or just occasionally, or does it work for a while and then start failing?

From the I2C driver code, I see ESP_FAIL being returned when the I2C FSM gets stuck. Usually this indicates some noise on I2C lines. Please check your PCB or wiring to make sure the connections are reliable and have pull-up resistors.

akhilgupta1093 commented 1 year ago

Ah it worked after disconnecting and re-connecting the wires. Thanks so much for all your help! Seems like the integration is complete on our part.