OPEnSLab-OSU / Loom-V4

Open Source Internet Of Things Rapid Prototyping Framework For Environmental Sensing Applications
5 stars 1 forks source link

Chime crash due to "Alarm triggered during sample" #146

Closed Sarvesh-Thiruppathi closed 2 months ago

Sarvesh-Thiruppathi commented 3 months ago

Describe the bug Chime 8 crashed with all LEDs turned on and stopped sending data to MongoDB. A zipped version of the SD card is uploaded here (Chime8Backup.zip) for reference.

Hardware in Use WeatherChime PCB, Hypnos Board, Feather M0, TSL2591, Sparkfun LTE Board, MS5803 118/119, SHT31, Terros, and Tipping Bucket

Expected behavior Chime to continue reading and uploading data without crashing

Code Unsure who uploaded the code, but should be roughly this:

/**
 * In lab use case example for the WeatherChimes project
 * 
 * This project uses SDI12, TSL2591 and an SHT31 sensor to log environment data and logs it to both the SD card and also MQTT/MongoDB
 * 
 * MANAGER MUST BE INCLUDED FIRST IN ALL CODE
 */
#include <Loom_Manager.h>
#include <Logger.h>

#include <Hardware/Loom_Hypnos/Loom_Hypnos.h>

#include <Sensors/Loom_Analog/Loom_Analog.h>
#include <Sensors/I2C/Loom_SHT31/Loom_SHT31.h>
#include <Sensors/I2C/Loom_TSL2591/Loom_TSL2591.h>
#include <Sensors/I2C/Loom_MS5803/Loom_MS5803.h>
#include <Hardware/Loom_TippingBucket/Loom_TippingBucket.h>
#include <Sensors/Analog/Loom_Teros10/Loom_Teros10.h>

#include <Internet/Logging/Loom_MQTT/Loom_MQTT.h>
#include <Internet/Connectivity/Loom_LTE/Loom_LTE.h>

// Pin to have the secondary interrupt triggered from
#define INT_PIN A0

volatile bool sampleFlag = true; // Sample flag set to 1 so we sample in the first cycle, set to 1 in the ISR, set ot 0 end of sample loop
volatile bool tipFlag = false;

// Used to track timing for debounce
unsigned long tip_time = 0;
unsigned long last_tip_time = 0;

Manager manager("KhutiChime", 2);

// Create a new Hypnos object
Loom_Hypnos hypnos(manager, HYPNOS_VERSION::V3_3, TIME_ZONE::PST, true);

// Analog for reading battery voltage
Loom_Analog analog(manager);

// Create sensor classes
Loom_SHT31 sht(manager);
Loom_TSL2591 tsl(manager);
Loom_MS5803 ms_water(manager, 119); // 119(0x77) if CSB=LOW external, 118(0x76) if CSB=HIGH on WC PCB
Loom_MS5803 ms_air(manager, 118); // 118(0x76) if CSB=HIGH on WC PCB
Loom_TippingBucket bucket(manager, COUNTER_TYPE::MANUAL, 0.01f);
Loom_Teros10 teros(manager, A1);

Loom_LTE lte(manager, "hologram", "", "");
Loom_MQTT mqtt(manager, lte.getClient());

/* Calculate the water height based on the difference of pressures*/
float calculateWaterHeight(){
  // ((Water Pressure - Air Pressure) * 100 (conversion to pascals)) / (Water Density * Gravity)
  return (((ms_water.getPressure()-ms_air.getPressure()) * 100) / (997.77 * 9.81));
}

// Called when the interrupt is triggered 
void isrTrigger(){
  sampleFlag = true;
  detachInterrupt(INT_PIN);
  hypnos.wakeup();
}

void tipTrigger() {
  hypnos.shouldPowerUp = false;
  tipFlag = true;
  detachInterrupt(INT_PIN);
}

void setup() {

  // Enable debug SD logging and function summaires
  ENABLE_SD_LOGGING;
  ENABLE_FUNC_SUMMARIES;

  // Set the interrupt pin to pullup
  pinMode(INT_PIN, INPUT);

  // Wait 20 seconds for the serial console to open
  manager.beginSerial();

  // Enable the hypnos rails
  hypnos.enable();

  hypnos.setNetworkInterface(&lte);

  // Give the bucket an instance of the hypnos
  bucket.setHypnosInstance(hypnos);

  // Read the MQTT creds file to supply the device with MQTT credentials
  mqtt.loadConfigFromJSON(hypnos.readFile("mqtt_creds.json"));

  // Initialize all in-use modules
  manager.initialize();

  // Register the ISR and attach to the interrupt
  hypnos.registerInterrupt(isrTrigger);

  attachInterrupt(INT_PIN, tipTrigger, FALLING);
  hypnos.networkTimeUpdate();
}

void loop() {

  if(sampleFlag){
    // Set the RTC interrupt alarm to wake the device in 15 min, this should be done as soon as the device enters sampling mode for consistant sleep cycles
    hypnos.setInterruptDuration(TimeSpan(0, 0, 15, 0));

    // Measure and package the data
    manager.measure();
    manager.package();

    // Add the water height calculation to the data
    manager.addData("Water", "Height_(m)", calculateWaterHeight());

    // Print the current JSON packet
    manager.display_data();            

    // Log the data to the SD card              
    hypnos.logToSD();

    // Publish the collected data to MQTT
    mqtt.publish();

    // Reattach to the interrupt after we have set the alarm so we can have repeat triggers
    hypnos.reattachRTCInterrupt();
    attachInterrupt(INT_PIN, tipTrigger, FALLING);
    attachInterrupt(INT_PIN, tipTrigger, FALLING);
    sampleFlag = false;
  }

  if(tipFlag){
    digitalWrite(LED_BUILTIN, HIGH);
    delay(20);
    bucket.incrementCount();
    tipFlag = false;
    attachInterrupt(INT_PIN, tipTrigger, FALLING);
    attachInterrupt(INT_PIN, tipTrigger, FALLING);
    digitalWrite(LED_BUILTIN, LOW);
  }

  // Put the device into a deep sleep, operation HALTS here until the interrupt is triggered
  hypnos.sleep();
  hypnos.networkTimeUpdate();
}

Output

[2024.03.09 18:11:58] [DEBUG] [Loom_MS5803.cpp:initialize:22] Successfully Initialized!
[2024.03.09 18:12:01] [DEBUG] [Loom_MS5803.cpp:initialize:22] Successfully Initialized!
[2024.03.09 18:12:04] [DEBUG] [Loom_LTE.cpp:power_up:98] Powering up GPRS Modem. This should take about 10 seconds...
[2024.03.09 18:12:26] [DEBUG] [Loom_LTE.cpp:power_up:105] Powering up complete!
[2024.03.09 18:12:26] [DEBUG] [Loom_LTE.cpp:connect:154] Waiting for network...
[2024.03.09 18:12:28] [DEBUG] [Loom_LTE.cpp:connect:167] Connected to network!
[2024.03.09 18:12:28] [DEBUG] [Loom_LTE.cpp:connect:171] Attempting to connect to LTE Network: hologram
[2024.03.09 18:12:29] [DEBUG] [Loom_LTE.cpp:connect:173] Successfully Connected!
[2024.03.09 18:12:29] [DEBUG] [Loom_Hypnos.cpp:post_sleep:466] Device has awoken from sleep!
[2024.03.09 18:12:29] [DEBUG] [Loom_Hypnos.cpp:setInterruptDuration:388] Current Time: 2024.03.09 18:12:29
[2024.03.09 18:12:29] [DEBUG] [Loom_Hypnos.cpp:setInterruptDuration:391] Next Interrupt Alarm Set For: 2024.03.09 18:27:29
[2024.03.09 18:12:29] [DEBUG] [Loom_Hypnos.cpp:networkTimeUpdate:269] Attempting to set RTC time to the current network time...
[2024.03.09 19:12:30] [DEBUG] [Loom_Hypnos.cpp:networkTimeUpdate:275] Custom time successfully set to: 2024.03.09 19:12:30
[2024.03.09 19:12:30] [DEBUG] [Loom_Manager.cpp:measure:68] ** Measuring **
[2024.03.09 19:12:30] [ERROR] [Loom_MS5803.cpp:measure:47] No acknowledge received from the device
[2024.03.09 19:12:31] [DEBUG] [Loom_Manager.cpp:measure:85] ** Measuring Complete **
[2024.03.09 19:12:31] [DEBUG] [Loom_Manager.cpp:package:95] ** Packaging **
[2024.03.09 19:12:31] [DEBUG] [Loom_Manager.cpp:package:125] ** Packaging Complete **
[2024.03.09 19:12:31] [DEBUG] [Loom_Manager.cpp:display_data:198] Data Json: 

{
  "type": "data",
  "id": {
    "name": "KhutiChime",
    "instance": 8
  },
  "contents": [
    {
      "module": "Packet",
      "data": {
        "Number": 794
      }
    },
    {
      "module": "Analog",
      "data": {
        "Vbat": 3.930029392,
        "Vbat_MV": 3901.025391
      }
    },
    {
      "module": "SHT31",
      "data": {
        "Temperature": 10.27999973,
        "Humidity": 54.81999969
      }
    },
    {
      "module": "TSL2591",
      "data": {
        "Visible": 3,
        "Infrared": 1,
        "Full_Spectrum": 4
      }
    },
    {
      "module": "MS5803_119",
      "data": {
        "Temperature": 21.47999954,
        "Pressure": 4554.899902
      }
    },
    {
      "module": "MS5803_118",
      "data": {
        "Temperature": 10.26000023,
        "Pressure": 998.4699707
      }
    },
    {
      "module": "TippingBucket",
      "data": {
        "Tips": 201,
        "Total_Rainfall(in)": 2.00999999,
        "Hourly_Tips": 0,
        "Hourly_Rainfall(in)": 0
      }
    },
    {
      "module": "Teros10",
      "data": {
        "Millivolt_Reading": 2044.468872,
        "Dielectric_Permittivity": 34.11444473,
        "Volumetric_Water_Content": 0.416022032
      }
    },
    {
      "module": "LTE",
      "data": {
        "RSSI": 99
      }
    },
    {
      "module": "Water",
      "data": {
        "Height_(m)": 36.33413315
      }
    }
  ],
  "timestamp": {
    "time_utc": "2024-03-10T3:12:31Z",
    "time_local": "2024-03-09T19:12:31Z"
  }
}
[2024.03.09 19:12:32] [DEBUG] [SDManager.cpp:log:159] Successfully logged data to KhutiChime7.csv
[2024.03.09 19:12:32] [DEBUG] [MQTTComponent.cpp:connectToBroker:46] Attempting to connect to broker: cas-mosquitto.biossys.oregonstate.edu:1883
[2024.03.09 19:12:34] [DEBUG] [MQTTComponent.cpp:connectToBroker:58] Successfully connected to broker!
[2024.03.09 19:12:37] [DEBUG] [MQTTComponent.cpp:publishMessage:96] Data has been successfully sent!
[2024.03.09 19:12:37] [DEBUG] [Loom_Hypnos.cpp:reattachRTCInterrupt:161] Interrupt successfully reattached!
[2024.03.09 19:12:37] [WARNING] [Loom_Hypnos.cpp:sleep:424] Alarm triggered during sample, specified sample duration was too short! Resampling...
[2024.03.09 19:12:37] [WARNING] [Loom_Hypnos.cpp:sleep:424] Alarm triggered during sample, specified sample duration was too short! Resampling...
[2024.03.09 19:12:37] [WARNING] [Loom_Hypnos.cpp:sleep:424] Alarm triggered during sample, specified sample duration was too short! Resampling...
[2024.03.09 19:12:37] [WARNING] [Loom_Hypnos.cpp:sleep:424] Alarm triggered during sample, specified sample duration was too short! Resampling...
WL-Richards commented 3 months ago

This kind of situation should be resolved by using UTC time as the reference for everything as there would be no daylight savings