Ciao a tutti,
Avevo fatto questo progetto molto più in grande su un Arduino mega e non mi aveva dato nessun problema, adesso ho provato a spostarlo su un Arduino uno per poi fare uno stand-alone ma ho tantissimi problemi di memoria. Volevo chiedere a voi esperti se è normale che finisco sia la flash che la ram con questo sketch ? a me non sembra tanto grosso ma sono al limite con la flash e se ad esempio aggiungo qualche altra riga sul client senza usare la sd mi si impalla perché finisce la ram. Secondo voi sbaglio qualcosa io ho sono le normali limitazioni del chip ATmega328.
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h> // serve solo per che i pin della sd sia indeterminati
#include <Time.h>
#include <OneWire.h>
#include <DallasTemperature.h>
String HTTP_req; // stores the HTTP request
// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 0,...); // IP address, may need to change depending on network
EthernetServer server(...); // create a server at port 80
File webFile; // the web page file on the SD card
byte Pin_termometro = 2;
byte Pin_riscaldatore = 3;
byte Pin_lampade = 7;
OneWire oneWire(Pin_termometro);
DallasTemperature sensors_dallas(&oneWire);
float temp_acquario=0; //dichiarazione di variabile
float temp_impostata=25.2; //dichiarazione di variabile
int luci_on=12;
int luci_off=22;
void setup()
{
/*
pinMode(SS_PIN, OUTPUT);
digitalWrite(SS_PIN, 1);
pinMode(10, OUTPUT);
digitalWrite(10, 1);
pinMode(4, OUTPUT);
digitalWrite(4, 1);
*/
pinMode(Pin_riscaldatore, OUTPUT);
pinMode(Pin_lampade, OUTPUT);
Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin();
// start to listen for clients
Serial.begin(9600);
setTime(23,59,59,31,12,1999);
sensors_dallas.begin();
if (!SD.begin(4)) {
Serial.println("SD init failed");
return; // init failed
}
// check for index.htm file
if (!SD.exists("index.htm")) {
Serial.println("NOt find index.htm");
return; // can't find index file
}
}
void loop()
{
EthernetClient client = server.available(); // try to get client
sensors_dallas.requestTemperatures();
//per evitare le fluttuazione che farebbere accendere e spenere il riscaldatore
if (abs(temp_acquario-sensors_dallas.getTempCByIndex(0))>0.2){
temp_acquario=sensors_dallas.getTempCByIndex(0);
}
Serial.print("temperatura acqua: ");
Serial.println(temp_acquario);
if (temp_acquario<temp_impostata){
digitalWrite(Pin_riscaldatore, HIGH);
Serial.println("riscalcamendo ON");
}
else{
digitalWrite(Pin_riscaldatore, LOW);
Serial.println("riscalcamendo OFF");
}
if (hour()>luci_on && hour()<luci_off){
digitalWrite(Pin_lampade, HIGH);
Serial.println("lampade ON");
}
else{
digitalWrite(Pin_lampade, LOW);
Serial.println("lampade OFF");
}
if (client) { // got client?
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) { // client data available to read
char c = client.read(); // read 1 byte (character) from client
HTTP_req += c;
if (c == '\n') {
// send a standard http response header
Serial.println(HTTP_req);
client.println("HTTP/1.1 200 OK");
if (HTTP_req.indexOf("ora_data") > 0) {
char charBuf[40];
HTTP_req.toCharArray(charBuf, 30) ;
char delimiters[] = "=, ";
char* valPosition;
valPosition = strtok(charBuf, delimiters);
int tempo[] = {0, 0, 0,0,0,0,0,0};
for(int i = 0; i < 8; i++){
tempo[i] = atoi(valPosition);
valPosition = strtok(NULL, delimiters);
}
setTime(tempo[2],tempo[3],tempo[4],tempo[5],tempo[6],tempo[7]);
}
// AJAX request for switch state
if (HTTP_req.indexOf("aggiorna_XML") > -1) {
// read switch state and send appropriate paragraph text
client.println("Content-Type: text/xml");
client.println("Connection: keep-alive");
client.println("");
GetXMLdata(client);
}
else { // HTTP request for web page
client.println("Content-Type: text/html");
client.println("Connection: keep-alive");
client.println("");
webFile = SD.open("index.htm"); // open web page file
if (webFile) {
while(webFile.available()) {
client.write(webFile.read()); // send web page to client
}
webFile.close();
}
}
HTTP_req="";
break;
}
}
}
delay(1);
client.stop();
}
}
// send the state of the switch to the web browser
void GetXMLdata(EthernetClient cl)
{
cl.print("<?xml version = \"1.0\" ?><inputs><orario1>");
if (hour()<10){
cl.print("0");
}
cl.print(hour());
cl.print(":");
if (minute()<10){
cl.print("0");
}
cl.print(minute());
cl.print(":");
if (second()<10){
cl.print("0");
}
cl.print(second());
cl.println("</orario1><data1>");
cl.print(day());
cl.print("/");
cl.print(month());
cl.print("/");
cl.print(year());
cl.println("</data1><temp1>");
cl.print(temp_acquario);
cl.println("</temp1><luci1>");
cl.println("");
if(digitalRead(Pin_lampade)){
cl.print("ON");
}else{
cl.print("OFF");
}
cl.println("</luci1><risc1>");
if(digitalRead(Pin_riscaldatore)){
cl.print("ON");
}else{
cl.print("OFF");
}
cl.println("</risc1></inputs>");
}
Grazie a tutti.
P.S. se a qualcuno interessa lo sketch posso darli altre informazioni ed evoluzioni.