MQTT Communication
Smart Factory
MQTTCommunication.h
Go to the documentation of this file.
1 
17 #ifndef MQTTCOMMUNICATION_H__
18 #define MQTTCOMMUNICATION_H__
19 
20 #include "Arduino.h"
21 
23 
24 #include <CircularBuffer.h>
25 #include "Network/Network.h"
26 #include "myJSON/myJSON.h"
27 #include "myMQTT/myMQTT.h"
28 
49 void callback(char* topic, byte* payload, unsigned int length);
50 
55 extern myJSON _myjson;
56 
65 extern CircularBuffer<myJSONStr, MAX_JSON_MESSAGES_SAVED> _buffer;
66 
72  public:
77  Communication(String Hostname);
78 
79  //==INTERFACE===================================
84  inline void init() {
85  pNetwork.init();
87  }
88 
97  inline bool loop() {
98  return pMymqtt.loop();
99  }
100 
101  //=======NETWORK============
102  // inline void connectToWiFi() {
103  // pNetwork.connectToWiFi();
104  // }
105 
106  inline void printNetworkInfo() {
108  }
109 
110  //=======myMQTT============
122  inline bool subscribe(const String topic) {
124  return pMymqtt.subscribe(topic);
125  }
126 
137  inline bool unsubscribe(const String topic) {
139  return pMymqtt.unsubscribe(topic);
140  }
141 
157  inline bool publishMessage(const String topic, const String msg) {
159  return pMymqtt.publishMessage(topic, msg);
160  }
161 
162  //=======myJSON/Buffer============
163  //https://github.com/rlogiacco/CircularBuffer
171  inline myJSONStr last() {
172  return _buffer.last();
173  }
174 
181  inline myJSONStr getElement(int x) {
182  return _buffer[x];
183  }
184 
192  inline myJSONStr pop() {
193  return _buffer.pop();
194  }
195 
202  inline bool isEmpty() {
203  return _buffer.isEmpty();
204  }
205 
213  inline myJSONStr first() {
214  return _buffer.first();
215  }
223  inline myJSONStr shift() {
224  return _buffer.shift();
225  }
226 
232  inline int size() {
233  return _buffer.size();
234  }
235 
240  inline void clear() {
241  _buffer.clear();
242  }
243 
244  private:
245  String pHostname;
246  WiFiClient pClient = WiFiClient();
252  DEFAULT_WIFI_EN);
259 
260  void (*funcPointer)(char*, unsigned char*, unsigned int) = callback;
261 };
262 #endif
#define DEFAULT_MQTT_BROKER_IP2
broker IP distributed in 4 values
The Network class establishes a WLAN-Connection.
myJSON class handels the conversion from the JSON-Format into a struct
myJSONStr getElement(int x)
Get the Element object.
void callback(char *topic, byte *payload, unsigned int length)
If the client is used to subscribe to topics, a callback function must be provided in the constructor...
#define DEFAULT_WIFI_SSID
SSID to connect to.
bool loop()
This should be called regularly to allow the client to process incoming messages and maintain its con...
Definition: myMQTT.h:101
myJSON _myjson
Declare a global variable from typ myJSON so it can be accessed in callback.
#define DEFAULT_WIFI_RST
Reset pin.
#define DEFAULT_MQTT_BROKER_IP1
broker IP distributed in 4 values
Handels the MQTT connection.
Definition: myMQTT.h:32
void clear()
resets the whole buffer to its initial state
#define DEFAULT_WIFI_PASSWORD
Password to corresponding SSID.
file that contains the default communication configuration
#define DEFAULT_WIFI_IRQ
Interrupt pin.
myJSON Struct defines the format of the messages
Definition: myJSONStr.h:33
#define DEFAULT_MQTT_PORT
MQTT connection port.
void connectToWiFi()
Connects to a WiFi with the given Credential.
Definition: Network.cpp:40
Communication(String Hostname)
Construct a new Communication object.
void printNetworkInfo()
Prints all relevant Network-Information of the connected network to serial.
Definition: Network.cpp:64
void(* funcPointer)(char *, unsigned char *, unsigned int)
functionpointer to callback-function
This class handels the MQTT connection.
bool publishMessage(const String topic, const String msg)
handles outgoing MQTT messages to Server
bool subscribe(const String topic)
subsribes to MQTT topic on Server
myJSONStr last()
Retriev data from the Tail (oldest Element) of the Buffer.
myMQTT pMymqtt
instance of myMQTT
bool loop()
This should be called regularly to allow the client to process incoming messages and maintain its con...
#define DEFAULT_WIFI_EN
Enable pin.
void init()
Intitialize WLan-Hardware.
Definition: Network.cpp:25
#define DEFAULT_MQTT_BROKER_IP3
broker IP distributed in 4 values
WiFiClient pClient
instance of WiFiClient
void init(WiFiClient *myClient, void(*pToCallback)(char *, unsigned char *, unsigned int))
Definition: myMQTT.cpp:33
CircularBuffer< myJSONStr, MAX_JSON_MESSAGES_SAVED > _buffer
Declares a global Circular Buffer of type myJSONStr with max Elements of MAX_JSON_MESSAGES_SAVED.
void init()
Initializes the Hard- and Software for WLAN and MQTT-Connection.
int size()
returns the number of elements currently stored in the buffer
The Connection-class provides the interface.
Network pNetwork
instance of Network
bool isEmpty()
Check if the Buffer is empty.
#define DEFAULT_WIFI_CS
Chipselct pin.
myJSONStr shift()
Retriev data from the Head (newest Element) of the Buffer.
bool unsubscribe(const String topic)
unsubsribes from MQTT topic on Server
Definition: myMQTT.cpp:58
#define DEFAULT_MQTT_BROKER_IP4
broker IP distributed in 4 values
myJSONStr pop()
Retriev data from the Tail (oldest Element) of the Buffer.
bool subscribe(const String topic)
subsribes to MQTT topic on Server
Definition: myMQTT.cpp:41
Handels the WLAN-Connection.
Definition: Network.h:30
bool unsubscribe(const String topic)
unsubsribes from MQTT topic on Server
bool publishMessage(const String topic, const String msg)
handles outgoing MQTT messages to Server
Definition: myMQTT.cpp:75
myJSONStr first()
Retriev data from the Head (newest Element) of the Buffer.
myJSON class handels the conversion from the JSON-Format into a struct
Definition: myJSON.h:28