Example code with external sensor and sleepy sensor

Hi,
I can get everything to work with the SleepySensor code example and the external I2c sensor code example (example is a BMP 680, although I used an Adafruit BMP390 and just a little code change), but I when I try merge the programs and try and get a 390 sensor sample and then sleep, I’m having no luck getting a valid reading from the I2C sensor. Sleep mixed with an I2C sensor seems like a very common use-case, so maybe you all can provide an example (and I can see where I’m going wrong)? Using the ESp32 Feather. Thank you!

1 Like

Hi @brecky !

Welcome to the forums, we are glad to have you!

Based on setup you’ve described, I would expect your scenario to work. :thinking: Let’s dig in a little further…

Can you share your code? Also, if you are able to share a photograph of your hardware? Perhaps somewhere between the two I could spot something out of place.

Best,
Zak

Hi Zak,

Yeah, I’m sure I’m doing something where I don’t totally understand the API or something.
Hard to tell from the picture, but EN->ATTN. Sensor works fine with the provided Arduino example slightly modified for the BMP390.

Thanks!
Brecky

Here is a picture:

Here is the code:

/*
   Sleepy Sensor Application

   The goal of this application is to show the elements required to construct an
   application which leverages the capabilities of the Notecard to minimize
   battery consumption by controlling the wake time of the host MCU.

   Use the following diagram to wire up the circuit. Note the Notecard's `ATTN`
   pin (exposed via the Notecarrier) is wired to the enable pin of the host MCU.
    ________________________________________
   |  ____________________________________  |               ____________________
   | |                                    | |              /
   | |        _________________           | |             |  O
   | |       |O|             |O|          | |             |     (Notecarrier-AL)
   | --> SDA-|*  ___________  *|-21       | |       VUSB>-|[]
   ----> SCL-|* | ESPRESSIF | *|-TX       | |  ----> BAT>-|[]
          14-|* |ESP32-WROOM| *|-RX       | |  |    MAIN>-|[]
          32-|* |CE         | *|-MI       | |  |     VIO>-|[]
          15-|* |           | *|-MO       | |  |          |[]
          33-|* |       ___ | *|-SCK      | |  |       V+-|[]
          27-|* |      |   || *|-A5    ---^-^--^----> GND-|[]
          12-|* |______|___|| *|-A4    |  | |  |       EN-|[]
          13-|*               *|-A3    |  | |  |      RST-|[]
         USB-|*               *|-A2    |  | |  |          |[]
   -----> EN-|*         ----- *|-A1    |  | ---^----> SCL-|[]
   | --> BAT-|*___      |   | *|-A0    |  -----^----> SDA-|[]
   | |       |    |     ----- *|-GND <--       | --> ATTN-|[]
   | |       |    |        _  *|-NC            | |  AUXEN-|[]
   | |       |-----       |O| *|-3V            | |  AUXRX-|[]
   | |       |      -----     *|-RST           | |  AUXTX-|[]
   | |       |O___0_|___|_0___O|               | |   AUX1-|[]
   | |                                         | |   AUX2-|[]
   | |_________________________________________| |   AUX3-|[]
   |_____________________________________________|   AUX4-|[]
                                                       RX-|[]
                                                       TX-|[]
                                                          |
                                                          |  O
                                                           \____________________

   NOTE: This sample is intended to compile for any Arduino compatible MCU
   architecture and Notecard/Notecarrier combination. However, it was created
   and tested using the Adafruit Huzzah32 and Notecarrier-AL.

   NOTE: This example has intentionally omitted error checking in order to
   highlight the elements required to make a power-efficient application.
*/

#include <Notecard.h>
#include <NotecardPseudoSensor.h>
#ifdef ARDUINO_ARCH_ESP32
#include <WiFi.h>
#endif

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BMP3XX.h"

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BMP3XX bmp;

// #define productUID "com.your-company.your-name:sensor" // redacted my account
#define serialDebug Serial

// This period controls the waking frequency of your host MCU, and will have a
// direct impact on battery conservation. It should be used to strike a balance
// between battery performance and data collection requirements.
static const size_t PERIOD_S = 30;

Notecard notecard;
blues::NotecardPseudoSensor pseudo_sensor(notecard);

float temperature = 0.0;
float pressure = 0.0;

void setup() {

#ifdef ARDUINO_ARCH_ESP32
  // Disable radios to improve power profile
  WiFi.mode(WIFI_OFF);
  ::btStop();
#endif

  // Provide visual signal when the Host MCU is powered
  ::pinMode(LED_BUILTIN, OUTPUT);
  ::digitalWrite(LED_BUILTIN, HIGH);

  // Initialize Debug Output
  serialDebug.begin(115200);
  static const size_t MAX_SERIAL_WAIT_MS = 5000;
  size_t begin_serial_wait_ms = ::millis();
  while (!serialDebug && (MAX_SERIAL_WAIT_MS > (::millis() - begin_serial_wait_ms))) {
    ; // wait for serial port to connect. Needed for native USB
  }
  notecard.setDebugOutputStream(serialDebug);

  // Initialize Notecard
  notecard.begin();

  if (!bmp.begin_I2C()) {   // hardware I2C mode, can pass in address & alt Wire
    //if (! bmp.begin_SPI(BMP_CS)) {  // hardware SPI mode
    //if (! bmp.begin_SPI(BMP_CS, BMP_SCK, BMP_MISO, BMP_MOSI)) {  // software SPI mode
    serialDebug.println("Could not find a valid BMP3 sensor, check wiring!");
    while (1);
  }

  // Set up oversampling and filter initialization
  bmp.setTemperatureOversampling(BMP3_OVERSAMPLING_8X);
  bmp.setPressureOversampling(BMP3_OVERSAMPLING_4X);
  bmp.setIIRFilterCoeff(BMP3_IIR_FILTER_COEFF_3);
  bmp.setOutputDataRate(BMP3_ODR_50_HZ);

  // Configure Notecard to synchronize with Notehub periodically, as well as
  // adjust the frequency based on the battery level
  {
    J * req = notecard.newRequest("hub.set");
    JAddStringToObject(req, "product", productUID);
    JAddStringToObject(req, "mode", "periodic");
    JAddStringToObject(req, "vinbound", "usb:60;high:120;normal:240;low:480;dead:0");
    JAddStringToObject(req, "voutbound", "usb:30;high:60;normal:90;low:120;dead:0");
    // JAddNumberToObject(req, "inbound", 5);
    // JAddNumberToObject(req, "outbound", 2);
    notecard.sendRequest(req);
  }

  // Optimize voltage variable behaviors for LiPo battery
  {
    J * req = notecard.newRequest("card.voltage");
    JAddStringToObject(req, "mode", "lipo");
    notecard.sendRequest(req);
  }

  // Establish a template to optimize queue size and data usage
  {
    J * req = notecard.newRequest("note.template");
    JAddStringToObject(req, "file", "sensor.qo");
    J * body = JAddObjectToObject(req, "body");
    JAddNumberToObject(body, "loggertemp", 12.1);
    JAddNumberToObject(body, "voltage", 12.1);
    JAddNumberToObject(body, "temperature", 12.1);
    JAddNumberToObject(body, "pressure", 12.1);
    notecard.sendRequest(req);
  }

  // Sample Sensor and queue results
  {
    double loggertemperature = 0;
    J *rsp = notecard.requestAndResponse(notecard.newRequest("card.temp"));
    if (rsp != NULL) {
      loggertemperature = JGetNumber(rsp, "value");
      notecard.deleteResponse(rsp);
    }

    double voltage = 0;
    rsp = notecard.requestAndResponse(notecard.newRequest("card.voltage"));
    if (rsp != NULL) {
      voltage = JGetNumber(rsp, "value");
      notecard.deleteResponse(rsp);
    }

    J * req = notecard.newRequest("note.add");
    JAddStringToObject(req, "file", "sensor.qo");
    J * body = JAddObjectToObject(req, "body");
    JAddNumberToObject(body, "loggertemp", loggertemperature);
    JAddNumberToObject(body, "voltage", voltage);
    JAddNumberToObject(body, "temperature", temperature);
    JAddNumberToObject(body, "pressure", pressure);
    notecard.sendRequest(req);
  }
}

void loop() {
  // Request sleep from loop to safeguard against transmission failure, and
  // ensure sleep request is honored so power usage is minimized.


  if (! bmp.performReading()) {
    serialDebug.println("Failed to obtain a reading...");
    delay(15000);
    return;
  }

  temperature = bmp.temperature;
  pressure = bmp.pressure / 100.0;

  serialDebug.print("Temperature = ");
  serialDebug.print(temperature);
  serialDebug.println(" *C");

  serialDebug.print("Pressure = ");
  serialDebug.print(pressure);
  serialDebug.println(" hPa");


  delay(100);

  // Create a "command" instead of a "request", because the host
  // MCU is going to power down and cannot receive a response.
  J * req = NoteNewCommand("card.attn");
  JAddStringToObject(req, "mode", "sleep");
  JAddNumberToObject(req, "seconds", PERIOD_S);
  notecard.sendRequest(req);

  // Wait 1s before retrying
  ::delay(1000);
}

I see the problem, and it’s nothing that you’ve done.

The EN pin on the Notecarrier-AF was originally designed to power down the entire board. As you now know, you don’t want this behavior when you are trying to use SleepySensor.

If you are comfortable modifying the hardware, you can cut the trace between the EN pin and the button, as discussed here. If you have an exact-o-knife, it is a snap!

I’ll DM and see if I can’t help fix your hardware situation.

Cheers,
Zak

1 Like

Hi. Thanks for the kind response. I cut the trace, but I don’t really think that’s my problem. I see the external sensor collect valid looking data periodically and I see my card sending data periodically in the terminal monitor when I connect through the Feather. This all seems to work as I think it would and I don’t think that would be true if my board was powering off during sensor collection (in the loop section). The problem, as far as I can tell, is that while I save the sensor data to global variables and then try and send that data to the cloud, the values collected are either not transmitted at all or depending on a code change from what I posted, come out as 0.0 for the temp and the pressure. Either way, I think I’m making a coding mistake. Maybe you can look over the program? I’m new to the API and am probably just not understanding something correctly. Thank you, Brecky

Hi again, @brecky!

Could you try moving this bit:

 if (! bmp.performReading()) {
    serialDebug.println("Failed to obtain a reading...");
    delay(15000);
    return;
  }

  temperature = bmp.temperature;
  pressure = bmp.pressure / 100.0;

  serialDebug.print("Temperature = ");
  serialDebug.print(temperature);
  serialDebug.println(" *C");

  serialDebug.print("Pressure = ");
  serialDebug.print(pressure);
  serialDebug.println(" hPa");

  delay(100);

Up into the setup function, just above:

    J * req = notecard.newRequest("note.add");
    JAddStringToObject(req, "file", "sensor.qo");
    J * body = JAddObjectToObject(req, "body");
    JAddNumberToObject(body, "loggertemp", loggertemperature);
    JAddNumberToObject(body, "voltage", voltage);
    JAddNumberToObject(body, "temperature", temperature);
    JAddNumberToObject(body, "pressure", pressure);
    notecard.sendRequest(req);

and see how you fair?

Detailed Summary:

The second block of code is what is organizing the data into a Note in preparation to send it to Notehub.io. So before you execute that block, you will need to sample the BMP, and populate your temperature and pressure fields.

In the sleepy sensor application, all work is done in the setup function (which executes every time the device wakes), and the request to sleep is the only thing that should be performed in the loop (to make sure it can’t fail).

Best,
Zak

The controller records a value, but it’s bogus. It’s always the same temp measurement (23.72733879089355), which I’m sure is the value of 00000000. I’ve repeatedly tried the Adafruit 388 example program on the same hardware setup and it works fine, so I know the sensor is connected and should return valid data. Here is the specific program that I’m not now using:

thanks!

/*
 * Sleepy Sensor Application
 *
 * The goal of this application is to show the elements required to construct an
 * application which leverages the capabilities of the Notecard to minimize
 * battery consumption by controlling the wake time of the host MCU.
 *
 * Use the following diagram to wire up the circuit. Note the Notecard's `ATTN`
 * pin (exposed via the Notecarrier) is wired to the enable pin of the host MCU.
 *  ________________________________________
 * |  ____________________________________  |               ____________________
 * | |                                    | |              /
 * | |        _________________           | |             |  O
 * | |       |O|             |O|          | |             |     (Notecarrier-AL)
 * | --> SDA-|*  ___________  *|-21       | |       VUSB>-|[]
 * ----> SCL-|* | ESPRESSIF | *|-TX       | |  ----> BAT>-|[]
 *        14-|* |ESP32-WROOM| *|-RX       | |  |    MAIN>-|[]
 *        32-|* |CE         | *|-MI       | |  |     VIO>-|[]
 *        15-|* |           | *|-MO       | |  |          |[]
 *        33-|* |       ___ | *|-SCK      | |  |       V+-|[]
 *        27-|* |      |   || *|-A5    ---^-^--^----> GND-|[]
 *        12-|* |______|___|| *|-A4    |  | |  |       EN-|[]
 *        13-|*               *|-A3    |  | |  |      RST-|[]
 *       USB-|*               *|-A2    |  | |  |          |[]
 * -----> EN-|*         ----- *|-A1    |  | ---^----> SCL-|[]
 * | --> BAT-|*___      |   | *|-A0    |  -----^----> SDA-|[]
 * | |       |    |     ----- *|-GND <--       | --> ATTN-|[]
 * | |       |    |        _  *|-NC            | |  AUXEN-|[]
 * | |       |-----       |O| *|-3V            | |  AUXRX-|[]
 * | |       |      -----     *|-RST           | |  AUXTX-|[]
 * | |       |O___0_|___|_0___O|               | |   AUX1-|[]
 * | |                                         | |   AUX2-|[]
 * | |_________________________________________| |   AUX3-|[]
 * |_____________________________________________|   AUX4-|[]
 *                                                     RX-|[]
 *                                                     TX-|[]
 *                                                        |
 *                                                        |  O
 *                                                         \____________________
 *
 * NOTE: This sample is intended to compile for any Arduino compatible MCU
 * architecture and Notecard/Notecarrier combination. However, it was created
 * and tested using the Adafruit Huzzah32 and Notecarrier-AL.
 *
 * NOTE: This example has intentionally omitted error checking in order to
 * highlight the elements required to make a power-efficient application.
 */

#include <Notecard.h>
#include <NotecardPseudoSensor.h>
#ifdef ARDUINO_ARCH_ESP32
  #include <WiFi.h>
#endif

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BMP3XX.h"

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BMP3XX bmp;

// #define productUID "com.your-company.your-name:sensor" -- real address redacted
#define serialDebug Serial

// This period controls the waking frequency of your host MCU, and will have a
// direct impact on battery conservation. It should be used to strike a balance
// between battery performance and data collection requirements.
static const size_t PERIOD_S = 30;

Notecard notecard;
blues::NotecardPseudoSensor pseudo_sensor(notecard);

void setup() {
#ifdef ARDUINO_ARCH_ESP32
  // Disable radios to improve power profile
  WiFi.mode(WIFI_OFF);
  ::btStop();
#endif

  // Provide visual signal when the Host MCU is powered
  ::pinMode(LED_BUILTIN, OUTPUT);
  ::digitalWrite(LED_BUILTIN, HIGH);

  // Initialize Debug Output
  serialDebug.begin(115200);
  static const size_t MAX_SERIAL_WAIT_MS = 5000;
  size_t begin_serial_wait_ms = ::millis();
  while (!serialDebug && (MAX_SERIAL_WAIT_MS > (::millis() - begin_serial_wait_ms))) {
    ; // wait for serial port to connect. Needed for native USB
  }
  notecard.setDebugOutputStream(serialDebug);

  // Initialize Notecard
  notecard.begin();

  if (!bmp.begin_I2C()) {   // hardware I2C mode, can pass in address & alt Wire
    //if (! bmp.begin_SPI(BMP_CS)) {  // hardware SPI mode
    //if (! bmp.begin_SPI(BMP_CS, BMP_SCK, BMP_MISO, BMP_MOSI)) {  // software SPI mode
    serialDebug.println("Could not find a valid BMP3 sensor, check wiring!");
    while (1);
  }

  // Set up oversampling and filter initialization
  bmp.setTemperatureOversampling(BMP3_OVERSAMPLING_8X);
  bmp.setPressureOversampling(BMP3_OVERSAMPLING_4X);
  bmp.setIIRFilterCoeff(BMP3_IIR_FILTER_COEFF_3);
  bmp.setOutputDataRate(BMP3_ODR_50_HZ);

  // Configure Notecard to synchronize with Notehub periodically, as well as
  // adjust the frequency based on the battery level
  {
    J * req = notecard.newRequest("hub.set");
    JAddStringToObject(req, "product", productUID);
    JAddStringToObject(req, "mode", "periodic");
    // JAddStringToObject(req, "vinbound", "usb:60;high:120;normal:240;low:480;dead:0");
    // JAddStringToObject(req, "voutbound", "usb:30;high:60;normal:90;low:120;dead:0");
    JAddNumberToObject(req, "inbound", 5);
    JAddNumberToObject(req, "outbound", 2);
    notecard.sendRequest(req);
 
  }

  // Optimize voltage variable behaviors for LiPo battery
  {
    J * req = notecard.newRequest("card.voltage");
    JAddStringToObject(req, "mode", "lipo");
    notecard.sendRequest(req);
  }

  // Establish a template to optimize queue size and data usage
  {
    J * req = notecard.newRequest("note.template");
    JAddStringToObject(req, "file", "sensor.qo");
    J * body = JAddObjectToObject(req, "body");
    JAddNumberToObject(body, "temp", 12.1);
    notecard.sendRequest(req);
  }

  // Sample Pseudo Sensor and queue results
  {
    // float temperature = pseudo_sensor.temp();

    if (! bmp.performReading()) {
      serialDebug.println("Failed to obtain a reading...");
      return;
    }

    float temperature = bmp.temperature;
    serialDebug.print("Temperature = ");
    serialDebug.print(temperature);
    serialDebug.println(" *C");

    J * req = notecard.newRequest("note.add");
    JAddStringToObject(req, "file", "sensor.qo");
    J * body = JAddObjectToObject(req, "body");
    JAddNumberToObject(body, "temp", temperature);
    notecard.sendRequest(req);
  }
}

void loop() {
  // Request sleep from loop to safeguard against tranmission failure, and
  // ensure sleep request is honored so power usage is minimized
  {
    // Create a "command" instead of a "request", because the host
    // MCU is going to power down and cannot receive a response.
    J * req = NoteNewCommand("card.attn");
    JAddStringToObject(req, "mode", "sleep");
    JAddNumberToObject(req, "seconds", PERIOD_S);
    notecard.sendRequest(req);
  }

  // Wait 1s before retrying
  ::delay(1000);
}

Think I fixed it by removing the pseudo_sensor code header file and related code. This seems to work if anyone is interested:

/*
   Sleepy Sensor Application

   The goal of this application is to show the elements required to construct an
   application which leverages the capabilities of the Notecard to minimize
   battery consumption by controlling the wake time of the host MCU.

   Use the following diagram to wire up the circuit. Note the Notecard's `ATTN`
   pin (exposed via the Notecarrier) is wired to the enable pin of the host MCU.
    ________________________________________
   |  ____________________________________  |               ____________________
   | |                                    | |              /
   | |        _________________           | |             |  O
   | |       |O|             |O|          | |             |     (Notecarrier-AL)
   | --> SDA-|*  ___________  *|-21       | |       VUSB>-|[]
   ----> SCL-|* | ESPRESSIF | *|-TX       | |  ----> BAT>-|[]
          14-|* |ESP32-WROOM| *|-RX       | |  |    MAIN>-|[]
          32-|* |CE         | *|-MI       | |  |     VIO>-|[]
          15-|* |           | *|-MO       | |  |          |[]
          33-|* |       ___ | *|-SCK      | |  |       V+-|[]
          27-|* |      |   || *|-A5    ---^-^--^----> GND-|[]
          12-|* |______|___|| *|-A4    |  | |  |       EN-|[]
          13-|*               *|-A3    |  | |  |      RST-|[]
         USB-|*               *|-A2    |  | |  |          |[]
   -----> EN-|*         ----- *|-A1    |  | ---^----> SCL-|[]
   | --> BAT-|*___      |   | *|-A0    |  -----^----> SDA-|[]
   | |       |    |     ----- *|-GND <--       | --> ATTN-|[]
   | |       |    |        _  *|-NC            | |  AUXEN-|[]
   | |       |-----       |O| *|-3V            | |  AUXRX-|[]
   | |       |      -----     *|-RST           | |  AUXTX-|[]
   | |       |O___0_|___|_0___O|               | |   AUX1-|[]
   | |                                         | |   AUX2-|[]
   | |_________________________________________| |   AUX3-|[]
   |_____________________________________________|   AUX4-|[]
                                                       RX-|[]
                                                       TX-|[]
                                                          |
                                                          |  O
                                                           \____________________

   NOTE: This sample is intended to compile for any Arduino compatible MCU
   architecture and Notecard/Notecarrier combination. However, it was created
   and tested using the Adafruit Huzzah32 and Notecarrier-AL.

   NOTE: This example has intentionally omitted error checking in order to
   highlight the elements required to make a power-efficient application.
*/

#include <Notecard.h>
#ifdef ARDUINO_ARCH_ESP32
#include <WiFi.h>
#endif

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BMP3XX.h"

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BMP3XX bmp;

// #define productUID "com.your-company.your-name:sensor"
#define serialDebug Serial

// This period controls the waking frequency of your host MCU, and will have a
// direct impact on battery conservation. It should be used to strike a balance
// between battery performance and data collection requirements.
static const size_t PERIOD_S = 30;

Notecard notecard;

void setup() {
#ifdef ARDUINO_ARCH_ESP32
  // Disable radios to improve power profile
  WiFi.mode(WIFI_OFF);
  ::btStop();
#endif

  // Provide visual signal when the Host MCU is powered
  ::pinMode(LED_BUILTIN, OUTPUT);
  ::digitalWrite(LED_BUILTIN, HIGH);

  // Initialize Debug Output
  serialDebug.begin(115200);
  static const size_t MAX_SERIAL_WAIT_MS = 5000;
  size_t begin_serial_wait_ms = ::millis();
  while (!serialDebug && (MAX_SERIAL_WAIT_MS > (::millis() - begin_serial_wait_ms))) {
    ; // wait for serial port to connect. Needed for native USB
  }
  notecard.setDebugOutputStream(serialDebug);

  if (!bmp.begin_I2C()) {   // hardware I2C mode, can pass in address & alt Wire
    //if (! bmp.begin_SPI(BMP_CS)) {  // hardware SPI mode
    //if (! bmp.begin_SPI(BMP_CS, BMP_SCK, BMP_MISO, BMP_MOSI)) {  // software SPI mode
    serialDebug.println("Could not find a valid BMP3 sensor, check wiring!");
    while (1);
  }

  // Set up oversampling and filter initialization
  bmp.setTemperatureOversampling(BMP3_OVERSAMPLING_8X);
  bmp.setPressureOversampling(BMP3_OVERSAMPLING_4X);
  bmp.setIIRFilterCoeff(BMP3_IIR_FILTER_COEFF_3);
  bmp.setOutputDataRate(BMP3_ODR_50_HZ);

  if (! bmp.performReading()) {
    serialDebug.println("Failed to obtain a reading...");
    return;
  }

  float temperature = bmp.temperature;
  serialDebug.print("Temperature = ");
  serialDebug.print(temperature);
  serialDebug.println(" *C");

  Serial.print("Pressure = ");
  Serial.print(bmp.pressure / 100.0);
  Serial.println(" hPa");

  Serial.print("Approx. Altitude = ");
  Serial.print(bmp.readAltitude(SEALEVELPRESSURE_HPA));
  Serial.println(" m");

  // Initialize Notecard
  notecard.begin();

  // Configure Notecard to synchronize with Notehub periodically, as well as
  // adjust the frequency based on the battery level
  {
    J * req = notecard.newRequest("hub.set");
    JAddStringToObject(req, "product", productUID);
    JAddStringToObject(req, "mode", "periodic");
    // JAddStringToObject(req, "vinbound", "usb:60;high:120;normal:240;low:480;dead:0");
    // JAddStringToObject(req, "voutbound", "usb:30;high:60;normal:90;low:120;dead:0");
    JAddNumberToObject(req, "inbound", 5);
    JAddNumberToObject(req, "outbound", 2);
    notecard.sendRequest(req);

  }

  // Optimize voltage variable behaviors for LiPo battery
  {
    J * req = notecard.newRequest("card.voltage");
    JAddStringToObject(req, "mode", "lipo");
    notecard.sendRequest(req);
  }

  // Establish a template to optimize queue size and data usage
  {
    J * req = notecard.newRequest("note.template");
    JAddStringToObject(req, "file", "sensor.qo");
    J * body = JAddObjectToObject(req, "body");
    JAddNumberToObject(body, "temp", 12.1);
    JAddNumberToObject(body, "pressure", 12.1);
    JAddNumberToObject(body, "loggertemperature", 12.1);
    JAddNumberToObject(body, "voltage", 12.1);
    notecard.sendRequest(req);
  }

  // Sample and queue results
  {

     double loggertemperature = 0;
    J *rsp = notecard.requestAndResponse(notecard.newRequest("card.temp"));
    if (rsp != NULL) {
      loggertemperature = JGetNumber(rsp, "value");
      notecard.deleteResponse(rsp);
    }

    double voltage = 0;
    rsp = notecard.requestAndResponse(notecard.newRequest("card.voltage"));
    if (rsp != NULL) {
      voltage = JGetNumber(rsp, "value");
      notecard.deleteResponse(rsp);
    }

    if (! bmp.performReading()) {
      serialDebug.println("Failed to obtain a reading...");
      return;
    }

    float temperature = bmp.temperature;
    serialDebug.print("Temperature = ");
    serialDebug.print(temperature);
    serialDebug.println(" *C");

    float pressure = bmp.pressure / 100.0;
    serialDebug.print("Pressure = ");
    serialDebug.print(pressure);
    serialDebug.println(" hPa");

    serialDebug.print("Approx. Altitude = ");
    serialDebug.print(bmp.readAltitude(SEALEVELPRESSURE_HPA));
    serialDebug.println(" m");

    J * req = notecard.newRequest("note.add");
    JAddStringToObject(req, "file", "sensor.qo");
    J * body = JAddObjectToObject(req, "body");
    JAddNumberToObject(body, "temp", temperature);
    JAddNumberToObject(body, "pressure", pressure);
    JAddNumberToObject(body, "voltage", voltage);
    JAddNumberToObject(body, "loggertemperature", loggertemperature);
    
    notecard.sendRequest(req);
  }
}

void loop() {
  // Request sleep from loop to safeguard against tranmission failure, and
  // ensure sleep request is honored so power usage is minimized
  {
    // Create a "command" instead of a "request", because the host
    // MCU is going to power down and cannot receive a response.
    J * req = NoteNewCommand("card.attn");
    JAddStringToObject(req, "mode", "sleep");
    JAddNumberToObject(req, "seconds", PERIOD_S);
    notecard.sendRequest(req);
  }

  // Wait 1s before retrying
  ::delay(1000);
}
2 Likes