Hi
I use an Arduino MKRGSM to connect successfully to various brokers WITHOUT TLS and I am confident that my code is correct:
#include <MKRGSM.h>
#include <MQTT.h>
const char pin[] = YOUR_PIN;
const char apn[] = YOUR_APN;
const char login[] = "";
const char password[] = "";
GSMClient net;
GPRS gprs;
GSM gsmAccess;
MQTTClient client;
unsigned long lastMillis = 0;
void connect() {
bool connected = false;
Serial.print("connecting to cellular network ...");
while (!connected) {
if ((gsmAccess.begin(pin) == GSM_READY) &&
(gprs.attachGPRS(apn, login, password) == GPRS_READY)) {
connected = true;
} else {
Serial.print(".");
delay(1000);
}
}
Serial.print("\nconnecting...");
while (!client.connect("arduino", "public", "public")) {
Serial.print(".");
delay(1000);
}
Serial.println("\nconnected!");
client.subscribe("/hello");
}
void messageReceived(String &topic, String &payload) {
Serial.println("incoming: " + topic + " - " + payload);
}
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
Serial.println("Starting");
client.begin("public.cloud.shiftr.io", net);
client.onMessage(messageReceived);
connect();
}
void loop() {
client.loop();
if (!client.connected()) {
connect();
}
if (millis() - lastMillis > 1000) {
lastMillis = millis();
client.publish("/hello", "2");
}
}
I would like to connect to my hivemq cloud account which requires TLS.
I have tried various libraries for the GSM part and for the MQTT part but I can not connect with TLS:
#include <MKRGSM.h>
#include <MQTT.h>
const char pin[] = YOUR_PIN;
const char apn[] = YOUR_APN;
const char login[] = "";
const char password[] = "";
GSMSSLClient net;
GPRS gprs;
GSM gsmAccess;
MQTTClient client;
unsigned long lastMillis = 0;
void connect() {
bool connected = false;
Serial.print("connecting to cellular network ...");
while (!connected) {
if ((gsmAccess.begin(pin) == GSM_READY) &&
(gprs.attachGPRS(apn, login, password) == GPRS_READY)) {
connected = true;
} else {
Serial.print(".");
delay(1000);
}
}
Serial.print("\nconnecting...");
while (!client.connect("arduino", "username", "password")) {
Serial.print(".");
delay(1000);
}
Serial.println("\nconnected!");
client.subscribe("/hello");
}
void messageReceived(String &topic, String &payload) {
Serial.println("incoming: " + topic + " - " + payload);
}
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
Serial.println("Starting");
client.begin("your_account.hivemq.cloud", 8883, net);
client.onMessage(messageReceived);
connect();
}
void loop() {
client.loop();
if (!client.connected()) {
connect();
}
if (millis() - lastMillis > 1000) {
lastMillis = millis();
client.publish("/hello", "2");
}
}
Here, the program can't connect to the broker.
Has anybody a working example off how to connect to a free hivemq cloud account (or another TLS mqtt broker) with the MKRGSM ?
Thanks
Peter