Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Current codeline for the elevation platforms (single MPL 3115A2 chip with a Feather board)
- // xxx eta formula as a low pass filter ... average = (eta * float(reading) + ((1.0 - eta) * average))
- // xxx change to a periodic polling of the sensor (fixed interval) and then when the ble call comes-in
- // return the current aggregated value
- // xxx add battery level command
- #include <Wire.h>
- #include <SPI.h>
- #include <Adafruit_Sensor.h>
- #include <Adafruit_MPL3115A2.h>
- #if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_)
- #include <SoftwareSerial.h>
- #endif
- #include "Adafruit_BLE.h"
- #include "Adafruit_BluefruitLE_SPI.h"
- // xxx remove #include "Adafruit_BluefruitLE_UART.h"
- #include "BluefruitConfig.h"
- Adafruit_MPL3115A2 baro = Adafruit_MPL3115A2();
- Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);
- const String PRES_HG, ELV_HR, TEMP_MPL;
- static float baroCorrectionDistance, baroMeasuredAltitude;
- int loopCount = 0; // currently not used anywhere
- float eta = 0.8;
- const float lowBattVolt = 3.0, highBattVolt = 4.3;
- float filtered_pressure, filtered_temp, filtered_alt;
- int check = 0;
- // set initial to half-way between min and max (cf atlb)
- float battVolt = lowBattVolt + (.5 * (highBattVolt - lowBattVolt));
- void error(const __FlashStringHelper*err) {
- Serial.println(err);
- while (1);
- }
- int readBattery() {
- float rawBattVolt = 0.0;
- int _battPercent = 0;
- // this bit of magic code from https://learn.adafruit.com/pages/6437/elements/1753818/
- rawBattVolt = analogRead(A9); // the Feather's location for vBatt
- rawBattVolt *= 2; // we divided by 2, so multiply back
- rawBattVolt *= 3.3; // Multiply by 3.3V, our reference voltage
- rawBattVolt /= 1024; // convert to voltage
- battVolt = (eta * float(rawBattVolt)) + ((1.0 - eta) * battVolt);
- _battPercent = map(battVolt, lowBattVolt, highBattVolt, 1, 100);
- return(_battPercent);
- }
- void setup() {
- Serial.begin(9600);
- if (!ble.begin(VERBOSE_MODE)){
- error(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?"));
- }
- Serial.println(F("ok!"));
- while (!ble.isConnected()){
- if (check == 0) {
- //String* answer = whichDevice();
- //PRES_HG = answer[0];
- //ELV_HR = answer[1];
- //TEMP_MPL = answer[2];
- //ble.waitForOK();
- String name;
- ble.println("at+gapdevname");
- ble.readline();
- name = ble.buffer;
- name.toLowerCase();
- if (name == "maria"){
- PRES_HG = "77a";
- ELV_HR = "77b";
- TEMP_MPL = "77c";
- }
- else if (name == "wanda"){
- PRES_HG = "79a";
- ELV_HR = "79b";
- TEMP_MPL = "79c";
- }
- else if (name == "sadie"){
- PRES_HG = "80a";
- ELV_HR = "80b";
- TEMP_MPL = "80c";
- }
- else if (name == "gurdy"){
- PRES_HG = "81a";
- ELV_HR = "81b";
- TEMP_MPL = "81c";
- }
- else if (name == "buzz"){
- PRES_HG = "sa1";
- ELV_HR = "sa2";
- TEMP_MPL= "sa3";
- }
- else if (name == "ada"){
- PRES_HG = "82a";
- ELV_HR = "82b";
- TEMP_MPL= "82c";
- }
- else if (name == "laura"){
- PRES_HG = "83a";
- ELV_HR = "83b";
- TEMP_MPL= "83c";
- }
- // xxx add an else statement when we have an unrecognized device
- check = 1;
- }
- // xxx slow this down? it seems awfully frequent given the context
- delay(500);
- }
- Serial.println( F("Switching to DATA mode!") );
- ble.setMode(BLUEFRUIT_MODE_DATA);
- filtered_pressure = 1000;
- filtered_alt = 50;
- filtered_temp = 22;
- }
- String req = "";
- String getSensors() {
- String MPL3115A2 = "MPL3115A2-Pressure," + PRES_HG + ",Hg;MPL3115A2-Altitude," + ELV_HR + ",m;MPL3115A2-Air Temperature,"
- + TEMP_MPL + ",C;";
- return (MPL3115A2 + "done");
- }
- String getValues() {
- static char outstr[20];
- float sensor;
- String response;
- sensor = baro.getPressure();
- filtered_pressure = eta * float(sensor) + ((1.0 - eta) * filtered_pressure);
- response = "MPL3115A2-Pressure," + PRES_HG + "," + String(dtostrf(filtered_pressure, sizeof(filtered_pressure), 3, outstr)) + ";";
- sensor = baro.getAltitude();
- sensor = sensor - baroCorrectionDistance;
- filtered_alt = eta * float(sensor) + ((1.0 - eta) * filtered_alt);
- response += "MPL3115A2-Altitude," + ELV_HR + "," + String(dtostrf(filtered_alt, sizeof(filtered_alt), 3, outstr)) + ";";
- sensor = baro.getTemperature();
- filtered_temp = eta * float(sensor) + ((1.0 - eta) * filtered_temp);
- response += "MPL3115A2-Air Temperature," + TEMP_MPL + "," + String(dtostrf(filtered_temp, sizeof(filtered_temp), 3, outstr)) + ";";
- return (response + "done");
- }
- void loop() {
- String calSensorID;
- String calValue;
- int battPercent = 0;
- // check sensors
- if (!baro.begin()) {
- Serial.println("Could not find the MPL3115A2 sensor");
- }
- if (ble.isConnected()) {
- while (ble.available()) {
- // convert integer read to character
- char c = (char) ble.read();
- req += String(c);
- }
- Serial.println("Request:" + req);
- // Connected Field Day device asked for the sensors available on this platform.
- // xxx why doesn't this come from the database via an include file that is built
- // at compile time (or have FD and the platform build from a common place and then
- // just assume that it's so and not query the platform).
- // update - chau, eli and i are going to re-factor the interface between field day and the platform code,
- // association, data collection, dis-association
- if (req == "00") {
- String sensors = getSensors();
- String substr = "";
- for (int i = 0; i < sensors.length(); i++) {
- substr += sensors[i];
- if ((sensors[i] == ';') || (substr == "done")) {
- byte responseString[substr.length()];
- substr.getBytes(responseString, substr.length() + 1);
- ble.print(substr);
- Serial.println("00 Substring: " + substr);
- Serial.print("battVolt = "); Serial.println(battVolt);
- substr = "";
- }
- }
- // connected Field Day device asked for the current data values for the sensors on this platform.
- } else if (req == "vals") {
- String values = getValues();
- String substr = "";
- // xxx this code looks just like the stuff above
- for (int i = 0; i < values.length(); i++) {
- substr += values[i];
- if ((values[i] == ';') || (substr == "done")) {
- byte responseString[substr.length()];
- substr.getBytes(responseString, substr.length() + 1);
- ble.print(substr);
- Serial.println("vals Substring: " + substr);
- Serial.print("battVolt = "); Serial.println(battVolt);
- substr = "";
- }
- }
- } else if (req == "batt") {
- battPercent = readBattery();
- ble.print(battPercent);
- } else { // xxx what is req set to in this clause?
- int firstIndex = req.indexOf(";");
- String request = req.substring(0, firstIndex);
- if (request == "cal") {
- // figure-out which parameter to calibrate with the passed value
- int secondIndex = req.indexOf(";", firstIndex + 1);
- String sensorID = req.substring(firstIndex + 1, secondIndex);
- String calibValue = req.substring(secondIndex + 1);
- Serial.print("sensorID="); Serial.print(sensorID); Serial.print(", calibValue="); Serial.println(calibValue);
- // regardless of which sensor is chosen/passed, calibrate the altitude with the passed value
- baroMeasuredAltitude = baro.getAltitude();
- baroCorrectionDistance = baroMeasuredAltitude - calibValue.toFloat();
- Serial.print("baroMeasuredAltitude="); Serial.print(baroMeasuredAltitude);
- Serial.print(" baroCorrectionDistance="); Serial.println(baroCorrectionDistance);
- ble.print("calibrated");
- }
- }
- req = "";
- // xxx why sleep here? reduces energy consumption, makes typical wait time .5 * 5000 plus .5 * sample interval
- // determine a reasonable local collection interval (knob) and check for bt message (knob)
- delay(5000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement