|
| 1 | +/* |
| 2 | +******************************************************************************* |
| 3 | +* Copyright (c) 2021 by M5Stack |
| 4 | +* Equipped with M5Core sample source code |
| 5 | +* 配套 M5Core 示例源代码 |
| 6 | +* Visit the website for more |
| 7 | +information:https://docs.m5stack.com/en/unit/nbiot_global |
| 8 | +* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/unit/nbiot_global |
| 9 | +* |
| 10 | +* describe:NBIoT UNIT. |
| 11 | +* date:2022/01/04 |
| 12 | +* Dependent library download: |
| 13 | +* TinyGSM: |
| 14 | +https://m5stack.oss-cn-shenzhen.aliyuncs.com/resource/arduino/lib/TinyGSM.zip |
| 15 | +******************************************************************************* |
| 16 | + Connect NBIoT UNIT to PORT C port (G16/17), connect to MQTT server for data |
| 17 | +sending and receiving test. Before compiling, please install and visit the link |
| 18 | +above to download the TinyGSM dependency library. 将NBIoT UNIT连接到PORT |
| 19 | +C端口(G16/17), |
| 20 | +连接MQTT服务器进行数据收发测试。编译前,请安装访问上方链接,下载TinyGSM依赖库。 |
| 21 | +*/ |
| 22 | + |
| 23 | +#include "M5Stack.h" |
| 24 | +#include "M5GFX.h" |
| 25 | + |
| 26 | + |
| 27 | +#define TINY_GSM_MODEM_SIM7020 |
| 28 | + |
| 29 | +#include <PubSubClient.h> |
| 30 | +#include <TinyGsmClient.h> |
| 31 | +#include <sys/time.h> |
| 32 | +#include <time.h> |
| 33 | + |
| 34 | + |
| 35 | +#define SerialAT Serial1 |
| 36 | +#define SIM7020_BAUDRATE 115200 |
| 37 | + |
| 38 | +#define UNIT_SIM7020_TX 17 |
| 39 | +#define UNIT_SIM7020_RX 16 |
| 40 | + |
| 41 | +#define MQTT_BROKER "mqtt.m5stack.com" |
| 42 | +#define MQTT_PORT 1883 |
| 43 | +#define MQTT_USERNAME "UNIT_NBIoT_USER_NAME" |
| 44 | +#define MQTT_PASSWORD "UNIT_NBIoT_PASSWORD" |
| 45 | +#define MQTT_SYNC_TIME_D_TOPIC "UNIT_NBIoT/D" |
| 46 | +#define MQTT_SYNC_TIME_U_TOPIC "UNIT_NBIoT/U" |
| 47 | + |
| 48 | +#define UPLOAD_INTERVAL 10000 |
| 49 | + |
| 50 | +TinyGsm modem(SerialAT, -1); |
| 51 | + |
| 52 | +TinyGsmClient tcpClient(modem); |
| 53 | +PubSubClient mqttClient(MQTT_BROKER, MQTT_PORT, tcpClient); |
| 54 | + |
| 55 | +void mqttCallback(char *topic, byte *payload, unsigned int len); |
| 56 | +void mqttConnect(void); |
| 57 | +void nbConnect(void); |
| 58 | + |
| 59 | +struct tm now; |
| 60 | +char s_time[50]; |
| 61 | + |
| 62 | +M5GFX display; |
| 63 | +M5Canvas canvas(&display); |
| 64 | + |
| 65 | +void log(String info) { |
| 66 | + canvas.println(info); |
| 67 | + canvas.pushSprite(0, 0); |
| 68 | + Serial.println(info); |
| 69 | +} |
| 70 | + |
| 71 | +void setup() { |
| 72 | + M5.begin(); |
| 73 | + display.begin(); |
| 74 | + canvas.setColorDepth(1); // mono color |
| 75 | + canvas.setFont(&fonts::efontCN_14); |
| 76 | + canvas.createSprite(display.width(), display.height()); |
| 77 | + canvas.setTextSize(2); |
| 78 | + canvas.setPaletteColor(1, GREEN); |
| 79 | + canvas.setTextScroll(true); |
| 80 | + canvas.println(">>UNIT NBIoT"); |
| 81 | + canvas.println(">>MQTT TEST"); |
| 82 | + canvas.pushSprite(0, 0); |
| 83 | + SerialAT.begin(SIM7020_BAUDRATE, SERIAL_8N1, UNIT_SIM7020_RX, |
| 84 | + UNIT_SIM7020_TX); |
| 85 | + nbConnect(); |
| 86 | + mqttClient.setBufferSize(4096); |
| 87 | + mqttClient.setCallback(mqttCallback); |
| 88 | + mqttClient.setKeepAlive(300); |
| 89 | +} |
| 90 | + |
| 91 | +void loop() { |
| 92 | + static unsigned long timer = 0; |
| 93 | + |
| 94 | + if (!mqttClient.connected()) { |
| 95 | + if (!modem.isNetworkConnected()) { |
| 96 | + nbConnect(); |
| 97 | + } |
| 98 | + log(">>MQTT NOT CONNECTED"); |
| 99 | + mqttConnect(); |
| 100 | + } |
| 101 | + |
| 102 | + if (millis() >= timer) { |
| 103 | + timer = millis() + UPLOAD_INTERVAL; |
| 104 | + mqttClient.publish(MQTT_SYNC_TIME_U_TOPIC, "hello"); // 发送数据 |
| 105 | + } |
| 106 | + |
| 107 | + mqttClient.loop(); |
| 108 | +} |
| 109 | + |
| 110 | +void mqttCallback(char *topic, byte *payload, unsigned int len) { |
| 111 | + log("Message arrived [" + String(topic) + "]: "); |
| 112 | + String payload_str = String((char *)payload); |
| 113 | + log(payload_str); |
| 114 | +} |
| 115 | + |
| 116 | +void mqttConnect(void) { |
| 117 | + log("Connecting to " + String(MQTT_BROKER) + "..."); |
| 118 | + |
| 119 | + // Connect to MQTT Broker |
| 120 | + String mqttid = ("MQTTID_" + String(random(65536))); |
| 121 | + while (!mqttClient.connect(mqttid.c_str(), MQTT_USERNAME, MQTT_PASSWORD)) { |
| 122 | + log(" fail"); |
| 123 | + } |
| 124 | + log(" success"); |
| 125 | + mqttClient.subscribe(MQTT_SYNC_TIME_D_TOPIC); |
| 126 | + while (!mqttClient.publish(MQTT_SYNC_TIME_U_TOPIC, "Device Online")) { |
| 127 | + delay(1000); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +void nbConnect(void) { |
| 132 | + unsigned long start = millis(); |
| 133 | + log("Initializing modem..."); |
| 134 | + while (!modem.init()) { |
| 135 | + log("waiting...." + String((millis() - start) / 1000) + "s"); |
| 136 | + }; |
| 137 | + |
| 138 | + start = millis(); |
| 139 | + log("Waiting for network..."); |
| 140 | + while (!modem.waitForNetwork()) { |
| 141 | + log("waiting...." + String((millis() - start) / 1000) + "s"); |
| 142 | + } |
| 143 | + log("success"); |
| 144 | +} |
0 commit comments