|
| 1 | +/* |
| 2 | +******************************************************************************* |
| 3 | +* Copyright (c) 2022 by M5Stack |
| 4 | +* Equipped with M5Core sample source code |
| 5 | +* 配套 M5Core 示例源代码 |
| 6 | +* Visit the website for more |
| 7 | +information:https://docs.m5stack.com/en/module/comx_cat1 |
| 8 | +* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/module/comx_cat1 |
| 9 | +* |
| 10 | +* describe: comx_cat1. |
| 11 | +* date:2022/01/11 |
| 12 | +******************************************************************************* |
| 13 | +This case will use COM.CAT1 Module combined with M5Core to implement MQTT |
| 14 | +Client. After successfully connecting to MQTT, press button B to realize data |
| 15 | +publishing. Before use, adjust the DIP switch of the module base to G16/17 ON |
| 16 | +Libraries: |
| 17 | +- [TinyGSM](https://github.com/vshymanskyy/TinyGSM) |
| 18 | +- [PubSubClient](https://github.com/knolleary/pubsubclient.git) |
| 19 | +*/ |
| 20 | + |
| 21 | +#include <M5Stack.h> |
| 22 | +#include "M5GFX.h" |
| 23 | + |
| 24 | +// Compatible with SIM76XX series. |
| 25 | +#define TINY_GSM_MODEM_SIM7600 |
| 26 | + |
| 27 | +// Set serial for debug console (to the Serial Monitor, default speed 115200) |
| 28 | +#define SerialMon Serial |
| 29 | +#define SerialAT Serial2 |
| 30 | + |
| 31 | +uint32_t lastReconnectAttempt = 0; |
| 32 | + |
| 33 | +#define TINY_GSM_RX_BUFFER 650 |
| 34 | + |
| 35 | +#define TINY_GSM_DEBUG SerialMon |
| 36 | + |
| 37 | +#define MODULE_BAUD 115200 |
| 38 | + |
| 39 | +// Your GPRS credentials, if any |
| 40 | +const char apn[] = "YourAPN"; |
| 41 | +const char gprsUser[] = ""; |
| 42 | +const char gprsPass[] = ""; |
| 43 | + |
| 44 | +#include <TinyGsmClient.h> |
| 45 | +#include <PubSubClient.h> |
| 46 | + |
| 47 | +TinyGsm modem(SerialAT); |
| 48 | +TinyGsmClient client(modem); |
| 49 | + |
| 50 | +M5GFX display; |
| 51 | +M5Canvas canvas(&display); |
| 52 | + |
| 53 | +const char* broker = "mqtt.m5stack.com"; |
| 54 | + |
| 55 | +const char* topic_up = "cat1/up"; |
| 56 | +const char* topic_down = "cat1/down"; |
| 57 | + |
| 58 | +PubSubClient mqtt(client); |
| 59 | + |
| 60 | +unsigned long start; |
| 61 | + |
| 62 | +inline String time() { |
| 63 | + return "..." + String((millis() - start) / 1000) + 's'; |
| 64 | +} |
| 65 | + |
| 66 | +void log(String info) { |
| 67 | + SerialMon.println(info); |
| 68 | + canvas.println(info); |
| 69 | + canvas.pushSprite(0, 0); |
| 70 | +} |
| 71 | + |
| 72 | +void mqttCallback(char* topic, byte* payload, unsigned int len) { |
| 73 | + log("Message arrived :"); |
| 74 | + log(topic); |
| 75 | + log("payload: "); |
| 76 | + char _payload[len]; |
| 77 | + memcpy(_payload, payload, len); |
| 78 | + _payload[len] = '\0'; |
| 79 | + log(_payload); |
| 80 | +} |
| 81 | + |
| 82 | +boolean mqttConnect() { |
| 83 | + log("Connecting to "); |
| 84 | + log(broker); |
| 85 | + |
| 86 | + // Connect to MQTT Broker |
| 87 | + boolean status = mqtt.connect("GsmClientTest"); |
| 88 | + |
| 89 | + // Or, if you want to authenticate MQTT: |
| 90 | + // boolean status = mqtt.connect("GsmClientName", "mqtt_user", "mqtt_pass"); |
| 91 | + |
| 92 | + if (status == false) { |
| 93 | + SerialMon.println(" fail"); |
| 94 | + return false; |
| 95 | + } |
| 96 | + SerialMon.println(" success"); |
| 97 | + mqtt.publish(topic_up, "GsmClientTest started"); |
| 98 | + mqtt.subscribe(topic_down); |
| 99 | + log("Subscribe Topic: " + String(topic_down)); |
| 100 | + return mqtt.connected(); |
| 101 | +} |
| 102 | + |
| 103 | +void setup() { |
| 104 | + M5.begin(); |
| 105 | + display.begin(); |
| 106 | + start = millis(); |
| 107 | + canvas.setColorDepth(1); // mono color |
| 108 | + canvas.setFont(&fonts::efontCN_14); |
| 109 | + canvas.createSprite(display.width(), display.height()); |
| 110 | + canvas.setTextSize(2); |
| 111 | + canvas.setPaletteColor(1, GREEN); |
| 112 | + canvas.setTextScroll(true); |
| 113 | + |
| 114 | + log("Initializing modem..." + time()); |
| 115 | + |
| 116 | + // Set GSM module baud rate |
| 117 | + if (TinyGsmAutoBaud(SerialAT, MODULE_BAUD, MODULE_BAUD) == 0) { |
| 118 | + log("UART connect error" + time()); |
| 119 | + } |
| 120 | + // modem.restart(); |
| 121 | + modem.init(); |
| 122 | + String modemInfo = modem.getModemInfo(); |
| 123 | + log("Modem Info: "); |
| 124 | + log(modemInfo + time()); |
| 125 | + while (!modem.getSimStatus()) { |
| 126 | + log("not sim card" + time()); |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +void loop() { |
| 131 | + log("Waiting for network...." + time()); |
| 132 | + if (!modem.waitForNetwork()) { |
| 133 | + log("fail" + time()); |
| 134 | + delay(10000); |
| 135 | + return; |
| 136 | + } |
| 137 | + if (modem.isNetworkConnected()) { |
| 138 | + log("Network connected" + time()); |
| 139 | + } |
| 140 | + log("GPRS connect..." + time()); |
| 141 | + if (!modem.gprsConnect(apn, gprsUser, gprsPass)) { |
| 142 | + log("fail"); |
| 143 | + delay(10000); |
| 144 | + return; |
| 145 | + } |
| 146 | + if (modem.isGprsConnected()) { |
| 147 | + log("GPRS connected"); |
| 148 | + } |
| 149 | + |
| 150 | + String ccid = modem.getSimCCID(); |
| 151 | + log("CCID: "); |
| 152 | + log(ccid); |
| 153 | + |
| 154 | + String imei = modem.getIMEI(); |
| 155 | + log("IMEI: " + imei); |
| 156 | + |
| 157 | + String imsi = modem.getIMSI(); |
| 158 | + log("IMSI: " + imsi); |
| 159 | + |
| 160 | + String cop = modem.getOperator(); |
| 161 | + log("Operator: " + cop); |
| 162 | + |
| 163 | + IPAddress local = modem.localIP(); |
| 164 | + log("REMOTE IP: " + local.toString()); |
| 165 | + |
| 166 | + int csq = modem.getSignalQuality(); |
| 167 | + log("RSSI:" + String(csq) + time()); |
| 168 | + log("IP:" + local.toString() + time()); |
| 169 | + |
| 170 | + // MQTT Broker setup |
| 171 | + mqtt.setServer(broker, 1883); |
| 172 | + mqtt.setCallback(mqttCallback); |
| 173 | + |
| 174 | + while (true) { |
| 175 | + M5.update(); |
| 176 | + if (!mqtt.connected()) { |
| 177 | + log("=== MQTT NOT CONNECTED ==="); |
| 178 | + // Reconnect every 10 seconds |
| 179 | + uint32_t t = millis(); |
| 180 | + if (t - lastReconnectAttempt > 3000L) { |
| 181 | + lastReconnectAttempt = t; |
| 182 | + if (mqttConnect()) { |
| 183 | + lastReconnectAttempt = 0; |
| 184 | + log("mqtt.m5stack.com" + time()); |
| 185 | + log("MQTT Connected!" + time()); |
| 186 | + log("Press Btn B to Publish"); |
| 187 | + log("Topic: " + String(topic_up)); |
| 188 | + } |
| 189 | + } |
| 190 | + delay(100); |
| 191 | + } else { |
| 192 | + mqtt.loop(); |
| 193 | + M5.update(); |
| 194 | + if (M5.BtnB.wasPressed()) { |
| 195 | + log("Publish:" + String(topic_up)); |
| 196 | + mqtt.publish(topic_up, "Hello From COM.X CAT1"); |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | +} |
0 commit comments