Project won't compile

This basically the same type of problem which I solved in Getting Error 1 when building [Solved] - #2 by maxgerhardt. WHen you create a new object in a header file, it gets duplicated by every .cpp file that includes it. Thus the error “multiple definition of …” occurs.

To resolve it you must only declare the variables extern in your header files without constructor arguments. Then you define the objects once in only 1 cpp file.

I.e., your wireless.h header would be

#ifndef wireless_h
#define wireless_h

//Includes Wireless Transmission libaries
#include <Adafruit_MQTT.h>
#include <Adafruit_MQTT_Client.h>
#include <ESP8266WiFi.h>
#include "configuration.h"

extern WiFiClient espClient;
extern Adafruit_MQTT_Client mqtt;
extern Adafruit_MQTT_Publish publishData;
extern long lastMsg;

void MQTT_connect();
void setup_wifi();

#endif

And at the top of wireless.cpp you’d have

//all include fiels [..]

//Define objects once.
WiFiClient espClient;
Adafruit_MQTT_Client mqtt(&espClient, mqtt_server, 1883);
Adafruit_MQTT_Publish publishData = Adafruit_MQTT_Publish(&mqtt, "/");
long lastMsg = 0;

Do that with all the other header files, too.

2 Likes