Flash/Status Failed

After flashing this code, it doesn’t allow me to flash the code again. Moreover, it does not allow me to communicate with boron using particle cli. On the contrary, it works fine when I flash Blink Led code. I can upload code again without issue and send commands via particle cli.

Usually, the code I shared got stuck at “sensor not found”. This means it stops at while(1) in void setup.

It seems to me that when the code is stuck at while(1), it engages the serial and does not allow me to perform further actions.

Additionally, when I meet this situation, I press the combination of mode and reset buttons to go into listening mode and then upload the code.

Kindly help me out.
Thanks.


//////////////////////////////////////////  Main Code /////////////////////////////////////////////////////////////////////////////////
//#include <Particle.h>
#include <Ubidots.h>
#include <Adafruit_VL6180X.h>
#include <Wire.h>
#include <movingAvg.h>   
#define WINDOW_SIZE 10

int samples=30;
int cumulative_dist=0;
float average_dist=0;
int raw;
int ActivatedTimes;
int buttonState = 1; //reads pushbutton status
bool sensorstatus;
uint32_t msDelay = 0;


////UBIDOTS VARIBLES////////////////////////////////////////////////////////////////////////////////////////////////////////

#ifndef TOKEN
#define TOKEN ".........................." // 
#endif

Ubidots ubidots(TOKEN, UBI_TCP); // Comment this line to use another protocol.
//Ubidots ubidots(TOKEN, UBI_HTTP); // Uncomment this line to use HTTP protocol.
//Ubidots ubidots(TOKEN, UBI_UDP); // Uncomment this line to use UDP protocol

///////sensor Varibles//////////////////////////////////////////////////////////////////////////////////////////////////////////


Adafruit_VL6180X vl = Adafruit_VL6180X();

int range;
int xx;
int pump_pin= D6;                 
int valveLed = D7;
int valve_status=0;
float value2;
int FloatSensor = D5;
int Count = 0;

//Serial output refresh time
const long SERIAL_REFRESH_TIME = 10;
long refresh_time;
bool bufferSent;

/////////////////////////////////////////////////////////Watchdog//////////////////////////////////////////////////////////////////////////

 ApplicationWatchdog *wd;

void watchdogHandler() 
{
  System.reset(RESET_NO_WAIT);
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void setup() 
{
    Serial.begin(115200);
    Wire.begin(); 
  
    wd = new ApplicationWatchdog(600000, watchdogHandler, 1536); 
    pinMode(pump_pin, OUTPUT);
    pinMode(valveLed, OUTPUT);
    pinMode(FloatSensor, INPUT_PULLUP);
    
    // wait for serial port to open on native usb devices
    while (!Serial) 
    {
        Delay(1);
    }
  
    Serial.println("Adafruit VL6180x test!");
    if (! vl.begin()) 
    {
        Serial.println("Failed to find sensor");
        while (1);
    }
    Serial.println("Sensor found!");
}

/////////////////////////////////////////////////////////////////////////////////////////////////

void loop() 
{
    
  ////////////// LEVEL////////////////////////////////////
    buttonState = digitalRead(FloatSensor);
    if (buttonState == HIGH) 
    {
        Serial.println("WATER LEVEL - HIGH");
    }
    else 
    {
        Serial.println("WATER LEVEL - LOW");
    }
    Delay(1000);

  
  ////////////////////////////////////////
  
    Sensorstatus();
 
    if(sensorstatus==1)
    {
        DAQ();
    }
    else
    {
        //do nothing
    }
    ubidotsData();
    Delay(20000);
  
 
    if ((xx <80) && (Count<=5))   //   xx= how far the object is in mm  
    {
    
        digitalWrite(pump_pin,HIGH);
        digitalWrite(D7,HIGH);
  
        Delay(5000);
        digitalWrite(pump_pin,LOW);
        digitalWrite(D7,LOW);
        
        Delay(20000);
        valve_status = 1;
        
        Count ++;
        ActivatedTimes= ActivatedTimes+1;
    }

    else if((Count>5 )||(xx>=80) &&(bufferSent==true))           
    {   
        digitalWrite(pump_pin,LOW);
        digitalWrite(D7,LOW);
        valve_status = 0;
        Count=0;
    
        SystemSleepConfiguration config;
        config.mode(SystemSleepMode::STOP)
        .gpio(WKP, RISING)
        .duration(20min);
        System.sleep(config);
    }  

    else
    {
        //Do nothing
    }

}

  

/////////////////Sensor status/////

void Sensorstatus()
{
    int nDevices = 0;

    Serial.println("Scanning...");

    for (byte address = 1; address < 127; ++address) 
    {
    // The i2c_scanner uses the return value of
    // the Wire.endTransmission to see if
    // a device did acknowledge to the address.
        Wire.beginTransmission(address);
        byte error = Wire.endTransmission();

        if (error == 0) 
        {
            Serial.print("I2C device found at address 0x");
            if (address < 16) 
            {
                Serial.print("0");
            }
            Serial.print(address, HEX);
            Serial.println("  !");
            ++nDevices;
        } 
        else if (error == 4) 
        {
            Serial.print("Unknown error at address 0x");
            if (address < 16) 
            {
                Serial.print("0");
            }
            Serial.println(address, HEX);
        }
    }
    if (nDevices == 0) 
    {
        Serial.println("No I2C devices found\n");
        sensorstatus=0;
    } 
    else 
    {
        Serial.println("done\n");
        sensorstatus=1;
    }
    Delay(5000); // Wait 5 seconds for next scan
}


///////////////////////////data acquisation///////////////


void DAQ()
{
  ///////////////////////////////////////Debug the error
  uint8_t status = vl.readRangeStatus();
  
  if  ((status >= VL6180X_ERROR_SYSERR_1) && (status <= VL6180X_ERROR_SYSERR_5)) {
    Serial.println("System error");
  }
  else if (status == VL6180X_ERROR_ECEFAIL) {
    Serial.println("ECE failure");
  }
  else if (status == VL6180X_ERROR_NOCONVERGE) {
    Serial.println("No convergence");
  }
  else if (status == VL6180X_ERROR_RANGEIGNORE) {
    Serial.println("Ignoring range");
  }
  else if (status == VL6180X_ERROR_SNR) {
    Serial.println("Signal/Noise error");
  }
  else if (status == VL6180X_ERROR_RAWUFLOW) {
    Serial.println("Raw reading underflow");
  }
  else if (status == VL6180X_ERROR_RAWOFLOW) {
    Serial.println("Raw reading overflow");
  }
  else if (status == VL6180X_ERROR_RANGEUFLOW) {
    Serial.println("Range reading underflow");
  }
  else if (status == VL6180X_ERROR_RANGEOFLOW) {
    Serial.println("Range reading overflow");
  }
  
  /////////////////////////////////

  float lux = vl.readLux(VL6180X_ALS_GAIN_5);
  Serial.print("Lux: "); Serial.println(lux);
  
  cumulative_dist=0;

    for (int i = 0; i < samples; i++) 
    {
        uint8_t range = vl.readRange();
        cumulative_dist += range;
        Delay(10);
    }

    average_dist = cumulative_dist / samples;   // average the n values
    xx=int(average_dist);
  
  
    if (status == VL6180X_ERROR_NONE) 
    {
        Serial.print("Range: "); Serial.println(range);
    }

}


/////////////loop for ubidots//////////////////////////////////////////////////////////////////////////////////////

void ubidotsData()

{
   
    FuelGauge fuel;
    value2 = fuel.getVCell();
    
    
    ubidots.add("distance mm", xx);
    ubidots.add("count",  ActivatedTimes); 
    ubidots.add("Battery%", fuel.getSoC());
    ubidots.add("BatteryV", value2);
    ubidots.add("ON/OFF",  valve_status);
    ubidots.add("Level",  buttonState);
    ubidots.add("SensorStatus",sensorstatus);
      
   bufferSent = false;
   bufferSent = ubidots.send(); // Will send data to a device label that matches the device Id

    Delay(100);

    if(bufferSent)

    {
      // Do something if values were sent properly
        Serial.println("Values sent by the device");
    }
    else
    {
        Serial.println("Error Sending Values,retry!!!");
    }
}


void Delay(int del)
{
  if (millis() - msDelay < del) return;
  msDelay = millis();
}

This behaviour is by design.

When your code is stuck in while(1); it won’t do anything but spin in that loop.
In order to accept OTA updates or respond to cloud commands the cloud task needs to be executed.
This happens

Hence while(1); is a big no-no!
while(1) delay(1); would be less bad and while(1) Particle.process(); even better.

You can also use Safe Mode to prevent your code from running to allow for OTA updates again.

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.