Firebase web hook

Thanks. I am trying your code and I get this error “‘class FuelGauge’ has no member named ‘getSoc’” which I find strange because I did not receive that error on my previous code that uses the same " , Fuel.getSoc()" code.

My bad, this should be a capital C in Fuel.getSoC() (corrected above)

Thanks I made all the changes and it seems to be pushing the correct data and the hook is working properly.
I only need a few more tweaks and the firmware will be done. Thanks for all the help.
Now on to sleep cycle to save battery.

any idea why I am now only getting a -1 for the powersource?

You already got -1 for it when you posted this

Since my crystal ball :crystal_ball: is broken and you don't really show how you detect your power source and where you set lastPowerSource it's rather difficult to advise :wink:

I should order you a new crystal ball for all the help.

SerialLogHandler logHandler;

unsigned long lastPublish = 0;
int lastPowerSource = -1;
 FuelGauge Fuel;
void Setup()
Serial.begin()

void batloop()
{
  	int powerSource = DiagnosticsHelper::getValue(DIAG_ID_SYSTEM_POWER_SOURCE);
	if (powerSource != lastPowerSource) {
		if (millis() - lastPublish >= 1000 && Particle.connected()) {
			lastPublish = millis();

		    // POWER_SOURCE_UNKNOWN = 0,
			// POWER_SOURCE_VIN = 1,
			// POWER_SOURCE_USB_HOST = 2,
			// POWER_SOURCE_USB_ADAPTER = 3,
			// POWER_SOURCE_USB_OTG = 4,
			// POWER_SOURCE_BATTERY = 5

			char buf[128];
 			snprintf(buf, sizeof(buf), "powerSource=%d", powerSource);

// 			Particle.publish("powerSource", buf, PRIVATE);
			Log.info(buf);

			lastPowerSource = powerSource;
			Fuel.getSoC();
            }
		}
}

void loop()
{
        /* Get a new sensor event */
        sensors_event_t event;
        bno.getEvent(&event);
                char publishString[240] = "empty"; // averaging around 104 chars
                
    snprintf(publishString
        , sizeof(publishString)
        , "{\"x\":%.2f,\"y\":%.2f,\"z\":%.2f,\"c\":%d,\"s\":%.2f}"
        , event.orientation.x
        , event.orientation.y
        , event.orientation.z
        , lastPowerSource
        , Fuel.getSoC()
        );
   Particle.publish("FBpush", publishString, PRIVATE);
     
   //delay is 1sec=1000
   delay(100000);
1 Like

Use 'powerSource' in your publish snprintf. You're currently creating some weird lastPowerSource char.

Since your code is compiling I'm presuming that DiagnosticsHelper::getValue is meant to return an int related to your commented out enum list.

Simply use the powerSource variable instead of lastPowerSource in your firebase push. For what really is an enum, it's far better practice to have integers than strings in your database for searchability and indexing.

Thanks. I’ll try that tomorrow. I believe I tried that earlier and it gave me an error. I will let you know what it does.

Since powerSource is local to batloop() it won't be accessible in loop() but lastPowerSource (which is also an int as exected) is global.
I'm not quite sure where this comes from

I can't see evidence for that.

I'd agree on the integer vs. string part, but since enum are not strings but provide readable meaning for an integral value using enum is usually better practice than using anonymous integer values.

I also think this code doesn't compile due to the missing curly braces around void Setup() body (and the final closing brace for loop() but that's probably only a copy/paste error) and missing semicolons.

Furthermore it should be a small s in void setup() where @txsrooster has void Setup() - C/C++ is case sensitive.

However, I guess the lastPowerSource = powerSource; line may not be reached since I can't see a call to batloop() anywhere (that should become evident by not getting the expected Log.info("powerSource=xxxx") in your serial terminal - that's what log statements are for :wink: ).

There is also no need to call Fuel.getSoC() in batloop() as it doesn't even catch the return value.

Thanks for all the help. Ok I removed the batloop() and moved the code to inside the loop() this has solved my issue with the powersource. Everything seems to be running as it should. Now on to the sleep cycle.

The weird ‘lastpowersource’ is in his publish. The code starts by retrieving an int and saving as powerSource. Later on he creates a char with “powerSource=%d”. That’s the weird char. It’s pointless and contradicts his printf format of c:%d in his publish. Per my comment, just use powerSource.

Re enum v int, we’re on the same page religiously. I’m not going to stackoverflow it that enums just make life easier for humans (and IDE’s!) reading code. He’s typed it as int, there’s been no special effort to enum powerSource so it’s lost the lookup glue.

I still can't follow :confused:

powerSource being local makes it inaccessible in loop() while lastPowerSource is global and hence the only way for the respective value to become known in loop().

Yes, he creates a local string (stored in buf) which would read as something like "powerSource=3" which he then logs to serial (Log.info(buf)).
After that both powerSource and buf will vanish as batloop() ends.

There is nothing weird in that at all IMO.

We can't really say that. There may be reasons to log the value in one format (e.g. for debugging an individual unit whithout knowledge of data created in another) but publish it in combination with a bunch of other data of various sources in a different format.
IMO it does make sense to use the stored 3 in multiple ways (coming from DiagnosticsHelper::getValue() into local powerSource to be logged and stored for global use lastPowerSource for publishing along with other data).
The resulting publishString would be featuring a ..."c": 3,... as intended, no matter whether there was another string created and discarded prior to that or not.

Granted, the whole process could be streamlined, but as is, my point still stands, the actual issue with that code was that the function that would set lastPowerSource (or powerSource or buf for the matter) was never called and hence the variable never changed from its initial value of -1 to anything meaningful. Anything else wouldn't address the problem at all.