Hi,
I'm using an Arduino UNO + Gsm shield in a project that aims at controlling an instrument remotely. It runs continuously without problems for the first week approximately, but after that the Uno seems unresponsive and it does not process any sms received and looks unresponsive. The only way in order to get back to the normal functioning is by pushing the reset button on the UNO board.
I don't know whether it can help to understand the issue, but when I call the mobile number, the line is ok.
Thank you for help
The following is the code:
# www.biemmeitalia.net
#include <GSM.h>
#include <MemoryFree.h> //library for calculate free memory
#define WATCHDOG 1 //commentare questa riga per disabilitare il watchdog
#ifdef WATCHDOG
#include <avr/wdt.h> //watchdog library
#endif
//#define DEBUG 1
#define SOGLIA_MEMORIA_DISPONIBILE 600
#define SENDSMS 1
#define PIN_SONDATEMP 1
#define PINCALDAIA_ON 8
#define PIN_TEMP_MINIMA 9
#define ledantigelo 4
#define PINALARM 10
//impostazione da pulsanti su box
#define PIN_ON 5
#define PIN_OFF 6
//numero di campionamenti analogica
#define NUM_LETTURE_AN 30
#define SET_TEMP 2.0
#define DELTA 5.0
// initialize the library instances
GSM gsmAccess;
GSM_SMS sms;
char senderNumber[20];
char remoteNumM[20]="33912345678";
char remoteNumCliente[20]="34712345677";
byte flagAbilitaLogica = 1;
byte flagSMSAllarme = 0;
byte flagSMSRicevutoON = 0;
byte flagSMSRicevutoOFF= 0;
byte flagTempSottoZero = 0;
byte flagTempNegativa = 0;
byte flagSMSDestinatarioM = 0;
//stato letture pin digitali
byte statoPinON = 0;
byte statoPinOFF = 0;
byte statoPinALARM = 0;
//logica temperatura
int count = 0;
unsigned long cum = 0;
int reading;
double temp_avg = 150.0;
double temperatureC = 15.0;
void setup()
{
#ifdef DEBUG
Serial.begin(9600);
#endif
#ifdef DEBUG
Serial.println("init pins");
#endif
pinMode(PINCALDAIA_ON, OUTPUT);
pinMode(PIN_TEMP_MINIMA, OUTPUT);
pinMode(PINALARM, INPUT_PULLUP);
pinMode(ledantigelo,OUTPUT);
pinMode(PIN_ON, INPUT_PULLUP);
pinMode(PIN_OFF,INPUT_PULLUP);
digitalWrite(PINCALDAIA_ON,LOW);
digitalWrite(PIN_TEMP_MINIMA,LOW);
// connection state
boolean notConnected = true;
// Start GSM connection
while(notConnected)
{
if(gsmAccess.begin()==GSM_READY)
notConnected = false;
else
{
#ifdef DEBUG
Serial.println("Not connected");
#endif
delay(1000);
LogicaTemperatura();
}
}
#ifdef DEBUG
Serial.println("GSM initialized");
Serial.println("Waiting for messages");
#endif
delay(1000);
#ifndef WATCHDOG
sendAlarmSMS("ENEL riattivato.");
#endif
#ifdef WATCHDOG
wdt_enable(WDTO_8S);
#endif
}
void loop()
{
String smsTextReceived;
char c;
// If there are any SMSs available()
if (sms.available()){
Serial.println("Message received from:");
sms.remoteNumber(senderNumber, 20);
#ifdef DEBUG
Serial.println(senderNumber);
#endif
smsTextReceived="";
while(c=sms.read()){
smsTextReceived+=c;
#ifdef DEBUG
Serial.print(c);
#endif
}
#ifdef DEBUG
Serial.println("\nEND OF MESSAGE");
#endif
// Delete message from modem memory
sms.flush();
#ifdef DEBUG
Serial.println("MESSAGE DELETED");
#endif
//se ricevo un sms con il testo "on"
if (smsTextReceived.equalsIgnoreCase("on")){
flagSMSRicevutoON = 1;
}
if (smsTextReceived.equalsIgnoreCase("off")){
flagSMSRicevutoOFF = 1;
}
if (smsTextReceived.equalsIgnoreCase("mario")){
flagSMSDestinatarioM = 1;
sendAlarmSMS_M("Messaggi diretti a M.");
}
if (smsTextReceived.equalsIgnoreCase("cliente")){
flagSMSDestinatarioM = 0;
sendAlarmSMS_M("Messaggi diretti al C.");
}
if (smsTextReceived.equalsIgnoreCase("temp")){
char messaggio[100];
String mess;
String str_intero;
String str_virgola;
int intero;
int virgola;
Serial.println(temperatureC);
flagTempNegativa = 0;
if (temperatureC < 0){
flagTempNegativa = 1;
temperatureC = abs(temperatureC);
}
intero = (int)temperatureC;
virgola = (int)((temperatureC - (double)intero)*100.0);
#ifdef DEBUG
Serial.println(intero);
Serial.println(virgola);
#endif
str_intero = String(intero);
str_virgola = String(virgola);
if (flagTempNegativa == 0){
mess = String("Temperatura corrente: " + str_intero + "," + str_virgola);
}else{
mess = String("Temperatura corrente: -" + str_intero + "," + str_virgola);
}
mess.toCharArray(messaggio, mess.length());
sendAlarmSMS(messaggio);
delay(100);
}
}
statoPinON = digitalRead(PIN_ON);
statoPinOFF = digitalRead(PIN_OFF);
if ((flagSMSRicevutoON) || (statoPinON == LOW)){
#ifdef DEBUG
Serial.println("ON");
#endif
digitalWrite(PINCALDAIA_ON, HIGH);
digitalWrite(PIN_TEMP_MINIMA, LOW);
digitalWrite(ledantigelo,LOW);
flagAbilitaLogica = 0;
sendAlarmSMS("Caldaia attiva da termostato ambiente.");
}else{
if ((flagSMSRicevutoOFF) || (statoPinOFF == LOW)){
#ifdef DEBUG
Serial.println("OFF");
#endif
digitalWrite(PINCALDAIA_ON, LOW);
digitalWrite(ledantigelo,HIGH);
cum = 0; //azzera la variabile per il calcolo delle temp medie
flagAbilitaLogica = 1;
count = 0;
sendAlarmSMS("Abilitazione temperatura minima.");
}
}
LogicaTemperatura();
delay(10);
statoPinALARM = digitalRead(PINALARM);
if (statoPinALARM == LOW){
if (!flagSMSAllarme){
sendAlarmSMS("Blocco.");
flagSMSAllarme=!flagSMSAllarme;
}
}else{
flagSMSAllarme=0;
}
flagSMSRicevutoON = 0;
flagSMSRicevutoOFF = 0;
#ifdef WATCHDOG
int mem = freeMemory();
if (mem > SOGLIA_MEMORIA_DISPONIBILE){
wdt_reset();
}
#endif
}
void LogicaTemperatura(){
if (count < NUM_LETTURE_AN){
count++;
reading = analogRead(PIN_SONDATEMP);
cum += reading;
}
else{
temp_avg = (double)cum/(double)NUM_LETTURE_AN;
double voltage = temp_avg * 5.0;
voltage /= 1024.0;
temperatureC = ((voltage - 0.5) * 100)-5.0 ;
#ifdef DEBUG
Serial.print(temperatureC);
Serial.println(" degrees C");
#endif
if (flagAbilitaLogica){
if (temperatureC < SET_TEMP){
#ifdef DEBUG
Serial.println("temp bassa");
#endif
digitalWrite(PIN_TEMP_MINIMA, HIGH);
}else if (temperatureC > SET_TEMP + DELTA){
#ifdef DEBUG
Serial.println("temp alta");
#endif
digitalWrite(PIN_TEMP_MINIMA, LOW);
}
}
count = 0;
cum = 0;
}
}
void sendAlarmSMS(char * txtMsg){
#ifdef SENDSMS
#ifdef DEBUG
Serial.println("SPEDISCO:");
Serial.println();
Serial.println("il seguente messaggio:");
Serial.println(txtMsg);
#endif
if (flagSMSDestinatarioM){
sms.beginSMS(remoteNumM);
}else{
sms.beginSMS(remoteNumCliente);
}
sms.print(txtMsg);
sms.endSMS();
#ifdef DEBUG
Serial.println("\nSpedito!\n");
#endif
#endif
delay(10);
}
void sendAlarmSMS_M(char * txtMsg){
#ifdef SENDSMS
#ifdef DEBUG
Serial.println("SPEDISCO:");
Serial.println();
Serial.println("il seguente messaggio:");
Serial.println(txtMsg);
#endif
sms.beginSMS(remoteNumM);
sms.print(txtMsg);
sms.endSMS();
#ifdef DEBUG
Serial.println("\nSpedito!\n");
#endif
#endif
delay(10);
}