MQTT Communication
Smart Factory
MQTTCommunication.cpp
Go to the documentation of this file.
1 
14 /*
15 Useful Links
16 https://techtutorialsx.com/2017/04/29/esp32-sending-json-messages-over-mqtt/
17 https://randomnerdtutorials.com/decoding-and-encoding-json-with-arduino-or-esp8266/
18 https://arduinodiy.wordpress.com/2017/07/30/json-mqtt-and-openhab/
19 https://assetwolf.com/learn/sending-data-from-arduino-to-cloud
20 */
21 
22 #include "MQTTCommunication.h"
23 
24 //==Global Vairable====
26 CircularBuffer<myJSONStr, MAX_JSON_MESSAGES_SAVED> _buffer;
27 
28 //======Func====
32 void callback(char* topic, byte* payload, unsigned int length) {
33  DBFUNCCALLln("callback(const char[] topic, byte* payload, unsigned int length)");
34  char payload_str[length];
35  for (unsigned int i = 0; i < length; i++) { // iterate message till lentgh caus it's not 0-terminated
36  payload_str[i] = (char)payload[i];
37  }
38  payload_str[length] = '\0';
39 
40  String topic_str = String((char*)topic);
41  String currentMessage = topic_str + " " + payload_str;
42  DBINFO3("CurrMessage: ");
43  DBINFO3ln(currentMessage);
44  DBINFO3("LastMessage: ");
45  DBINFO3ln(_myjson.lastMessage);
46 
47  if ((_myjson.lastMessage == currentMessage) && (_buffer.size() != 0)) {
48  DBINFO2ln("Duplicated Message");
49  } else {
50  DBINFO2ln("Add to Buffer");
51  //https://stackoverflow.com/questions/1360183/how-do-i-call-a-non-static-method-from-a-static-method-in-c
52  myJSONStr newMessage = _myjson.parsingJSONToStruct((char*)payload_str);
53  newMessage.topic = topic_str;
54  DBINFO3("ID: ");
55  DBINFO3ln(newMessage.id);
56  DBINFO3("Topic: ");
57  DBINFO3ln(newMessage.topic);
58  DBINFO3("Status: ");
59  DBINFO3ln(newMessage.status);
60  DBINFO3("sector: ");
61  DBINFO3ln(newMessage.sector);
62  DBINFO3("line: ");
63  DBINFO3ln(newMessage.line);
64  DBINFO3("ack: ");
65  DBINFO3ln(newMessage.ack);
66  DBINFO3("req: ");
67  DBINFO3ln(newMessage.req);
68  DBINFO3("cargo: ");
69  DBINFO3ln(newMessage.cargo);
70  DBINFO3("error: ");
71  DBINFO3ln(newMessage.error);
72  DBINFO3("token: ");
73  DBINFO3ln(newMessage.token);
74  // _myjson.StructIsEqual(newMessage, _buffer.first()); //bug this calls changes the message chars somehow?
75  _buffer.unshift(newMessage);
76  _myjson.lastMessage = currentMessage;
77  }
78 }
79 
80 Communication::Communication(String Hostname) : pHostname(Hostname) {
81  init();
82 }
String req
Request.
Definition: myJSONStr.h:40
The Connection-class is used as interface.
CircularBuffer< myJSONStr, MAX_JSON_MESSAGES_SAVED > _buffer
instance of CircularBuffer
myJSON Struct defines the format of the messages
Definition: myJSONStr.h:33
Communication(String Hostname)
Construct a new Communication object.
String cargo
Cargo.
Definition: myJSONStr.h:41
int line
Line.
Definition: myJSONStr.h:38
bool error
Error-token.
Definition: myJSONStr.h:43
String lastMessage
Definition: myJSON.h:48
String status
Status.
Definition: myJSONStr.h:36
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...
String id
Hostname.
Definition: myJSONStr.h:34
#define MAX_JSON_PARSE_SIZE
max buffer size to parse JSON objects, size of the pool in bytes, can be calculated in https://arduin...
String topic
MQTT topic.
Definition: myJSONStr.h:35
myJSON _myjson(MAX_JSON_PARSE_SIZE)
instance of myJSON
String sector
Sector.
Definition: myJSONStr.h:37
void init()
Initializes the Hard- and Software for WLAN and MQTT-Connection.
myJSONStr parsingJSONToStruct(const char *json)
Parsing JSON-Format into myJSONStr.
Definition: myJSON.cpp:19
String ack
Acknoledgement.
Definition: myJSONStr.h:39
bool token
Gateway and Reset-token.
Definition: myJSONStr.h:42
myJSON class handels the conversion from the JSON-Format into a struct
Definition: myJSON.h:28