I’m wondering if theirs an easy way to determine if the device my firmware is running on is an Electron or a Photon?
I would like to be able to have the same code base deployed to both Photon and Electron but with minor differences (e.g. less frequent publishing from the Electron).
Is their a #define somewhere I can use or should I try and inspect WiFi.ready / cellular.ready to determine if the code is on one or the other? Ideally I would like to know before either of those are available.
Just for fun, here’s the use case…
MightOhm Geiger counter with Electron on top sending radiation levels back to Tinamous, I’d like to be able to swap a Photon/Electron and not have different versions of the code.
user firmware code (Arduino style) will remain the same. You only need to select the correct device during compilation and the binary output will be for the device you required.
You can check PLATFORM_ID but I would test for the feature you want, not the platform it’s running on, for better future compatibility. For example, put the code in one of these #if tests:
@kennethlimcp Thanks for your answer, but that’s not really what I had in mind, I need different settings based on the device.
I have a timer that ticks every n seconds, for the Photon I have it every 30 but for the Electron I set it to 600 (at present), I don’t want to have to change the code based on which platform, so @rickkas7’s PLATFORM_ID looks like what I’m after.
The answers given here have been fabulous, thanks all!
If you want to specifically target the Core, Photon, P1 and Electron, I would recommend:
#if PLATFORM_ID == 0 // Core
// your code
#elif PLATFORM_ID == 6 // Photon
// your code
#elif PLATFORM_ID == 8 // P1
// your code
#elif PLATFORM_ID == 10 // Electron
// your code
#else
#error "*** PLATFORM_ID not supported by this firmware. PLATFORM should be Core, Photon, P1 or Electron ***"
#endif
Reviving this old thread – we are adding Electrons to our product line and want to use the exact same codebase. Our code has references to WiFi, which will simply be unused for Electrons, but how can I get the code to compile?
e.g. I have:
// determine platform
#if PLATFORM_ID == 6 // Photon
#define PLATFORM "PHOTON"
#elif PLATFORM_ID == 10 // Electron
#define PLATFORM "ELECTRON"
#else
#error "*** PLATFORM_ID not supported by this firmware. PLATFORM should be Core, Photon, P1 or Electron ***"
#endif
And then in loop():
if (PLATFORM == "PHOTON") {
int found = WiFi.scan(aps, 20);
// more code here
}
Another thing I’m regularly using to save myself some of these compiler directives for all the functions that do exist and behave the same way for CellularandWiFi is to add this to the top of my code