AM7020 (SIMCOM SIM7020E) MQTT(S), HTTP(S) Library(Protocol Stack on SIM7020E)
Declare nbiot instance using:
#define SerialAT Serial2
#define AM7020_RESET 5
SIM7020NB nb(SerialAT, AM7020_RESET);
Initialize nb and connect to NBIOT base station:
void nbConnect(void)
{
Serial.println(F("Initializing modem..."));
while (!nb.init() || !nb.nbiotConnect(APN, BAND)) {
Serial.print(F("."));
};
Serial.print(F("Waiting for network..."));
while (!nb.waitForNetwork()) {
Serial.print(F("."));
delay(5000);
}
Serial.println(F(" success"));
}
Check NBIOT connection status:
if (!nb.chkNet()) {
nbConnect();
}
Declare instance using:
- MQTT
SIM7020EMQTT mqtt(SerialAT);
- MQTTS
You can use the openssl command to get the Server CA.
openssl s_client -connect {HOSTNAME}:{PORT} -showcerts
const char root_ca[] PROGMEM = {"-----BEGIN CERTIFICATE-----\\r\\n"
"MIIEAzCCAuugAwIBAgIUBY1hlCGvdj4NhBXkZ/uLUZNILAwwDQYJKoZIhvcNAQEL\\r\\n"
...
"-----END CERTIFICATE-----"};
SIM7020EMQTTS mqtts(SerialAT, root_ca);
Publish and subscribe:
mqtt.publish(MQTT_TOPIC, message);
void callback(const char *msg)
{
Serial.print(MQTT_TOPIC ": ");
Serial.println(msg);
}
mqtt.subscribe(MQTT_TOPIC, callback);
Listen for messages from the broker:
mqtt.procSubs();
Declare instance using:
- HTTP
SIM7020EHTTP http(SerialAT, HTTP_SERVER);
- HTTPS
You can use the openssl command to get the Server CA.
openssl s_client -connect {HOSTNAME}:{PORT} -showcerts
const char root_ca[] PROGMEM = {"-----BEGIN CERTIFICATE-----\n"
"MIIEdTCCA12gAwIBAgIJAKcOSkw0grd/MA0GCSqGSIb3DQEBCwUAMGgxCzAJBgNV\n"
...
"-----END CERTIFICATE-----"};
SIM7020EHTTPS https(SerialAT, HTTP_SERVER, root_ca);
HTTP Get and Post:
http.get(HTTP_GET_API);
http.post(HTTP_POST_API, "application/json", "{\"value\": \"POST\"}");