I'm trying to create a class library that works with network communications. I've created a structure and a class file in a header, and I'm trying to use that structure and class in my main sketch.
/*
HomeAssistantMQTT.h - Library for communicating with Home Assistant
*/
#ifndef HomeAssistantMQTT_h
#define HomeAssistantMQTT_h
#include "Arduino.h"
struct NetworkInfo
{
char* wifiSSID;
char* wifiPassword;
char* mqttServer;
char* mqttUser;
int mqttPort;
};
class HomeAssistantMQTT
{
public:
HomeAssistantMQTT();
NetworkInfo network;
private:
int _pin;
};
#endif
I want to have a global variable in this class, so it needs to be instantiated outside of the setup or loop routines. In C# this would be a snap, but I'm missing something crucial here. I can't seem to declare hamqtt an instance of hamqtt, nor pass along the structure so the eventual classes will be able to use that info.
Everytime I add variables to my functions, I don't want to have to modify the constructors, but rather use a class which allows me to make changes in fewer places.
#include "HomeAssistantMQTT.h"
#include "Arduino.h"
HomeAssistantMQTT hamqtt();
void setup() {
NetworkInfo network;
network.wifiSSID = "mywifi"; // Your WiFi network name
network.wifiPassword = "1234"; // Your WiFi network password
network.mqttServer = "172.16.68.67"; // Your MQTT server IP address
network.mqttPort = 1883;
//I Want to instantiate an instance of HomeAssistantMQTT and make it global in this class and store"network" info above
}
void loop() {
//use hamqtt, it's functions, properties here.
}
I just ran your code (from the command line) and it works fine. ???
I ran this from the command line which may be closer to what you're trying to do.
test.cpp
#include <stdio.h>
#include "hamqtt.h"
NetworkInfo network;
HomeAssistantMQTT sdf;
HomeAssistantMQTT ghi;
HomeAssistantMQTT::NetworkInfo hamqtt;
int main()
{
network.wifiSSID = (char*)"mywifi"; // Your WiFi network name
network.wifiPassword = (char*)"1234"; // Your WiFi network password
network.mqttServer = (char*)"172.16.68.67"; // Your MQTT server IP address
network.mqttPort = 1883;
hamqtt.wifiSSID = (char*)"my2wifi";
sdf.network.wifiSSID = (char*)"my3wifi";
ghi.network.wifiSSID = (char*)"my4wifi";
//I Want to instantiate an instance of HomeAssistantMQTT and make it global
// in this class and store"network" info above
printf("%s\n", network.wifiSSID);
printf("%s\n", hamqtt.wifiSSID);
printf("%s\n", sdf.network.wifiSSID);
printf("%s\n", ghi.network.wifiSSID);
}
/* Output
g++.exe -std=c++11 test.cpp -Wall -o test.exe
Process started >>>
<<< Process finished. (Exit code 0)
test
Process started >>>
mywifi
my2wifi
my3wifi
my4wifi
<<< Process finished. (Exit code 0)
*/
hamqtt.h
/*
HomeAssistantMQTT.h - Library for communicating with Home Assistant
*/
#ifndef hamqtt_h
#define hamqtt_h
struct NetworkInfo
{
char* wifiSSID;
char* wifiPassword;
char* mqttServer;
char* mqttUser;
int mqttPort;
};
class HomeAssistantMQTT
{
int _pin;
public:
typedef struct NetworkInfo NetworkInfo;
NetworkInfo network;
};
#endif
This code:
HomeAssistantMQTT hamqtt();
is not an instantiation of an object of the HomeAssistantMQTT class. It’s a prototype for a function that takes no arguments and returns a HomeAssistantMQTT object.
You want:
HomeAssistantMQTT hamqtt;
Also, your use of ‘char *’ variables in the NetworkInfo struct is problematic. It will allow you to change string literals in RAM and/or strand those literals if the pointer is changed. At the very least they should be ‘const char *’.
Even better would something like:
struct NetworkInfo
{
char wifiSSID[20];
char wifiPassword[20];
char mqttServer[18];
char mqttUser[20];
uint16_t mqttPort;
};
Finally, this is not how you access a data member of an object:
network.wifiSSID = "mywifi";
In fact, your data members should be private and only accessible using a public member functions (i.e. set() / get())