Good morning all,
Relatively new to Arduino, I'm having some issues with my project, in finding consistency for a long term running code for a smart agricolture project. I would really appreciate any feedback from people who are successfully using low power capability with periodical sensors reading and recurrent connections.
I have a GSM 1400 with 2 analogic soil moisture sensor connected, which should read every (example) 5 minutes and send to my web server where a PHP script put the readings into a MYSQL database. All is powered by a 12V 7Ah battery with 20W solar panel and voltage regulator with USB 5V power output, which I use to power the GSM 1400 by its micro-usb input port.
This is the very 'core' code of the program:
#include <SPI.h>
#include <MKRGSM.h>
// PIN Number
#define PINNUMBER "999999999"
// APN data
#define GPRS_APN "" // replace your GPRS APN
#define GPRS_LOGIN "" // replace with your GPRS login
#define GPRS_PASSWORD "" // replace with your GPRS password
// GSM PARAMETERS
GSMClient client;
GPRS gprs;
GSM gsmAccess;
// CLOUD SERVER
char servername[] = "some.server.com";
int port = 80;
// MILLIS ROUTINE
unsigned long previousMillis = 0;
const long interval = 5L * 60L * 1000L;
// SENSORS READINGS
int hum1, hum2;
// DEFINIZIONE PARAMETRI DI LETTURA SENSORI ANALOGICI
const int SENSOR_PIN_1 = A1;
const int SENSOR_PIN_2 = A2;
const int MAXADC = 1023;
const int MINADC = 0;
// SETUP
void setup() {
gsm_start();
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
gsm_start();
// SENSOR READING
hum1 = map(analogRead(SENSOR_PIN_1), MINADC, MAXADC, 0, 100);
hum2 = map(analogRead(SENSOR_PIN_2), MINADC, MAXADC, 0, 100);
// SEND TO DB
if (client.connect(servername, port))
{
[i]routine for sending data is here - omitted[/i]
client.stop();
}
}
gsmAccess.shutdown();
}
void gsm_start()
{
bool connected = false; // connection state
while (!connected) {
if ((gsmAccess.begin(PINNUMBER) == GSM_READY) &&
(gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) {
connected = true;
Serial.println("connected");
} else {
Serial.println("Not connected");
delay(1000);
}
}
// LOW POWER ON
gsmAccess.lowPowerMode();
}
Basically what I'm trying to write is a program to connect internet + read sensor + send data, at a given interval, saving as much as power possible while not connected.
Any support, advise or feedback would be very appreciated.
Many thanks!
Michele