I’m having a problem with the Firebase streaming example in the eBook on Firebase. I successfully do the example in Streaming Database and Updating States (starting on pg 76). I think I understand the code, but prefer organizing it “my way”, and it doesn’t function correctly (I know that your organization facilitates learning, but I’m focused on reading the code down the road for projects). I can only come up with the thought that the order in which my declarations is written is wrong, and so a basic question is: does the order of declaring constants and modules matter in the non-interpretive C++ language (i.e. compiled code) . Probably a related question: is it the ESP32 that wants to see the setup() and loop() functions (like most other systems want to see a main() function? I’m using VBCode/IOPlatform – is it this platform that needs to see the order, and needs to see a setup() and then a loop()?
If declaration order is important, here is the way I arrange my code, and I can move things around, but first, I need to know if my ordering is incorrect:
/**** constants and variable declaration ****/
/**** object declarations *****/
/**** MIsc. modules ****/ (in here are sendFloat, displayMessage, streamCallback and streamTimeoutCallback)
/**** initialization modules ***/ (in here are initBME, initWifi, initOLED, initLEDs, and initFirebaseStuff)
/**** Initialization (setup) *****/ (in here are SerialBegin, and then the 5 init modules listed above)
/**** main (loop) *****/ (in here is the exact stuff in the eBook example)
NOTE: initLEDs() and intiFirebaseStuff() are simply the exact code in the eBook’s setup() moved to form 2 modules, making the setup() section easier to follow.
void setup(){
Serial.begin(115200);
initBME();
initOLED();
initWiFi();
initLEDs();
initFirebaseStuff(); }
When I run my code, it responds to changes in the database as expected, but it’s the 1st run thru the streamCallback that is failing to initialize the LEDs per the database. What I see is that the argument of the if (json->get(result, “/digital/” + String(output1), false)) is not true, and so doesn’t proceed to update the output1 LED or any of the components per the database.
I have changed names in the json file from /outputs/digital/2 to /LEDs+Display/onOffLEDs/yellowLED , so it could be related to that change, but manual changes to the database cause proper changes in the LEDs (the BME280 and OLED function fine). It’s this 1st run triggered on the root (/) path (which I confirm happens by serial.prints) that gets to that get() statement and skips doing anything.
Before getting too deep into the weeds here, a basic question is: does the order of declaring constants and modules matter in the non-interpretive C++ language (i.e. compiled code) ? Thanks.
Hi.
The order matters.
You need to declare a function first before calling it.
You need to declare all objects before using them.
Let me know if this helps.
The rest of your message was a bit confusing… But, let me know if you need further help (if you share your code, it would be easier to spot issues—use GitHub gist to share code).
Regards
Sara
Hi Sara … thanks for pushing me to use a gist, and thanks for agreeing to look at the code. The urls for the code are below. I found 1 solution to the on/off LEDs, but still have a problem with the pwm LEDs.
The solution to the on/off LEDs was that it didn’t like json->get(result, “/onOffLEDs/” + String(yellowLED), false), but wanted json->get(result, “/onOffLEDs/yellowLED”, false) i.e. didn’t like String(string).
However the pwm led section doesn’t set the LEDs. The is the code:
if (json->get(result, “/pwmLEDs/redSliderLED”, false)){
int pwmValue = result.to<int>();
Serial.print(“redSliderLED value is: “); Serial.println(pwmValue);
ledcWrite(redSliderChannel, map(pwmValue, 0, 100, 0, 255));
The realtime database sets the redSliderLED to 20, and the Serial.print shows that the get() does get that value. However, the ledcWrite() function causes the ESP32 to reboot, stating “- assert failed!” …. any idea why? (I tried changing redSliderChannel to 0, and same result)
Here is the url to the main.ccp code:
https://gist.github.com/jamargevicius/93e34964ac77843f27d0d63513cd2f09
and here is the database_joe.json link:
https://gist.github.com/jamargevicius/79f9df14f4e5889d47f85749cfddeaaa
– Joe
Hi.
I took a look at your code and I don’t know why you’re getting that issue.
Something might be in the wrong order, but I’m not sure what it is.
Regards,
Sara
Thanks for checking Sara.
… just figured out the problem. In hardware engineering, we’d say that a “solder blob” is shorting out something and that’s why it won’t work. In software, it’s a typo 😉 … so I had been initializing the pwm LEDs with ledcSetup(pin#, freq, resolution), instead of ledcSetup(channel#, freq, resolution). That along with the String(string) “bug”, kept it from working … I learn a lot at least.
You can close this issue. Thanks for the time you spent on it. (I changed the code in the gist in case anyone attempts to use the code).