[solved] How to determine Electron vs Photon in Firmware?

Hi,

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.

Thanks,

Steve.

@TinamousSteve,

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:

#if Wiring_WiFi
#if Wiring_Cellular
4 Likes

Perfect, Thank you @rickkas7

Where do I find the list of definitions? (e.g. the Wiring_WiFi).

Thanks,

Steve.

@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.

You don’t need to include anything to use them, but to browse the definitions, they’re in spark_wiring_platform.h:

2 Likes

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
4 Likes

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
  }

but when I compile for Electron, I get:

'WiFi was not declared in this scope'

#if ( PLATFORM_ID == PLATFORM_PHOTON_PRODUCTION )
    int found = WiFi.scan(aps, 20);
#endif

Try this.

2 Likes

@rac_atx you can also do this now so you shouldn’t have to keep adding individual product ID’s

void loop() {
#if Wiring_WiFi
    #error "replace error with wifi platform code"
#elif Wiring_Cellular
    #error "replace error with cellular platform code"
#endif
}
1 Like

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 Cellular and WiFi is to add this to the top of my code

#if Wiring_WiFi
  #define mainRADIO WiFi
#elif Wiring_Cellular
  #define mainRADIO Cellular
#endif

and then in code I just use it like

  mainRADIO.on();
  mainRADIO.connect();
  waitUntil(mainRADIO.ready);

For missing functions or functions that behave differently (e.g. xxxx.RSSI()) you’ll still need dedicated conditional compiling blocks tho’.

4 Likes

Thanks, guys!