Skip to content

Commit

Permalink
Add MqttsBuiltlnHivemq example
Browse files Browse the repository at this point in the history
  • Loading branch information
lewisxhe committed Nov 29, 2023
1 parent efeb43c commit 5bb3aa9
Show file tree
Hide file tree
Showing 6 changed files with 547 additions and 2 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
test
script
build
MqttsBuiltlnHivemq
extars
Empty file.
94 changes: 94 additions & 0 deletions examples/MqttsBuiltlnHivemq/HivemqRootCA.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include <pgmspace.h>
// Amazon Root CA
static const char HivemqRootCA[] PROGMEM = R"EOF(
-----BEGIN CERTIFICATE-----
MIIFBzCCA++gAwIBAgISA3nTO8URQRBuVpc/TcIA/pSpMA0GCSqGSIb3DQEBCwUA
MDIxCzAJBgNVBAYTAlVTMRYwFAYDVQQKEw1MZXQncyBFbmNyeXB0MQswCQYDVQQD
EwJSMzAeFw0yMzEwMDQyMzE3NTNaFw0yNDAxMDIyMzE3NTJaMB8xHTAbBgNVBAMM
FCouczIuZXUuaGl2ZW1xLmNsb3VkMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB
CgKCAQEAxKOySSNPeWJ6cIWbmm1Psc3VmL/YK+K5NZj2GlOJJaMKopb6y0A8V7wy
YM0kaiIe4fGfdLxSKwoFrSXe8gsmaOkgdU734OweOGCPaU3W0ZjIqZzRzhpT8FY9
zj8NlpZpV/QGbbmYCOy6YfAixpZ10L9L1g74CO9M6Guf706Dfg9E1O4N5IwWQr1i
Yv1hvdr1tTHlblj0DcFVu+nYaJ0RIPsiHt3lfldphXGuw1pIEpuv61D121furZqQ
D3onWw1oQ3eUhJYUI/nKqrNSyrYDzvYckdzOnnit/lCuJqAzmFdYvEmyHvmWwS7H
GOEYKiOYjtWkrfK6siEs2pqk/zONaQIDAQABo4ICKDCCAiQwDgYDVR0PAQH/BAQD
AgWgMB0GA1UdJQQWMBQGCCsGAQUF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ubGV0c2VuY3J5cHQu
b3JnMDwGA1UdHwQ1MDMwMaAvoC2GK2h0dHA6Ly9jcmwuaWRlbnRydXN0LmNvbS9E
U1RST09UQ0FYM0NSTC5jcmwwHQYDVR0OBBYEFHm0WeZ7tuXkAXOACIjIGlj26Ztu
MA0GCSqGSIb3DQEBCwUAA4IBAQAKcwBslm7/DlLQrt2M51oGrS+o44+/yQoDFVDC
5WxCu2+b9LRPwkSICHXM6webFGJueN7sJ7o5XPWioW5WlHAQU7G75K/QosMrAdSW
9MUgNTP52GE24HGNtLi1qoJFlcDyqSMo59ahy2cI2qBDLKobkx/J3vWraV0T9VuG
WCLKTVXkcGdtwlfFRjlBz4pYg1htmf5X6DYO8A4jqv2Il9DjXA6USbW1FzXSLr9O
he8Y4IWS6wY7bCkjCWDcRQJMEhg76fsO3txE+FiYruq9RUWhiF1myv4Q6W+CyBFC
Dfvp7OOGAN6dEOM4+qR9sdjoSYKEBpsr6GtPAQw4dy753ec5
-----END CERTIFICATE-----
)EOF";
247 changes: 247 additions & 0 deletions examples/MqttsBuiltlnHivemq/MqttsBuiltlnHivemq.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
/**
* @file MqttsBuiltlnHivemq.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://www.hivemq.com/
* * 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 "HivemqRootCA.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

// Pay attention to the validity period of the certificate. You can use the following command to obtain the latest certificate again.
// $ openssl s_client -connect brokerurl.s2.eu.hivemq.cloud:8883 -showcerts < /dev/null 2> /dev/null | sed -n '/BEGIN/,/END/p' > server.pem

// MQTT details
// The URL of xxxxx.s2.eu.hivemq.cloud can be found in the settings for endpoint in your Hivemq account
const char *broker = "xxxxx.s2.eu.hivemq.cloud";
const uint16_t broker_port = 8883;
const char *broker_username = "Your Hivemq username";
const char *broker_password = "Your Hivemq password";
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, broker_username, broker_password);
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
// Hivemq needs to enable sni before it can be run and connected.
bool enableSSL = true;
bool enableSNI = true;
modem.mqtt_begin(enableSSL, enableSNI);

// Set up Hivemq ROOT certificate
modem.mqtt_set_certificate(HivemqRootCA);

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);
}
Loading

0 comments on commit 5bb3aa9

Please sign in to comment.