Electron RGB LED turns off

electron
Tags: #<Tag:0x00007fe21bd49090>

#1

I’m having problems with 3 Electrons: they start up, make connection and connect to cloud. So the RGB LED is breathing cyan then. After a short period, the RGB LED is white and then it turns off. Pressing the RESET button, starts it up again.
I’m not using any sleep modes in my program. And when I flash the Tinker program, this behauviour isn’t happening.
Any ideas why my Electrons go down and what I have to check?


#2

Can we see your code?


#3

This is the main code with the setup and loop

#include "AQM.h"
#include "AD7798.h"
#include "eeprom.h"
#include "AM4100.h"
#include <Wire.h>

// Pin definitions for Electron
int PID_AH2 = A6;
int Temp_V1 = A7;
int setup_mode = B0;
int Temp_V2 = B2;
int Temp_V3 = B3;
int CS1 = C0;
int RS485_TE = C1;
int CS_OPC_N2 = C2;
int CS_Bias = C3;
int CS2 = D5;

// buffers to store sampled voltages for later handling ( average, report, ... )
extern float main_ain_valu[32];
extern float aux_ain_valu[32];

// state variable for the state machine
uint8_t cState;
// timer variables
unsigned long ulNowMilli, ulLastPub;
// AM41000 status byte
byte AM4100stat;
// structures to hold results
struct AM4100 am4100;
struct CONC_RESULT conc_result;
// string variables to print stored serialnumber and calibdate from eeprom
char mySerial[12];
char myDate[11];
// buffer for the menu input
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;

// Zero background current temperature compensation factors
//MULTIPLIED by 10!!!!
const int8_t tempcor[10][9] = {
    {-30, -20, -10, 0, 10, 20, 30, 40, 50},//temp
    {13, 13, 13, 13, 10, 6, 4, 2, -15},//NO2-B43F algoritm1
    {-6, -6, 1, 8, -7, -25, -25, -22, -18},//H2S-B4 algoritm1
    {9, 9, 10, 13, 15, 17, 20, 25, 37},//OX-B431 algoritm1
    {22, 22, 22, 22, 17, 10, 7, 3, -25},//NO2-B43F algoritm2
    {2, 2, 0, -3, 3, 10, 10, 9, 7},//H2S-B4 algoritm2
    {5, 5 , 6, 8, 9, 10, 12, 15, 22},//OX-B431 algoritm2
    {10, 10, 10, 10, 10, 10, 4, -1, -40},//NO2-B43F algoritm3
    {-127, -127, 30, 30, 20, 10, -12, -12, -12},//H2S-B4 algoritm3 (-127 should normally be -140: clipped to int8_t )
    {5, 5, 5, 6, 6, 10, 28, 50, 53}//OX-B431 algoritm3
};

// structure containing device data and calibration
extern SETTINGS_STRUCT store;

// mirror RGB led to external LED
STARTUP(RGB.mirrorTo(B1,D3,D2,true,true));
// mirror mode button to external pushbutton
STARTUP(System.buttonMirror(B0, FALLING));

void setup()
{
  // temporary float value
  float temporary_value;

  // register Particle function ( function used to remotely set the publish interval )
  Particle.function("Set_pub_int", setPublishInterval);

  // set pins in correct IO mode
  pinMode(PID_AH2, INPUT);
  pinMode(Temp_V1, INPUT);
  pinMode(Temp_V2, INPUT);
  pinMode(Temp_V3, INPUT);
  pinMode(setup_mode, INPUT);
  pinMode(RS485_TE, OUTPUT);
  pinMode(CS_OPC_N2, OUTPUT);
  digitalWrite(CS_OPC_N2,HIGH);
  pinMode(CS_Bias, OUTPUT);
    digitalWrite(CS_Bias,HIGH);   // CS start high -> not selected
  pinMode(CS2, OUTPUT);
    digitalWrite(CS2,HIGH);       // CS start high -> not selected
  pinMode(CS1, OUTPUT);
    digitalWrite(CS1,HIGH);       // CS start high -> not selected

  // initialise SPI port ( used for AD7798 ADCs )
  SPI_Init();
  delay(1000);

  // open serial port for communication to terminal
  Serial.begin(9600);
  // open seral port for communication to AM4100
  Serial1.begin(9600);

  // initialise I²C port ( used for eeprom )
  Wire.begin();

  // char pointer to calibration structure in eeprom
  char *data_ptr;
  data_ptr = (char*)&store;

  // read calibration from external eeprom to structure
  readEEPROM(eeprom1, 0, data_ptr, sizeof(SETTINGS_STRUCT));

  // print info and calibration settings in terminal
  Serial.println("****************************************************");
  Serial.println("*                Air Quality Monitoring                    *");
  Serial.println("*        ------------------------------------      *");
  Serial.println("*      AQM3_e V3 (c)2019  DVDB-electronics bvba    *");
  Serial.println("****************************************************");

  strncpy(mySerial,store.ucSerial,11);
  mySerial[11] = '\0';
  Serial.printf("Serialn°:\t\t");
  Serial.println(mySerial);

  strncpy(myDate,store.ucDate,10);
  myDate[10] = '\0';
  Serial.printf("Calib date:\t\t");
  Serial.println(myDate);

  Serial.printf("Data publish interval:\t%d sec.\n",store.DataInter);
  Serial.printf("AVDD:\t\t\t%dmV\n",store.AVDD);

  Serial.printf("\tmV\t\tNO2-B43F\tH2S-B4\t\tOX-B431\n");
  Serial.printf("──────────────────────────────────────────────────────────────────────────────────\n");
  Serial.printf("WE elec offset\t\t%.3f\t\t%.3f\t\t%.3f\n",store.NO2_B43F_WEe, store.H2S_B4_WEe, store.OX_B431_WEe);
  Serial.printf("WE total zero\t\t%.3f\t\t%.3f\t\t%.3f\n",store.NO2_B43F_WET, store.H2S_B4_WET, store.OX_B431_WET);
  Serial.printf("AE elec offset\t\t%.3f\t\t%.3f\t\t%.3f\n",store.NO2_B43F_AEe, store.H2S_B4_AEe, store.OX_B431_AEe);
  Serial.printf("AE total zero\t\t%.3f\t\t%.3f\t\t%.3f\n",store.NO2_B43F_AET, store.H2S_B4_AET, store.OX_B431_AET);

  Serial.printf("WE sens (nA/10ppm)\t%d\t\t%d\t\tO3: %d\tNO2: %d\n",store.NO2_B43F_sens, store.H2S_B4_sens, store.OX_B431_O3_sens, store.OX_B431_NO2_sens);
  Serial.printf("PCB gain (µV/µA)\t%d\t\t%d\t\t%d\n\n",store.NO2_B43F_gain, store.H2S_B4_gain, store.OX_B431_gain);

  // Reset AD converters
  AD7798_Reset(AD7798_unbiased1);
  AD7798_Reset(AD7798_unbiased2);
  AD7798_Reset(AD7798_biased); // Not installed in this version
  delay(500);

  // initialise and setup AD converters
  for (int i = 1; i < 3; i++)
  {
    if(AD7798_Init(i) == 48); // do this only if ADC is present
    {
      AD7798_SetReference(i,AD7798_REFDET_ENA);
      AD7798_calibrate(i);
    }
  }

  // Check AVDD on ADC and measure sensor temperatures
  AD7798_read_signal(AVDD,8); // average over 8 samples
  temporary_value = signalStatistics(main_ain_valu,8,rep_important);
  Serial.printf("AVDD @ADC1: %.3fmV\n",temporary_value);
  temporary_value = signalStatistics(aux_ain_valu,8,rep_important);
  Serial.printf("AVDD @ADC2: %.3fmV\n",temporary_value);


  Serial.printf("\nTemp_V1 = %+.1f°C\t",sensor_temperature(RT1));
  Serial.printf("Temp_V2 = %+.1f°C\n",sensor_temperature(RT2));
  Serial.printf("Temp_V3 = %+.1f°C\n",sensor_temperature(RT3));

  // open AM4100 particle measurement
  AM4100stat = open_close_particle_measurement(AM4100_Open);
  //Serial.print("AM4100 measurement ");
  if(AM4100stat == 2)
    Serial.println("opened");
  else if (AM4100stat == 1)
    Serial.println("closed");
  else
    Serial.println("AM4100 not detected!");
  Serial.println("");

  Serial.print("Enter service code to enter calibration menu");
  for(uint8_t t = 0; t<3; t++)
  {
    delay(1000);
    Serial.print(".");
  }
  delay(1000);
  Serial.println();

  // To enter setup menu, type in the password "236958"
  if(readSerialInput(6) == "236958")
    cState = setup_menu;
  else
    cState = AM4100_measure;
}

void loop()
{
  // start state machine
  cState_machine_run();
}

void cState_machine_run()
{
  float correction_factor;
  float WE_corrected;
  float temporary_value;
  uint8_t compensation_algorithm;

  switch(cState)
  {
    case setup_menu:
      {
        Menu();
        cState = AM4100_measure;
      }
    case AM4100_measure:
      {
        am4100 = read_particle_measuring_results();
        Serial.print("*** AM4100 -> ");
        Serial.printf("CO2 = %dppm | ",am4100.uiCO2);
        Serial.printf("Temp. = %.1f°C | ",am4100.fTemperature/10);
        Serial.printf("RH = %.1f% | ", am4100.fRH/10);
        Serial.printf("VOC = %.2fppm | ",am4100.fVOC/100);
        Serial.printf("PM2.5 = %dµg/m³\n", am4100.iPM2_5);
        cState = NO2_B43F_measure;
        break;
      }
    case NO2_B43F_measure:
      {
        compensation_algorithm = 1;
        correction_factor = lookupCompensationFactor(NO2_B43F, compensation_algorithm, sensor_temperature(RT1))/10;
        // Serial.printf("corr = %f\n",correction_factor);
        // take 16 samples of the voltage for both the working and auxilary electrode
        AD7798_read_signal(NO2_B43F,16);
        // average the measured value on the working electrode
        temporary_value = signalStatistics(main_ain_valu,16,rep_nothing);
        //Serial.printf("NO2-B43F W-> %.3fmV\t",temporary_value);
        // calculate WEu
        WE_corrected = temporary_value - store.NO2_B43F_WET;
        // average the measured value on the auxilary electrode
        temporary_value = signalStatistics(aux_ain_valu,16,rep_nothing);
        //Serial.printf("NO2-B43F A-> %.3fmV\t",temporary_value);
        if(compensation_algorithm == 1)
            WE_corrected = WE_corrected - (correction_factor * (temporary_value - store.NO2_B43F_AET));
        else if(compensation_algorithm == 3)
            WE_corrected = WE_corrected - ((store.NO2_B43F_WET - store.NO2_B43F_WEe) - (store.NO2_B43F_AET - store.NO2_B43F_AEe)) - (correction_factor * (temporary_value - store.NO2_B43F_AET));
        //Serial.printf("Compensated value->\t%.3fmV\n",WE_corrected);
        // calculate the concentration
        conc_result.NO2 = 1000 * 1000 * 1000 * WE_corrected / (store.NO2_B43F_sens/10 * store.NO2_B43F_gain);
        Serial.printf("NO2 conc. = \t%.0fppb\n", conc_result.NO2);
        cState = H2S_B4_measure;
        break;
        //no temperature probe near this sensor :(
      }
    case H2S_B4_measure:
      {
        compensation_algorithm = 1;
        correction_factor = lookupCompensationFactor(H2S_B4, compensation_algorithm, sensor_temperature(RT2))/10;
        // Serial.printf("corr = %f\n",correction_factor);
        // take 16 samples of the voltage for both the working and auxilary electrode
        AD7798_read_signal(H2S_B4,16);
        // average the measured value on the working electrode
        temporary_value = signalStatistics(main_ain_valu,16,rep_nothing);
        //Serial.printf("H2S-B4 W-> %.3fmV\t",temporary_value);
        // calculate WEu
        WE_corrected = temporary_value - store.H2S_B4_WET;
        // average the measured value on the auxilary electrode
        temporary_value = signalStatistics(aux_ain_valu,16,rep_nothing);
        //Serial.printf("H2S-B4 A-> %.3fmV\t",temporary_value);
        if(compensation_algorithm == 1)
            WE_corrected = WE_corrected - (correction_factor * (temporary_value - store.H2S_B4_AET));
        else if(compensation_algorithm == 2)
            WE_corrected = WE_corrected - (correction_factor * ((store.H2S_B4_WET - store.H2S_B4_WEe) / (store.H2S_B4_AET - store.H2S_B4_AEe)) * (temporary_value - store.H2S_B4_AET));
        //Serial.printf("Compensated value->\t%.3fmV\n",WE_corrected);
        // calculate the concentration
        conc_result.H2S = 1000 * 1000 * 1000 * WE_corrected / (store.H2S_B4_sens/10 * store.H2S_B4_gain);
        Serial.printf("H2S conc. = \t\t%.0fppb\n", conc_result.H2S);
        cState = OX_B431_measure;
        break;
      }
    case OX_B431_measure:
      {
        compensation_algorithm = 1;
        correction_factor = lookupCompensationFactor(OX_B431, compensation_algorithm, sensor_temperature(RT3))/10;
        // Serial.printf("corr = %f\n",correction_factor);
        // take 16 samples of the voltage for both the working and auxilary electrode
        AD7798_read_signal(OX_B431,16);
        // average the measured value on the working electrode
        temporary_value = signalStatistics(main_ain_valu,16,rep_nothing);
        //Serial.printf("OX-B431 W-> %.3fmV\t",temporary_value);
        WE_corrected = temporary_value - store.OX_B431_WET;
        // average the measured value on the auxilary electrode
        temporary_value = signalStatistics(aux_ain_valu,16,rep_nothing);
        //Serial.printf("OX-B431 A-> %.3fmV\t",temporary_value);
        if(compensation_algorithm == 1)
            WE_corrected = WE_corrected - (correction_factor * (temporary_value - store.OX_B431_AET));
        else if(compensation_algorithm == 3)
            WE_corrected = WE_corrected - ((store.OX_B431_WET - store.OX_B431_WEe) - (store.OX_B431_AET - store.OX_B431_AEe)) - (correction_factor * (temporary_value - store.OX_B431_AET));
        //Serial.printf("Compensated value->\t%.3fmV\n",WE_corrected);
        // calculate the concentration
        conc_result.O3 = (1000 * 1000 * 1000 * WE_corrected - conc_result.NO2 * store.OX_B431_NO2_sens/10 * store.OX_B431_gain) / (store.OX_B431_O3_sens/10 * store.OX_B431_gain);
        Serial.printf("O3 conc. = \t\t\t%.0fppb\n", conc_result.O3);
        Serial.println("");
        cState = measurement_delay;
        break;
      }
    case publish_data:
      {
        Serial.println("### Publishing AM4100 + ECS data ###");
        Serial.println("####################################");
        String publishmessage = "{ \"CO2\" : \"" + String(am4100.uiCO2, DEC) + "\"";
        // Change in V2: subtract 4°C from measured temperature by AM4100 -> this should be aprox. the outside temperature
        publishmessage += ", \"Temp\" : \"" + String(am4100.fTemperature/10 - 4, 1) + "\"";
        publishmessage += ", \"RH\" : \"" + String(am4100.fRH/10, 1) + "\"";
        publishmessage += ", \"VOC\" : \"" + String(am4100.fVOC/100, 2) + "\"";
        publishmessage += ", \"PM2_5\" : \"" + String(am4100.iPM2_5, DEC) + "\"";
        publishmessage += ", \"NO2\" : \"" + String(conc_result.NO2, 0) + "\"";
        publishmessage += ", \"H2S\" : \"" + String(conc_result.H2S, 0) + "\"";
        publishmessage += ", \"O3\" : \"" + String(conc_result.O3, 0) + "\"}";
        if(store.DataInter != 0)
            Particle.publish("AQM03_data", publishmessage, PRIVATE);
        ulLastPub = ulNowMilli;
        cState = measurement_delay;
        break;
      }
    case measurement_delay:
      {
        ulNowMilli = millis();
        if((ulNowMilli - ulLastPub) >= (store.DataInter * 1000))
        {
          cState = publish_data;
        }
        else
          cState = AM4100_measure;
        break;
      }
    default:
        break;
  }
}

void Menu()
{
  Serial.println("*************************************************************************************************");
  Serial.println("**                                  AQM Configuration Menu                                     **");
  Serial.println("*************************************************************************************************");
  Serial.println("");
  Serial.println("# Select one of the following options:");
  Serial.println("\t1 --> Set SN");
  Serial.println("\t2 --> Set publish interval");
  Serial.println("\t3 --> Set AVDD (mV)");
  Serial.println("\t4 --> Measure elec. offsets - Ee (mV)");
  Serial.println("\t5 --> Measure tot. offsets - Et (mV)");
  Serial.println("\t6 --> NO2-B43F sensitivity (nA/10ppm)");
  Serial.println("\t7 --> H2S-B4 sensitivity (nA/10ppm)");
  Serial.println("\t8 --> OX-B431 O3 sens (nA/10ppm)");
  Serial.println("\t9 --> OX-B431 NO2 sens (nA/10ppm)");
  Serial.println("\tA --> NO2-B43F gain (µV/µA)");
  Serial.println("\tB --> H2S-B4 gain (µV/µA)");
  Serial.println("\tC --> OX-B431 gain (µV/µA)");
  Serial.println("\tD --> Set calib date ( xx/xx/xxxx )");
  Serial.println("\tX --> Exit menu ( + save )");
  Serial.println("");
  Serial.println("");

  while(1)
  {
    switch(Serial.read())
    {
      case '1': while(Serial.read() >= 0);
                Serial.print("Input serial number : ");
                while(1)
                {
                  recvWithEndMarker();
                  if (newData == true)
                  {
                    memcpy(store.ucSerial, receivedChars, strlen(receivedChars));
                    strncpy(mySerial,store.ucSerial,11);
                    mySerial[11] = '\0';
                    Serial.println(mySerial);
                    newData = false;
                    break;
                  }
                }
                break;
      case '2': while(Serial.read() >= 0);
                Serial.print("Input publish interval (min. 5sec., max. 32767sec. / 0 to not publish) : ");
                while(1)
                {
                  recvWithEndMarker();
                  if (newData == true)
                  {
                    setPublishInterval(receivedChars);
                    Serial.println(receivedChars);
                    newData = false;
                    break;
                  }
                }
                break;
      case '3': while(Serial.read() >= 0);
                Serial.print("Input measured AVDD (mV) : ");
                while(1)
                {
                  recvWithEndMarker();
                  if (newData == true)
                  {
                    store.AVDD = (int16_t)atoi(receivedChars);
                    Serial.println((int16_t)atoi(receivedChars));
                    newData = false;
                    break;
                  }
                }
                break;
      case '4': Serial.print("Measuring electronic zeros and average over 32 samples: BUSY...-> ");
                AD7798_read_signal(NO2_B43F,32);
                store.NO2_B43F_WEe = signalStatistics(main_ain_valu, 32, rep_important);
                store.NO2_B43F_AEe = signalStatistics(aux_ain_valu, 32, rep_important);

                AD7798_read_signal(H2S_B4,32);
                store.H2S_B4_WEe = signalStatistics(main_ain_valu, 32, rep_important);
                store.H2S_B4_AEe = signalStatistics(aux_ain_valu, 32, rep_important);

                AD7798_read_signal(OX_B431,32);
                store.OX_B431_WEe = signalStatistics(main_ain_valu, 32, rep_important);
                store.OX_B431_AEe = signalStatistics(aux_ain_valu, 32, rep_important);

                Serial.println("FINISHED!!!");
                break;
      case '5': Serial.print("Measure sensor zeros and average over 32 samples: BUSY...-> ");

                AD7798_read_signal(NO2_B43F,32);
                store.NO2_B43F_WET = signalStatistics(main_ain_valu, 32, rep_important);
                store.NO2_B43F_AET = signalStatistics(aux_ain_valu, 32, rep_important);

                AD7798_read_signal(H2S_B4,32);
                store.H2S_B4_WET = signalStatistics(main_ain_valu, 32, rep_important);
                store.H2S_B4_AET = signalStatistics(aux_ain_valu, 32, rep_important);

                AD7798_read_signal(OX_B431,32);
                store.OX_B431_WET = signalStatistics(main_ain_valu, 32, rep_important);
                store.OX_B431_AET = signalStatistics(aux_ain_valu, 32, rep_important);

                Serial.println("FINISHED!!!");
                break;
      case '6': while(Serial.read() >= 0);
                Serial.print("Input NO-B43F sensitivity (nA/10ppm) : ");
                while(1)
                {
                  recvWithEndMarker();
                  if (newData == true)
                  {
                    store.NO2_B43F_sens = (int16_t)atoi(receivedChars);
                    Serial.println((int16_t)atoi(receivedChars));
                    newData = false;
                    break;
                  }
                }
                break;
      case '7': while(Serial.read() >= 0);
                Serial.print("Input H2S-B4 sensitivity (nA/10ppm) : ");
                while(1)
                {
                  recvWithEndMarker();
                  if (newData == true)
                  {
                    store.H2S_B4_sens = (int16_t)atoi(receivedChars);
                    Serial.println((int16_t)atoi(receivedChars));
                    newData = false;
                    break;
                  }
                }
                break;
      case '8': while(Serial.read() >= 0);
                Serial.print("Input OX-B431 O3 sens (nA/10ppm) : ");
                while(1)
                {
                  recvWithEndMarker();
                  if (newData == true)
                  {
                    store.OX_B431_O3_sens = (int16_t)atoi(receivedChars);
                    Serial.println((int16_t)atoi(receivedChars));
                    newData = false;
                    break;
                  }
                }
                break;
      case '9': while(Serial.read() >= 0);
                Serial.print("Input OX-B431 NO2 sens (nA/10ppm) : ");
                while(1)
                {
                  recvWithEndMarker();
                  if (newData == true)
                  {
                    store.OX_B431_NO2_sens = (int16_t)atoi(receivedChars);
                    Serial.println((int16_t)atoi(receivedChars));
                    newData = false;
                    break;
                  }
                }
                break;
      case 'A': while(Serial.read() >= 0);
                Serial.print("Input NO-B43F gain (µV/µA) : ");
                while(1)
                {
                  recvWithEndMarker();
                  if (newData == true)
                  {
                    store.NO2_B43F_gain = atoi(receivedChars);
                    Serial.println(atoi(receivedChars));
                    newData = false;
                    break;
                  }
                }
                break;
      case 'B': while(Serial.read() >= 0);
                Serial.print("Input H2S_B4 gain (µV/µA) : ");
                while(1)
                {
                  recvWithEndMarker();
                  if (newData == true)
                  {
                    store.H2S_B4_gain = atoi(receivedChars);
                    Serial.println(atoi(receivedChars));
                    newData = false;
                    break;
                  }
                }
                break;
      case 'C': while(Serial.read() >= 0);
                Serial.print("Input OX_B431 gain (µV/µA) : ");
                while(1)
                {
                  recvWithEndMarker();
                  if (newData == true)
                  {
                    store.OX_B431_gain = atoi(receivedChars);
                    Serial.println(atoi(receivedChars));
                    newData = false;
                    break;
                  }
                }
                break;
     case 'D':  while(Serial.read() >= 0);
                Serial.print("Input calibration date ( xx/xx/xxxx ) : ");
                while(1)
                {
                  recvWithEndMarker();
                  if (newData == true)
                  {
                    memcpy(store.ucDate, receivedChars, strlen(receivedChars));
                    strncpy(myDate,store.ucDate,10);
                    myDate[10] = '\0';
                    Serial.println(myDate);
                    newData = false;
                    break;
                  }
                }
                break;
      case 'X': {
                  // byte pointer to calib struct
                  char *data_ptr;
                  data_ptr = (char*)&store;
              		writeEEPROM(eeprom1,0,data_ptr,sizeof(SETTINGS_STRUCT));
                  Serial.println("Saving and exiting menu...");
                  return;
                }
      default:
                continue;
    }
  }
}

String readSerialInput(byte bNumberOfbytes)
{
int i = 0;
String result = "";
byte a = 0xFF;

  while(Serial.available() and i<bNumberOfbytes and a!=0x0D)
  {
    a = Serial.read();
    result += String((char)a);
    i++;
  }

return result;
}

void recvWithEndMarker()
{
 static byte ndx = 0;
 char endMarker = '\n';
 char rc;

 while (Serial.available() > 0 && newData == false)
 {
   rc = Serial.read();

   if (rc != endMarker)
   {
     receivedChars[ndx] = rc;
     ndx++;
     if (ndx >= numChars)
     {
       ndx = numChars - 1;
     }
   }
   else
   {
     receivedChars[ndx] = '\0'; // terminate the string
     ndx = 0;
     newData = true;
   }
 }
}

bool setPublishInterval(String int_text)
{
  int value;
  value = atoi(int_text);
  Serial.print("-> Data publish interval is now ");
  if(value>=5 or value==0)
    {
      store.DataInter = value;
      return 1;
    }
  else Serial.println("invalid!");
  return -1;
}

float lookupCompensationFactor(uint8_t sensor, uint8_t algorithm, float temp)
{
  uint8_t upper_index = 0;
  uint8_t column = sizeof(tempcor[0]);
  uint8_t row = sizeof(tempcor)/column;
  float compensation;

  algorithm = algorithm - 1;
  while(upper_index<column)
    {
      if(tempcor[0][upper_index] < temp)
        upper_index++;
      else
        break;
    }
  if(temp <= tempcor[0][0])
      compensation = tempcor[sensor + 3*algorithm][0];
  else if(temp >= tempcor[0][column-1])
      compensation = tempcor[sensor + 3*algorithm][column-1];
  else {
    compensation = tempcor[sensor + 3*algorithm][upper_index] - (tempcor[0][upper_index]-temp) * (tempcor[sensor + 3*algorithm][upper_index] - tempcor[sensor + 3*algorithm][upper_index-1]) / (tempcor[0][upper_index] - tempcor[0][upper_index-1]);
  }
  return compensation;
}

float signalStatistics(float *ain_values, uint8_t samples, uint8_t report_level)
{
  //uint8_t diff_values = 0;
  uint8_t n;
  float ain_average;
  float ain_min;
  float ain_max;

  ain_average = 0.0F;
  ain_min = 9999999.0F;
  ain_max = 0.0F;

  for(n=0; n<samples; n++)
    {
      if(report_level>rep_important) Serial.printf("\t\t%.3f\n",*ain_values);
      if(*ain_values > ain_max) ain_max = *ain_values;
      if(*ain_values < ain_min) ain_min = *ain_values;
      ain_average = ain_average + *ain_values;
      ain_values++;
    }
  ain_average = ain_average / samples;
  if(report_level>rep_nothing) Serial.printf("\tavg=%.3fmV\tmin=%.3fmV\tmax=%.3fmV\tdelta=%.3fmV\n", ain_average, ain_min, ain_max, ain_max-ain_min);
  return ain_average;
}

// measure temperature at sensors
float sensor_temperature(uint8_t sensor)
{
  int sensorValue;
  float temperature = 0;

  // take 1024 samples
  for(int i = 0; i<1024; i++)
  {
    if(sensor==RT1)
      sensorValue = analogRead(Temp_V1);
    if(sensor==RT2)
      sensorValue = analogRead(Temp_V2);
    if(sensor==RT3)
      sensorValue = analogRead(Temp_V3);

    // calculate temperature from AD value
    temperature = temperature + ( (sensorValue * 3300 /4095 - 500)/10 );
  }
  // average over 1024 samples
  return temperature/1024;
}

#4

I can’t see an obvious reason for that behaviour, but some notes.

What device OS version are you targeting?
Wire.h is not needed.
About your multiple STARTUP() macros the docs would state this
https://docs.particle.io/reference/device-os/firmware/electron/#startup-

You have multiple while(1) loops with no obvious timeout in case your newData doesn’t become true in time. Try SYSTEM_THREAD(ENABLED) and/or placing some Particle.process() calls in these loops.

And - as always - I’d advise against the use of String for the potential risk of heap fragmentation causing unexpeted behaviour.

How long is a short period?

Can you wake the device via pulling WKP high (this would suggest a deep sleep).
There is a “magic button” which would power down the Electron by double clicking the MODE button - could this be triggered by your external button on B0?


#5

device OS version I’m using is 1.0.0

OK, I will remove wire.h and put the things that should be executed during startup in 1 function;
What is prefered: using the SYSTEM_THREAD(ENABLED) or adding some Particle.process() in the loops?

What is the alternative for using String ?

The RGB LED goes white and turns off, sometimes already during setup() and sometimes after a about 15 seconds in loop():
I will also check if something is happening on track to the external button.

I forgot to mention that I have 7 pcs with the same hardware and firmware that run without any problems. Could it be a problem with the SIM cards?

If I pull WKP high, the Electron starts up again. First blinking white shortly, then green, cyan for some time and then blinking blue


#6

A similar thing you used elsewhere in your code… snprintf()!


#7

The Electrons I use are 2G versions. I did a test with a 3G version ( also V1.0.1, same code, same PCB ) and that doesn’t show the problem. So it looks that reception on 2G is poor.
Can it happen it that case that the Electron decides to go into sleep? How can I prevent this, and make sure that it keeps trying to get connected?