Advertisement
Guest User

updated .ino code

a guest
May 27th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.36 KB | None | 0 0
  1. // Current codeline for the elevation platforms (single MPL 3115A2 chip with a Feather board)
  2.  
  3. // xxx eta formula as a low pass filter ... average = (eta * float(reading) + ((1.0 - eta) * average))
  4.  
  5. // xxx change to a periodic polling of the sensor (fixed interval) and then when the ble call comes-in
  6. //     return the current aggregated value
  7.  
  8. // xxx add battery level command
  9.  
  10. #include <Wire.h>
  11. #include <SPI.h>
  12.  
  13. #include <Adafruit_Sensor.h>
  14. #include <Adafruit_MPL3115A2.h>
  15.  
  16. #if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_)
  17. #include <SoftwareSerial.h>
  18. #endif
  19.  
  20. #include "Adafruit_BLE.h"
  21. #include "Adafruit_BluefruitLE_SPI.h"
  22. // xxx remove #include "Adafruit_BluefruitLE_UART.h"
  23. #include "BluefruitConfig.h"
  24.  
  25. Adafruit_MPL3115A2 baro = Adafruit_MPL3115A2();
  26. Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);
  27.  
  28. const String PRES_HG, ELV_HR, TEMP_MPL;
  29. static float baroCorrectionDistance, baroMeasuredAltitude;
  30. int loopCount = 0; // currently not used anywhere
  31. float eta = 0.8;
  32. const float lowBattVolt = 3.0, highBattVolt = 4.3;
  33. float filtered_pressure, filtered_temp, filtered_alt;
  34. int check = 0;
  35.  
  36. // set initial to half-way between min and max (cf atlb)
  37. float battVolt = lowBattVolt + (.5 * (highBattVolt - lowBattVolt));
  38.  
  39. void error(const __FlashStringHelper*err) {
  40.   Serial.println(err);
  41.   while (1);
  42. }
  43.  
  44.  
  45. int readBattery() {
  46.   float rawBattVolt = 0.0;
  47.   int _battPercent = 0;
  48.  
  49.   // this bit of magic code from https://learn.adafruit.com/pages/6437/elements/1753818/
  50.   rawBattVolt = analogRead(A9); // the Feather's location for vBatt
  51.   rawBattVolt *= 2;             // we divided by 2, so multiply back
  52.   rawBattVolt *= 3.3;           // Multiply by 3.3V, our reference voltage
  53.   rawBattVolt /= 1024;          // convert to voltage
  54.  
  55.   battVolt = (eta * float(rawBattVolt)) + ((1.0 - eta) * battVolt);
  56.  
  57.   _battPercent = map(battVolt, lowBattVolt, highBattVolt, 1, 100);
  58.  
  59.   return(_battPercent);
  60. }
  61.  
  62.  
  63. void setup() {
  64.   Serial.begin(9600);
  65.    
  66.   if (!ble.begin(VERBOSE_MODE)){
  67.     error(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?"));
  68.   }
  69.   Serial.println(F("ok!"));
  70.  
  71.   while (!ble.isConnected()){
  72.     if (check == 0) {
  73.       //String* answer = whichDevice();
  74.       //PRES_HG = answer[0];
  75.       //ELV_HR = answer[1];
  76.       //TEMP_MPL = answer[2];
  77.       //ble.waitForOK();
  78.       String name;
  79.       ble.println("at+gapdevname");
  80.       ble.readline();
  81.       name = ble.buffer;
  82.       name.toLowerCase();
  83.      
  84.       if (name == "maria"){
  85.         PRES_HG = "77a";
  86.         ELV_HR = "77b";
  87.         TEMP_MPL = "77c";
  88.       }
  89.            
  90.       else if (name == "wanda"){
  91.         PRES_HG = "79a";
  92.         ELV_HR = "79b";
  93.         TEMP_MPL = "79c";
  94.       }
  95.      
  96.       else if (name == "sadie"){
  97.         PRES_HG = "80a";
  98.         ELV_HR = "80b";
  99.         TEMP_MPL = "80c";
  100.       }
  101.      
  102.       else if (name == "gurdy"){
  103.         PRES_HG = "81a";
  104.         ELV_HR = "81b";
  105.         TEMP_MPL = "81c";
  106.       }
  107.      
  108.       else if (name == "buzz"){
  109.         PRES_HG = "sa1";
  110.         ELV_HR = "sa2";
  111.         TEMP_MPL= "sa3";
  112.       }
  113.  
  114.       else if (name == "ada"){
  115.         PRES_HG = "82a";
  116.         ELV_HR = "82b";
  117.         TEMP_MPL= "82c";
  118.       }
  119.  
  120.       else if (name == "laura"){
  121.         PRES_HG = "83a";
  122.         ELV_HR = "83b";
  123.         TEMP_MPL= "83c";
  124.       }
  125.  
  126.       // xxx add an else statement when we have an unrecognized device
  127.  
  128.       check = 1;
  129.     }
  130.     // xxx slow this down? it seems awfully frequent given the context
  131.     delay(500);
  132.   }
  133.    
  134.   Serial.println( F("Switching to DATA mode!") );
  135.   ble.setMode(BLUEFRUIT_MODE_DATA);
  136.  
  137.   filtered_pressure = 1000;      
  138.   filtered_alt = 50;
  139.   filtered_temp = 22;
  140. }
  141.  
  142. String req = "";
  143.  
  144. String getSensors() {
  145.   String MPL3115A2 = "MPL3115A2-Pressure," + PRES_HG + ",Hg;MPL3115A2-Altitude," + ELV_HR + ",m;MPL3115A2-Air Temperature,"
  146.                      + TEMP_MPL + ",C;";
  147.   return (MPL3115A2 + "done");
  148. }
  149.  
  150. String getValues() {
  151.  
  152.   static char outstr[20];
  153.   float sensor;
  154.   String response;
  155.  
  156.   sensor = baro.getPressure();
  157.   filtered_pressure = eta * float(sensor) + ((1.0 - eta) * filtered_pressure);
  158.   response = "MPL3115A2-Pressure," + PRES_HG + "," + String(dtostrf(filtered_pressure, sizeof(filtered_pressure), 3, outstr)) + ";";
  159.  
  160.   sensor = baro.getAltitude();
  161.   sensor = sensor - baroCorrectionDistance;
  162.   filtered_alt = eta * float(sensor) + ((1.0 - eta) * filtered_alt);
  163.   response += "MPL3115A2-Altitude," + ELV_HR + "," + String(dtostrf(filtered_alt, sizeof(filtered_alt), 3, outstr)) + ";";
  164.  
  165.   sensor = baro.getTemperature();
  166.   filtered_temp = eta * float(sensor) + ((1.0 - eta) * filtered_temp);
  167.   response += "MPL3115A2-Air Temperature," + TEMP_MPL + "," + String(dtostrf(filtered_temp, sizeof(filtered_temp), 3, outstr)) + ";";
  168.  
  169.   return (response + "done");
  170. }
  171.  
  172. void loop() {
  173.   String calSensorID;
  174.   String calValue;
  175.   int battPercent = 0;
  176.  
  177.   // check sensors
  178.   if (!baro.begin()) {
  179.     Serial.println("Could not find the MPL3115A2 sensor");
  180.   }
  181.  
  182.   if (ble.isConnected()) {
  183.     while (ble.available()) {
  184.       // convert integer read to character
  185.       char c = (char) ble.read();
  186.       req += String(c);
  187.     }
  188.  
  189.     Serial.println("Request:" + req);
  190.  
  191.     // Connected Field Day device asked for the sensors available on this platform.
  192.     // xxx why doesn't this come from the database via an include file that is built
  193.     // at compile time (or have FD and the platform build from a common place and then
  194.     // just assume that it's so and not query the platform).
  195.    
  196.     // update - chau, eli and i are going to re-factor the interface between field day and the platform code,
  197.     // association, data collection, dis-association
  198.     if (req == "00") {
  199.       String sensors = getSensors();
  200.       String substr = "";
  201.       for (int i = 0; i < sensors.length(); i++) {
  202.         substr += sensors[i];
  203.         if ((sensors[i] == ';') || (substr == "done")) {
  204.           byte responseString[substr.length()];
  205.           substr.getBytes(responseString, substr.length() + 1);
  206.           ble.print(substr);
  207.           Serial.println("00 Substring: " + substr);
  208.           Serial.print("battVolt = "); Serial.println(battVolt);
  209.           substr = "";
  210.         }
  211.       }
  212.  
  213.     // connected Field Day device asked for the current data values for the sensors on this platform.
  214.     } else if (req == "vals") {
  215.       String values = getValues();
  216.       String substr = "";
  217.  
  218.       // xxx this code looks just like the stuff above
  219.       for (int i = 0; i < values.length(); i++) {
  220.         substr += values[i];
  221.         if ((values[i] == ';') || (substr == "done")) {
  222.           byte responseString[substr.length()];
  223.           substr.getBytes(responseString, substr.length() + 1);
  224.           ble.print(substr);
  225.           Serial.println("vals Substring: " + substr);
  226.           Serial.print("battVolt = "); Serial.println(battVolt);
  227.           substr = "";
  228.         }
  229.       }
  230.  
  231.     } else if (req == "batt") {
  232.       battPercent = readBattery();
  233.       ble.print(battPercent);
  234.  
  235.     } else { // xxx what is req set to in this clause?
  236.       int firstIndex = req.indexOf(";");
  237.       String request = req.substring(0, firstIndex);
  238.  
  239.       if (request == "cal") {
  240.  
  241.         // figure-out which parameter to calibrate with the passed value
  242.         int secondIndex = req.indexOf(";", firstIndex + 1);
  243.         String sensorID = req.substring(firstIndex + 1, secondIndex);
  244.         String calibValue = req.substring(secondIndex + 1);
  245.         Serial.print("sensorID="); Serial.print(sensorID); Serial.print(", calibValue="); Serial.println(calibValue);
  246.  
  247.         // regardless of which sensor is chosen/passed, calibrate the altitude with the passed value
  248.         baroMeasuredAltitude = baro.getAltitude();
  249.         baroCorrectionDistance = baroMeasuredAltitude - calibValue.toFloat();
  250.         Serial.print("baroMeasuredAltitude="); Serial.print(baroMeasuredAltitude);
  251.         Serial.print(" baroCorrectionDistance="); Serial.println(baroCorrectionDistance);
  252.  
  253.         ble.print("calibrated");
  254.       }
  255.     }
  256.    
  257.     req = "";
  258.     // xxx why sleep here? reduces energy consumption, makes typical wait time .5 * 5000 plus .5 * sample interval
  259.     // determine a reasonable local collection interval (knob) and check for bt message (knob)
  260.     delay(5000);
  261.   }
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement