Hi, if anyone is able to help me out on this one issue that basically stalls my whole project?
I'm using an Arduino Uno with an A7 Ai Thinker GSM/GPRS/GPS module, together with a hologram sim received from hologram.io.
I have turned google upside down in an attempt to resolve this issue.
The basics are as follows:
I'm using the MqttClient example from the TinyGSM library and just substituting the required variables. (See code attached).
#define TINY_GSM_MODEM_A6
#include <TinyGsmClient.h>
#include <PubSubClient.h>
#define TINY_GSM_USE_GPRS true
// Your GPRS credentials
// Leave empty, if missing user or pass
const char apn[] = "hologram";
const char user[] = "";
const char pass[] = "";
// Use Hardware Serial on Mega, Leonardo, Micro
//#define SerialAT Serial1
// or Software Serial on Uno, Nano
#include <SoftwareSerial.h>
SoftwareSerial SerialAT(7, 8); // RX, TX
SoftwareSerial SerialATStart(7, 8); // RX, TX
TinyGsm modem(SerialAT);
TinyGsmClient client(modem);
PubSubClient mqtt(client);
const char* broker = "test.mosquitto.org";
const char* topicLed = "GsmClientTest/led";
const char* topicInit = "GsmClientTest/init";
const char* topicLedStatus = "GsmClientTest/ledStatus";
#define LED_PIN 2
int ledStatus = LOW;
long lastReconnectAttempt = 0;
void mqttCallback(char* topic, byte* payload, unsigned int len) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("]: ");
Serial.write(payload, len);
Serial.println();
// Only proceed if incoming message's topic matches
if (String(topic) == topicLed) {
ledStatus = !ledStatus;
digitalWrite(LED_PIN, ledStatus);
mqtt.publish(topicLedStatus, ledStatus ? "1" : "0");
}
}
void setup() {
Serial.begin(9600);
// Custom AT Command code I added to bring communication to
// 9600 between Arduino and A7
SerialATStart.begin(115200);
SerialATStart.println("AT+IPR=9600");
delay(2000);
// Set GSM module baud rate
SerialAT.begin(9600);
delay(3000);
// Restart takes quite some time
// To skip it, call init() instead of restart()
Serial.println("Initializing modem...");
modem.restart();
Serial.print("Waiting for network...");
if (!modem.waitForNetwork()) {
Serial.println(" fail");
while (true)
;
}
Serial.println(" OK");
Serial.print("Connecting to ");
Serial.print(apn);
if (!modem.gprsConnect(apn, user, pass)) {
Serial.println(" fail");
while (true)
;
}
Serial.println(" OK");
// MQTT Broker setup
mqtt.setServer(broker, 1883);
mqtt.setCallback(mqttCallback);
// SerialAT.println("at+cifsr");
// delay(3000);
// SerialAT.println("at+cifsr");
}
boolean mqttConnect() {
Serial.print("Connecting to ");
Serial.print(broker);
if (!mqtt.connect("GsmClientTest")) {
Serial.println(" fail");
return false;
}
Serial.println(" OK");
mqtt.publish(topicInit, "GsmClientTest started");
mqtt.subscribe(topicLed);
return mqtt.connected();
}
void loop() {
if (mqtt.connected()) {
mqtt.loop();
} else {
// Reconnect every 10 seconds
unsigned long t = millis();
if (t - lastReconnectAttempt > 10000L) {
lastReconnectAttempt = t;
if (mqttConnect()) {
lastReconnectAttempt = 0;
}
}
}
}
Here's the output from my serial monitor:
14:39:53.330 -> Initializing modem...
14:39:56.413 -> [10169] ### TinyGSM Version: 0.11.5
14:39:56.445 -> [10170] ### TinyGSM Compiled Module: TinyGsmClientA6
14:40:01.799 -> [15550] ### Modem: Ai Thinker Co.LTD A7
14:40:01.832 -> [15550] ### Modem: Ai Thinker Co.LTD A7
14:40:03.181 -> Modem Info: Ai Thinker Co.LTD A7 V03.03.20161110018H03
14:40:03.376 -> Waiting for network... success
14:40:07.521 -> Network connected
14:40:07.521 -> Connecting to hologram success
14:40:13.474 -> GPRS connected
14:42:50.405 -> [110827] ### Got: 4 -> 63
14:42:50.437 -> success
14:42:51.513 -> [111944] ### Unhandled:
14:42:53.637 -> [114049] ### Unhandled:
14:42:55.629 -> [116063] ### Unhandled:
14:42:57.849 -> [118272] ### Unhandled:
14:42:58.858 -> === MQTT NOT CONNECTED ===
14:42:58.890 -> Connecting to broker.hivemq.com[120287] ### Unhandled:
14:43:01.893 -> [122302] ### Unhandled:
14:43:04.725 -> [125147] ### Got: 4 -> 63
14:43:04.758 -> success
14:43:05.835 -> [126263] ### Unhandled:
14:43:07.953 -> [128366] ### Unhandled:
14:43:09.972 -> [130380] ### Unhandled:
14:43:12.169 -> [132587] ### Unhandled:
From what I can gather, the connection is established successfully through the APN using GPRS, as I can see on the hologram.io dashboard that the connection status has changed and data is transmitted.
But mqtt.connected() always seems to be false.
Maybe I'm doing something seriously wrong or something simple?
Also please note that I have defined the module as #define TINY_GSM_MODEM_A6 due to the underlying library performing an if statement on both TINY_GSM_MODEM_A6 and TINY_GSM_MODEM_A7 and doing the same thing.