MKRNB
MKRNB copied to clipboard
SARA R4 cannot detach from GPRS
I want to periodically power the cellular module and then shut it down again to save energy. But when I detach it after sending data via a UDP socket, the device becomes unresponsive When it turns on for the second time (when detaching asynchronously) it returns at the AT+CPIN?
command. I then get the response Not connected, retying
from the function connectToNetwork()
. It then indefinetly tries again to open a connection but it doesn't work.
Edit: Removed everything from the code not necessary to replicate this behaviour. Also I added a generous 1 mF of capacitors which greatly improved stability.
It now takes an unknown amount of time to disconnect from the network using gprs.detachGPRS(true); (in synchronous mode). As soon as I have sent the first bytes.
#include <MKRNB.h>
#include <RTCZero.h>
// initialize the library instance
NBClient client;
GPRS gprs;
NB nbAccess(true);
NBUDP udp;
RTCZero rtcZero;
void connectToNetwork(){
auto connected = false;
Serial.println(F("init..."));
while (!connected)
{
if ((nbAccess.begin("", "hologram", true, true) == NB_READY) &&
(gprs.attachGPRS() == GPRS_READY))
{
connected = true;
}
else
{
Serial.println(F("Not connected, retying"));
delay(200);
}
}
}
void disconnectFromNetwork(){
Serial.println(F("Disconnecting from network"));
//////////////////////////////////////////////////////////////////////
gprs.detachGPRS(true); // here the module stops responding indefinetly
nbAccess.secureShutdown();
}
void setup()
{
Serial.begin(9600);
// set time
connectToNetwork();
rtcZero.setEpoch(nbAccess.getTime());
disconnectFromNetwork();
// endless event loop
while (true)
{
Serial.println(F("New interation"));
// do some measurements here every couple hours
// every day connect to the network and upload them
connectToNetwork();
if (udp.begin(80))
{
Serial.println("udp socket opened");
// make a udp request (i know this wouldn't work but for this example it is sufficient)
udp.beginPacket("example.com", 80);
// todo send multiple packets if one packet exceeds 300bytes
udp.write("17 bytes of data", 17);
udp.endPacket();
udp.stop();
}
else
{
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
disconnectFromNetwork();
}
}
void loop() { }
The response of the device when debug is enabled (and detatching is async) on the second connection is:
init...
AT
OK
AT
OK
AT+CMEE=0
OK
AT+CFUN=0
OK
AT+CPIN?
+CPIN: READY
OK
and after that it just stops.