Boron 2G/3G sleepwake issue

Hello Guys,
I am using Boron 2G/3G particle for publish some data to cloud after every 3 minutes. After 3 minutes it wake up and send data to cloud then goes to sleep. I want to test this loop for 1000 times. It was going very well till 823. I don’t know what happen after that Boron wake up after 3 minutes looking for internet (Green light blink ) for 40-50 seconds then it goes back to sleep. it is not connected to internet and not post data after 823 cycle. By resetting, it shows the same behavior even after 1st cycle now.
I run the other codes, it is working fine and connecting to internet and posting data accurately both on particle and our dashboard.
For your reference, please find the code. Thanks in advance

// This #include statement was automatically added by the Particle IDE.
#include <PowerShield.h>

// -----------------------------------------------------------------
// Function and Variable with photo sensors (resistor or transistor)
// -----------------------------------------------------------------
// In this example, we're going to register a Particle.variable() with the cloud so that we can read brightness levels from the photoresistor or phototransistor.
// We'll also register a Particle.function so that we can turn the LED on and off remotely.
PowerShield batteryMonitor;
// We're going to start by declaring which pins everything is plugged into.

int led = D6; // This is where your LED is plugged in. The other side goes to a resistor connected to GND.

int photosensor = A0; // This is where your photoresistor or phototransistor is plugged in. The other side goes to the "power" pin (below).

int analogvalue; // Here we are declaring the integer variable analogvalue, which we will use later to store the value of the photoresistor or phototransistor.

int ledToggle(String command);
int x=0;// Forward declaration
int i=0;
int a=1;
// EXAMPLE
FuelGauge fuel;
// Next we go into the setup function.

void setup() {
	// This is here to allow for debugging using the USB serial port
	Serial.begin();
    // This essentially starts the I2C bus
    batteryMonitor.begin();
    // This sets up the fuel gauge
    batteryMonitor.quickStart();
   
    // Wait for it to settle down
    delay(500);
	// First, declare all of our pins. This lets our device know which ones will be used for outputting voltage, and which ones will read incoming voltage.
	pinMode(led, OUTPUT); // Our LED pin is output (lighting up the LED)
	digitalWrite(led, LOW);

	// We are going to declare a Particle.variable() here so that we can access the value of the photosensor from the cloud.
	Particle.variable("analogvalue", &analogvalue, INT);
	// This is saying that when we ask the cloud for "analogvalue", this will reference the variable analogvalue in this app, which is an integer variable.
//	Particle.publish("resistor", String(analogvalue), PRIVATE);
		
	// We are also going to declare a Particle.function so that we can turn the LED on and off from the cloud.
	Particle.function("Post per day",ledToggle);
	// This is saying that when we ask the cloud for the function "led", it will employ the function ledToggle() from this app.

}

// Next is the loop function...

void loop() {
    Particle.function("Post per day",ledToggle);
    delay (10000);
}

// Finally, we will write out our ledToggle function, which is referenced by the Particle.function() called "led"
int ledToggle(String command) {
    int x = atoi(command);
      Particle.function("Post per day",ledToggle);
    for ( i=0; i<x; i++)
    {   
      analogvalue = analogRead(photosensor);
      analogvalue=(analogvalue)+6400;
      delay (100);
      Particle.publish("resistor", String(analogvalue), PRIVATE);
      float cellVoltage = batteryMonitor.getVCell();
      Particle.publish("ps-voltage", String(cellVoltage), PRIVATE);
      Particle.publish("ps-voltage", String(i), PRIVATE);
      System.sleep(D6,RISING,86400/x);
      delay(100);
    }
  
    return 1;
}

Without having had a close look at your code I can say a few things already

  • Particle.function() is not a function that should be called repeatedly (not in loop() nor in the function callback) - I don’t understand the intent behind these calls anyhow
  • the docs also state that you should not use blanks/spaces in your function name
  • you should not send the device to sleep inside a Particle.function() callback
  • why would you have the sleep statement inside a for() loop inside the callback?
  • you may want to unify your three Particle.publish() calls into one single one
  • you may want to avoid using String()
  • you don’t want delay(10000) in your loop() when you want your device to stay responsive to Particle.function() calls
  • the syntax for Particle.variable() has been updated and should now be Particle.variable("analogvalue", analogvalue)
  • it’s OK to use single letter variable names as locals, but for global variables it’s better style to use more descriptive names - this also helps avoid mistakes like in your callback function where you don’t update the global variable x but only the local x which will vanish as soon your code leaves the function.

BTW

These are not forward declarations but fully functional definitions.

2 Likes

Thanks for your reply.
my intention is to wakeup the boron after every 3 minutes and post data and then go back to sleep for 1000 times. thats why i use the sleep function in for loop.

do you refer me that docs where we can use only one particle function

This is not a Particle specific topic and hence not documented in their docs.
It’s a common C/C++ question which can be tackled in multiple ways but may preferred way is via snprintf()

e.g.

  char msg[64];
  snprintf(msg, sizeof(msg), "i: %d, resistor: %d, cell: %.1f V", i, analogvalue, cellVoltage);
  Particle.publish("info", msg, PRIVATE);

(since cellVoltage seems undefined I assumed float for the datatype)

As said, you would not do that inside the Particle.function() callback but rather in loop() depending on the value you may set via Particle.function().
Your code does also not support the notion of a 3 minute sleep nor 1000 iterations. It rather suggests you a want to have your device wake x times in one day, where x is set via the Particle.function() call.