Skip to content

Commit dfb7286

Browse files
committed
add CATM+GNSS 7080G examples
1 parent 195e991 commit dfb7286

File tree

3 files changed

+574
-0
lines changed

3 files changed

+574
-0
lines changed
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
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/unit/catm_gnss
8+
* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/module/catm_gnss
9+
*
10+
* describe: catm_gnss.
11+
* date:2022/03/03
12+
*******************************************************************************
13+
This case will use UNIT CATM+GNSS combined with M5Core
14+
Connect to the CoAP server and send a request.
15+
Before use, connect the UNIT CATM+GNSS to PORT C(G16/17)
16+
Libraries:
17+
- [TinyGSM](https://github.com/vshymanskyy/TinyGSM)
18+
*/
19+
20+
#include <M5Stack.h>
21+
#include "M5GFX.h"
22+
23+
#define TINY_GSM_MODEM_SIM7080
24+
25+
// Set serial for debug console (to the Serial Monitor, default speed 115200)
26+
#define SerialMon Serial
27+
#define SerialAT Serial2
28+
29+
uint32_t lastReconnectAttempt = 0;
30+
31+
#define TINY_GSM_RX_BUFFER 650
32+
33+
#define TINY_GSM_DEBUG SerialMon
34+
35+
#define MODULE_BAUD 115200
36+
37+
// Your GPRS credentials, if any
38+
const char apn[] = "YourAPN";
39+
const char gprsUser[] = "";
40+
const char gprsPass[] = "";
41+
42+
#include <TinyGsmClient.h>
43+
44+
TinyGsm modem(SerialAT);
45+
TinyGsmClient client(modem);
46+
47+
M5GFX display;
48+
M5Canvas canvas(&display);
49+
50+
unsigned long start;
51+
52+
typedef enum { GET = 1, POST, PUT, DELETE } coap_method_t;
53+
54+
inline String time() {
55+
return "..." + String((millis() - start) / 1000) + 's';
56+
}
57+
58+
void log(String info) {
59+
SerialMon.println(info);
60+
canvas.println(info);
61+
canvas.pushSprite(0, 0);
62+
}
63+
64+
String waitMsg(unsigned long time) {
65+
String restr = "";
66+
unsigned long start = millis();
67+
while (1) {
68+
if (SerialAT.available() || (millis() - start) < time) {
69+
String str = SerialAT.readString();
70+
restr += str;
71+
} else {
72+
break;
73+
}
74+
}
75+
return restr;
76+
}
77+
78+
bool craeteCoAPPacket(String url, String url_query, coap_method_t method,
79+
String payload) {
80+
log("Create CoAPPacket");
81+
// SerialAT.println("AT+CCOAPPARA=\"CODE\",4,uri-path,0,\"/m5stack-get\",uri-query,0,\"catm_test=1\",payload,0,\"hello
82+
// world\"");
83+
SerialAT.println("AT+CCOAPPARA=\"CODE\"," + String(method) +
84+
",uri-path,0,\"" + url + "\",uri-query,0,\"" + url_query +
85+
"\",payload,0,\"" + payload + "\"");
86+
if (waitMsg(2000).indexOf("OK") != -1) {
87+
return true;
88+
} else {
89+
return false;
90+
}
91+
}
92+
93+
bool connectCoAP(String ip, String port) {
94+
SerialAT.println("AT+CNACT=0,0");
95+
log(waitMsg(500));
96+
SerialAT.println("AT+CNACT=0,1");
97+
log(waitMsg(500));
98+
SerialAT.println("AT+CCOAPINIT");
99+
log(waitMsg(500));
100+
SerialAT.println("AT+CCOAPURL=\"coap://120.77.157.90:5683\"");
101+
String result = waitMsg(1000);
102+
log(result);
103+
if (result.indexOf("ERROR") != -1) {
104+
SerialAT.println("AT+CCOAPTERM");
105+
String result = waitMsg(1000);
106+
return false;
107+
} else {
108+
return true;
109+
}
110+
}
111+
112+
String sendCoAPRequest() {
113+
SerialAT.println("AT+CCOAPACTION");
114+
String result = waitMsg(5000);
115+
log(result);
116+
if (result.indexOf("+CCOAPRECV") != -1) {
117+
String str1 = result.substring(result.indexOf("+CCOAPRECV") + 11);
118+
String str2 = str1.substring(1, str1.indexOf(","));
119+
return str2;
120+
} else {
121+
return "ERROR";
122+
}
123+
}
124+
125+
String getCoAPreceive(String msgid) {
126+
SerialAT.println("AT+CCOAPACTION=4");
127+
log(waitMsg(2000));
128+
// Read the packet header with messageidof 1and print
129+
SerialAT.println("AT+CCOAPHEAD=" + msgid + ",1");
130+
log(waitMsg(2000));
131+
// Read the receive packet payload with messageid of 1
132+
SerialAT.println("AT+CCOAPREAD=" + msgid);
133+
log(waitMsg(2000));
134+
}
135+
136+
void setup() {
137+
M5.begin();
138+
display.begin();
139+
start = millis();
140+
canvas.setColorDepth(1); // mono color
141+
canvas.setFont(&fonts::efontCN_14);
142+
canvas.createSprite(display.width(), display.height());
143+
canvas.setTextSize(2);
144+
canvas.setPaletteColor(1, GREEN);
145+
canvas.setTextScroll(true);
146+
147+
log("Initializing modem..." + time());
148+
149+
// Set GSM module baud rate
150+
if (TinyGsmAutoBaud(SerialAT, MODULE_BAUD, MODULE_BAUD) == 0) {
151+
log("UART connect error" + time());
152+
}
153+
// modem.restart();
154+
modem.init();
155+
String modemInfo = modem.getModemInfo();
156+
log("Modem Info: ");
157+
log(modemInfo + time());
158+
while (!modem.getSimStatus()) {
159+
log("not sim card" + time());
160+
}
161+
}
162+
163+
void loop() {
164+
log("Waiting for network...." + time());
165+
if (!modem.waitForNetwork()) {
166+
log("fail" + time());
167+
log(waitMsg(1000));
168+
169+
return;
170+
}
171+
if (modem.isNetworkConnected()) {
172+
log("Network connected" + time());
173+
}
174+
175+
IPAddress local = modem.localIP();
176+
log("REMOTE IP: " + local.toString());
177+
178+
int csq = modem.getSignalQuality();
179+
log("RSSI:" + String(csq) + time());
180+
log("IP:" + local.toString() + time());
181+
182+
if (connectCoAP("120.77.157.90", "5683")) {
183+
log("connect successful, click button a to send request");
184+
}
185+
while (1) {
186+
M5.update();
187+
if (M5.BtnA.wasPressed()) {
188+
craeteCoAPPacket("/m5stack-get", "catm_test=1", GET, "hello world");
189+
String msgid = sendCoAPRequest();
190+
log(msgid);
191+
if (msgid != "ERROR") {
192+
getCoAPreceive(msgid);
193+
}
194+
}
195+
String result = waitMsg(0);
196+
if (result != "") {
197+
log(result);
198+
}
199+
}
200+
}
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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/unit/catm_gnss
8+
* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/module/catm_gnss
9+
*
10+
* describe: catm_gnss.
11+
* date:2022/03/03
12+
*******************************************************************************
13+
This case will use UNIT CATM+GNSS combined with M5Core
14+
Obtain positioning information through GNSS
15+
Before use, connect the UNIT CATM+GNSS to PORT C(G16/17)
16+
Libraries:
17+
- [TinyGSM](https://github.com/vshymanskyy/TinyGSM)
18+
*/
19+
20+
#include <M5Stack.h>
21+
#include "M5GFX.h"
22+
23+
#define TINY_GSM_MODEM_SIM7080
24+
25+
// Set serial for debug console (to the Serial Monitor, default speed 115200)
26+
#define SerialMon Serial
27+
#define SerialAT Serial2
28+
29+
uint32_t lastReconnectAttempt = 0;
30+
31+
#define TINY_GSM_RX_BUFFER 650
32+
33+
#define TINY_GSM_DEBUG SerialMon
34+
35+
#define MODULE_BAUD 115200
36+
37+
// Your GPRS credentials, if any
38+
const char apn[] = "YourAPN";
39+
const char gprsUser[] = "";
40+
const char gprsPass[] = "";
41+
42+
#include <TinyGsmClient.h>
43+
#include <PubSubClient.h>
44+
45+
TinyGsm modem(SerialAT);
46+
TinyGsmClient client(modem);
47+
48+
M5GFX display;
49+
M5Canvas canvas(&display);
50+
51+
const char* broker = "mqtt.m5stack.com";
52+
53+
const char* topic_up = "cat1/up";
54+
const char* topic_down = "cat1/down";
55+
56+
PubSubClient mqtt(client);
57+
58+
unsigned long start;
59+
60+
inline String time() {
61+
return "..." + String((millis() - start) / 1000) + 's';
62+
}
63+
64+
void log(String info) {
65+
SerialMon.println(info);
66+
canvas.println(info);
67+
canvas.pushSprite(0, 0);
68+
}
69+
70+
void mqttCallback(char* topic, byte* payload, unsigned int len) {
71+
log("Message arrived :");
72+
log(topic);
73+
log("payload: ");
74+
char _payload[len];
75+
memcpy(_payload, payload, len);
76+
_payload[len] = '\0';
77+
log(_payload);
78+
}
79+
80+
boolean mqttConnect() {
81+
log("Connecting to ");
82+
log(broker);
83+
84+
// Connect to MQTT Broker
85+
boolean status = mqtt.connect("GsmClientTest");
86+
87+
// Or, if you want to authenticate MQTT:
88+
// boolean status = mqtt.connect("GsmClientName", "mqtt_user", "mqtt_pass");
89+
90+
if (status == false) {
91+
SerialMon.println(" fail");
92+
return false;
93+
}
94+
SerialMon.println(" success");
95+
mqtt.publish(topic_up, "GsmClientTest started");
96+
mqtt.subscribe(topic_down);
97+
log("Subscribe Topic: " + String(topic_down));
98+
return mqtt.connected();
99+
}
100+
101+
void setup() {
102+
M5.begin();
103+
display.begin();
104+
start = millis();
105+
canvas.setColorDepth(1); // mono color
106+
canvas.setFont(&fonts::efontCN_14);
107+
canvas.createSprite(display.width(), display.height());
108+
canvas.setTextSize(2);
109+
canvas.setPaletteColor(1, GREEN);
110+
canvas.setTextScroll(true);
111+
112+
log("Initializing modem..." + time());
113+
114+
// Set GSM module baud rate
115+
if (TinyGsmAutoBaud(SerialAT, MODULE_BAUD, MODULE_BAUD) == 0) {
116+
log("UART connect error" + time());
117+
}
118+
// modem.restart();
119+
modem.init();
120+
String modemInfo = modem.getModemInfo();
121+
log("Modem Info: ");
122+
log(modemInfo + time());
123+
}
124+
125+
bool gps_flag = false;
126+
127+
void loop() {
128+
canvas.println("INIT GNSS" + time());
129+
canvas.pushSprite(0, 0);
130+
while (1) {
131+
DBG("Enabling GPS/GNSS/GLONASS and waiting 15s for warm-up");
132+
modem.enableGPS();
133+
delay(15000L);
134+
float lat2 = 0;
135+
float lon2 = 0;
136+
float speed2 = 0;
137+
float alt2 = 0;
138+
int vsat2 = 0;
139+
int usat2 = 0;
140+
float accuracy2 = 0;
141+
int year2 = 0;
142+
int month2 = 0;
143+
int day2 = 0;
144+
int hour2 = 0;
145+
int min2 = 0;
146+
int sec2 = 0;
147+
for (int8_t i = 15; i; i--) {
148+
DBG("Requesting current GPS/GNSS/GLONASS location");
149+
if (modem.getGPS(&lat2, &lon2, &speed2, &alt2, &vsat2, &usat2,
150+
&accuracy2, &year2, &month2, &day2, &hour2, &min2,
151+
&sec2)) {
152+
log("Latitude:" + String(lat2, 8) +
153+
"\tLongitude:" + String(lon2, 8));
154+
log("Speed:" + String(speed2) + "\tAltitude:" + String(alt2));
155+
log("Visible Satellites:" + String(vsat2) +
156+
"\tUsed Satellites:" + String(usat2));
157+
log("Accuracy:" + String(accuracy2));
158+
log("Year:" + String(year2) + "\tMonth:" + String(month2) +
159+
"\tDay:" + String(day2));
160+
log("Hour:" + String(hour2) + "\tMinute:" + String(min2) +
161+
"\tSecond:" + String(sec2));
162+
break;
163+
} else {
164+
log("Get GPS/GNSS/GLONASS location.." + time());
165+
DBG("Couldn't get GPS/GNSS/GLONASS location, retrying in 10s.");
166+
delay(10000L);
167+
}
168+
}
169+
// modem.disableGPS();
170+
}
171+
while (1) {
172+
delay(100);
173+
}
174+
}

0 commit comments

Comments
 (0)