MKRGSM
MKRGSM copied to clipboard
Reliable example
I have now been attempting for quite some time to get a reliable gprs connection for uploading data to server. I do not need more advanced than a get request every x minutes. Before I start posting my code attempts for troubleshooting I here ask if anyone has an example code that works for weeks without any manual resets and is able to handle the unit moving around with varying network signal/no signal?
My experience is that I get pretty stable connection whenever the unit is at one place, even when disconnecting/connecting the antenna. But as I drive(unit is placed in a car) I often get a frozen board that is not able to recover.
Here is the latest code I have been using. Please consider that I am no programming expert, so likely to find some things to do better :-) I have tried removing most of while loops where it could be possible that the board gets stuck. This code uploads temperature and battery voltage to Blynk server. When I get frozen board it does not help to press reset button or remove power supply. It is still frozen until I plug it in the computer and upload a new code.
The rebooting of the GSM module is something I picked up from the tinyGSM library. I am not sure if it really does make a difference in my project. But I am now at a point, where I try virtually everything to try making the application stabile. Any help would be very much appreciated, either corrections of my code, or new example that is more stable than mine!
BElow is my code. You need to put in your own auth from Blynk in "authcode" to be able to use for your own project.
` //Web client //This sketch connects to Blynk through a MKR GSM 1400 board. // libraries #include <MKRGSM.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 5
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
unsigned long initTime = millis();
#include "arduino_secrets.h"
// Please enter your sensitive data in the Secret tab or arduino_secrets.h
// PIN Number
const char PINNUMBER[] = SECRET_PINNUMBER;
// APN data
const char GPRS_APN[] = SECRET_GPRS_APN;
const char GPRS_LOGIN[] = SECRET_GPRS_LOGIN;
const char GPRS_PASSWORD[] = SECRET_GPRS_PASSWORD;
// initialize the library instance
GSMClient client;
GPRS gprs;
GSM gsm;
// URL, path and port. Replace autcode with your auth
char server[] = "blynk-cloud.com";
String path = "/authcode/update/";
int port = 8080; // port 80 is the default for HTTP
void ConnectGSM() {
pinMode(GSM_DTR, OUTPUT);
digitalWrite(GSM_DTR, LOW);
delay(5);
// Turn on the GSM module by triggering GSM_RESETN pin
pinMode(GSM_RESETN, OUTPUT);
digitalWrite(GSM_RESETN, HIGH);
delay(100);
digitalWrite(GSM_RESETN, LOW);
delay(1000);
// initialize serial communications and wait for port to open:
Serial.begin(9600);
initTime = millis();
Serial.println("Starting Arduino web client.");
// connection state
bool connected = false;
gprs.setTimeout(20000);
gsm.setTimeout(20000);
// After starting the modem with GSM.begin()
// attach the shield to the GPRS network with the APN, login and password
if ((gsm.begin(PINNUMBER) == GSM_READY) &&
(gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) {
connected = true;
Serial.println("Connected in ConnectGSM loop");
} else {
Serial.println("Not connected in ConnectGSM loop");
delay(1000);
pinMode(GSM_DTR, OUTPUT);
digitalWrite(GSM_DTR, LOW);
delay(5);
// Turn on the GSM module by triggering GSM_RESETN pin
pinMode(GSM_RESETN, OUTPUT);
digitalWrite(GSM_RESETN, HIGH);
delay(100);
digitalWrite(GSM_RESETN, LOW);
delay(1000);
// initialize serial communications and wait for port to open:
Serial.begin(9600);
Serial.println("SERIAL CONNECT");
delay(1000);
}
}
void setup() {
// initialize serial communications and wait for port to open:
Serial.begin(9600):
Serial.println("You are now in setup");
delay(1000);
ConnectGSM();
}
void loop() {
Serial.println("Start loop");
while(analogRead(ADC_BATTERY)*(4.3/1023)<3.6){
Serial.println("battery level too low to start cellular traffic");
delay(20000);}
Serial.println("Making GET request");
sensors.requestTemperatures();
sensors.getTempCByIndex(0);
float h = sensors.getTempCByIndex(0);
h = h*10;
h=round(h);
float t = h;
t=t/10;
String temppath= "V2?value=";
String V2value= String(t);
String input = path+temppath+V2value;
Serial.println("temperature is");
Serial.println(t);
String voltagepath="V3?value=";
int sensorValue = analogRead(ADC_BATTERY);
float voltage = sensorValue*(4.3/1023);
int percent=((voltage-3.6)/0.6)*100;
String V3value=String(voltage);
String inputvoltage= path+voltagepath+V3value;
Serial.println(input);
Serial.println(inputvoltage);
Serial.println("connecting...");
if (gsm.isAccessAlive()) {
// if you get a connection, report back via serial:
if (client.connect(server, port)) {
Serial.println("connected");
// Make a HTTP request:
client.print("GET ");
client.print(input);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(server);
client.println("Connection: close");
client.println();
client.flush();
client.stop();
delay(100);
client.connect(server, port);
Serial.println("connected for voltage");
client.print("GET ");
client.print(inputvoltage);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(server);
client.println("Connection: close");
client.println();
Serial.println((millis() - initTime) / 1000);
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
}
else{
Serial.println("connection to GSM is no longer active ");
ConnectGSM();
}
client.flush();
client.stop();
Serial.println("loop:sketch ");
delay(100000);
}
`
hi @eiriksels, thank for open the issue, i'll check your code soon as possible.