Declaration of Wire, WiFi,... Object

Hi everyone,

Short summary: Where are the Wire and WiFi objects declared?

I’ve done some Arduino projects on my ESP8266s and created some small private libraries. But I still can’t figure out one important point in Arduino:

For example, in the TwoWire Class (Wire.h) [same for WiFi Class], all public functions are non-static, thus I must call them on an instance of TwoWire. In Arduino there is the “Wire” object, on which I can call all the non-static functions, e.g. begin().

Now the question: Where are the Wire object (instance of TwoWire) and WiFi object declared? I want to get a deeper understanding of Arduino and manipulate these objects for my purpose.

Thanks you. :slight_smile:

As far as I understand it, last line in Wire.h before the #endif

extern TwoWire Wire;

Not sure about Wifi (there are too many different libraries), but serial works te same; check the end of HardwareSerial.h.

For me, Wire for example is here:

ECLIPSE_HOME\arduinoPlugin\packages\esp8266\hardware\esp8266\2.4.1\libraries\Wire

or

C:\Users\6V6GT\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0\libraries\Wire

Thank you so much for the fast answer!!

I didn’t know the concept of extern declaration in C++. Here is a short explanation of how the Wire object works in Arduino:

Wire.h:

extern TwoWire Wire; // declares the variable Wire as an object of class TwoWire

Wire.cpp:

TwoWire Wire; // initializes the variable Wire with a new instance of TwoWire

Due to the declaration of Wire with extern, every source file that includes Wire.h can access the Wire object, which is for everyone the same instance. It is “a global accessible static object of the class Wire” (hope this sounds correct).

On the one hand, this concept makes programming for beginners much easier, because the libraries and the main code can easily access the same “physical Wire” / I2C Bus (where your external devices are on) via the global Wire object. You don’t have to pass the Wire objects by hand to every sensor initialization. On the other hand, when every device library uses the Wire object, you can’t differentiate between two different “physical Wires” / I2C Bus.

Hope this can help everyone else.

nfw:
ne the same instance. It is “a global accessible static object of the class Wire” (hope this sounds correct).

On the one hand, this concept makes programming for beginners much easier, because the libraries and the main code can easily access the same “physical Wire” / I2C Bus (where your external devices are on) via the global Wire object. You don’t have to pass the Wire objects by hand to every sensor initialization. On the other hand, when every device library uses the Wire object, you can’t differentiate between two different “physical Wires” / I2C Bus.

It was written for boards with one I2C interface. For the second interface Wire1 would be defined as in the case of Serials