-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
447 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,236 @@ | ||
/** | ||
* @file MqttsBuiltlnSSL.ino | ||
* @author Lewis He (lewishe@outlook.com) | ||
* @license MIT | ||
* @copyright Copyright (c) 2023 Shenzhen Xin Yuan Electronic Technology Co., Ltd | ||
* @date 2023-11-28 | ||
* @note | ||
* * * Example is suitable for A7670X/A7608X series, SIM7672 is not adapted. | ||
* * Connect MQTT Broker as https://test.mosquitto.org/ MQTT, encrypted, unauthenticated | ||
* * Example uses a forked TinyGSM <https://github.com/lewisxhe/TinyGSM>, which will not compile successfully using the mainline TinyGSM. | ||
*/ | ||
#define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb | ||
|
||
// See all AT commands, if wanted | ||
#define DUMP_AT_COMMANDS | ||
|
||
#include "utilities.h" | ||
#include <TinyGsmClient.h> | ||
|
||
#ifdef DUMP_AT_COMMANDS // if enabled it requires the streamDebugger lib | ||
#include <StreamDebugger.h> | ||
StreamDebugger debugger(SerialAT, Serial); | ||
TinyGsm modem(debugger); | ||
#else | ||
TinyGsm modem(SerialAT); | ||
#endif | ||
|
||
|
||
// MQTT details | ||
const char *broker = "test.mosquitto.org"; | ||
const uint16_t broker_port = 8883; | ||
const char *clien_id = "A76XX"; | ||
|
||
const char *subscribe_topic = "GsmMqttTest/subscribe"; | ||
const char *publish_topic = "GsmMqttTest/publish"; | ||
|
||
// Current connection index, range 0~1 | ||
const uint8_t mqtt_client_id = 0; | ||
uint32_t check_connect_millis = 0; | ||
|
||
void mqtt_callback(const char *topic, const uint8_t *payload, uint32_t len) | ||
{ | ||
Serial.println(); | ||
Serial.println("======mqtt_callback======"); | ||
Serial.print("Topic:"); Serial.println(topic); | ||
Serial.println("Payload:"); | ||
for (int i = 0; i < len; ++i) { | ||
Serial.print(payload[i], HEX); Serial.print(","); | ||
} | ||
Serial.println(); | ||
Serial.println("========================="); | ||
} | ||
|
||
bool mqtt_connect() | ||
{ | ||
Serial.print("Connecting to "); | ||
Serial.print(broker); | ||
|
||
bool ret = modem.mqtt_connect(mqtt_client_id, broker, broker_port, clien_id); | ||
if (!ret) { | ||
Serial.println("Failed!"); return false; | ||
} | ||
Serial.println("successed."); | ||
|
||
if (modem.mqtt_connected()) { | ||
Serial.println("MQTT has connected!"); | ||
} else { | ||
return false; | ||
} | ||
// Set MQTT processing callback | ||
modem.mqtt_set_callback(mqtt_callback); | ||
// Subscribe to topic | ||
modem.mqtt_subscribe(mqtt_client_id, subscribe_topic); | ||
|
||
return true; | ||
} | ||
|
||
|
||
void setup() | ||
{ | ||
Serial.begin(115200); // Set console baud rate | ||
|
||
Serial.println("Start Sketch"); | ||
|
||
SerialAT.begin(115200, SERIAL_8N1, MODEM_RX_PIN, MODEM_TX_PIN); | ||
|
||
#ifdef BOARD_POWERON_PIN | ||
pinMode(BOARD_POWERON_PIN, OUTPUT); | ||
digitalWrite(BOARD_POWERON_PIN, HIGH); | ||
#endif | ||
|
||
// Set modem reset pin ,reset modem | ||
pinMode(MODEM_RESET_PIN, OUTPUT); | ||
digitalWrite(MODEM_RESET_PIN, !MODEM_RESET_LEVEL); delay(100); | ||
digitalWrite(MODEM_RESET_PIN, MODEM_RESET_LEVEL); delay(2600); | ||
digitalWrite(MODEM_RESET_PIN, !MODEM_RESET_LEVEL); | ||
|
||
pinMode(BOARD_PWRKEY_PIN, OUTPUT); | ||
digitalWrite(BOARD_PWRKEY_PIN, LOW); | ||
delay(100); | ||
digitalWrite(BOARD_PWRKEY_PIN, HIGH); | ||
delay(100); | ||
digitalWrite(BOARD_PWRKEY_PIN, LOW); | ||
|
||
// Check if the modem is online | ||
Serial.println("Start modem..."); | ||
|
||
int retry = 0; | ||
while (!modem.testAT(1000)) { | ||
Serial.println("."); | ||
if (retry++ > 10) { | ||
digitalWrite(BOARD_PWRKEY_PIN, LOW); | ||
delay(100); | ||
digitalWrite(BOARD_PWRKEY_PIN, HIGH); | ||
delay(1000); | ||
digitalWrite(BOARD_PWRKEY_PIN, LOW); | ||
retry = 0; | ||
} | ||
} | ||
Serial.println(); | ||
|
||
// Check if SIM card is online | ||
SimStatus sim = SIM_ERROR; | ||
while (sim != SIM_READY) { | ||
sim = modem.getSimStatus(); | ||
switch (sim) { | ||
case SIM_READY: | ||
Serial.println("SIM card online"); | ||
break; | ||
case SIM_LOCKED: | ||
Serial.println("The SIM card is locked. Please unlock the SIM card first."); | ||
// const char *SIMCARD_PIN_CODE = "123456"; | ||
// modem.simUnlock(SIMCARD_PIN_CODE); | ||
break; | ||
default: | ||
break; | ||
} | ||
delay(1000); | ||
} | ||
|
||
//SIM7672G Can't set network mode | ||
#ifndef TINY_GSM_MODEM_SIM7672 | ||
if (!modem.setNetworkMode(MODEM_NETWORK_AUTO)) { | ||
Serial.println("Set network mode failed!"); | ||
} | ||
String mode = modem.getNetworkModes(); | ||
Serial.print("Current network mode : "); | ||
Serial.println(mode); | ||
#endif | ||
|
||
// Check network registration status and network signal status | ||
int16_t sq ; | ||
Serial.print("Wait for the modem to register with the network."); | ||
RegStatus status = REG_NO_RESULT; | ||
while (status == REG_NO_RESULT || status == REG_SEARCHING || status == REG_UNREGISTERED) { | ||
status = modem.getRegistrationStatus(); | ||
switch (status) { | ||
case REG_UNREGISTERED: | ||
case REG_SEARCHING: | ||
sq = modem.getSignalQuality(); | ||
Serial.printf("[%lu] Signal Quality:%d", millis() / 1000, sq); | ||
delay(1000); | ||
break; | ||
case REG_DENIED: | ||
Serial.println("Network registration was rejected, please check if the APN is correct"); | ||
return ; | ||
case REG_OK_HOME: | ||
Serial.println("Online registration successful"); | ||
break; | ||
case REG_OK_ROAMING: | ||
Serial.println("Network registration successful, currently in roaming mode"); | ||
break; | ||
default: | ||
Serial.printf("Registration Status:%d\n", status); | ||
delay(1000); | ||
break; | ||
} | ||
} | ||
Serial.println(); | ||
|
||
|
||
Serial.printf("Registration Status:%d\n", status); | ||
delay(1000); | ||
|
||
String ueInfo; | ||
if (modem.getSystemInformation(ueInfo)) { | ||
Serial.print("Inquiring UE system information:"); | ||
Serial.println(ueInfo); | ||
} | ||
|
||
if (!modem.enableNetwork()) { | ||
Serial.println("Enable network failed!"); | ||
} | ||
|
||
delay(5000); | ||
|
||
String ipAddress = modem.getLocalIP(); | ||
Serial.print("Network IP:"); Serial.println(ipAddress); | ||
|
||
// Initialize MQTT, use SSL, skip authentication server | ||
modem.mqtt_begin(true); | ||
|
||
|
||
if (!mqtt_connect()) { | ||
return ; | ||
} | ||
|
||
while (1) { | ||
// Check the connection every ten seconds | ||
if (millis() > check_connect_millis) { | ||
check_connect_millis = millis() + 10000UL; | ||
if (!modem.mqtt_connected()) { | ||
mqtt_connect(); | ||
} else { | ||
// If connected, send information once every ten seconds | ||
String payload = "RunTime:" + String(millis() / 1000); | ||
modem.mqtt_publish(0, publish_topic, payload.c_str()); | ||
} | ||
} | ||
// MQTT handling | ||
modem.mqtt_handle(); | ||
delay(5); | ||
} | ||
} | ||
|
||
void loop() | ||
{ | ||
// Debug AT | ||
if (SerialAT.available()) { | ||
Serial.write(SerialAT.read()); | ||
} | ||
if (Serial.available()) { | ||
SerialAT.write(Serial.read()); | ||
} | ||
delay(1); | ||
} |
Oops, something went wrong.