From 6d88e4cdda6388a29738bd20fb07ca2d8f271dbf Mon Sep 17 00:00:00 2001 From: lewishe Date: Tue, 28 Nov 2023 15:39:53 +0800 Subject: [PATCH] Add MqttsBuiltlnSSL example --- .github/workflows/platformio.yml | 1 + README.md | 2 + examples/MqttsBuiltlnSSL/.skip.T-SIM7672G | 0 examples/MqttsBuiltlnSSL/MqttsBuiltlnSSL.ino | 236 +++++++++++++++++++ examples/MqttsBuiltlnSSL/utilities.h | 204 ++++++++++++++++ platformio.ini | 5 +- 6 files changed, 447 insertions(+), 1 deletion(-) create mode 100644 examples/MqttsBuiltlnSSL/.skip.T-SIM7672G create mode 100644 examples/MqttsBuiltlnSSL/MqttsBuiltlnSSL.ino create mode 100644 examples/MqttsBuiltlnSSL/utilities.h diff --git a/.github/workflows/platformio.yml b/.github/workflows/platformio.yml index b5ba220..a68b14c 100644 --- a/.github/workflows/platformio.yml +++ b/.github/workflows/platformio.yml @@ -41,6 +41,7 @@ jobs: examples/SerialRS485, examples/VoiceCalls, examples/MqttsBuiltlnAuth, + examples/MqttsBuiltlnSSL ] steps: diff --git a/README.md b/README.md index fc3089a..03cc1f1 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,8 @@ | LBSExample | ✅ | ✅ | ❌ (No support) | ✅ | ✅ | ✅ | | Network | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | MqttsBuiltlnAuth | ✅ | ✅ | ❌ (Not adapted) | ✅ | ✅ | ✅ | +| MqttsBuiltlnSSL | ✅ | ✅ | ❌ (Not adapted) | ✅ | ✅ | ✅ | + # Quick start ⚡ diff --git a/examples/MqttsBuiltlnSSL/.skip.T-SIM7672G b/examples/MqttsBuiltlnSSL/.skip.T-SIM7672G new file mode 100644 index 0000000..e69de29 diff --git a/examples/MqttsBuiltlnSSL/MqttsBuiltlnSSL.ino b/examples/MqttsBuiltlnSSL/MqttsBuiltlnSSL.ino new file mode 100644 index 0000000..90119a1 --- /dev/null +++ b/examples/MqttsBuiltlnSSL/MqttsBuiltlnSSL.ino @@ -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 , 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 + +#ifdef DUMP_AT_COMMANDS // if enabled it requires the streamDebugger lib +#include +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); +} diff --git a/examples/MqttsBuiltlnSSL/utilities.h b/examples/MqttsBuiltlnSSL/utilities.h new file mode 100644 index 0000000..4d1b085 --- /dev/null +++ b/examples/MqttsBuiltlnSSL/utilities.h @@ -0,0 +1,204 @@ +/** + * @file utilities.h + * @author Lewis He (lewishe@outlook.com) + * @license MIT + * @copyright Copyright (c) 2023 Shenzhen Xin Yuan Electronic Technology Co., Ltd + * @date 2023-11-15 + * + */ + +#pragma once + +// Note: +// When using ArduinoIDE, you must select a corresponding board type. +// If you don’t know which board type you have, please click on the link to view it. +// 使用ArduinoIDE ,必须选择一个对应的板型 ,如果你不知道你的板型是哪种,请点击链接进行查看 + +// Products Link:https://www.lilygo.cc/products/t-sim-a7670e +// #define LILYGO_T_A7670 + +// #define LILYGO_T_CALL_A7670 + +// #define LILYGO_T_SIM7672G_S3 + +// #define LILYGO_T_A7608X + +// #define LILYGO_T_A7608X_S3 + +// #define LILYGO_T_A7608X_DC_S3 + + + + + +#if defined(LILYGO_T_A7670) + +#define MODEM_BAUDRATE (115200) +#define MODEM_DTR_PIN (25) +#define MODEM_TX_PIN (26) +#define MODEM_RX_PIN (27) +// The modem boot pin needs to follow the startup sequence. +#define BOARD_PWRKEY_PIN (4) +#define BOARD_ADC_PIN (35) +// The modem power switch must be set to HIGH for the modem to supply power. +#define BOARD_POWERON_PIN (12) +#define MODEM_RING_PIN (33) +#define MODEM_RESET_PIN (5) +#define BOARD_MISO_PIN (2) +#define BOARD_MOSI_PIN (15) +#define BOARD_SCK_PIN (14) +#define BOARD_SD_CS_PIN (13) +#define BOARD_BAT_ADC_PIN (35) +#define MODEM_RESET_LEVEL HIGH +#define SerialAT Serial1 + +#define MODEM_GPS_ENABLE_GPIO (-1) + + +#ifndef TINY_GSM_MODEM_A7670 +#define TINY_GSM_MODEM_A7670 +#endif + +#elif defined(LILYGO_T_CALL_A7670) +#define MODEM_BAUDRATE (115200) +#define MODEM_DTR_PIN (14) +#define MODEM_TX_PIN (26) +#define MODEM_RX_PIN (25) +// The modem boot pin needs to follow the startup sequence. +#define BOARD_PWRKEY_PIN (4) +#define BOARD_LED_PIN (12) +// There is no modem power control, the LED Pin is used as a power indicator here. +#define BOARD_POWERON_PIN (BOARD_LED_PIN) +#define MODEM_RING_PIN (13) +#define MODEM_RESET_PIN (27) +#define MODEM_RESET_LEVEL LOW +#define SerialAT Serial1 + + +#define MODEM_GPS_ENABLE_GPIO (-1) + + +#ifndef TINY_GSM_MODEM_A7670 +#define TINY_GSM_MODEM_A7670 +#endif + +#elif defined(LILYGO_T_SIM7672G_S3) +#define MODEM_BAUDRATE (115200) +#define MODEM_DTR_PIN (9) +#define MODEM_TX_PIN (11) +#define MODEM_RX_PIN (10) +// The modem boot pin needs to follow the startup sequence. +#define BOARD_PWRKEY_PIN (18) +#define BOARD_LED_PIN (12) +// There is no modem power control, the LED Pin is used as a power indicator here. +#define BOARD_POWERON_PIN (BOARD_LED_PIN) +#define MODEM_RING_PIN (3) +#define MODEM_RESET_PIN (17) +#define MODEM_RESET_LEVEL LOW +#define SerialAT Serial1 + +#define BOARD_BAT_ADC_PIN (4) +#define BOARD_SOLAR_ADC_PIN (5) +#define BOARD_MISO_PIN (47) +#define BOARD_MOSI_PIN (14) +#define BOARD_SCK_PIN (21) +#define BOARD_SD_CS_PIN (13) + +#ifndef TINY_GSM_MODEM_SIM7672 +#define TINY_GSM_MODEM_SIM7672 +#endif + +#define MODEM_GPS_ENABLE_GPIO (4) + + +#elif defined(LILYGO_T_A7608X) + +#define MODEM_BAUDRATE (115200) +#define MODEM_DTR_PIN (25) +#define MODEM_TX_PIN (26) +#define MODEM_RX_PIN (27) +// The modem boot pin needs to follow the startup sequence. +#define BOARD_PWRKEY_PIN (4) +#define BOARD_BAT_ADC_PIN (35) +// The modem power switch must be set to HIGH for the modem to supply power. +#define BOARD_POWERON_PIN (12) +#define MODEM_RING_PIN (33) +#define MODEM_RESET_PIN (5) +#define BOARD_MISO_PIN (2) +#define BOARD_MOSI_PIN (15) +#define BOARD_SCK_PIN (14) +#define BOARD_SD_CS_PIN (13) + +#define MODEM_RESET_LEVEL HIGH +#define SerialAT Serial1 + + +#ifndef TINY_GSM_MODEM_A7608 +#define TINY_GSM_MODEM_A7608 +#endif + +// 127 is defined in GSM as the AUXVDD index +#define MODEM_GPS_ENABLE_GPIO (127) + +#elif defined(LILYGO_T_A7608X_S3) + +#define MODEM_BAUDRATE (115200) +#define MODEM_DTR_PIN (7) +#define MODEM_TX_PIN (17) +#define MODEM_RX_PIN (18) +// The modem boot pin needs to follow the startup sequence. +#define BOARD_PWRKEY_PIN (15) +#define BOARD_BAT_ADC_PIN (35) +// The modem power switch must be set to HIGH for the modem to supply power. +#define BOARD_POWERON_PIN (12) +#define MODEM_RING_PIN (6) +#define MODEM_RESET_PIN (16) +#define BOARD_MISO_PIN (47) +#define BOARD_MOSI_PIN (14) +#define BOARD_SCK_PIN (21) +#define BOARD_SD_CS_PIN (13) + +#define MODEM_RESET_LEVEL LOW +#define SerialAT Serial1 + +#ifndef TINY_GSM_MODEM_A7608 +#define TINY_GSM_MODEM_A7608 +#endif + +// 127 is defined in GSM as the AUXVDD index +#define MODEM_GPS_ENABLE_GPIO (127) + +#elif defined(LILYGO_T_A7608X_DC_S3) + +#define MODEM_DTR_PIN (5) +#define MODEM_RX_PIN (42) +#define MODEM_TX_PIN (41) +// The modem boot pin needs to follow the startup sequence. +#define BOARD_PWRKEY_PIN (38) +#define MODEM_RING_PIN (6) +#define MODEM_RESET_PIN (40) +#define MODEM_RTS_PIN (1) +#define MODEM_CTS_PIN (2) + +#define MODEM_RESET_LEVEL LOW +#define SerialAT Serial1 + +#ifndef TINY_GSM_MODEM_A7608 +#define TINY_GSM_MODEM_A7608 +#endif + +// 127 is defined in GSM as the AUXVDD index +#define MODEM_GPS_ENABLE_GPIO (127) + +#else +#error "Use ArduinoIDE, please open the macro definition corresponding to the board above " +#endif + + + + + + + + + diff --git a/platformio.ini b/platformio.ini index 50bf179..a2c717f 100644 --- a/platformio.ini +++ b/platformio.ini @@ -56,7 +56,10 @@ default_envs = T-A7608X-DC-S3 ; src_dir = examples/SendSMS ; src_dir = examples/LBSExample ; src_dir = examples/Network -src_dir = examples/MqttsBuiltlnAuth +; src_dir = examples/MqttsBuiltlnAuth +src_dir = examples/MqttsBuiltlnSSL + + [env] platform = espressif32