Airspeed Device - Arduino sketch for data BLE serial print using the (new) TotalVario sentence protocol
TotalVario Data Sentences: A NMEA(like) sentence with the following parameters:
$PTVSOAR
- MNA Manufacturer name, ASCII 127, no [$*,]
- MMO Model, ASCII 127, no [$*,]
- MSN Manufacturers device serial number, ASCII 127, no [$*,]
- OAT Outside air temperature in deg C
- OAH Outside air humidity
- PRS Static pressure in hPa
- PIT Pitot differential pressure in Pa
- VOL Battery voltage in V
- PCT Battery percent
- VAR Vario in m/s
- TEV TE Vario in m/s
- EXT Reserved, control button/event identifer etc, ASCII 127, no [$*,]
Examples:
$PTVSOAR,OAT,21.45,OAH,42.42,PRS,1013.25,PIT,88.5,PCT,34.5
$PTVSOAR,MNA,FlyingSilicon,MMO,FS1,MSN,A9999-,OAT,21.45,OAH,42.42,PRS,1013.25,PIT,88.5,VOL,3.7,PCT,50.0,VAR,-1.3,TEV,1.2,EXT,XYZ
Using the same hardware as previously, the following Arduino sketch was written:
/*********************************************************************
AN AIRSPEED DEVICE FOR USE WITH TOTALVARIO SENTENCE STRUCTURE
Tools > Board > Arduino MBed OS Boards > Seeed XIAO nRF52840
XIAO BLE Arduino device driver bug in v2.7.2
See: https://forum.seeedstudio.com/t/xiao-ble-sense-mbed-2-7-2-
battery-charge-and-voltage-monitor-analogread-p0-31-does-not-work
/266438
Note: HardwareBLESerial library needs to be modified
(HardwareBLESerial.h file) so that the BLE characterstics used
are HM-10 compatible (not Nordic nRF)
Current draw with BLE paired and sensors enabled = 7.7mA
*********************************************************************/
// ----------LIBRARIES--------------
#include <HardwareBLESerial.h>
#include <Wire.h> // For I2C comms
#include <sdpsensor.h> // Sensirion SDP3x library
#include <AHTxx.h> // Asair AHTx library
// ---------CONSTANTS---------------
// Function Timing
const int IntervalSDP = 199;
const int IntervalPOL = 500;
const int IntervalAHT = 4800;
const int IntervalHUM = 9700;
const int IntervalBTV = 9963;
const int IntervalLED = 3000;
const int LightLED = 500;
// -----------CLASSES---------------
// BLE Services
HardwareBLESerial &bleSerial = HardwareBLESerial::getInstance();
// Dual Pressure Sensor
SDP3XSensor sdp;
// Temperature & Humidity Sensor
AHTxx aht20(AHTXX_ADDRESS_X38, AHT2x_SENSOR); //sensor address, sensor type
//------------VARIABLES-------------
// Startup Melody Notes & Variables
#define NOTE_C6 1047
#define NOTE_E6 1319
#define NOTE_G6 1568
#define REST 0
int tempo = 90;
int buzzer = 1;
int melody[] = {
NOTE_C6,
16,
NOTE_G6,
16,
NOTE_E6,
16,
NOTE_C6,
32,
NOTE_G6,
-16,
NOTE_E6,
8,
};
int notes = sizeof(melody) / sizeof(melody[0]) / 2;
int wholenote = (60000 * 4) / tempo;
int divider = 0, noteDuration = 0;
// Sensirion Differential Pressure Variables
float sdpPres; // Variable for the differential pressure
// Battery Voltage Variables
int adcRead = 0; // Variable for reading the Voltage-Divide network on Xiao
float batVolt = 3.40; // Battery Volts
// Battery Charging Variable
#define charging D10 // Variable for detecting USB plugged in - so battery is charging
// Ambient Environment Variables
float ahtValue; // To store T/Rh result temporarily
float ambTemp = 0;
float ambHumi = 0;
// Sentence Buffer (String)
String msg = ""; // The final constructed sentence is: msg
// Function Timing
unsigned long currentMillis = 0; // Stores the value of millis() in each iteration of loop()
unsigned long previousSDPMillis = 0;
unsigned long previousPOLMillis = 0;
long previousAHTMillis = 0;
long previousHUMMillis = 0;
long previousBTVMillis = 0;
long previousLEDMillis = 0;
bool flashLEDstate = 0; // Used to record whether the LED is on or off
//===================SETUP========================
void setup() {
// Initialize the LED's as outputs and ensure they are off.
pinMode(LEDR, OUTPUT);
digitalWrite(LEDR, HIGH); // Ensure the LED off
pinMode(LEDG, OUTPUT);
digitalWrite(LEDG, HIGH); // Ensure the LED off
pinMode(LEDB, OUTPUT);
digitalWrite(LEDB, HIGH); // Ensure the LED off
// Setup Xiao Battery Monitoring
pinMode(P0_31, INPUT); // Battery Voltage monitoring pin
pinMode(P0_14, OUTPUT); // Enable Battery Voltage monitoring pin
digitalWrite(P0_14, LOW); // Enable
// Setup Xiao Battery Monitoring ADC
analogReference(AR_INTERNAL2V4); //Vref=2.4V
analogReadResolution(12); //12bits
// Setup Charge Monitoring
pinMode(D10, INPUT); //USB Voltage divided and fed to D10. Low =< 0.99V, High => 2.31V
// Setup Xiao Battery Charging Rate
pinMode(P0_13, OUTPUT); // Charge Current setting pin
digitalWrite(P0_13, LOW); // Charge Current = 100mA (13 High = 50mA)
// Setup Buzzer Output
pinMode(D1, OUTPUT); // D1 output to Buzzer
// Set up HardwareBLESerial and set BLE Name
bleSerial.beginAndSetupBLE("Airspeed_probe");
// Initialise I2C
Wire.begin();
// Initialise Serial Monitor (only used for debugging)
//Serial.begin(115200);
//delay(200); // Let serial console settle
// Initialise Sensirion SDP
int ret = sdp.init();
if (ret == 0) {
} else {
while (true) {
delay(1000);
}
}
// Initialise Asair AHT2x Temp|Humidity Sensor
while (aht20.begin() != true) {
delay(200);
}
// Startup Melody
{
for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) {
divider = melody[thisNote + 1];
if (divider > 0) {
// regular note, just proceed
noteDuration = (wholenote) / divider;
} else if (divider < 0) {
// dotted notes are represented with negative durations!!
noteDuration = (wholenote) / abs(divider);
noteDuration *= 1.5; // increases the duration in half for dotted notes
}
tone(buzzer, melody[thisNote], noteDuration * 0.9);
delay(noteDuration);
noTone(buzzer);
}
delay(200);
}
// Battery SoC Beeps
adcRead = analogRead(P0_31);
batVolt = ((510e3 + 1000e3) / 510e3) * 2.4 * adcRead / 4096;
int n;
{
if (batVolt > 3.9) {
// fully charged ... four beeps
for (n = 1; n <= 4; n++) {
tone(buzzer, 880, 200);
delay(400);
}
} else if (batVolt > 3.8) {
// well charged ... three beeps
for (n = 1; n <= 3; n++) {
tone(buzzer, 880, 200);
delay(400);
}
} else if (batVolt > 3.7) {
// some charge ... two beeps
for (n = 1; n <= 2; n++) {
tone(buzzer, 880, 200);
delay(400);
}
} else {
// discharged ... one beep
tone(buzzer, 440, 300);
delay(400);
}
}
noTone(buzzer);
delay(100);
}
//=================== LOOP ========================
void loop() {
currentMillis = millis() // Capture the latest value of millis()
pollBLE(); // This function must be called regularly to perform BLE updates
readSDP(); // This function reads IASsends to buildMSG for BLEprinting
readAHT(); // This function reads ambient Temperature and sends to buildMSG
readHUM(); // This function prints ambient Humidity and sends to buildMSG
readBTV(); // This function reads Battery Voltage and sends to buildMSG
flashLED(); // This function flashes the LED
}
//================ POLL BLESERIAL FUNCTION =====================
// pollBLE+++++++++++
void pollBLE() // Call function to poll BLE
{
if (currentMillis >= previousPOLMillis + IntervalPOL) // run 'pollBLE' function
{
previousPOLMillis = currentMillis; // save the time when change was made to bleSerial.poll();
bleSerial.poll(); // this must be called regularly to perform BLE updates
}
}
//================ SENSOR FUNCTIONS =====================
// readSPD+++++++++++
void readSDP() // read Differential Pressure from SDP sensor
{
if (currentMillis >= previousSDPMillis + IntervalSDP) // run 'readIAS' function only once time is up
{
previousSDPMillis = currentMillis; // save the time when change was made
int ret = sdp.readSample();
if (ret == 0) {
sdpPres = sdp.getDifferentialPressure();
}
sdpPres = sdpPres - 0.02;
if (sdpPres < 0) { // If there is a negative pressure set Differential pressure to zero
sdpPres = 0;
}
buildMSG(); // Call function to build the TotalVario sentence, then transmit to BLE
}
}
// readAHT+++++++++++
void readAHT() // read Temp & Hum and print Temp (only temperature)
{
if (currentMillis >= previousAHTMillis + IntervalAHT)
{
previousAHTMillis = currentMillis; // save the time when change was made
ahtValue = aht20.readTemperature(); //read 6-bytes via I2C, takes 80 milliseconds
ambTemp = ahtValue;
}
}
// readHUM+++++++++++
void readHUM() // print Humidity
{
if (currentMillis >= previousHUMMillis + IntervalHUM)
{
previousHUMMillis = currentMillis; // save the time when change was made
ahtValue = aht20.readHumidity(); //read another 6-bytes via I2C, takes 80 milliseconds
ambHumi = ahtValue;
}
}
// readBTV+++++++++++
void readBTV() // read battery Volts
{
if (currentMillis >= previousBTVMillis + IntervalBTV)
{
previousBTVMillis = currentMillis; // save the time when change was made
adcRead = analogRead(P0_31);
batVolt = ((510e3 + 1000e3) / 510e3) * 2.4 * adcRead / 4096;
}
}
// flashLED+++++++++++
void flashLED() // flash the LED if USB is plugged in
{
// If the USB power is connected flash Xiao LED
if (flashLEDstate == 0) // if the LED is off, wait for the interval to expire
{
if (currentMillis - previousLEDMillis >= IntervalLED) // time is up, so turn LED on
{
// Test to see if USB power is connected (so if battery is charging)
int value = digitalRead(charging);
if (value > 0) {
//bleSerial.println("Battery is charging"); // BLE Print
//Serial.println("Battery is charging"); // Serial Print
digitalWrite(LEDB, LOW); // LED on
flashLEDstate = 1; // Set the LED state to 1 or "on"
previousLEDMillis += IntervalLED; // save the time when change was made
}
}
} else // ie if LED is on.f
{
if (currentMillis - previousLEDMillis >= LightLED) // time is up, so turn LED off
{
digitalWrite(LEDB, HIGH); // LED off
flashLEDstate = 0; // Set the LED state to 0 or "off"
previousLEDMillis += LightLED; // save the time when change was made
}
}
}
//================ MESSAGE BUILD & BLE PRINT FUNCTION =====================
// $PTVSOAR,<type>,<value>,<type>,<value>,...*<checksum>
// Note: Checksum is optional
// buildMSG+++++++++++
void buildMSG() {
// TotalVario Sentence without checksum (TotalVario $PTVSOAR checksum is optional)
msg += F("$PTVSOAR");
msg += F(",");
msg += F("PIT");
msg += F(",");
msg += String(sdpPres, 2);
msg += F(",");
msg += F("OAT");
msg += F(",");
msg += String(ambTemp, 2);
msg += F(",");
msg += F("OAH");
msg += F(",");
msg += String(ambHumi, 2);
msg += F(",");
msg += F("VOL");
msg += F(",");
msg += String(batVolt, 1);
// Serial.println(msg); // Print msg for debugging
// Convert string to a character array and BLE Print
int buff_len = msg.length() + 1;
char buff_array[buff_len];
msg.toCharArray(buff_array, buff_len);
bleSerial.println(buff_array); // Print the entire message string
// Clear the msg buffer for next read
msg = "";
}