Hello,
I’ve been connecting my MKR GSM 1400 to blynk both using the blynk library/tiny gsm and also the http API. I am able to connect both ways but the connection is not stable enough to supply data for more than maximum an hour so far. What my project does is measure temperature end sending value and battery voltage.
I have connected the MKR GSM 1400 to a large LiPo battery pack. I have checked that the battery supplies the board with approx 4v on the battery connector. My question is if there is some error in my code that causes this instability or if it is more likely to be caused by battery or something else? Thanks for all feedback on this issue.
My code for Blynk library is here:
/**************************************************************
*
* For this example, you need to install Blynk library:
* https://github.com/blynkkk/blynk-library/releases/latest
*
* TinyGSM Getting Started guide:
* http://tiny.cc/tiny-gsm-readme
*
**************************************************************
*
* Blynk is a platform with iOS and Android apps to control
* Arduino, Raspberry Pi and the likes over the Internet.
* You can easily build graphic interfaces for all your
* projects by simply dragging and dropping widgets.
*
* Blynk supports many development boards with WiFi, Ethernet,
* GSM, Bluetooth, BLE, USB/Serial connection methods.
* See more in Blynk library examples and community forum.
*
* http://www.blynk.io/
*
* Change GPRS apm, user, pass, and Blynk auth token to run :)
**************************************************************/
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 5
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// Default heartbeat interval for GSM is 60
// If you want override this value, uncomment and set this option:
#define BLYNK_HEARTBEAT 60
// Select your modem:
//#define TINY_GSM_MODEM_SIM800
// #define TINY_GSM_MODEM_SIM808
// #define TINY_GSM_MODEM_SIM900
#define TINY_GSM_MODEM_UBLOX
// #define TINY_GSM_MODEM_BG96
// #define TINY_GSM_MODEM_A6
// #define TINY_GSM_MODEM_A7
// #define TINY_GSM_MODEM_M590
// #define TINY_GSM_MODEM_ESP8266
// #define TINY_GSM_MODEM_XBEE
#include <SimpleTimer.h>
#include <TinyGsmClient.h>
#include <BlynkSimpleSIM800.h>
// Set serial for debug console (to the Serial Monitor, default speed 115200)
#define SerialMon Serial
// Hardware Serial on Mega, Leonardo, Micro
#define SerialAT SerialGSM
#define BLYNK_DEFAULT_PORT 8080
// or Software Serial on Uno, Nano
//#include <SoftwareSerial.h>
//SoftwareSerial SerialAT(2, 3); // RX, TX
// Your GPRS credentials
// Leave empty, if missing user or pass
const char apn[] = "netcom";
const char user[] = "";
const char pass[] = "";
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
const char auth[] = "XXX";
TinyGsm modem(SerialAT);
SimpleTimer timer;
void ReadTemp(){
sensors.requestTemperatures();
sensors.getTempCByIndex(0);
float h = sensors.getTempCByIndex(0);
h = h*10;
h=round(h);
float t = h;
t=t/10;
Blynk.virtualWrite(2,t);
int sensorValue = analogRead(ADC_BATTERY);
float voltage = sensorValue*(4.3/1023);
int percent=((voltage-3.6)/0.6)*100;
Blynk.virtualWrite(3,voltage);
Blynk.virtualWrite(4,percent);
}
void setup()
{
// Set console baud rate
SerialMon.begin(115200);
delay(10);
SerialAT.begin(115200);
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);
// 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");
Blynk.begin(auth, modem, apn, user, pass);
timer.setInterval(30000, ReadTemp);
}
void loop()
{
Blynk.run();
timer.run();
}
My code for Http API:
#include <MKRGSM.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SimpleTimer.h>
#define ONE_WIRE_BUS 5
OneWire oneWire(ONE_WIRE_BUS);
/********************************************************************/
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
/*************************************************************
Blynk is a platform with iOS and Android apps to control
Arduino, Raspberry Pi and the likes over the Internet.
You can easily build graphic interfaces for all your
projects by simply dragging and dropping widgets.
Downloads, docs, tutorials: http://www.blynk.cc
Sketch generator: http://examples.blynk.cc
Blynk community: http://community.blynk.cc
Follow us: http://www.fb.com/blynkapp
http://twitter.com/blynk_app
This example code is in public domain.
*************************************************************
Project setup in the Blynk app:
Value Display widget on V2
*************************************************************/
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
const char auth[] = "XXX";
// Blynk cloud server
const char* host = "blynk-cloud.com";
unsigned int port = 80;
// Network settings
#define PINNUMBER ""
// APN data
#define GPRS_APN "netcom" // replace your GPRS APN
#define GPRS_LOGIN "" // replace with your GPRS login
#define GPRS_PASSWORD "" // replace with your GPRS password
GSMClient client;
GPRS gprs;
GSM gsmAccess;
// Start the GSM connection
void connectNetwork()
{
Serial.println("Connecting to GSM...");
bool status = false;
// After starting the modem with GSM.begin()
// attach the shield to the GPRS network with the APN, login and password
while (status == false) {
if ((gsmAccess.begin(PINNUMBER) == GSM_READY) &
(gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) {
status = true;
} else {
Serial.print(".");
delay(1000);
}
}
Serial.println();
Serial.println("GSM connected");
}
bool httpRequest(const String& method,
const String& request,
String& response)
{
Serial.print(F("Connecting to "));
Serial.print(host);
Serial.print(":");
Serial.print(port);
Serial.print("... ");
if (client.connect(host, port)) {
Serial.println("OK");
} else {
Serial.println("failed");
return false;
}
client.print(method); client.println(F(" HTTP/1.1"));
client.print(F("Host: ")); client.println(host);
client.println(F("Connection: close"));
if (request.length()) {
client.println(F("Content-Type: application/json"));
client.print(F("Content-Length: ")); client.println(request.length());
client.println();
client.print(request);
} else {
client.println();
}
//Serial.println("Waiting response");
int timeout = millis() + 5000;
while (client.available() == 0) {
if (timeout - millis() < 0) {
Serial.println(">>> Client Timeout !");
client.stop();
return false;
}
}
//Serial.println("Reading response");
int contentLength = -1;
while (client.available()) {
String line = client.readStringUntil('\n');
line.trim();
line.toLowerCase();
if (line.startsWith("content-length:")) {
contentLength = line.substring(line.lastIndexOf(':') + 1).toInt();
} else if (line.length() == 0) {
break;
}
}
//Serial.println("Reading response body");
response = "";
response.reserve(contentLength + 1);
while (response.length() < contentLength && client.connected()) {
while (client.available()) {
char c = client.read();
response += c;
}
}
client.stop();
return true;
}
SimpleTimer timer;
void ReadTemp(){
String response;
// Send value to the cloud
// similar to Blynk.virtualWrite()
sensors.requestTemperatures();
sensors.getTempCByIndex(0);
float h = sensors.getTempCByIndex(0);
h = h*10;
h=round(h);
float t = h;
t=t/10;
Serial.print("Sending value: ");
Serial.println(t);
String putData = String("[\"") + t + "\"]";
if (httpRequest(String("PUT /") + auth + "/update/V2", putData, response)) {
if (response.length() != 0) {
Serial.print("WARNING: ");
Serial.println(response);
}
}
int sensorValue = analogRead(ADC_BATTERY);
float voltage = sensorValue*(4.3/1023);
int percent=((voltage-3.6)/0.6)*100;
String putVoltage = String("[\"") + voltage + "\"]";
if (httpRequest(String("PUT /") + auth + "/update/V3", putVoltage, response)) {
if (response.length() != 0) {
Serial.print("WARNING: ");
Serial.println(response);
}
}
String putPercent = String("[\"") + percent + "\"]";
if (httpRequest(String("PUT /") + auth + "/update/V4", putPercent, response)) {
if (response.length() != 0) {
Serial.print("WARNING: ");
Serial.println(response);
}
}
}
void setup()
{
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
delay(100);
Serial.println();
Serial.println();
connectNetwork();
timer.setInterval(30000, ReadTemp);
}
void loop() {
timer.run();
// For more HTTP API, see http://docs.blynkapi.apiary.io
// Wait
}