I am trying to connect to losant using sim7600x 4g module with arduino via mqtt. I am trying to send temp data to the losant (I have used tinyGSM library).
Now for this I have used this code:
#define TINY_GSM_MODEM_SIM7600
#include <TinyGsmClient.h>
#include <Losant.h>
// Set serial for debug console (to the Serial Monitor, default speed 115200)
#define SerialMon Serial
// Use Hardware Serial on Mega, Leonardo, Micro
#define SerialAT Serial1
// Your GPRS credentials
// Leave empty, if missing user or pass
const char apn[] = "*****";
const char user[] = "";
const char pass[] = "";
// Losant credentials.
const char* LOSANT_DEVICE_ID = "***"
const char* LOSANT_ACCESS_KEY = "***"
const char* LOSANT_ACCESS_SECRET = "**"
TinyGsm modem(SerialAT);
TinyGsmClient client(modem);
LosantDevice device(LOSANT_DEVICE_ID);
void connect() {
// Restart takes quite some time
// To skip it, call init() instead of restart()
SerialMon.println("Initializing modem...");
modem.restart();
String modemInfo = modem.getModemInfo();
SerialMon.print("Modem: ");
SerialMon.println(modemInfo);
// Unlock your SIM card with a PIN
modem.simUnlock("1234");
SerialMon.print("Waiting for network...");
if (!modem.waitForNetwork()) {
SerialMon.println(" fail");
while (true);
}
SerialMon.println(" OK");
SerialMon.print("Connecting to ");
SerialMon.print(apn);
if (!modem.gprsConnect(apn, user, pass)) {
SerialMon.println(" fail");
while (true);
}
SerialMon.println(" OK");
// Connect to Losant.
SerialMon.println();
SerialMon.print("Connecting to Losant...");
device.connect(client, LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET);
while (!device.connected()) {
delay(500);
SerialMon.print(".");
}
SerialMon.println("Connected!");
}
void setup() {
// Set console baud rate
SerialMon.begin(115200);
delay(10);
// Set GSM module baud rate
SerialAT.begin(115200);
delay(3000);
connect();
}
void loop() {
bool toReconnect = false;
if (!device.connected()) {
SerialMon.println("Disconnected from Losant");
//SerialMon.println(device.mqttClient.state());
toReconnect = true;
}
if (toReconnect) {
connect();
}
StaticJsonDocument<2500> jsonBuffer;
JsonObject root = jsonBuffer.to<JsonObject>();
root["temp"] = 25.5;
device.sendState(root);
delay(1000);
device.loop();
delay(100);
}
Now you can see i Have hardcoded the data in the Json document so as to directly send the my previously made device dashboard in specific attribute.
main issue:
the device connects to the sim network but doesnt proceed to connect to losant. the code below runs but never shows “connected” in the serial monitor.
device.connect(client, LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET);
while (!device.connected()) {
// device.connect(client, LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET);
delay(500);
SerialMon.print(".");
}
SerialMon.println("Connected!");
}
I am attaching the SS of the serial monitor here for better understanding .
Now upon inspection of the losant device log I find that it only connects for a milisecond and then immediately disconnects.Below i have attached the SS.
I would be very thankful if anyone of you can look into the issues I am facing .