diff --git a/.gitignore b/.gitignore index 613a74d..b3418be 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,6 @@ .vscode test script -build \ No newline at end of file +build +MqttsBuiltlnHivemq +extars \ No newline at end of file diff --git a/examples/MqttsBuiltlnAWS/.skip.T-SIM7672G b/examples/MqttsBuiltlnAWS/.skip.T-SIM7672G new file mode 100644 index 0000000..e69de29 diff --git a/examples/MqttsBuiltlnAWS/MqttsBuiltlnAWS.ino b/examples/MqttsBuiltlnAWS/MqttsBuiltlnAWS.ino new file mode 100644 index 0000000..ff1c5c5 --- /dev/null +++ b/examples/MqttsBuiltlnAWS/MqttsBuiltlnAWS.ino @@ -0,0 +1,248 @@ +/** + * @file MqttsBuiltlnAWS.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://aws.amazon.com/campaigns/IoT + * * Example uses a forked TinyGSM , which will not compile successfully using the mainline TinyGSM. + * *!!!! When using ESP to connect to AWS, the AWS IOT HUB policy must be set to all devices, otherwise the connection cannot be made. + * *!!!! When using ESP to connect to AWS, the AWS IOT HUB policy must be set to all devices, otherwise the connection cannot be made. + * *!!!! When using ESP to connect to AWS, the AWS IOT HUB policy must be set to all devices, otherwise the connection cannot be made. + * *!!!! When using ESP to connect to AWS, the AWS IOT HUB policy must be set to all devices, otherwise the connection cannot be made. + */ +#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 +#include "certs/AWSClientCertificate.h" +#include "certs/AWSClientPrivateKey.h" +#include "certs/AmazonRootCA.h" + +#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 +// The URL of xxxxxxx-ats.iot.ap-southeast-2.amazonaws.com can be found in the settings for endpoint in your AWS IOT Core account +const char *broker = "xxxxxxx.iot.ap-southeast-2.amazonaws.com"; +const uint16_t broker_port = 8883; +const char *clien_id = "A76XX"; + +// Replace the topic you want to subscribe to +const char *subscribe_topic = "GsmMqttTest/subscribe"; +// Replace the theme you want to publish +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 + modem.mqtt_begin(true); + + // Set Amazon Certificate + modem.mqtt_set_certificate(AmazonRootCA, AWSClientCertificate, AWSClientPrivateKey); + + // Connecting to AWS IOT Core + 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/MqttsBuiltlnAWS/certs/AWSClientCertificate.h b/examples/MqttsBuiltlnAWS/certs/AWSClientCertificate.h new file mode 100644 index 0000000..db8e183 --- /dev/null +++ b/examples/MqttsBuiltlnAWS/certs/AWSClientCertificate.h @@ -0,0 +1,8 @@ +#include + +// Device Certificate +static const char AWSClientCertificate[] PROGMEM = R"EOF( +-----BEGIN CERTIFICATE----- +// Please download the device certificate from AWS IoT Core when you create the thing and paste it here// +-----END CERTIFICATE----- +)EOF"; diff --git a/examples/MqttsBuiltlnAWS/certs/AWSClientPrivateKey.h b/examples/MqttsBuiltlnAWS/certs/AWSClientPrivateKey.h new file mode 100644 index 0000000..cd6013c --- /dev/null +++ b/examples/MqttsBuiltlnAWS/certs/AWSClientPrivateKey.h @@ -0,0 +1,8 @@ +#include + +// Device Private Key +static const char AWSClientPrivateKey[] PROGMEM = R"EOF( +-----BEGIN RSA PRIVATE KEY----- +// Please download the device private key from AWS IoT Core when you create the thing and paste it here// +-----END RSA PRIVATE KEY----- +)EOF"; diff --git a/examples/MqttsBuiltlnAWS/certs/AmazonRootCA.h b/examples/MqttsBuiltlnAWS/certs/AmazonRootCA.h new file mode 100644 index 0000000..41787f4 --- /dev/null +++ b/examples/MqttsBuiltlnAWS/certs/AmazonRootCA.h @@ -0,0 +1,8 @@ +#include + +// Amazon Root CA +static const char AmazonRootCA[] PROGMEM = R"EOF( +-----BEGIN ROOT CERTIFICATE----- +// Please download the device certificate from AWS IoT Core when you create the thing and paste it here +//-----END ROOT CERTIFICATE----- +)EOF"; \ No newline at end of file diff --git a/examples/MqttsBuiltlnAWS/utilities.h b/examples/MqttsBuiltlnAWS/utilities.h new file mode 100644 index 0000000..4d1b085 --- /dev/null +++ b/examples/MqttsBuiltlnAWS/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 a2c717f..1d1385f 100644 --- a/platformio.ini +++ b/platformio.ini @@ -19,7 +19,7 @@ ; https://www.lilygo.cc/products/t-sim-a7670e ; default_envs = T-A7670X -; default_envs = T-Call-A7670X +default_envs = T-Call-A7670X ; default_envs = T-SIM7672G @@ -28,7 +28,7 @@ ; default_envs = T-A7608X-S3 -default_envs = T-A7608X-DC-S3 +; default_envs = T-A7608X-DC-S3 @@ -57,7 +57,8 @@ default_envs = T-A7608X-DC-S3 ; src_dir = examples/LBSExample ; src_dir = examples/Network ; src_dir = examples/MqttsBuiltlnAuth -src_dir = examples/MqttsBuiltlnSSL +; src_dir = examples/MqttsBuiltlnSSL +src_dir = examples/MqttsBuiltlnAWS