What are some great ways to deal with code for different platforms?

Imagine you have a project where you need to write some specific code for a WiFi device and something else for a cellular device.

One way to do this, from the docs, is the following:

// code for wifi device

// code for cellular device

Are there other ways, for better or for worse?
Object-oriented ways?
Library/external files ways?


1 Like

Instead of checking for platform, you should check for features:

#if Wiring_WiFi
    const char *wifiScan();

#if Wiring_Cellular
    const char *cellularScan();


In some cases you may want to use the Network class instead of Cellular or WiFi. The Network class is the base class of specific types of networking, and some common methods like connect() and ready() exist in the base Network class. This is handy if you have a library that works for both Cellular and Wi-Fi, and you want the caller of the library to specify which to use. It also can greatly reduce the number of #ifdef you have in your code.


Hey Rick,
I would like to learn how to do that.
Are there any examples somewhere that I can look at or can you point me in the right direction?
Thank you!

The methods available in the Network class are here:

You’ll find most of the common ones like on, off, connect, disconnect, ready, etc. are all implemented in the Network class which will do the right thing for Cellular and WiFi devices.

Though you do need to be a little careful if you have Ethernet because it’s ambiguous to which you really want.

The other thing that’s good for libraries is passing in a NetworkClass & as a parameter during library setup. The caller can pass Cellular or WiFi or Ethernet as the parameter and you can call methods like on, off, connect, etc. for the interface type that they’ve selected. Cellular is a singleton instance of CellularClass and that is a subclass of NetworkClass so it works really well.

1 Like

Documented the Network class, which can be used to write code that is able to do things like connect() and ready() independent of the type of networking so you can write code that works on both cellular and Wi-Fi without using #ifdef.



This is very useful to me, thank you!