Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3bf64e3

Browse files
committedJul 7, 2022
Init
1 parent 814f8ad commit 3bf64e3

File tree

9 files changed

+271
-248
lines changed

9 files changed

+271
-248
lines changed
 

‎README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
# M5Stack UNIT MQTT
1+
# M5Unit-MQTT
22

33
## Overview
44

55
Contains M5Stack **UNIT MQTT** related case programs. Realize MQTT server connection, subscribe and publish information.
66

77
## Related Link
88

9-
[Document & AT Command](https://docs.m5stack.com/en/unit/mqtt)
9+
[Document & AT Command - M5Unit-MQTT](https://docs.m5stack.com/en/unit/mqtt)
1010

1111
## License
1212

13-
[UNIT MQTT - MIT](LICENSE)
13+
[M5Unit-MQTT - MIT](LICENSE)
1414

‎examples/MQTT/MQTT.ino

-77
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
*******************************************************************************
3+
* Copyright (c) 2021 by M5Stack
4+
* Equipped with M5Atom sample source code
5+
* 配套 M5Atom 示例源代码
6+
* Visit for more information: https://docs.m5stack.com/en/unit/mqtt
7+
* 获取更多资料请访问: https://docs.m5stack.com/zh_CN/unit/mqtt
8+
*
9+
* Product: Unit MQTT.
10+
* Date: 2022/7/7
11+
*******************************************************************************
12+
Connect to M5 MQTT server and subscribe to publish topics
13+
连接至M5 MQTT服务器,并订阅发布话题
14+
*/
15+
#include "M5Atom.h"
16+
#include "M5_MQTT.h"
17+
18+
M5_MQTT unit;
19+
uint32_t chipId = 0;
20+
unsigned long start = 0;
21+
22+
void setup() {
23+
M5.begin(true, false, true);
24+
// INIT UNIT MQTT
25+
unit.Init(&Serial2, 9600, 32, 26);
26+
Serial.println("Waiting LAN Connect");
27+
M5.dis.fillpix(0x0000ff);
28+
while (!unit.isConnectedLAN()) Serial.print('.');
29+
30+
Serial.println("LAN Connected");
31+
Serial.println("Config MQTT");
32+
for (int i = 0; i < 17; i = i + 8) {
33+
chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
34+
}
35+
36+
String id = String(chipId, HEX);
37+
Serial.println("ID: " + id);
38+
39+
unit.configMQTT("mqtt.m5stack.com", // host
40+
"1883", // port
41+
id, // client id
42+
"user" + id, // user name
43+
"pwd", // password
44+
"60" // keepalive
45+
);
46+
47+
Serial.println("Subcribe Topic");
48+
49+
SubscribeTopic topic1 = {
50+
"1", // No 1~-4
51+
"UNIT_MQTT", // Topic
52+
"0" // QoS
53+
};
54+
55+
unit.subscribe(topic1);
56+
Serial.println("Save config and reset");
57+
M5.dis.fillpix(0x5400ff);
58+
unit.configSave();
59+
unit.startMQTT();
60+
Serial.println("Start MQTT Connect");
61+
while (!unit.isConnectedMQTT())
62+
;
63+
Serial.println("MQTT Server Connected");
64+
}
65+
66+
void loop() {
67+
if (unit.isConnectedMQTT()) {
68+
M5.dis.fillpix(0x00ff00);
69+
if (unit.receiveMessage()) {
70+
Serial.println(unit.payload.Topic);
71+
Serial.println(unit.payload.Len);
72+
Serial.println(unit.payload.Data);
73+
}
74+
if (millis() - start > 5000) {
75+
unit.publish({
76+
"UNIT_MQTT_TOPIC_1", // Topic
77+
"Hello UNIT MQTT: " + String(millis()), // Data
78+
"2" // QoS
79+
});
80+
start = millis();
81+
}
82+
} else {
83+
M5.dis.fillpix(0x00ff00);
84+
Serial.print("MQTT disconnect");
85+
}
86+
}

‎library.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "UNIT_MQTT",
2+
"name": "M5Unit-MQTT",
33
"description": "Library for M5Stack UNIT MQTT",
44
"keywords": "MQTT",
55
"authors": {
@@ -8,9 +8,9 @@
88
},
99
"repository": {
1010
"type": "git",
11-
"url": "https://github.com/m5stack/UNIT_MQTT.git"
11+
"url": "https://github.com/m5stack/M5Unit-MQTT.git"
1212
},
1313
"version": "0.0.1",
1414
"frameworks": "arduino",
1515
"platforms": "espressif32"
16-
}
16+
}

‎library.properties

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
name=UNIT_MQTT
1+
name=M5Unit-MQTT
22
version=0.0.1
33
author=M5Stack
44
maintainer=M5Stack
55
sentence=Library for M5Stack UNIT MQTT
6-
paragraph=See more on http://M5Stack.com
6+
paragraph=See more on https://docs.m5stack.com/en/unit/mqtt
77
category=Device Control
8-
url=https://github.com/m5stack/UNIT_MQTT
8+
url=https://github.com/m5stack/M5Unit-MQTT
99
architectures=esp32
10-
includes=UNIT_MQTT.h
10+
includes=M5_MQTT.h

‎src/M5_MQTT.cpp

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#include "M5_MQTT.h"
2+
3+
/*! @brief Initialize the Unit MQTT.*/
4+
void M5_MQTT::Init(HardwareSerial *serial, int baud, uint8_t RX, uint8_t TX) {
5+
_serial = serial;
6+
_serial->begin(baud, SERIAL_8N1, RX, TX);
7+
}
8+
9+
/*! @brief Waiting for a period of time to receive a message
10+
@return Received messages.. */
11+
String M5_MQTT::waitMsg(unsigned long time) {
12+
String restr = "";
13+
unsigned long start = millis();
14+
while (1) {
15+
if (Serial2.available() || (millis() - start) < time) {
16+
String str = Serial2.readString();
17+
restr += str;
18+
} else {
19+
break;
20+
}
21+
}
22+
return restr;
23+
}
24+
25+
/*! @brief Send a message */
26+
void M5_MQTT::sendMsg(String command) {
27+
_serial->print(command);
28+
delay(10);
29+
}
30+
31+
/*! @brief Subscribe to an MQTT topic */
32+
void M5_MQTT::subscribe(SubscribeTopic Topic) {
33+
String readstr;
34+
sendMsg("AT+MQSUBSCRIBE=" + Topic.No + ",1,\"" + Topic.Topic + "\"," +
35+
Topic.QoS + "\r\n");
36+
readstr = waitMsg(200);
37+
}
38+
39+
/*! @brief Send a message in a topic */
40+
void M5_MQTT::publish(PublishTopic Topic) {
41+
String readstr;
42+
sendMsg("AT+MQPUBLISH=\"" + Topic.Topic + "\",\"" + Topic.Data + "\"," +
43+
Topic.QoS + "\r\n");
44+
readstr = waitMsg(200);
45+
}
46+
47+
/*! @brief Start MQTT service */
48+
void M5_MQTT::startMQTT() {
49+
String readstr;
50+
sendMsg("AT+MQSTART\r\n");
51+
}
52+
53+
/*! @brief Check if you are connected to the network.
54+
@return True if connected to the network, false otherwise. */
55+
bool M5_MQTT::isConnectedLAN() {
56+
String readstr;
57+
sendMsg("AT+NETIP?\r\n");
58+
readstr = waitMsg(50);
59+
if (readstr.indexOf("0.0.0.0") == -1 && readstr.indexOf("192.168") != -1) {
60+
return true;
61+
} else {
62+
return false;
63+
}
64+
}
65+
66+
/*! @brief Check if you are connected to the MQTT serve.
67+
@return True if connected to the MQTT serve, false otherwise. */
68+
bool M5_MQTT::isConnectedMQTT() {
69+
String readstr;
70+
sendMsg("AT+MQSTATUS?\r\n");
71+
readstr = waitMsg(100);
72+
if (readstr.indexOf("+MQSTATUS=OK:1") != -1) {
73+
return true;
74+
} else {
75+
return false;
76+
}
77+
}
78+
79+
/*! @brief Check if a message is received.
80+
@return True if the message is received, false otherwise.. */
81+
bool M5_MQTT::receiveMessage() {
82+
String readstr;
83+
readstr = waitMsg(1);
84+
if (readstr.indexOf("+MQRECV:") != -1) {
85+
payload.Topic = readstr.substring(readstr.indexOf("+MQRECV:") + 9,
86+
readstr.indexOf(",") - 1);
87+
payload.Len =
88+
readstr
89+
.substring(readstr.indexOf("\",") + 2, readstr.indexOf(",\""))
90+
.toInt();
91+
payload.Data = readstr.substring(readstr.indexOf(",\"") + 2,
92+
readstr.lastIndexOf("\""));
93+
return true;
94+
} else {
95+
return false;
96+
}
97+
}
98+
99+
/*! @brief Storage Configuration. */
100+
void M5_MQTT::configSave() {
101+
String readstr;
102+
sendMsg("AT+SAVE\r\n");
103+
sendMsg("AT+RESET\r\n");
104+
while (!isConnectedLAN())
105+
;
106+
}
107+
108+
/*! @brief Configuring MQTT serve.*/
109+
void M5_MQTT::configMQTT(String host, String port, String clientId, String user,
110+
String pwd, String keepalive) {
111+
String readstr;
112+
sendMsg("AT+MQSTOP\r\n");
113+
sendMsg("AT+MQCLIENTID=\"" + clientId + "\"\r\n");
114+
sendMsg("AT+MQSERVER=\"" + host + "\"," + port + "\r\n");
115+
sendMsg("AT+MQUSERPWD=\"" + user + "\",\"" + pwd + "\"\r\n");
116+
sendMsg("AT+MQKEEP=" + keepalive + "\r\n");
117+
}

‎src/M5_MQTT.h

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*!
2+
* @brief An Ethernet MQTT communication module From M5Stack
3+
* @copyright Copyright (c) 2022 by M5Stack[https://m5stack.com]
4+
*
5+
* @Links [Unit MQTT](https://docs.m5stack.com/en/unit/mqtt)
6+
* @version V0.0.1
7+
* @date 2022-07-07
8+
*/
9+
#ifndef _M5_MQTT_H_
10+
#define _M5_MQTT_H_
11+
12+
#include <Arduino.h>
13+
14+
#include "pins_arduino.h"
15+
16+
struct SubscribeTopic {
17+
String No;
18+
String Topic;
19+
String QoS;
20+
};
21+
22+
struct PublishTopic {
23+
String Topic;
24+
String Data;
25+
String QoS;
26+
};
27+
28+
struct PayloadTopic {
29+
String Topic;
30+
int Len;
31+
String Data;
32+
};
33+
34+
class M5_MQTT {
35+
private:
36+
HardwareSerial *_serial;
37+
38+
public:
39+
void Init(HardwareSerial *serial = &Serial2, int baud = 9600,
40+
uint8_t RX = 16, uint8_t TX = 17);
41+
bool isConnectedLAN();
42+
void configMQTT(String host = "host", String port = "port",
43+
String clientId = "client id", String user = "user",
44+
String pwd = "pwd", String keepalive = "60");
45+
void subscribe(SubscribeTopic Topic);
46+
void publish(PublishTopic Topic);
47+
void configSave();
48+
bool isConnectedMQTT();
49+
bool receiveMessage();
50+
void startMQTT();
51+
String waitMsg(unsigned long time);
52+
void sendMsg(String command);
53+
54+
public:
55+
PayloadTopic payload;
56+
};
57+
58+
#endif
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Failed to load comments.