Hello everyone!!
I built an irrigation system for my bonsai collection using an Arduino UNO + SIM900 shield which acts over 2 solenoid valves using blynk server. Blynk library version is 0.6.1.
Blynk runs ok, but it disconnects and reconnects aproximately each 12 hours, and I am struggling to find the error.
Now, in summer the irrigation should be aprox. each 5 hours during 1 minute meanwhile the sun is up, then I need 2 timers per valve/bonsai zone.
To set the timers, I simply used several Timer widget related with a timer dedicated vitual pin. I added a led widget just to know the status of valves.
Some times this lost of connections occurred while watering and the close valve instrucction did not arrive to the system. The next day I found my bonsai overflooded.
To avoid this, I included Blynk.connected function as a safe interlock position runned every 5 min.
This is the complete code.
Thanks in advance for your support.
#define BLYNK_PRINT Serial
// Select your modem:
#define TINY_GSM_MODEM_SIM900
// Default heartbeat interval for GSM is 60
// If you want override this value, uncomment and set this option:
//#define BLYNK_HEARTBEAT 30
#include <TinyGsmClient.h>
#include <BlynkSimpleTinyGSM.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";
// Your GPRS credentials
// Leave empty, if missing user or pass
char apn[] = "orangeworld";
char user[] = "orange";
char pass[] = "orange";
// Hardware Serial on Mega, Leonardo, Micro
//#define SerialAT Serial1
// or Software Serial on Uno, Nano
#include <SoftwareSerial.h>
SoftwareSerial SerialAT(7, 8); // RX, TX
TinyGsm modem(SerialAT);
BlynkTimer timer; // Create a Timer object called "timer"!
//Valves Pin definition
#define pinMesa 6
#define pinTroncos 5
void setup()
{
//SIM SHIELD SWICTH ON
pinMode(9,OUTPUT);
digitalWrite(9,HIGH);
delay(1000);
digitalWrite(9,LOW);
delay(5000);
//PULL_UP RELAYS
pinMode(pinMesa,OUTPUT);
digitalWrite(pinMesa,HIGH);
pinMode(pinTroncos,OUTPUT);
digitalWrite(pinTroncos,HIGH);
// Debug console
Serial.begin(19200);
delay(10);
// Set GSM module baud rate
SerialAT.begin(19200);
delay(3000);
// Restart takes quite some time
// To skip it, call init() instead of restart()
Serial.println("Initializing modem...");
modem.restart();
delay(5000);
// Unlock your SIM card with a PIN
modem.simUnlock("");
Blynk.begin(auth, modem, apn, user, pass);
timer.setInterval(30000L, conexion);
}
void loop()
{
Blynk.run();
timer.run(); // BlynkTimer is working...
}
//TIMERS MESA
BLYNK_WRITE(V1){ // Timer1
int flagMesa = param.asInt(); //Timer 1 status reception
if (flagMesa == 1){
digitalWrite(pinMesa,LOW); //Opens Valve and water
Blynk.virtualWrite(V7,255); //app Led On
}
else{
digitalWrite(pinMesa,HIGH); //Valve closes and no watering
Blynk.virtualWrite(V7,0); //app Led Off
}
}
BLYNK_WRITE(V2){ // Timer2
int flagMesa = param.asInt(); //Recibimos el status del timer 2
if (flagMesa == 1){
digitalWrite(pinMesa,LOW); //Opens Valve and watering
Blynk.virtualWrite(V7,255);
}
else{
digitalWrite(pinMesa,HIGH); // Valve closes and no watering
Blynk.virtualWrite(V7,0);
}
}
BLYNK_WRITE(V3){ // Timer3
int flagMesa = param.asInt(); //Recibimos el status del timer 3
if (flagMesa == 1){
digitalWrite(pinMesa,LOW); //Abre la válvula y riega
Blynk.virtualWrite(V7,255);
}
else{
digitalWrite(pinMesa,HIGH); // Cierra la válvula y no riega
Blynk.virtualWrite(V7,0);
}
}
//TIMERS TRONCOS
BLYNK_WRITE(V4){ // Timer4
int flagTroncos = param.asInt(); //Recibimos el status del timer 4
if (flagTroncos == 1){
digitalWrite(pinTroncos,LOW); //Abre la válvula y riega
Blynk.virtualWrite(V8,255);
}
else{
digitalWrite(pinTroncos,HIGH); // Cierra la válvula y no riega
Blynk.virtualWrite(V8,0);
}
}
BLYNK_WRITE(V5){ // Timer5
int flagTroncos = param.asInt(); //Recibimos el status del timer 5
if (flagTroncos == 1){
digitalWrite(pinTroncos,LOW); //Abre la válvula y riega
Blynk.virtualWrite(V8,255);
}
else{
digitalWrite(pinTroncos,HIGH); // Cierra la válvula y no riega
Blynk.virtualWrite(V8,0);
}
}
BLYNK_WRITE(V6){ // Timer6
int flagTroncos = param.asInt(); //Recibimos el status del timer 6
if (flagTroncos == 1){
digitalWrite(pinTroncos,LOW); //Abre la válvula y riega
Blynk.virtualWrite(V8,255);
}
else{
digitalWrite(pinTroncos,HIGH); //Cierra la válvula y no riega
Blynk.virtualWrite(V8,0);
}
}
BLYNK_WRITE(V9){ // MANUAL CONTROL Zone 1
int flagMesa = param.asInt(); //Button status
if (flagMesa == 1){
digitalWrite(pinMesa,LOW); //Open the valve and watering
Blynk.virtualWrite(V7,255);
}
else{
digitalWrite(pinMesa,HIGH);
Blynk.virtualWrite(V7,0);
}
}
BLYNK_WRITE(V10){ // MANUAL CONTROL Zone 2
int flagTroncos = param.asInt(); //Recibimos el status del boton manual
if (flagTroncos == 1){
digitalWrite(pinTroncos,LOW); //Abre la válvula y riega
Blynk.virtualWrite(V8,255);
}
else{
digitalWrite(pinTroncos,HIGH); //Cierra la válvula y no riega
Blynk.virtualWrite(V8,0);
}
}
// SAFE POSSITION INTERLOCK. ALL VALVES CLOSED.
void conexion(){
bool conectado = Blynk.connected();
if(conectado == 0){
digitalWrite(pinTroncos,HIGH);
digitalWrite(pinMesa,HIGH);
}}