13bool configureESPNOW(uint8_t *mac) {
14 logger->log(DEBUG,
"Setting up ESP-NOW and connection with broker at %02X:%02X:%02X:%02X:%02X:%02X.",
15 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
17 if (esp_now_init() != ESP_OK) {
18 logger->log(ERROR,
"Couldn't initialize ESP-NOW");
23 if(esp_now_is_peer_exist(mac)) {
26 esp_now_peer_info_t peerInfo;
27 memset(&peerInfo, 0,
sizeof(peerInfo));
28 memcpy(peerInfo.peer_addr, mac, 6);
30 peerInfo.encrypt =
false;
32 esp_err_t result = esp_now_add_peer(&peerInfo);
33 if (result != ESP_OK) {
34 logger->log(ERROR,
"Couldn't register peer: %d.", result);
41int fixTopicAndCheckLength(
char *topic) {
45 size_t len = strlen(topic);
46 if (len == 0 || len > MAXTOPICLENGTH)
49 if (topic[0] ==
'/') {
50 memmove(topic, topic + 1, len);
56 if (topic[len-1] ==
'/') {
67 return c >= 0 && c <= 127;
70int pubTopicCheck(
char *topic) {
74 for (
size_t i = 0; i < strlen(topic); i++) {
75 if (topic[i] ==
'+' || topic[i] ==
'#' || !isASCII(topic[i]))
81int subTopicCheck(
char *topic) {
86 for (
int i = 0; i < strlen(topic); i++) {
92 if ((prev !=
'\0' && prev !=
'/') || (topic[i+1] !=
'\0' && topic[i+1] !=
'/'))
95 }
else if (c ==
'#') {
96 if ((prev !=
'\0' && prev !=
'/') || topic[i+1] !=
'\0')
105LMQErrType publish(uint8_t *mac,
char *topic,
void *payload,
size_t payloadSize, Elog *_logger) {
107 if (!configureESPNOW(mac)) {
111 logger->log(ERROR,
"Invalid topic: '%s'", topic);
118 strcpy(pubMsg.
topic, topic);
120 memcpy(&pubMsg.
content, payload, payloadSize);
123 esp_err_t result = esp_now_send(mac, (uint8_t *) &pubMsg,
sizeof(pubMsg));
124 if (result != ESP_OK) {
125 logger->log(ERROR,
"Error sending message: %d.", result);
128 logger->log(INFO,
"Message of %dB published successfully to '%s'.", payloadSize, topic);
135 if (!configureESPNOW(mac)) {
139 logger->log(ERROR,
"Invalid topic: '%s'.", topic);
146 strcpy(subMsg.
topic, topic);
149 esp_err_t result = esp_now_send(mac, (uint8_t *) &subMsg,
sizeof(subMsg));
150 if (result != ESP_OK) {
151 logger->log(ERROR,
"Error sending message: %d.", result);
154 logger->log(INFO,
"Subscribed to '%s'.", subMsg.
topic);
161 if (!configureESPNOW(mac)) {
165 logger->log(ERROR,
"Invalid topic: '%s'.", topic);
172 strcpy(unsubMsg.
topic, topic);
175 esp_err_t result = esp_now_send(mac, (uint8_t *) &unsubMsg,
sizeof(unsubMsg));
176 if (result != ESP_OK) {
177 logger->log(ERROR,
"Error sending message: %d.", result);
180 logger->log(INFO,
"Unsubscribed from '%s'.", unsubMsg.
topic);
193 memcpy(&pubMsg, &incomingData,
sizeof(pubMsg));
LMQErrType
Enumerates every error code that can be returned by the library functions.
@ LMQ_ERR_INVAL_TOPIC
Invalid topic (no topic, too big, contains wildcard characters in wrong positions,...
@ LMQ_ERR_BAD_ESP_CONFIG
Couldn't initialize ESP-NOW.
@ LMQ_ERR_SUCCESS
No error, operation successful.
@ LMQ_ERR_ESP_SEND_FAIL
Couldn't send the message.
@ LMQ_ERR_VALID_TOPIC
No error, valid topic.
LMQErrType unsubscribe(uint8_t *mac, char *topic, Elog *_logger)
Unsubscribes from a topic on the broker.
LMQErrType subscribe(uint8_t *mac, char *topic, Elog *_logger)
Subscribes to a topic on the broker.
LMQErrType publish(uint8_t *mac, char *topic, void *payload, size_t payloadSize, Elog *_logger)
Publishes a message to the broker.
PayloadContent getLMQPayload(const uint8_t *incomingData)
Gets the payload content inside a published message This function extracts the payload from the bytes...
bool isLMQMessage(const uint8_t *incomingData)
Checks if the data received is a MQ message.
MessageType
Enumerates every type of message sent between the broker and the clients.
@ MSGTYPE_UNSUBSCRIBE
Unsubscribe message, sent from subscriber to broker.
@ MSGTYPE_PUBLISH
Publish message, sent from publisher to broker or from broker to subscriber.
@ MSGTYPE_SUBSCRIBE
Subscribe message, sent from subscriber to broker.
Structure that contains the fields used by every message.
Structure representing the content of a payload This structure holds properties of the content receiv...
void * content[MAXCONTENTSIZE]
Array to hold content.
size_t contentSize
Size of the content.
Structure that contains the fields used by a publish message, apart from those inherited from the Mes...
char topic[MAXTOPICLENGTH]
Topic where the message is published to.
size_t contentSize
Size of the content.
void * content[MAXCONTENTSIZE]
Any content stored as bytes.
Structure that contains the fields used by a subscribe message, apart from those inherited from the M...
char topic[MAXTOPICLENGTH]
Topic that the subscriber shows interest in.
Structure that contains the fields used by a unsubscribe message, apart from those inherited from the...
char topic[MAXTOPICLENGTH]
Topic that the subscriber no longer shows interest in.