Electron Not updating variable after sleep

Hi,
I’ve been struggling with getting my new electron to update a “Particle.variable()”, first time trying this so I know I’m doing something wrong.

here’s the code:

// This #include statement was automatically added by the Particle IDE.
#include "SHT1x.h"

// Specify data and clock connections and instantiate SHT1x object
#define dataPin  D0
#define clockPin D1
SHT1x sht1x(dataPin, clockPin);
FuelGauge fuel;
//CellularData data;
String sensorData = "";
long sleepTime = 900; // 15 mins
retained float lastTempC;
retained float lastHumidity;

void setup()
{
   // Time setup for serial print
   Time.zone(10.00);
   Time.setFormat(TIME_FORMAT_ISO8601_FULL);
   
   //Serial.begin(115200); // Open serial connection to report values to host
   //Serial.println(Time.timeStr() + " - Starting up...");
   //Serial.printlnf(Time.timeStr() + " - System version: %s", System.version().c_str());
   
   
   if(Particle.variable("data", sensorData) == false)
   {
        //Serial.printlnf("Failed to register 'data' with Particle...");
   }
}

void loop()
{
    float _tempC;
    float _humidity;
    
    // Read cell data
    //Cellular.getDataUsage(data);
    
    // Read values from the sensor
    _tempC= sht1x.readTemperatureC();
    _humidity = sht1x.readHumidity();
    
    // Print the values to the serial port
    //Serial.printlnf("Temperature: %.4f°C, Humidity: %.4f%%, Battery: %.4fVDC, Charge: %.2f%%", _tempC, _humidity, fuel.getVCell(), fuel.getSoC());
    //Serial.printlnf("Reading Good: %d, CID: %d, Session Tx: %d, Rx: %d, Total Tx: %d, Rx: %d", data.ok, data.cid, data.tx_session, data.rx_session, data.tx_total, data.rx_total);
    
    if (abs(_tempC - lastTempC) > 0.2 || abs(_humidity - lastHumidity) > 0.5)
    {
        //Particle.publish("D", String::format("%.2f,%.2f,%.2f,%.2f",_tempC, _humidity, fuel.getVCell(), fuel.getSoC()), PRIVATE, NO_ACK );
        sensorData = String::format("%.2f,%.2f,%.2f,%.2f", _tempC, _humidity, fuel.getVCell(), fuel.getSoC());
        //Serial.printlnf(Time.timeStr() + " - " + sensorData);
        
        //Particle.variable("data", sensorData);
        lastTempC = _tempC;
        lastHumidity = _humidity;
    }
    
    //delay(60000);
    System.sleep(SLEEP_MODE_DEEP, sleepTime);
}

it seems to work if I use a delay but when I use sleep I see it connect but no updates.

any help would be appreciated.

In order to request the current value of a Particle.variable() the device needs to be awake to service the request.
Particle.variable()s are not pushed to the cloud but only exposed to be requested when needed and while the device sleeps it won’t “hear” the request.

Thanks ScruffR,

Maybe I need to rethink what I’m trying to do.

I’m trying to get data from from the electron to a database so I can trend and do stuff in the cloud, I also want the electron to be remote and solar powered so I think I’ll need to use deep sleep.

Do you have any suggestions on the best way of doing this kind of thing?

Thanks again

Using Particle.publish() and webhooks might be an option.
There are also some services that already have libraries that do the talking with their servers.
If you have your own server, you can just use TCPClient() to send the data there.

1 Like

Thanks ScruffR, I’ll have a read and a play when I get sometime.