rbaron / b-parasite

🌱💧 An open source DIY soil moisture sensor
1.85k stars 143 forks source link

Migrate code to the nRF Connect SDK #76

Closed rbaron closed 1 year ago

rbaron commented 1 year ago

This PR rewrites all the code from the legacy nRF SDK to the nRF Connect SDK. I will keep pushing updates to this PR and merge it when all tasks are finished and tested. I am only going for feature parity with this rewrite, but I'm factoring out a common core that can be reused in different applications/samples.

All help is welcome in testing the parts that are already implemented.

Tasks

Notes

Building the Samples

With nRF Connect for Desktop + Visual Studio Code

This is the usual way of building the nRF Connect SDK samples. The only caveat is that, for each sample here, you also need to add the code/nrf-connect/prstlib directory to your workspace in addition to the sample directory itself. The prstlib/ contains common source code for reading sensors and also common board definitions. There is a .code-workspace file inside each sample that does this automatically.

With Docker

To build the ble sample for the nRF52840, revision 1.2.0, cd into code/nrf-connect/ and run:

$ docker run --rm -v ${PWD}:/code -w /code/samples/ble nordicplayground/nrfconnect-sdk:v2.1-branch west build --build-dir ./build --pristine --board bparasite_nrf52840@1.2.0

The output will be in code/nrf-connect/samples/ble/build/zephyr/zephyr.hex.

[Experimental but super cool] With GitHub Codespaces

GitHub Codespaces let you run an instance of VS Code directly in your browser. More importantly, it also supports plugins, including nRF Connect for VS Code! This PR introduces .devcontainer/devcontainer.json, which configures a Codespace with both the nRF VS Code plugin, as well as the whole nRF Connect SDK + toolchain. This lets you edit code and build the sample straight in your browser.

  1. Go to Codespaces and click "New codespace"
  2. Select rbaron/b-parasite, branch nrf-connect
  3. Wait. The underlying Docker image is pretty big and the build might fail. It worked on the second try for me
  4. Once the codespace is ready, navigate to code/nrf-connect/samples/ble, select ble.code-workspace and click on "Open workspace". This makes sure both the ble sample and the prstlib are present in the workspace
  5. Build the sample normally, as described in the nRF Connect SDK docs
  6. Download the built sample from ble/build/zephyr/zephyr.hex

Prebuilt Binaries

The GitHub workflow in this PR clang-format-checks, builds and store the built binaries for all sample as artifacts. You can download them here (example). These are built with the default settings, but we may extend this machinery to build a few variants with different configs for each sample.

rbaron commented 1 year ago

Hey @jhbruhn, I implemented support for the nRF52833 variant, but I don't have one to test. As you worked on #13, can I bug you to help me test it when you have some time?

I added some instructions on how to build in this PR's description (just use --board bparasite_nrf52833 instead), or you can also get some pre-built binaries as product of the workflow runs on this branch (example). Inside the sample-binaries artifact there's a ble_nrf52833_bthomev1.hex that's hopefully convenient. It uses a 10m sleep interval with the BTHome v1 BLE encoding (just like the one implemented in the legacy app).

jhbruhn commented 1 year ago

Oh this looks very nice! I don't know when I have the time to test this as I got a lot on my plate right now, but I also have some b-parasites waiting for their coating and then deployment with the new firmware revision (and nrf52833), so I'll test it there.

I noticed that the code does not support LDRs anymore. I have a lot of old b-parasites with a photoresistor and would love to flash the new firmware to these as well, do you think we can bring back the functions for that? They are unfortunately also using a different Pin for the resistor than the 1.2.0 boards are using for the phototransistor, but it looks like this can easily be fixed through the different board configurations in this project?

rbaron commented 1 year ago

@jhbruhn I think I found a nice way to do it. I added board revisions for 1.0.0, 1.1.0 (with LDR) and 1.2.0 (with phototransistor). We can just select the variant + revision in the VSCode UI:

image

For building from the command line, we can just append @$revision to the --board switch. For example to build for nRF52833@1.1.0 using the BTHome v1 protocol with sleep time of 1s for quick testing:

$ docker run --rm -v ${PWD}:/code -w /code/samples/ble nordicplayground/nrfconnect-sdk:v2.1-branch west build --build-dir ./build --pristine --board bparasite_nrf52833@1.1.0 -- -DCONFIG_PRST_BLE_ENCODING_BTHOME_V1=y -DCONFIG_PRST_SLEEP_DURATION_SEC=1
kzyapkov commented 1 year ago

I had started on the same effort, but this is much nicer than what I have. Kudos for the reusable lib part and the board revisions. Awesome addition to an awesome project.

rbaron commented 1 year ago

Thanks for the kind words @kzyapkov. I will finish writing the updated docs and merge this in.

rbaron commented 1 year ago

Tks for trying this out @jhbruhn. I will take a closer look today. From the logs it looks like the ADC channels are not set up properly (in addition too the wrong fast discharge pin).

Did you build the firmware with VSCode or Docker? For the 1.1.0 revision, right?