I’m building a project in which I use a Controlino MEGA to operate a system of valves and pumps. While doing that we get feeback from the valves in which position they are. I’m sending these positions to the ubidots dashboard. My controlino is connected to a 4G router with a sim-card via an ethernet cable.
Here is my problem, when I lose connectivity to the internet, lets say due to bad reception, the Controlino freezes. In specific I noticed via the serial monitor that it freezes at the “client.sendall()” function. I gues it is because it awaits a response of the server, but not getting one?
How do I solve this? I will post my code below:
#include <UbidotsEthernet.h>
#include <Controllino.h>
#include <Ethernet.h>
#include <SPI.h>
#include “Mudbus.h”
Mudbus Mb;
#define TOKEN “BBFF-o5PHDvp2QUW8SUjH1P0qzXQd9kQ1z6” // Put here your Ubidots TOKEN ----------
#define DEVICE_LABEL “controllino” // Put here your Ubidots variable ID ------------
#define ON_BUTTON “onbutton”
#define OFF_BUTTON “offbutton”
#define FLOAT_SWITCH “floatswitch”
#define VALVE1OPENSTATE “valve1openstate”
#define VALVE1CLOSESTATE “valve1closestate”
#define VALVE2OPENSTATE “valve2openstate”
#define VALVE2CLOSESTATE “valve2closestate”
#define VALVE3OPENSTATE “valve3openstate”
#define VALVE3CLOSESTATE “valve3closestate”
#define VALVE4OPENSTATE “valve4openstate”
#define VALVE4CLOSESTATE “valve4closestate”
#define POTENTIOMETER1FLUSHTIME “potentiometer1flushtime”
#define PRESSURETRANSMITTER1 “pressuretransmitter1”
#define PROCESSPHASE “processphase”
const long UpdateInterval = 5000;
const long UpdateInterval2 = 6000;
const long UpdateInterval3 = 7000;
const long UpdateInterval4 = 7000;
unsigned long PreviousMillis = 0;
unsigned long PreviousMillis2 = 5000;
unsigned long PreviousMillis3 = 10000;
unsigned long PreviousMillis4 = 6000;
unsigned long SubtractionCounter = 0;
unsigned long LastMillis =0;
unsigned long TimerInterval = 10000;
unsigned long TimeLeft =0;
bool OnButtonState;
bool OffButtonState;
bool FloatSwitch;
bool Valve1OpenState;
bool Valve1CloseState;
bool Valve2OpenState;
bool Valve2CloseState;
bool Valve3OpenState;
bool Valve3CloseState;
bool Valve4OpenState;
bool Valve4CloseState;
bool ShortFlushingFilter1 = 0;
bool ShortFlushingFilter2 = 0;
bool FlushingFilter1 = 0;
bool FlushingFilter2 = 0;
bool Filtering = 0;
int ProcessPhase = 0;
int MemoryRelay1 = 0; //Machine on memory
int MemoryRelay2 = 0; //Pauze memory
int MemoryRelay3 = 0; //Timer memory relay
int MemoryRelay4 = 0; //timer memory relay
int MemoryRelay5 = 0; //Short Flushing memory Relay
int MemoryRelay6 = 0; //Short Flushing memory Relay
int MemoryRelay7 = 0; //Filtering Relay
int MemoryRelay8 = 0; //Normal flushing Relay
int MemoryRelay9 = 0; //Normal flushing Relay
int MemoryRelay10 = 0; //Normal flushing Relay
unsigned long ShortFlushingInterval = 5000;
unsigned long FlushingInterval = 5000;
unsigned long FilteringInterval = 5000;
unsigned long TimerZero;
int Valve1OpenSignal = 2;
int Valve2OpenSignal = 3;
int Valve3OpenSignal = 4;
int Valve4OpenSignal = 5;
int Valve5CloseSignal=31;
int Pump1_Pin = 30;
bool LastOnButtonState = HIGH;
int Pot1Interval;
int Pot2Flushtime;
int PressureTransmitter1;
unsigned long CurrentMillis;
unsigned long CurrentMillis2; // debounce for pauze button (second time pressing onbutton)
unsigned long LastDebounceTime = 0; // the last time the output pin was toggled
unsigned long DebounceDelay = 100; // the debounce time; increase if the output flickers
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 1, 226);
Ubidots client(TOKEN);
void setup(){
uint8_t mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
uint8_t ip[] = { 192, 168, 1, 226 };
uint8_t gateway[] = { 192, 168, 0, 254 };
uint8_t subnet[] = { 255, 255, 255, 0 };
Ethernet.begin(mac, ip, gateway, subnet);
client.setDebug(100);
Serial.begin(9600);
/*
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
}
// give the Ethernet shield a second to initialize:
delay(1000);
*/
Ethernet.begin(mac, ip);
client.setDeviceLabel(DEVICE_LABEL);
pinMode(CONTROLLINO_A0, INPUT); //Valve1 open state
pinMode(CONTROLLINO_A1, INPUT); //Valve1 close state
pinMode(CONTROLLINO_A2, INPUT); //Valve2 open state
pinMode(CONTROLLINO_A3, INPUT); //Valve2 close state
pinMode(CONTROLLINO_A4, INPUT); //Valve3 open state
pinMode(CONTROLLINO_A5, INPUT); //Valve3 close state
pinMode(CONTROLLINO_A6, INPUT); //Valve4 open state
pinMode(CONTROLLINO_A7, INPUT); //Valve4 close state
pinMode(CONTROLLINO_A8, INPUT); //Valve4 open state
pinMode(CONTROLLINO_A9, INPUT); //Valve4 close state
pinMode(CONTROLLINO_I16, INPUT); //on button
pinMode(CONTROLLINO_I17, INPUT); //off button
pinMode(CONTROLLINO_I18, INPUT); //float switch
pinMode(CONTROLLINO_D0, OUTPUT); //Valve1 High = open LOW = close
pinMode(CONTROLLINO_D1, OUTPUT); //Valve2 High = open LOW = close
pinMode(CONTROLLINO_D2, OUTPUT); //Valve3 High = open LOW = close
pinMode(CONTROLLINO_D3, OUTPUT); //Valve4 High = open LOW = close
pinMode(CONTROLLINO_D4, OUTPUT); //Valve5 High = open LOW = close
pinMode(CONTROLLINO_D5, OUTPUT); // Pump1 On Signal
pinMode(CONTROLLINO_D6, OUTPUT); // Pump1 On Signal
pinMode(CONTROLLINO_R6, OUTPUT); // Pump1 On Signal
pinMode(CONTROLLINO_R7, OUTPUT); // Pump1 On Signal
pinMode(CONTROLLINO_R8, OUTPUT); // Pump1 On Signal
pinMode(CONTROLLINO_R9, OUTPUT); // Pump1 On Signal
}
void loop(){
/* Reading the sensor */
OnButtonState = digitalRead(CONTROLLINO_I16);
OffButtonState = digitalRead(CONTROLLINO_I17);
FloatSwitch = digitalRead(CONTROLLINO_I18);
Valve1OpenState = digitalRead(CONTROLLINO_A0);
Valve1CloseState = digitalRead(CONTROLLINO_A1);
Valve2OpenState = digitalRead(CONTROLLINO_A2);
Valve2CloseState = digitalRead(CONTROLLINO_A3);
Valve3OpenState = digitalRead(CONTROLLINO_A4);
Valve3CloseState = digitalRead(CONTROLLINO_A5);
Valve4OpenState = digitalRead(CONTROLLINO_A6);
Valve4CloseState = digitalRead(CONTROLLINO_A7);
Pot1Interval = analogRead(CONTROLLINO_A8);
Pot2Flushtime = analogRead(CONTROLLINO_A9);
// PressureTransmitter1 = analogRead(CONTROLLINO_AI10);
Mb.Run();
//digitalWrite(OnButtonState, Mb.R[0]);
//digitalWrite(Valve1OpenSignal, Mb.R[2]);
//Mb.R[1] = digitalRead(Valve2OpenState);
//Mb.R[3] = analogRead(Valve2OpenState);
StartButton();
ShortFlushingFilterProgram();
FilterFlushing();
Timer();
UpdateCloud();
CurrentMillis = millis();
}
void UpdateCloud(){
if ((CurrentMillis-PreviousMillis) >= UpdateInterval){
// Posting the variables to Ubidots
PreviousMillis = CurrentMillis;
client.add(ON_BUTTON, OnButtonState);
client.add(OFF_BUTTON, OffButtonState);
client.add(FLOAT_SWITCH, FloatSwitch);
client.add(VALVE1OPENSTATE, Valve1OpenState);
client.sendAll();
}
if ((CurrentMillis-PreviousMillis2) >= UpdateInterval2){
PreviousMillis2 = CurrentMillis;
client.add(VALVE1CLOSESTATE, Valve1CloseState);
client.add(VALVE2OPENSTATE, Valve2OpenState);
client.add(VALVE2CLOSESTATE, Valve2CloseState);
client.add(VALVE3OPENSTATE, Valve3OpenState);
client.sendAll();
}
if ((CurrentMillis-PreviousMillis3) >= UpdateInterval3){
PreviousMillis3 = CurrentMillis;
client.add(VALVE3CLOSESTATE, Valve3CloseState);
client.add(VALVE4OPENSTATE, Valve4OpenState);
client.add(VALVE4CLOSESTATE, Valve4CloseState);
client.add(POTENTIOMETER1FLUSHTIME, Pot1Interval);
client.sendAll();
}
if ((CurrentMillis-PreviousMillis4) >= UpdateInterval4){
PreviousMillis4 = CurrentMillis;
client.add(PRESSURETRANSMITTER1, PressureTransmitter1);
client.add(PROCESSPHASE, ProcessPhase);
client.sendAll();
}
}
void StartButton(){
/*Serial.print(“Waarde LastOnButtonState”);
Serial.print(LastOnButtonState);
Serial.print(“Waarde OnButtonState”);
Serial.print(OnButtonState);*/
Serial.print(“Waarde Memory5”);
Serial.print(MemoryRelay5);
Serial.print(“Waarde Memory6”);
Serial.print(MemoryRelay6);
Serial.print(“Waarde Memory7”);
Serial.print(MemoryRelay7);
Serial.print(“Waarde Memory8”);
Serial.print(MemoryRelay8);
Serial.print(“Waarde Memory9”);
Serial.print(MemoryRelay9);
Serial.print(“Waarde Memory10”);
Serial.print(MemoryRelay10); /*
Serial.print(“Tijd in sec:”);
Serial.print(millis()/1000);
Serial.print(“Tijd aftrek in sec:”);
Serial.print(SubtractionCounter); */
Serial.print(“Pot1Interval Waarde:”);
Serial.print(Pot1Interval);
Serial.print(“Tijd te gaan in sec:”);
Serial.println(TimerInterval/1000);
if (OnButtonState != LastOnButtonState){
LastDebounceTime = millis();
}
//start butten debounce
if (((millis()-LastDebounceTime) >=DebounceDelay) && MemoryRelay1 == 0){
LastOnButtonState = OnButtonState;
MemoryRelay1 = 1;
MemoryRelay2 = 0;
MemoryRelay3 = 1;
MemoryRelay4 = 0;
digitalWrite(Pump1_Pin,HIGH);
ShortFlushingFilter1 = 1;
ShortFlushingFilter2 = 0;
FlushingFilter1 = 0;
FlushingFilter2 = 0;
Filtering = 0;
}
if (OnButtonState==0 && MemoryRelay1 ==1){
MemoryRelay1 = 2;
}
// pauze debounce
if (((millis()-LastDebounceTime) >=DebounceDelay) && MemoryRelay1 == 2){
LastOnButtonState = OnButtonState;
MemoryRelay2 = 1;
MemoryRelay3 = 0;
MemoryRelay4 = 1;
digitalWrite(Pump1_Pin,LOW);
}
if (FloatSwitch == 1 & MemoryRelay10 ==0){
MemoryRelay2 = 1;
MemoryRelay3 = 0;
MemoryRelay4 = 1;
MemoryRelay10 = 1;
digitalWrite(Pump1_Pin,LOW);
}
if(FloatSwitch == 0 && MemoryRelay10 == 1){
MemoryRelay2 = 0;
MemoryRelay3 = 1;
MemoryRelay4 = 0;
MemoryRelay10 = 0;
digitalWrite(Pump1_Pin,HIGH);
}
if (OnButtonState==0 && MemoryRelay1 ==2 && MemoryRelay2 ==1){
MemoryRelay1 = 0;
}
if(OffButtonState==1){
MemoryRelay1 = 0;
MemoryRelay2 = 0;
MemoryRelay3 = 0;
MemoryRelay4 = 0;
MemoryRelay5 = 0;
MemoryRelay6 = 0;
MemoryRelay7 = 0;
MemoryRelay8 = 0;
MemoryRelay9 = 0;
MemoryRelay10 = 0;
digitalWrite(Valve1OpenSignal,LOW);
digitalWrite(Valve2OpenSignal,LOW);
digitalWrite(Valve3OpenSignal,LOW);
digitalWrite(Valve4OpenSignal,LOW);
digitalWrite(Pump1_Pin,LOW);
}
}
void ShortFlushingFilterProgram(){
//start shortflushing filter 1
if(MemoryRelay5 ==0 && MemoryRelay1 == 2){
Serial.println(“ShortflushingF1”);
digitalWrite(Valve1OpenSignal,LOW);
digitalWrite(Valve2OpenSignal,HIGH);
digitalWrite(Valve3OpenSignal,HIGH);
digitalWrite(Valve4OpenSignal,LOW);
TimerInterval = 20000;
MemoryRelay5 = 1;
}
//start shortflushing filter 2
if(MemoryRelay5 ==1 && TimerInterval <= 3000 && MemoryRelay6==0){
Serial.println(“ShortflushingF2”);
digitalWrite(Valve1OpenSignal,HIGH);
digitalWrite(Valve2OpenSignal,LOW);
digitalWrite(Valve3OpenSignal,LOW);
digitalWrite(Valve4OpenSignal,HIGH);
MemoryRelay6 = 1;
TimerInterval = 15000;
}
}
void FilterFlushing(){
//start filtering
if (MemoryRelay5 == 1 && MemoryRelay6 ==1 && MemoryRelay7 == 0 && TimerInterval <= 3000){
Serial.println(“Filtering”);
digitalWrite(Valve1OpenSignal,HIGH);
digitalWrite(Valve2OpenSignal,LOW);
digitalWrite(Valve3OpenSignal,HIGH);
digitalWrite(Valve4OpenSignal,LOW);
TimerInterval = 10000;
MemoryRelay7 = 1;
}
//start normal flushing filter 1
if(MemoryRelay5 == 1 && MemoryRelay6 == 1 && MemoryRelay7 == 1 && MemoryRelay8 == 0 && MemoryRelay9 == 0 && TimerInterval <= 3000){
Serial.println(“Normal flushing F1”);
digitalWrite(Valve1OpenSignal,LOW);
digitalWrite(Valve2OpenSignal,HIGH);
digitalWrite(Valve3OpenSignal,HIGH);
digitalWrite(Valve4OpenSignal,LOW);
TimerInterval = 9000;
MemoryRelay8 = 1;
MemoryRelay9 = 0;
}
// start normal flushing filter 2
if(MemoryRelay5 == 1 && MemoryRelay6 ==1 && MemoryRelay7 == 1 && MemoryRelay8 == 1 && MemoryRelay9 == 0 && TimerInterval <= 3000){
Serial.println(“Normal flushing F2”);
digitalWrite(Valve1OpenSignal,HIGH);
digitalWrite(Valve2OpenSignal,LOW);
digitalWrite(Valve3OpenSignal,LOW);
digitalWrite(Valve4OpenSignal,HIGH);
TimerInterval = 8000;
MemoryRelay9 = 1;
}
if(MemoryRelay5 == 1 && MemoryRelay6 ==1 && MemoryRelay7 == 1 && MemoryRelay8 == 1 && MemoryRelay9 == 1 && TimerInterval <= 3000){
Serial.println("Restart");
TimerInterval = 8000;
MemoryRelay7 = 0;
MemoryRelay8 = 0;
MemoryRelay9 = 0;
}
}
void Timer(){
if (MemoryRelay3 ==1 && MemoryRelay4 == 0){
SubtractionCounter = millis()-LastMillis;
}
else{
SubtractionCounter = 0;
}
LastMillis = millis();
TimerInterval =TimerInterval-SubtractionCounter;
if (TimerInterval > 1500000){
TimerInterval = 10000;
}
if(MemoryRelay3 == 1 && MemoryRelay5 == 1 && MemoryRelay6 == 0 && MemoryRelay7 == 0 && MemoryRelay8 ==0 && MemoryRelay9 ==0){ ProcessPhase = 1;}
if(MemoryRelay3 == 1 && MemoryRelay5 == 1 && MemoryRelay6 == 1 && MemoryRelay7 == 0 && MemoryRelay8 ==0 && MemoryRelay9 ==0){ ProcessPhase = 2;}
if(MemoryRelay3 == 1 && MemoryRelay5 == 1 && MemoryRelay6 == 1 && MemoryRelay7 == 1 && MemoryRelay8 ==0 && MemoryRelay9 ==0){ ProcessPhase = 3;}
if(MemoryRelay3 == 1 && MemoryRelay5 == 1 && MemoryRelay6 == 1 && MemoryRelay7 == 1 && MemoryRelay8 ==1 && MemoryRelay9 ==0){ ProcessPhase = 4;}
}