Hi,
I am using SparkFun LTE CAT M1/NB-IoT Shield - SARA-R4 (connected to an Arduino UNO) to send data to Thingspeak cloud. I need to read the registration status of the cellular network before sending data to the could (code below). I can successfully send data to the cloud when the registration status lines are commented (stable connectivity), but if I uncomment the registration status lines the cellular connectivity to the cloud wont be stable which means sometimes the cellular device connects to the cloud and sometimes it can not get connected (frequently print -1 on serial monitor which means the cellular device can not connect to the could). How can I solve this issue?
I appreciate any help in advance.
Thanks,
Abbas
#include <SparkFun_LTE_Shield_Arduino_Library.h>
SoftwareSerial lteSerial(8, 9);
LTE_Shield lte;
String HOLOGRAM_DEVICE_KEY = "Ab12CdE4";
const char HOLOGRAM_URL[] = "cloudsocket.hologram.io";
const unsigned int HOLOGRAM_PORT = 9999;
void setup() {
Serial.begin(9600);
if ( lte.begin(lteSerial, 9600) ) {
Serial.println(F("LTE Shield connected!"));
}
Serial.println(F("Type a message. Send a Newline (\\n) to send it..."));
}
void loop() {
int regStatus = lte.registration(); //registration status line
Serial.println(regStatus); //registration status line
String Data= "&field1=-5.00";
sendHologramMessage(Data);
lte.poll();
}
void sendHologramMessage(String message)
{
int socket = -1;
String hologramMessage;
// New lines are not handled well
message.replace('\r', ' ');
message.replace('\n', ' ');
// Construct a JSON-encoded Hologram message string:
hologramMessage = "{\"k\":\"" + HOLOGRAM_DEVICE_KEY + "\",\"d\":\"" +
message + "\"}";
// Open a socket
socket = lte.socketOpen(LTE_SHIELD_TCP);
// On success, socketOpen will return a value between 0-5. On fail -1.
if (socket >= 0) {
// Use the socket to connec to the Hologram server
Serial.println("Connecting to socket: " + String(socket));
if (lte.socketConnect(socket, HOLOGRAM_URL, HOLOGRAM_PORT) == LTE_SHIELD_SUCCESS) {
// Send our message to the server:
Serial.println("Sending: " + String(hologramMessage));
if (lte.socketWrite(socket, hologramMessage) == LTE_SHIELD_SUCCESS)
{
// On succesful write, close the socket.
if (lte.socketClose(socket) == LTE_SHIELD_SUCCESS) {
Serial.println("Socket " + String(socket) + " closed");
}
} else {
Serial.println(F("Failed to write"));
}
}
}
}