EnigmaIOT  0.9.8
Secure sensor and gateway platform based on ESP8266 and ESP32
GwOutput_mqtt.h
Go to the documentation of this file.
1 
11 #ifndef _GWOUTPUT_MQTT_h
12 #define _GWOUTPUT_MQTT_h
13 
14 #if defined(ARDUINO) && ARDUINO >= 100
15 #include "Arduino.h"
16 #else
17 #include "WProgram.h"
18 #endif
19 
20 #include <GwOutput_generic.h>
21 #include <ESPAsyncWiFiManager.h>
22 #include <EnigmaIOTGateway.h>
23 #include <PubSubClient.h>
24 #include <queue>
25 #include "dstrootca.h"
26 #ifdef SECURE_MQTT
27 #include <WiFiClientSecure.h>
28 #else
29 #include <WiFiClient.h>
30 #endif // SECURE_MQTT
31 
32 #define MQTT_BUFFER_SIZE 2048
33 
34 // DOWNLINK MESSAGES
35 #define GET_VERSION "get/version"
36 #define GET_VERSION_ANS "result/version"
37 #define GET_SLEEP "get/sleeptime"
38 #define GET_SLEEP_ANS "result/sleeptime"
39 #define SET_SLEEP "set/sleeptime"
40 #define SET_OTA "set/ota"
41 #define SET_OTA_ANS "result/ota"
42 #define SET_IDENTIFY "set/identify"
43 #define SET_RESET_CONFIG "set/reset"
44 #define SET_RESET_ANS "result/reset"
45 #define GET_RSSI "get/rssi"
46 #define GET_RSSI_ANS "result/rssi"
47 #define GET_NAME "get/name"
48 #define SET_NAME "set/name"
49 #define GET_NAME_ANS "result/name"
50 #define SET_USER_DATA "set/data"
51 #define GET_USER_DATA "get/data"
52 #define RESTART_NOTIF "restart"
53 #define NODE_DATA "data"
54 #define LOST_MESSAGES "debug/lostmessages"
55 #define NODE_STATUS "status"
56 #define GW_STATUS "/gateway/status"
57 #define SET_RESTART_MCU "set/restart"
58 
59 const time_t STATUS_SEND_PERIOD = 300000;
60 
61 constexpr auto CONFIG_FILE = "/mqtt.json";
62 
63 typedef struct {
64  char mqtt_server[41];
65 #ifdef SECURE_MQTT
66  int mqtt_port = 8883;
67 #else
68  int mqtt_port = 1883;
69 #endif //SECURE_MQTT
70  char mqtt_user[21];
71  char mqtt_pass[41];
73 
74 constexpr auto MAX_MQTT_TOPIC_LEN = 100;
75 constexpr auto MAX_MQTT_PLD_LEN = 2048;
76 
77 typedef struct {
78  char topic[MAX_MQTT_TOPIC_LEN];
79  char payload[MAX_MQTT_PLD_LEN];
80  size_t payload_len;
81  bool retain;
83 
84 
86 protected:
87  AsyncWiFiManagerParameter* mqttServerParam = NULL;
88  AsyncWiFiManagerParameter* mqttPortParam = NULL;
89  AsyncWiFiManagerParameter* mqttUserParam = NULL;
90  AsyncWiFiManagerParameter* mqttPassParam = NULL;
91 
92  //std::queue<mqtt_queue_item_t*> mqtt_queue; ///< @brief Output MQTT messages queue. It acts as a FIFO queue
94 
96  bool shouldSaveConfig = false;
97 
98 #ifdef SECURE_MQTT
99  WiFiClientSecure espClient;
100 #ifdef ESP8266
101  BearSSL::X509List certificate;
102 #endif // ESP8266
103 #else
104  WiFiClient espClient;
105 #endif // SECURE_MQTT
106  PubSubClient mqtt_client;
107 
112  bool saveConfig ();
113 //#ifdef SECURE_MQTT
117  void setClock ();
118 //#endif // SECURE_MQTT
126  void reconnect ();
127 
135  bool addMQTTqueue (const char* topic, char* payload, size_t len, bool retain = false);
136 
142 
146  void popMQTTqueue ();
147 
155  bool publishMQTT (const char* topic, const char* payload, size_t len, bool retain = false);
156 
163  static void onDlData (char* topic, uint8_t* data, unsigned int len);
164 
165 public:
171 #if defined ESP8266 && defined SECURE_MQTT
172  certificate (DSTroot_CA),
173 #endif // ESP8266 && SECURE_MQTT
175 
180  void configManagerStart (EnigmaIOTGatewayClass* enigmaIotGw);
181 
186  void configManagerExit (bool status);
187 
192  bool begin ();
193 
198  bool loadConfig ();
199 
207  bool outputControlSend (char* address, uint8_t* data, size_t length);
208 
215  bool newNodeSend (char* address, uint16_t node_id);
216 
223  bool nodeDisconnectedSend (char* address, gwInvalidateReason_t reason);
224 
233  bool outputDataSend (char* address, char* data, size_t length, GwOutput_data_type_t type = data);
234 
235 #if SUPPORT_HA_DISCOVERY
236 
244  bool rawMsgSend (const char* topic, char* payload, size_t len, bool retain);
245 #endif
246 
250  void loop ();
251 };
252 
253 extern GwOutput_MQTT GwOutput;
254 
255 #endif // _GWOUTPUT_MQTT_h
256 
GwOutput_MQTT::nodeDisconnectedSend
bool nodeDisconnectedSend(char *address, gwInvalidateReason_t reason)
Send node disconnection notification.
Definition: GwOutput_mqtt.cpp:696
GwOutput_MQTT::configManagerStart
void configManagerStart(EnigmaIOTGatewayClass *enigmaIotGw)
Called when wifi manager starts config portal.
Definition: GwOutput_mqtt.cpp:43
GwOutput_MQTT::mqttPassParam
AsyncWiFiManagerParameter * mqttPassParam
Configuration field for MQTT server password.
Definition: GwOutput_mqtt.h:90
mqtt_queue_item_t
Definition: GwOutput_mqtt.h:77
GwOutput_MQTT::newNodeSend
bool newNodeSend(char *address, uint16_t node_id)
Send new node notification.
Definition: GwOutput_mqtt.cpp:678
EnigmaIOTRingBuffer< mqtt_queue_item_t >
GatewayOutput_generic
Definition: GwOutput_generic.h:31
EnigmaIOTGatewayClass
Main gateway class. Manages communication with nodes and sends data to upper layer.
Definition: EnigmaIOTGateway.h:120
GwOutput_MQTT::mqtt_queue
EnigmaIOTRingBuffer< mqtt_queue_item_t > mqtt_queue
Definition: GwOutput_mqtt.h:93
GwOutput_generic.h
Generic Gateway output module template.
GwOutput_MQTT::reconnect
void reconnect()
This is called anytime MQTT client is disconnected.
Definition: GwOutput_mqtt.cpp:235
GwOutput_MQTT::onDlData
static void onDlData(char *topic, uint8_t *data, unsigned int len)
Function that processes downlink data from network to node.
Definition: GwOutput_mqtt.cpp:382
GwOutput_MQTT::mqttPortParam
AsyncWiFiManagerParameter * mqttPortParam
Configuration field for MQTT server port.
Definition: GwOutput_mqtt.h:88
GwOutput_MQTT::begin
bool begin()
Starts output module.
Definition: GwOutput_mqtt.cpp:203
GwOutput_MQTT::popMQTTqueue
void popMQTTqueue()
Deletes next item in the queue.
Definition: GwOutput_mqtt.cpp:515
GwOutput_MQTT::mqttServerParam
AsyncWiFiManagerParameter * mqttServerParam
Configuration field for MQTT server address.
Definition: GwOutput_mqtt.h:87
GwOutput_MQTT::loop
void loop()
Should be called regularly for module management.
Definition: GwOutput_mqtt.cpp:433
mqtt_queue_item_t::retain
bool retain
Definition: GwOutput_mqtt.h:81
GwOutput_MQTT::outputDataSend
bool outputDataSend(char *address, char *data, size_t length, GwOutput_data_type_t type=data)
Send data from nodes.
Definition: GwOutput_mqtt.cpp:551
MAX_MQTT_TOPIC_LEN
constexpr auto MAX_MQTT_TOPIC_LEN
Definition: GwOutput_mqtt.h:74
GwOutput_MQTT::mqttgw_config
mqttgw_config_t mqttgw_config
MQTT server configuration data.
Definition: GwOutput_mqtt.h:95
GwOutput_MQTT::setClock
void setClock()
Synchronizes time over NTP to check certifitate expiration time.
Definition: GwOutput_mqtt.cpp:467
GwOutput_MQTT::shouldSaveConfig
bool shouldSaveConfig
Flag to indicate if configuration should be saved.
Definition: GwOutput_mqtt.h:96
CONFIG_FILE
constexpr auto CONFIG_FILE
MQTT outout configuration file name.
Definition: GwOutput_mqtt.h:61
GwOutput_MQTT
Definition: GwOutput_mqtt.h:85
GwOutput_MQTT::configManagerExit
void configManagerExit(bool status)
Called when wifi manager exits config portal.
Definition: GwOutput_mqtt.cpp:166
GwOutput
GwOutput_MQTT GwOutput
Definition: GwOutput_dummy.cpp:29
GwOutput_MQTT::getMQTTqueue
mqtt_queue_item_t * getMQTTqueue()
Gets next item in the queue.
Definition: GwOutput_mqtt.cpp:507
GwOutput_data_type_t
enum GwOutput_data_type GwOutput_data_type_t
GwOutput_MQTT::outputControlSend
bool outputControlSend(char *address, uint8_t *data, size_t length)
Send control data from nodes.
Definition: GwOutput_mqtt.cpp:574
STATUS_SEND_PERIOD
const time_t STATUS_SEND_PERIOD
Definition: GwOutput_mqtt.h:59
GwOutput_MQTT::GwOutput_MQTT
GwOutput_MQTT()
Constructor to initialize MQTT client.
Definition: GwOutput_mqtt.h:169
GwOutput_MQTT::mqttUserParam
AsyncWiFiManagerParameter * mqttUserParam
Configuration field for MQTT server user name.
Definition: GwOutput_mqtt.h:89
GwOutput_MQTT::espClient
WiFiClient espClient
TCP client.
Definition: GwOutput_mqtt.h:104
GwOutput_MQTT::addMQTTqueue
bool addMQTTqueue(const char *topic, char *payload, size_t len, bool retain=false)
Add MQTT message to queue.
Definition: GwOutput_mqtt.cpp:484
data
@ data
Definition: GwOutput_generic.h:23
MAX_MQTT_PLD_LEN
constexpr auto MAX_MQTT_PLD_LEN
Definition: GwOutput_mqtt.h:75
mqtt_queue_item_t::payload_len
size_t payload_len
Definition: GwOutput_mqtt.h:80
GwOutput_MQTT::mqtt_client
PubSubClient mqtt_client
MQTT client.
Definition: GwOutput_mqtt.h:106
GwOutput_MQTT::saveConfig
bool saveConfig()
Saves output module configuration.
Definition: GwOutput_mqtt.cpp:59
MAX_MQTT_QUEUE_SIZE
static const size_t MAX_MQTT_QUEUE_SIZE
Maximum number of MQTT messages to be sent.
Definition: EnigmaIoTconfig.h:27
EnigmaIOTGateway.h
Library to build a gateway for EnigmaIoT system.
gwInvalidateReason_t
gwInvalidateReason_t
Key invalidation reason definition.
Definition: EnigmaIOTGateway.h:75
GwOutput_MQTT::loadConfig
bool loadConfig()
Loads output module configuration.
Definition: GwOutput_mqtt.cpp:101
mqttgw_config_t
Definition: GwOutput_mqtt.h:63
dstrootca.h
status
@ status
Definition: GwOutput_generic.h:25
GwOutput_MQTT::publishMQTT
bool publishMQTT(const char *topic, const char *payload, size_t len, bool retain=false)
Publishes data over MQTT.
Definition: GwOutput_mqtt.cpp:456