Particle Cloud publish and variable

Greetings,

I just got my photons and i was trying to use some of the examples from the IDE but it seems that i cannot make them to work.

To be short,

I wanted to read the battery status and voltage from the power shield and to put it on the cloud but no luck.

In the sample i have :

#include "PowerShield/PowerShield.h"

PowerShield batteryMonitor;

void setup() {
  
}

void loop() {    
    Wire.begin(); 
    batteryMonitor.reset();
    batteryMonitor.quickStart();
    delay(1000);
    float cellVoltage = batteryMonitor.getVCell();
    float stateOfCharge = batteryMonitor.getSoC();
    Spark.publish("voltage", String(cellVoltage), 60, PRIVATE);
    delay(100);
    Spark.publish("soc", String(stateOfCharge), 60, PRIVATE);
    delay(100);
    //System.sleep(SLEEP_MODE_DEEP, 600);
}

I tried to change Spark with Particle but still no result.

If i use this code :


#include "PowerShield/PowerShield.h"

PowerShield batteryMonitor;

void setup() {    
    Serial.begin(9600); 
    // This essentially starts the I2C bus
    batteryMonitor.begin(); 
    // This sets up the fuel gauge
    batteryMonitor.quickStart();
    // Wait for it to settle down
    delay(500);
}

void loop() {    
    // Read the volatge of the LiPo
    float cellVoltage = batteryMonitor.getVCell();
    // Read the State of Charge of the LiPo
    float stateOfCharge = batteryMonitor.getSoC();
    
    // Send the Voltage and SoC readings over serial
    Serial.print("Battery Voltage : ");
    Serial.print(cellVoltage);
    Serial.print(" ");
    Serial.print("Charging state : ");
    Serial.print(stateOfCharge);
    Serial.println("");
    delay(3000);
}

i can read over serial the values, but if i try to adapt that into variable part does not work.

Here is one of the multiple versions and still no luck


#include "PowerShield/PowerShield.h"

PowerShield batteryMonitor;

char returnString[63];
String someString;

char *BVoltage = "test";
int Voltage = 0;
int SOC = 0;
int rssi = 0;

void setup() {
    if (Particle.variable("Voltage", BVoltage, STRING)==false);
   memset(returnString, 0, 63);
   //Particle.variable("test", returnString, STRING);
}

void loop() {    
    Wire.begin(); 
    batteryMonitor.reset();
    batteryMonitor.quickStart();
    delay(100);
    float cellVoltage = batteryMonitor.getVCell();
    float stateOfCharge = batteryMonitor.getSoC();
    someString = String(cellVoltage);
    strcpy(returnString, someString.c_str());
    delay(100);
    
    Voltage = batteryMonitor.getVCell();
    SOC = stateOfCharge;
    rssi = WiFi.RSSI();
    
    //BVoltage = String(cellVoltage);
    
     Particle.variable("vbat", &Voltage, INT);
     Particle.variable("SOC", &SOC, INT);
     Particle.variable("RSSI", &rssi, INT);
     delay(100);
     Particle.variable("test", returnString, STRING);
    //System.sleep(SLEEP_MODE_DEEP, 20);
}

If i use variable it seems that no matter what i use i get always 0 and if i make that conversion from INT or float to string i get 0.00000.

Any ideas are more than welcomed.

Thanks

Hmmm 2 days back i used the example and it worked. Did you solder the shield to the Photon?

Nope, i got the shield and the photon with headers so just plug and play, it works nice on serial, i get the values, but no values over the cloud. so i guess the issue is somewhere in the middle or on the coding.

Well it seems that the issue was coding,

I just set the variables to String and then converted the result to string and now are working ok, but still i don`t get anything on publish, at least on private , so i guess it is still on pending or something there as well.

As for variables, try declaring those in your setup, rather than your loop.
As for publishes, please make sure you don’t cross the limit of 1p/s, with a burst of 4p/s allowed. The first example will exceed that limit, thus leave you at the risk of being rate-limited.

Well , regarding variables, i did put them on the Setup part and it seems that they don’t work or at least on me, if i put them on the setup() part i get empty values, when if i put them on the loop() part i get all the values so maybe i’m doing something wrong but it works this way .


#include "PowerShield/PowerShield.h"

PowerShield batteryMonitor;

String Voltage;
String soc;
int rssi = 0;

void setup() {
   Particle.variable("RSSI", &rssi, INT);
  
}

void loop() {    
    Wire.begin(); 
    batteryMonitor.reset();
    batteryMonitor.quickStart();
    delay(100);
    float cellVoltage = batteryMonitor.getVCell();
    float stateOfCharge = batteryMonitor.getSoC();
    delay(100);
    
    Voltage = String(batteryMonitor.getVCell());
    soc = String(batteryMonitor.getSoC());
    rssi = WiFi.RSSI();
    
    delay(100);
     
    Particle.variable("vbat", Voltage, STRING);
    Particle.variable("soc", soc, STRING);
    //System.sleep(SLEEP_MODE_DEEP, 20);
}

So this one works ok for me, as you see i have the RSSI in the Setup part and i get the value , but if i put the bat part i get “XZ” and on the soc part i get “”, nothing so i don’t know why, this is the first time for me to work with photon but it seems that in setup some functions are not called. so i keep it in loop, i don’t know if it’s better or not but it works.

Regarding the Publish part i use the code :

#include "PowerShield/PowerShield.h"

PowerShield batteryMonitor;

String Voltage;
String soc;
int rssi = 0;

void setup() {
   Particle.variable("RSSI", &rssi, INT);
}

void loop() {    
    Wire.begin(); 
    batteryMonitor.reset();
    batteryMonitor.quickStart();
    delay(100);
    float cellVoltage = batteryMonitor.getVCell();
    float stateOfCharge = batteryMonitor.getSoC();
    delay(1500);
    
    Voltage = String(batteryMonitor.getVCell());
    soc = String(batteryMonitor.getSoC());
    rssi = WiFi.RSSI();
    
    Particle.publish("voltage", String(cellVoltage), 60, PRIVATE);
    Particle.publish("soc", String(stateOfCharge), 60, PRIVATE);
    delay(1500);
    
    Particle.variable("vbat", Voltage, STRING);
    Particle.variable("soc", soc, STRING);
 
    System.sleep(SLEEP_MODE_DEEP, 60);
}

and it seems that it does not work,

i use from terminal "particle subscribe device_id" and it gives me

Subscribing to "device_id" from the firehose (all devices) 
Listening to: /v1/events/device_id

and that`s it , no response nothing.

i did tried to increase the delay so that i don`t get into that time issue but still no luck .

you need to replace device_id with the event name you want to monitor.

Try particle subscribe mine to see any events from devices under your account.

The easiest might be to just use: https://dashboard.particle.io/user/logs

i just put that so that i don`t publish my id , but the actual id it is there already

Cool thanks, i did not knew about that, it seems that it is publishing something there but all are 0 so i think that i have to edit the code as well to get the right result

Thanks .