Ho razionalizzato il codice:
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
#include <math.h>
#include <GSM.h>
#include <string.h>
#include <stdlib.h>
// Variabili Dichiarazione
// Display
Adafruit_PCD8544 display = Adafruit_PCD8544(8, 6, 5, 4, 9);
byte Btn;
boolean Caldaia = false;
boolean Manuale = false;
boolean notConnected = true;
float TempAmb;
float SetPoint = 20.5;
unsigned long time_attivazione;
#define RELEPin 12
// PIN Number for the SIM
#define PINNUMBER ""
// initialize the library instances
GSM gsmAccess;
GSM_SMS sms;
// Array to hold the number a SMS is retreived from
char senderNumber[14];
// Funzione traduzione Temp Locale
float Thermister(int RawADC) {
float Temp;
Temp = log(((10240000/RawADC) - 10000));
Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
Temp = Temp - 273.15; // Convert Kelvin to Celcius
return Temp;
}
// RAM libera
int freeRam () {
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}
void InviaSMS(byte Type) {
char msg_sms[70] = "";
char buffer[20] = "";
dtostrf(TempAmb, 3, 1, buffer);
switch (Type) {
case 1:
strcpy(msg_sms, "TempAtt: ");
strcat(msg_sms, buffer);
strcat(msg_sms, " - ACCENSIONE caldaia a: ");
dtostrf(SetPoint, 3, 1, buffer);
strcat(msg_sms, buffer);
break;
case 2:
strcpy(msg_sms, "TempAtt: ");
strcat(msg_sms, buffer);
strcat(msg_sms, "- SPEGNIMENTO caldaia");
break;
case 3:
strcpy(msg_sms, "TempAtt: ");
strcat(msg_sms, buffer);
strcat(msg_sms, " - Manuale: ");
if (Manuale) strcat(msg_sms, "ON - Caldaia: ");
else strcat(msg_sms, "OFF - Caldaia: ");
if (Caldaia) strcat(msg_sms, "ON");
else strcat(msg_sms, "OFF");
break;
}
sms.beginSMS(senderNumber);
sms.print(msg_sms);
sms.endSMS();
}
void DisegnaLCD(float TempAmb){
display.clearDisplay();
display.setTextSize(1);
// Scrivo la temperatura rilevata dal sensore
display.setCursor(0,0);
display.print("TempAmb:");
display.setCursor(54,0);
display.print(TempAmb);
// Scrivo la temperatura impostata da pulsanti
display.setCursor(0,10);
display.print("TempOtt:");
display.setCursor(54,10);
display.print(SetPoint);
// Scrivo lo stato del Termostato Interno
display.setCursor(0,20);
display.print("Manuale:");
display.setCursor(65,20);
if (Manuale) {
display.print("ON");
} else {
display.print("OFF");
}
//Scrivo lo stato della Caldaia
display.setCursor(0,30);
display.print("Caldaia:");
display.setCursor(65,30);
if (Caldaia) {
display.print("ON");
} else {
display.print("OFF");
}
display.setCursor(0,40);
display.print("GSM: ");
display.setCursor(65,40);
if (notConnected) {
display.print("OFF");
} else {
display.print("ON");
}
// Mando tutto al display
display.display();
}
void AccendiCaldaia() {
if (millis() - time_attivazione > 120000) {
// Devo eccitare il rele
digitalWrite(RELEPin, LOW);
// Mostro a schermo lo stato caldaia
Caldaia = 1;
time_attivazione = millis();
}
}
// 120000 -> 2 minuti
void SpegniCaldaia() {
if (millis() - time_attivazione > 120000) {
// Spengo il rele
digitalWrite(RELEPin, HIGH);
Caldaia = 0;
time_attivazione = millis();
}
}
void setup() {
pinMode(RELEPin, OUTPUT);
//Spengo la caldaia
digitalWrite(RELEPin, HIGH);
display.begin();
display.setContrast(60);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(0,0);
display.print("TERMOSTATO GSM");
display.setCursor(10,30);
display.print("GSM on ....");
display.display();
// initialize serial communications and wait for port to open:
Serial.begin(9600);
Serial.println(freeRam());
delay(1000);
}
void loop() {
// Varibili
char c;
// variabili per il controllo del mittente
byte i;
// String messaggio;
char cmd[5];
char msg[60] = "";
byte TypeOp;
// Controllo L'INPUT dei pulsanti
Btn = analogRead(1);
if (Btn < 1000) {
// Cambio stato Controllo Manuale
if ( (Btn >650) && (Btn <700) ) {
Manuale = !Manuale;
}
// ---
if (Btn < 20 ) {
SetPoint -= 0.5;
}
// +++
if ( (Btn >500) && (Btn <520) ) {
SetPoint += 0.5;
}
}
TempAmb = Thermister(analogRead(0));
// Controllo se devo accendere la caldaia manualmente
if (Manuale) {
if (TempAmb < SetPoint) {
AccendiCaldaia();
} else {
SpegniCaldaia();
}
} else {
SpegniCaldaia();
}
DisegnaLCD(TempAmb);
if (notConnected) {
if(gsmAccess.begin(PINNUMBER)==GSM_READY)
notConnected = false;
}
// If there are any SMSs available()
if (sms.available()) {
Serial.print("RAM: ");
Serial.println(freeRam());
// Get remote number
sms.remoteNumber(senderNumber, 14);
if (memcmp(senderNumber, "+76435765439",13) == 0 ) {
if(sms.peek()=='#') {
sms.flush();
}
// Read message bytes and print them
i = 0;
while(c=sms.read()) {
msg[i] = c;
i++;
}
if (memcmp(msg, "Caldaia ON ", 11) == 0) {
SpegniCaldaia();
for (i=0; i<=4; i++) {
cmd[i] = msg[i+10];
}
SetPoint = atof(cmd);
Manuale = true;
AccendiCaldaia();
TypeOp = 1;
}
if (memcmp(msg, "Caldaia OFF", 11) == 0) {
Manuale = false;
SpegniCaldaia();
TypeOp = 2;
}
if (memcmp(msg, "Stato", 5) == 0) TypeOp = 3;
// Delete message from modem memory
sms.flush();
delay(500);
InviaSMS(TypeOp);
} else {
Serial.println("Messaggio scartato");
// Delete message from modem memory
sms.flush();
}
}
// Serial.println("Nessun SMS");
delay(500);
}
Cosa potrei fare per risparmiare un po' di ram ancora ??
Ora sono liberi 290 ...