#define TINY_GSM_MODEM_SIM7080
#define TINY_GSM_RX_BUFFER 650
#define TINY_GSM_YIELD() { delay(2); }
#include<TinyGsmClient.h>#include<PubSubClient.h>#include<M5Stack.h>constcharapn[]="povo.jp";constchar*broker="XXX.com";// your mqtt server
constchar*topicTest="test";constchar*topicTest2="test2";#define GSM_AUTOBAUD_MIN 9600
#define GSM_AUTOBAUD_MAX 115200
// TinyGsmClient
TinyGsmmodem(Serial1);TinyGsmClientclient(modem);PubSubClientmqtt(client);uint32_tlastReconnectAttempt=0;voidmqttCallback(char*topic,byte*payload,unsignedintlen){M5.Lcd.clear(BLACK);M5.Lcd.setCursor(0,0);M5.Lcd.print("Message arrived [");M5.Lcd.print(topic);M5.Lcd.println("]: ");M5.Lcd.print("bin:");for(inti=0;i<len;i++){M5.Lcd.print(payload[i]);}M5.Lcd.println();charpayloadBuf[256]={0};strncpy(payloadBuf,(constchar*)payload,len);Stringstr=String(payloadBuf);M5.Lcd.print("str:");M5.Lcd.println(str);M5.Lcd.print("Message send to ");M5.Lcd.println(topicTest2);M5.Lcd.println(str);charbuf[256];str.toCharArray(buf,256);mqtt.publish(topicTest2,buf);}booleanmqttConnect(){M5.Lcd.print("Connecting to ");M5.Lcd.println(broker);booleanstatus=mqtt.connect("GsmClientTest");if(status==false){M5.Lcd.println(" fail");returnfalse;}M5.Lcd.println(" success");mqtt.subscribe(topicTest);returnmqtt.connected();}voidsetup(){M5.begin();//Init M5Core. 初始化 M5Core
M5.Lcd.setTextSize(2);M5.Power.begin();//Init Power module. 初始化电源模块
/* Power chip connected to gpio21, gpio22, I2C device
Set battery charging voltage and current
If used battery, please call this function in your project */Serial1.begin(9600,SERIAL_8N1,16,17);M5.Lcd.println("Wait...");// Print text on the screen (string) 在屏幕上打印文本(字符串)
// モデムのリスタート
M5.Lcd.println("Initializing modem...");// Print text on the screen (string) 在屏幕上打印文本(字符串)
modem.restart();// モデムの情報の取得
StringmodemInfo=modem.getModemInfo();M5.Lcd.print("Modem Info: ");M5.Lcd.println(modemInfo);M5.Lcd.print(F("Connecting to "));M5.Lcd.print(apn);if(!modem.gprsConnect(apn,"","")){M5.Lcd.println("-> fail");delay(10000);return;}M5.Lcd.println("-> success");if(modem.isGprsConnected()){M5.Lcd.println("GPRS connected");}mqtt.setServer(broker,1883);mqtt.setCallback(mqttCallback);mqtt.publish(topicTest2,"Hello Server! I'm m5stack");}voidloop(){if(!modem.isNetworkConnected()){M5.Lcd.println("Network disconnected");if(!modem.waitForNetwork(180000L,true)){M5.Lcd.println(" fail");delay(10000);return;}if(modem.isNetworkConnected()){M5.Lcd.println("Network re-connected");}// and make sure GPRS/EPS is still connected
if(!modem.isGprsConnected()){M5.Lcd.println("GPRS disconnected!");M5.Lcd.print(F("Connecting to "));M5.Lcd.print(apn);if(!modem.gprsConnect(apn,"","")){M5.Lcd.println(" fail");delay(10000);return;}if(modem.isGprsConnected()){M5.Lcd.println("GPRS reconnected");}}}if(!mqtt.connected()){M5.Lcd.println("=== MQTT NOT CONNECTED ===");// Reconnect every 10 seconds
uint32_tt=millis();if(t-lastReconnectAttempt>10000L){lastReconnectAttempt=t;if(mqttConnect()){lastReconnectAttempt=0;}}delay(100);return;}mqtt.loop();}