Skip to content

Commit

Permalink
Merge pull request #22 from renatobo/feature/battery_ble
Browse files Browse the repository at this point in the history
Feature/battery ble
  • Loading branch information
renatobo authored Jan 23, 2021
2 parents fd14fa9 + d28e504 commit d9b1b37
Show file tree
Hide file tree
Showing 14 changed files with 502 additions and 406 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ You can also interface your GPS to [uBlox u-center](https://www.u-blox.com/en/pr
### Daily Usage

1. Turn on the ESP32 and wait a few minutes for the GPS to get a fix on position (on BN devices, the red led will blink every second)
2. Open your preferred mobile app and connect it to your BonoGPS-*ABCD* device
2. Open your preferred mobile app and connect it to your BonoGPS-*ABCD* device (ABCD is a combination of 4 letters and numbers unique to your ESP32 device)
3. Enjoy your ride!

Most runtime configurations are managed via its web-based interface: you can use your phone or any device with a web browser which is able to connect to either the built-in Access Point the BonoGPS provides or (if you are home and on you have configured the WiFi Client credentials) [http://bonogps.local](http://bonogps.local)
Expand Down Expand Up @@ -93,7 +93,7 @@ There are many mobile apps to log lap times, few accept custom devices, in parti
| iOS | **BLE**, TCP-IP | | |
| Android | **BT-SPP**, TCP-IP | BT-SPP | BT-SPP | BT-SPP |

See more info in each subfolder of **connecting**.
See more info in each subfolder of **[connecting](software/connecting)**.

You can load a preset configuration from the configuration page selecting *Device > Load Preset* and then choosing your mobile phone device and app combination (when there are alternatives, the recommended option is in bold)

Expand Down Expand Up @@ -121,11 +121,11 @@ Examples are in [hardware/assembled](hardware/assembled)

Thanks to needs of a very active drone community, there are a lot of inexpensive GPS receivers: some considerations on performance/accuracy/cost are reported [in the hardware/GPS folder](hardware/GPS).

What you need to configure is documented [in the hardware/GPS folder](hardware/GPS). You can restore a pre-saved configuration from the same page.
What you need to configure is documented [in the hardware/GPS folder](hardware/GPS): this is **important**, the performances of your GPS won't be optimal until you do.

## Software build instructions

This code is developed specifically for ESP32, and tested with [PlatformIO](https://platformio.org/) (main development platform) and the [Arduino IDE (1.8.13)](https://www.arduino.cc/en/software). More information on what libraries are needed and software organization [in the software folder](software).
This code is developed specifically for ESP32, and tested with [PlatformIO](https://platformio.org/) (main development platform) and the [Arduino IDE (1.8.13)](https://www.arduino.cc/en/software). More information on what libraries are needed and software organization [in the software/building folder](software/building).

## Possible enhancements and ideas

Expand Down
1 change: 1 addition & 0 deletions git_rev_macro.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
.strip()
.decode("utf-8")
.replace('.git', '')
.replace('https://github.com/','')
)

repolink = re.sub(r'(v[\d+\.a-zA-Z]+)-.*', r"\1", revision)
Expand Down
6 changes: 4 additions & 2 deletions hardware/esp32/lolin_d32_pro.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# LOLIN D32 PRO
# LOLIN D32 PRO

- [No BOOT Button](#no-boot-button)
- [Serial2 PINs need to be assigned](#serial2-pins-need-to-be-assigned)
Expand Down Expand Up @@ -30,4 +30,6 @@ This is the simple function used to collect voltage
analogRead(GPIO_BATTERY) / 4096.0 * 7.445
```
Percentage is based on the common LiPo values as charged by its [TP4054](https://datasheetspdf.com/pdf/1090540/NanJingTopPower/TP4054/1): linear scale from 3.3V (0%) to 4.2V (100%).
Percentage is based on the common LiPo values as charged by its [TP4054](https://datasheetspdf.com/pdf/1090540/NanJingTopPower/TP4054/1): linear scale from 3.3V (0%) to 4.2V (100%).
These are enabled by the preprocessing macro `SHOWBATTERY`
17 changes: 16 additions & 1 deletion include/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@

# Include files

[include/bonogps_board_settings.h](include/bonogps_board_settings.h) includes board settings (PINS mostly) that are specific to a tested board.
- [include/bonogps_board_settings.h](bonogps_board_settings.h) includes board settings (PINS mostly) that are specific to a tested board.

## Generated files

These header files are generated so you should not manually modify them. Regenerate them with [generate_css.sh](../generate_css.sh)

- [include/bonogps_css_base.h](bonogps_css_base.h) preprocessing macro that defines the strings returned by the `http://bonogps.local/css` url in the portal
- [include/bonogps_css_base_battery.h](bonogps_css_base_battery.h) preprocessing macro that defines the strings returned by the `http://bonogps.local/css` url in the portal with additional definitions to show the battery (no need to increase the flash size if you don't need a battery gauge). This is used when macro `SHOWBATTERY` is used

## Source files

Apply your customizations to CSS files here in their native format: they are minimized when imported in the source code.

- [include/style_base.css](style_base.css) CSS definitions for the web portal
- [include/style_base_battery.css](style_base_battery.css) CSS definitions required to display the battery gauge in the web portal
3 changes: 0 additions & 3 deletions include/bonogps_board_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

#if defined(ARDUINO_LOLIN_D32_PRO)

#define BOARD_NAME "Lolin D32 PRO" // Display in information page
// Where is Serial2 connected - for Lolin we reconfigure to 2 free pins
// until https://github.com/espressif/arduino-esp32/pull/4520 is in place, we need to manually define it
#define RX2 GPIO_NUM_4 // 12
Expand All @@ -34,15 +33,13 @@

#elif defined(ARDUINO_ESP32_DEV)

#define BOARD_NAME "esp32-devkit" // Display in information page
#define RX2 16 // Standard label Rx2 on board
#define TX2 17 // Standard label Tx2 on board
#define WIFI_MODE_BUTTON 0 // default is: use the boot button to switch wifi modes
#define LED_BUILTIN 2 // this should not be needed if you choose the right board

#else

#define BOARD_NAME "esp32-custom" // Display in information page
// #define RX2 GPIO_NUM_17 // Serial2 standard location on devkit
// #define TX2 GPIO_NUM_16 // Serial2 standard location on devkit
#define LED_BUILTIN 2 // usually 2 or 5
Expand Down
17 changes: 10 additions & 7 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ build_flags = !python git_rev_macro.py
lib_ldf_mode = chain+

[env:esp32dev]
; basic configuration for a production build without logging
; OTA is disabled
board = esp32dev
monitor_port = /dev/cu.usbserial-0001
upload_port = /dev/cu.usbserial-0001
Expand All @@ -41,6 +43,8 @@ build_flags =
-DNO_GLOBAL_ARDUINOOTA

[env:esp32dev_ota]
; basic configuration for a production build without logging
; Update is OTA, OTA is enabled without timeout
board = esp32dev
upload_protocol = espota
upload_port = bonogps.local
Expand All @@ -54,6 +58,8 @@ build_flags =
-DBUILD_ENV_NAME='"Prod build - OTA"'

[env:esp32dev_debug]
; basic configuration for a troublshooting/debug build without verbose logging
; OTA is enabled without timeout
board = esp32dev
monitor_port = /dev/cu.usbserial-0001
upload_port = /dev/cu.usbserial-0001
Expand All @@ -78,9 +84,8 @@ build_flags =
${env.build_flags}
-DLOG_LOCAL_LEVEL=ESP_LOG_NONE
-DCORE_DEBUG_LEVEL=0
-DBUILD_ENV_NAME='"Prod build"'
-DNO_GLOBAL_ARDUINOOTA
-DARDUINO_LOLIN_D32_PRO
-DBUILD_ENV_NAME='"Prod build"'
; -DBOARD_HAS_PSRAM
; -mfix-esp32-psram-cache-issue

Expand All @@ -96,10 +101,9 @@ build_flags =
${env.build_flags}
-DLOG_LOCAL_LEVEL=ESP_LOG_NONE
-DCORE_DEBUG_LEVEL=0
-DBUILD_ENV_NAME='"Prod build - OTA"'
-D ENABLE_OTA
-DENABLE_OTA
-DOTA_AVAILABILITY_SECS=-1
-DARDUINO_LOLIN_D32_PRO
-DBUILD_ENV_NAME='"Prod build - OTA"'

[env:lolind32pro_debug]
; lolind32 pro target with extended logging
Expand All @@ -113,9 +117,8 @@ build_flags =
${env.build_flags}
-DLOG_LOCAL_LEVEL=ESP_LOG_DEBUG
-DCORE_DEBUG_LEVEL=4
-D ENABLE_OTA
-DENABLE_OTA
-DOTA_AVAILABILITY_SECS=-1
-DBUILD_ENV_NAME='"Test build - OTA"'
-DARDUINO_LOLIN_D32_PRO
; -DBOARD_HAS_PSRAM
; -mfix-esp32-psram-cache-issue
3 changes: 1 addition & 2 deletions platformio_custom.ini.template
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ build_flags =
${env.build_flags}
-DLOG_LOCAL_LEVEL=ESP_LOG_NONE
-DCORE_DEBUG_LEVEL=0
-DBUILD_ENV_NAME='"Prod build - OTA"'
-DENABLE_OTA
-DOTA_AVAILABILITY_SECS=-1
-DARDUINO_LOLIN_D32_PRO
-DBUILD_ENV_NAME='"Prod build - OTA"'

131 changes: 4 additions & 127 deletions software/README.md
Original file line number Diff line number Diff line change
@@ -1,130 +1,7 @@
# Software development and build
# Documentation on Software

- [IDE options/suggestions: Arduino IDE or VS Code+Platformio](#ide-optionssuggestions-arduino-ide-or-vs-codeplatformio)
- [Arduino IDE (recommended if you are new to the 'maker' approach)](#arduino-ide-recommended-if-you-are-new-to-the-maker-approach)
- [PlatformIO](#platformio)
- [Libraries](#libraries)
- [External Libraries](#external-libraries)
- [Optional external libraries](#optional-external-libraries)
- [Built-in libraries](#built-in-libraries)
- [Build options](#build-options)
- [Large use of preprocessing macro](#large-use-of-preprocessing-macro)
- [Logging facilities](#logging-facilities)
- [How to enable OTA build](#how-to-enable-ota-build)
- [OTA on Arduino IDE](#ota-on-arduino-ide)
- [OTA on PlatformIO](#ota-on-platformio)
- [Important: Partition size](#important-partition-size)
How to develop/build/upload the source code in [building](building)

## IDE options/suggestions: Arduino IDE or VS Code+Platformio
How to connect your running bonogps device to a mobile app in [connecting](connecting)

Development is active on the [VS Code + Platformio](https://platformio.org/install/ide?install=vscode) combination: you can download the complete repository and work directly.

### Arduino IDE (recommended if you are new to the 'maker' approach)

Code is written to be compatible with the Arduino IDE, there are a couple of steps required, starting from the assumption you have already installed and setup the Arduino IDE for ESP32 (if you have not, [here a nice tutorial](https://randomnerdtutorials.com/installing-the-esp32-board-in-arduino-ide-windows-instructions/))

- install 'External libraries' listed below
- create an empty project
- copy the content of `src/bonogps.cpp` in the `.ino` file just created
- copy `include/bonogps_board_settings.h` in the same folder
- choose your board: "ESP32 Dev Module" (the generic board that everyone has, often tagged DOIT) or ["LOLIN D32 PRO" are supported](hardware/esp32/lolin_d32_pro.md), otherwise you might have to redefine your pins in `bonogps_board_settings.h`
- choose a partition schema with enough space (e.g. the Minimal SPIFSS with 1.9Mb of flash space)

The rest is common to any other build on the Arduino IDE.

IF you are unsure of what board you are running, [check this introductory tutorial](https://randomnerdtutorials.com/getting-started-with-esp32/).

You can update software OTA, check a later paragraph here on how as it's not enabled by default.

### PlatformIO

Beside install PlatformIO (on VS Code as a recommendation), the build system uses a custom **python** script to determine the current software release version: `git_rev_macro.py` and it expects the project folder to be downloaded from github directly to build up the `GIT_REV` and `GIT_REPO` macro variables correctly.

If you are having issues, you can remove the line that invokes `git_rev_macro.py` and optionally set the 2 macro `GIT_REV` and `GIT_REPO` manually.

If you would like to define custom targets for your build, I recommend using a `platformio_custom.ini` file (there is a template in `platformio_custom.ini.template`).

## Libraries

### External Libraries

- [NimBLE-Arduino](https://github.com/h2zero/NimBLE-Arduino): 1.0.2 or better latest github version directly (default in platformio from v1.1 of bonogps)
- [Uptime Library](https://github.com/YiannisBourkelis/Uptime-Library) ^1.0.0
- [EasyButton](https://easybtn.earias.me/) ^2.0.1
- [Task Scheduler](https://github.com/arkhipenko/TaskScheduler) ^3.2.0

From v1.1 of this code and using the latest code of NimBLE-Arduino (after this [commit](https://github.com/h2zero/NimBLE-Arduino/commit/569eb8a188c78fe780f4c2a24cf9247532cf55ea)), unnecessary BLE code is not compiled in. This reduces flash size by ~30kb of Nimble-Arduino disabling roles `CONFIG_BT_NIMBLE_ROLE_CENTRAL` and `CONFIG_BT_NIMBLE_ROLE_OBSERVER`. Up to version 1.0.2 of NimBLE-Arduino, you can manually uncomment those roles in `nimconfig.h` .

### Optional external libraries

Included via `#define` options

- [NeoGPS](https://github.com/SlashDevin/NeoGPS) [not included right now, but coded and available for some additional cases]

### Built-in libraries

Always included

- WebServer
- FS
- Preferences
- WiFi
- DNSServer
- ESPmDNS
- Update
- BluetoothSerial

Included via `#define`

- ArduinoOTA: this really depends on how you prefer to update software on your device. It adds size to the flash and it uses some memory, so if you don't plan on using it, don't include it.

## Build options

### Large use of preprocessing macro

To keep flash size small and to avoid several versions of the software, almost all features are enabled/disabled via macro preprocessing

For example, if you don't need BT-SPP or BLE, you can remove them. The most important ones are

- `BTSPPENABLED` Include BT-SPP capabilities (default: enabled)
- `BLEENABLED` Include BLE capabilities (default: enabled)
- `ENABLE_OTA` Include OTA capabilities (default: disabled)

### Logging facilities

All logging on Serial Port is managed with `log_<i/d/e/w>()` functions (read more at [ESP32 Logging](https://thingpulse.com/esp32-logging/)). You can control how much logging is enabled within the Arduino IDE from the `Tools > Core Debug Level` menu.

![arduino_ide_logging_level](building/arduino_ide_logging_level.png)

### How to enable OTA build

OTA libraries are not enabled by default, to keep the binary size smaller and to have less software running at all times (it's a miracle there are no RAM issues with all tasks running).

Since updating via OTA is extremely convenient when you are testing, here is how to activate it via two preprocessing macro variables

- `ENABLE_OTA` if defined, libraries and code is builtin, if undefined no OTA is included
- `OTA_AVAILABILITY_SECS` defines for how long OTA is available after boot, either `-1` (forever) or a finite number of seconds. If undefined, it's `300`, to avoid any mistake on the field where you might mistakenly start flashing a firmare you are actually using

#### OTA on Arduino IDE

- uncomment `// #define ENABLE_OTA` at the beginning of `src/bonogps.cpp` (which you might have renamed to `bonogps.ino`)
- optionally, change for how long OTA is available definining `OTA_AVAILABILITY_SECS`

#### OTA on PlatformIO

- There are a couple of build targets ending in `_ota` that contain the command line define statements. If you need more build targets, I recommend using a local `platformio_custom.ini` that is not sync'd with the git repository
- you can also permanently enable it changing the source code as for the Arduino case (not recommended)

### Important: Partition size

You have to select a partitioning schema with 1.7 Mb of programming space (e.g. Minimal SPIFF with 1.9Mb), as the app with its libraries tend to be pretty large due to BT stacks.

Within PlatformIO, use the [platformio.ini](platformio.ini) available configuration

```ini
board_build.partitions = min_spiffs.csv
```

Within the Arduino IDE, from `Tools > Partition Scheme`

![Partition settings](software/building/partition_setting.png
Everyday usage and configurations in [using](using)
Loading

0 comments on commit d9b1b37

Please sign in to comment.