Help: calling wifi class from a 2nd cpp file

First of all, let me introduce myself and say that this is my first port on this community.
I’ve been programing arduinos and esp8266 for a couple of years. During all that time I was using Arduino IDE, and recently I’ve decided to move all my new programming to PlatformIO (+Atom).

I’m currently upgrading the code of my small meteo station and decided to go with PlatformIO.

I’ve already re-wrote a lot of the code, but now I need your support.

Currently I have several CPP files, since I’m trying to keep the code segmented so I can reuse the segments in other projects.

To this matter, let’s consider I have 3 CPP files, the main.cpp with all the basic code, the wifi.cpp with the code related to wifi connection and the mqtt.cpp with the code related to the mqtt client.

My issue is the following:

  1. At wifi.cpp I’m creating the wifi connection
#include <ESP8266WiFiMulti.h>   // Include the Wi-Fi-Multi library
ESP8266WiFiMulti wifiMulti;     // Create an instance of the ESP8266WiFiMulti class, called 'wifiMulti'
  1. At mqtt.cpp I need to create the mqtt connection, but for that I need to call wifiMulti from the wifi.cpp. The code below is giving an error (‘wifiMulti’ was not declared in this scope)

`

PubSubClient mqttclient(wifiMulti);

`

HOW should I do to call it?

This is probably a rookie question…

Thank you

You can declare your wifiMulti object in a header file as an extern object, thus allowing other .cpp files to see your global variable you defined in another cpp file.

For example, create a wifi.h with the contents

#ifndef OWN_WIFI_H
#define OWN_WIFI_H

#include <ESP8266WiFiMulti.h>
extern ESP8266WiFiMulti wifiMulti;

#endif

You may then #include "wifi.h" in your mqtt.cpp and have full access over to your wifiMulti variable, which was defined in wifi.cpp.

It makes sense, I will try it later today and let you know.
I’m used to Arduino IDE witch is much more simple (and less powerful).
I guess I’m currently learning C++ on a trial-error basis. Thank you for your help on this one.

Thanks, it worked just nice