Ciao ragazzi,
ho scritto un piccolo codice per riprodurre un termostato che tramite sonda prenda temperatura e accenda o spenga un frigo o una resistenza riscaldante per mantenere il range impostato.
Ho un problema e non comprendo il motivo.
Dopo alcune ore circa 6-7 (assolutamente non so di preciso) l'arduino non svolge piu' lo switching dei rele'.
Non so se si impalla qualcosa nel software o magari l'alimentazione non e' corretta, io sto usando un alimentatorino (riciclato da un vecchio HUB USB) che e' a 5V.
Non vorrei che questo alimentatore sia troppo piccolo, ma la cosa strana e' che le prime ore funziona senza problemi.
Secondo voi e' un problema sul codice o potrebbe anche essere un prob di alimentazione?
Che alimentatore mi consigliate?
Non sono a casa. Quando ritorno consulteró la mia sfera magica.
Ci dai troppo poche informazioni.
Per aiutarti ci serve lo sketch, il circuito, i dati di targa del alimentatore, il modello di scheda relé che usi e una foto del cablaggio.
Comunque io scometto sullo sketch.
Ciao Uwe
hai ragione... scusa per le poche informazioni, provo a migliorare:
Il progettino e' un "termostato" che accende e spegne un frigorifero e un cavo riscaldante (posto nel frigo) in modo da mantenere un certo range di temperatura che imposto. In piu' con lo shield ethernet (W5100) mi avvia un server web che mi dice la temperatura e mi fa impostare il range.
C'e' anche una funzione "salva frigorifero" che fa si che il frigo non si riaccenda prima di un toto di secondi dall'ultimo spegnimento.
#include <OneWire.h>
#include <DallasTemperature.h>
#include <secTimer.h>
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,2,177);
EthernetServer server(80);
String receivedText = String(255);
int cold = 7;
int hot = 6;
char floatbuf[32];
int MIN = 23;
int MAX = 30;
int time;
secTimer myTimer;
byte state=1;
unsigned long seconds=0;
const int chipSelect = 4;
#define CALDO 0
#define FREDDO 1
int ultimostato = NULL;
int ultimoff = 0;
int secure_time = 120;
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
void setup(void)
{
// start serial port
Serial.begin(9600);
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
// if the file opened okay, write to it:
myTimer.startTimer();
seconds=myTimer.readTimer();
pinMode(cold, OUTPUT);
pinMode(hot, OUTPUT);
// Start up the library
sensors.begin();
}
void loop(void)
{
EthernetClient client = server.available();
if (client) {
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (receivedText.length() < 55) {
receivedText += c;
}
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.print("<html><head><title>Arduino Webserver</title></head><body>");
client.print("La temperatura del sensore e':");
client.println(sensors.getTempCByIndex(0));
client.print("
");
client.print("La temperatura impostata e':");
client.print("Min: ");
client.print(MIN);
client.print(" Max: ");
client.print(MAX);
client.println("<h1>Controllo Temperatura!</h1>");
client.print("<form method=get>Inserisci la temperatura (MAX=? oppure MIN=?): <input type=text size=10 name=temp> <input type=submit></form>");
//client.print("<form method=get>Inserisci la temperatura Massima: <input type=text size=10 name=max> <input type=submit></form>");
client.println("</body></html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
delay(1);
Serial.println("questo e' il received text");
Serial.println(receivedText); // uncomment this line to debug ;-)
// do something with the text
int firstPos = receivedText.indexOf("?");
if (firstPos > -1) {
int lastPos = receivedText.indexOf(" ", firstPos);
String theText = receivedText.substring(firstPos+12, lastPos); // 10 is the length of "?the_text="
String theTextScelta = receivedText.substring(firstPos+6, firstPos+9); // 10 is the length of "?the_text="
// if your text contains spaces they will be replaced with "+"
Serial.println("***********************");
Serial.println(theTextScelta);
Serial.println(theText);
Serial.println("***********************");
theText.toCharArray(floatbuf, sizeof(floatbuf));
int TheTextINT = atoi(floatbuf);
if (theTextScelta == "MIN")
MIN = TheTextINT;
if (theTextScelta == "MAX")
MAX = TheTextINT;
}
receivedText = "";
client.stop();
Serial.println("client disonnected");
}
Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println("DONE");
Serial.print("Temperature for the device 1 (index 0) is: ");
Serial.println(sensors.getTempCByIndex(0));
time=myTimer.readTimer();
if (sensors.getTempCByIndex(0) < MIN)
{
digitalWrite(hot, LOW);
digitalWrite(cold, HIGH);
Serial.print("Riscaldamento...");
ultimostato = CALDO;
}
else if (sensors.getTempCByIndex(0) > MAX && (time-=secure_time) > ultimoff)
{
digitalWrite(hot, HIGH);
digitalWrite(cold, LOW);
Serial.print("Raffreddamento...");
ultimostato = FREDDO;
}
else if (sensors.getTempCByIndex(0) > MIN || sensors.getTempCByIndex(0) < MAX)
{
digitalWrite(hot, HIGH);
digitalWrite(cold, HIGH);
Serial.print("Temperatura OK!");
if (ultimostato == FREDDO)
{
ultimoff = myTimer.readTimer();
ultimostato = NULL;
}
}/*
Serial.println();
Serial.print("ULTIMOSTATO: ");
Serial.println(ultimostato);
Serial.print("ULTIMOFF: ");
Serial.println(ultimoff);
Serial.print("Timer: ");
Serial.println(myTimer.readTimer());
Serial.print("secure Timer: ");
Serial.println(secure_time);
Serial.print("MIN: ");
Serial.println(MIN);
Serial.print("MAX: ");
Serial.println(MAX); */
}
Purtroppo non ho le specifiche dell'alimentatore qui' con me ora.
Spero che le informazioni aggiunte siano leggermente utili.
Ciao e grazie ancora.
PaoloP:
Sostituisci tutti i Serial.print e Client.print e println in modo che diventino, da così
Serial.print("xxxx");
a così
Serial.print(F("xxxx"));
Devi sostituire t(" con t(F(" e "); con "));
Per i println n(" con n(F("
Usa trova e sostituisci nell'IDE.
in realta' ho gia' commentato tutti i serial.print in quanto non ho necessita' della console.
con tutto commentato i valori erano quelli del secondo avr:
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}
Basta inserire questa funzione nel vostro sketch e richiamarla: essa restituirà un valore di tipo int che rappresenta il numero di byte di SRAM in quel momento ancora liberi – tale valore potrete stamparlo su seriale oppure visualizzarlo su un LCD.
La funzione la trovi anche nella libreria SdFatUtil che includi con