Global Moderator
Milano, Italy
Offline
Edison Member
Karma: 10
Posts: 1099
Arduino rocks
|
 |
« Reply #30 on: August 11, 2012, 10:17:46 am » |
@Ettore: sulla stringa hai ragionissimo, m'era sfuggito. Riguardo la libreria per il calcolo di alba e tramonto mi hai fatto ricordare una mail che mi hai mandato un po' di tempo fa. Però sarà opportuno ripescare il vecchio topi oppure aprirne uno nuovo o continuare per email.
Con Arduino puoi sommare un intero ad una stringa perchè le dovute conversioni vengono fatte dalla apposita libreria per le stringhe (fino a 4 cifre). Con i float non è stato implementato. Elvis alla fine si salvava perchè tramite l'assegnamento di un float ad int otteneva solo la parte intera del float...un piccolo spreco di variabili ma funzionale.
|
|
|
|
|
Logged
|
F
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 57
|
 |
« Reply #31 on: August 12, 2012, 03:24:51 am » |
Bene, sono arrivato alla conclusione che i dati dalle due schede arduino che ho installato hanno unntenpo di refresh molto diverso perché lavorano a temperature diverse. Come posso fare per avere un refresh ogni 30sec. circa? C'è un modo per inviare i dati a cosm ogni X cicli di lettura dei sensori? Grazie
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Tesla Member
Karma: 87
Posts: 8501
:(){:|:&};:
|
 |
« Reply #32 on: August 12, 2012, 07:43:10 am » |
è vero che ci possono essere delle differenze per via della temperatura, ma parliamo di minuto al giorno a dir tanto, prendendo come base i dati del swrtc. comunque in caso la soluzione è appunto un RTC, ovvero un piccolo orologio indipendente. Prendili che copensano la temperatura
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 57
|
 |
« Reply #33 on: August 12, 2012, 08:48:32 am » |
Caspita, allora cosa può essere? ho invertito entrambi le schede ma ottengo lo stesso risultato, l'unica differenza è che uno dei sensori LM35 montati sull'impianto termosolare ha un cavo lungo 80cm circa. Per il resto è tutto identico. Vedo che il led giallo (L) della scheda ethernet lampeggia veramente a frequenze diverse. E' molto più veloce sulla scheda che ho in laboratorio a 24°C, ma in effetti, oggi in centrale termica ci sono solo 28°C... Non capisco dove sia il problema...
|
|
|
|
|
Logged
|
|
|
|
|
Selvazzano Dentro - Padova
Offline
God Member
Karma: 17
Posts: 546
"Chi sa fa, chi non sa insegna"
|
 |
« Reply #34 on: August 12, 2012, 10:16:58 am » |
Ti sei accorto, vero, che nel mio sketch ho inviato nell'ordine Temp. Aria, Temp Acqua, Ventola e Caldaia?
In COSM ho notato che l'ordine è Temp. Aria, Ventola, Temp Acqua e Caldaia.
Quindi basta che rimetti a posto l'ordine nello sketch, scambiando la posizione di Temp Acqua e Ventola.
Comunque, credo proprio sia impossibile scendere al di sotto dei 50 s di refresh.
Ettore Massimo Albani
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 57
|
 |
« Reply #35 on: August 12, 2012, 12:05:15 pm » |
Ciao Ettore, sull'Arduino che ho in studio ho ricaricato il file che mi hai passato, se vai a vedere il device "Ettore" su cosm (gelholder) noterai che le letture dei sensori LM35 danno dei valori molto strani. Di positivo però è che il refresh avviene ogni 50sec. circa. (tempo ottimo).
|
|
|
|
|
Logged
|
|
|
|
|
Selvazzano Dentro - Padova
Offline
God Member
Karma: 17
Posts: 546
"Chi sa fa, chi non sa insegna"
|
 |
« Reply #36 on: August 12, 2012, 07:15:50 pm » |
Ho modificato il codice per calcolare meglio le temperature secondo le cartteristiche del tuo sensore (alim. 5V e range tra +2°C e 100°C). Inoltre ho portato i due segnali "finti" analogici (Caldaia e Ventola) allo stato di digitali (0 e 1). CI sarebbe un'altra cosa da fare, il "resampling" delle misure di temperatura per evitare il rumore, ma per ora prova così. Ettore Massimo Albani /* IDE 1.01 Ethernet shield attached to pins 10, 11, 12, 13 Arduino uno This code is in the public domain. */
#include <SPI.h> #include <Ethernet.h>
#define APIKEY "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // replace your Cosm api key here #define FEEDID XXXXX // replace your feed ID #define USERAGENT "TEST 2" // user agent is the project name
byte mac[] = { // MAC address of Ethernet controller 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xBD};
IPAddress ip(192, 168, 11, 12); // IP address on your network here
EthernetClient client; // initialize the library instance:
// if you don't want to use DNS (and reduce your sketch size) // use the numeric IP instead of the name for the server:
// IPAddress server(216, 52, 233, 121); // numeric IP for api.cosm.com char server[] = "api.cosm.com"; // name address for Cosm API
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds boolean lastConnected = false; // state of the connection last time through the main loop const unsigned long postingInterval = 10000; // delay between updates to Cosm.com
String DataString = ""; // stringa per invio dati char Buffer[10]; // buffer per dftostr()
int CaldaiaStato = 0; // INGRESSO ANALOGICO PER CONTROLLO WEB STATO CALDAIA int VentolaStato = 0; // INGRESSO ANALOGICO PER CONTROLLO WEB STATO CALDAIA float TempAria = 0.0; // sensore di temperatura LM35DZ aria float TempAcqua = 0.0; // sensore di temperatura LM35DZ acqua
int SensorReading = 0; // lettura sensori
void setup() {
analogReference(DEFAULT); // DEFAULT (5V), INTERNAL (1,1V), EXTERNAL (0÷5V)
pinMode(A0, INPUT_PULLUP); // stato caldaia pinMode(A1, INPUT_PULLUP); // sensore di temperatura aria (LM35DZ) alim. 5V pinMode(A2, INPUT_PULLUP); // stato ventola pinMode(A3, INPUT_PULLUP); // sensore di temperatura acqua (LM35DZ) alim. 5V
pinMode(8, OUTPUT); // relay caldaia ON/OFF pinMode(6, OUTPUT); // relay caldaia ON/OFF (replica)
pinMode(9, OUTPUT); // relay ventola ON/OFF pinMode(5, OUTPUT); // relay ventola ON/OFF (replica)
Serial.begin(9600);
if (Ethernet.begin(mac) == 0) { // start the Ethernet connection Ethernet.begin(mac, ip); // DHCP failed, so use a fixed IP address Serial.println("Failed to configure Ethernet using DHCP"); } }
void loop() {
CaldaiaStato = analogRead(A0); // stato caldaia delayMicroseconds(120); // 120 µs (min time reading = 100 µs x channel)
TempAria = analogRead(A1) * (100 - 2) / 1024 + 2; // temperatura aria in °C (10 mV/°C, range +2°C - +100°C) delayMicroseconds(120); // 120 µs (min time reading = 100 µs x channel)
VentolaStato = analogRead(A2); // stato ventola delayMicroseconds(120); // 120 µs (min time reading = 100 µs x channel)
TempAcqua = analogRead(A3) * (100 - 2) / 1024 + 2; // temperatura acqua in °C (10 mV/°C, range +2°C - +100°C) delayMicroseconds(120); // 120 µs (min time reading = 100 µs x channel)
CaldaiaStato = map(CaldaiaStato, 0, 1023, 0, 1); VentolaStato = map(VentolaStato, 0, 1023, 0, 1);
Serial.print("Temp. Aria: "); Serial.println(TempAria, 1);
if ((TempAria > 0) and (TempAria < 35)) { digitalWrite(9, LOW); // relay ventola OFF digitalWrite(5, LOW); // relay ventola OFF (replica) Serial.println("Aria fredda - Ventola OFF"); } else if ((TempAria > 35) and (TempAria < 100)) { digitalWrite(9, HIGH); // relay ventola ON digitalWrite(5, HIGH); // relay ventola ON (replica) Serial.println("Aria calda - Ventola ON"); }
Serial.print("Temp. Acqua: "); Serial.println(TempAcqua, 1);
if ((TempAcqua > 0) and (TempAcqua < 42)) { digitalWrite(8, HIGH); // relay caldaia ON digitalWrite(6, HIGH); // relay caldaia ON (replica) Serial.println("Acqua fredda - Caldaia ON"); } else if ((TempAcqua > 42) and (TempAcqua < 100)) { digitalWrite(8, LOW); // relay caldaia OFF digitalWrite(6, LOW); // relay caldaia OFF (replica) Serial.println("Acqua Calda - Caldaia OFF"); }
DataString = "TempAria,"; DataString += FloatFormat(TempAria, 10, 1, false, true); DataString += "\nVentola,"; DataString += String(VentolaStato, DEC); DataString += "\nTempAcqua,"; DataString += FloatFormat(TempAcqua, 10, 1, false, true); DataString += "\nCaldaia,"; DataString += String(CaldaiaStato, DEC);
if (client.available()) { char c = client.read(); Serial.print(c); }
// if there's no net connection, but there was one last time // through the loop, then stop the client: if (!client.connected() && lastConnected) { Serial.println(); Serial.println("disconnecting..."); client.stop(); }
// if you're not connected, and ten seconds have passed since // your last connection, then connect again and send data:
if (millis() < lastConnectionTime) lastConnectionTime = millis(); // evita il blocco dopo 50gg poiché millis() si azzera
if (!client.connected() && (millis() - lastConnectionTime > postingInterval)) { sendData(DataString); } // store the state of the connection for next time through // the loop: lastConnected = client.connected(); }
// this method makes a HTTP connection to the server:
void sendData(String thisData) { // if there's a successful connection: if (client.connect(server, 80)) { Serial.println("connecting..."); // send the HTTP PUT request: client.print("PUT /v2/feeds/"); client.print(FEEDID); client.println(".csv HTTP/1.1"); client.println("Host: api.cosm.com"); client.print("X-ApiKey: "); client.println(APIKEY); client.print("User-Agent: "); client.println(USERAGENT); client.print("Content-Length: "); client.println(thisData.length());
// last pieces of the HTTP PUT request: client.println("Content-Type: text/csv"); client.println("Connection: close"); client.println();
// here's the actual content of the PUT request: client.println(thisData); Serial.println(thisData); } else { // if you couldn't make a connection: Serial.println("connection failed"); Serial.println(); Serial.println("disconnecting..."); client.stop(); } // note the time that the connection was made or attempted: lastConnectionTime = millis(); }
String FloatFormat(float X, char Size, unsigned char Decimal, boolean Plus, boolean AutoReduce) {
char Buffer[Size + 1];
String Z = dtostrf(X, Size, Decimal, Buffer); if (Plus && X > 0) Z[Z.lastIndexOf(' ')] = '+'; if (AutoReduce) Z.trim();
return Z; }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 57
|
 |
« Reply #37 on: August 13, 2012, 06:03:59 am » |
Ho capito! Un macello, ho smontato tutto ho invertito le schede nuovamente ... etc...etc... ed alla fine ho capito dov'è il problema del refresh con tempi abissali! /* IDE 1.01 Ethernet shield attached to pins 10, 11, 12, 13 Arduino uno This code is in the public domain. */
#include <SPI.h> #include <Ethernet.h>
#define APIKEY "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // replace your Cosm api key here #define FEEDID XXXXX // replace your feed ID #define USERAGENT "TEST 2" // user agent is the project name
// assign a MAC address for the ethernet controller. // fill in your address here: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
// fill in an available IP address on your network here, // for manual configuration: IPAddress ip(192,168,1,22);
// initialize the library instance: EthernetClient client;
// if you don't want to use DNS (and reduce your sketch size) // use the numeric IP instead of the name for the server: // IPAddress server(216,52,233,121); // numeric IP for api.cosm.com char server[] = "api.cosm.com"; // name address for Cosm API
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds boolean lastConnected = false; // state of the connection last time through the main loop const unsigned long postingInterval = 1; //delay between updates to Cosm.com
int caldaiaON = 0; //INGRESSO ANALOGICO PER CONTROLLO WEB STATO CALDAIA int Ventola = 0; //INGRESSO ANALOGICO PER CONTROLLO WEB STATO CALDAIA float tempAria = 0.0; //sensore di temperatura LM35DZ ARIA float tempAcqua = 0.0; // sensore di temperatura LM35DZ ACQUA int sensorReading = 0; String dataString = ""; int otherSensorReading = 0; int otherSensorReadingAria = 0; int otherSensorReadingCaldaia = 0;
void setup() { pinMode(5, OUTPUT); //USCITA IN PARALLELO AL RELE CALDAIA PER VISUALIZZAZIONE WEB STATO CALDAIA pinMode(6, OUTPUT); //USCITA IN PARALLELO AL RELE CALDAIA PER VISUALIZZAZIONE WEB STATO CALDAIA pinMode(8, OUTPUT); //relay controllo ACQUA pinMode(9, OUTPUT); //relay controllo ARIA
// start serial port: Serial.begin(9600); // give the ethernet module time to boot up: // start the Ethernet connection: if (Ethernet.begin(mac) == 0) { Serial.println("Failed to configure Ethernet using DHCP"); // DHCP failed, so use a fixed IP address: Ethernet.begin(mac, ip); } }
void loop() { caldaiaON = analogRead(A0); //INGRESSO ANALOGICO PER CONTROLLO WEB STATO CALDAIA Ventola = analogRead(A2); //INGRESSO ANALOGICO PER CONTROLLO WEB STATO CALDAIA // read the analog sensor: tempAria = analogRead(A1) * 0.5; //sensore di temperatura LM35DZ ARIA sensorReading = tempAria; Serial.print ("Temp. Aria: "); Serial.println (tempAria);
dataString = "sensor1,"; dataString += sensorReading;
if ((tempAria > 0) and (tempAria < 27)){ Serial.println("Aria fredda"); digitalWrite(9, LOW); //VENTILAZIONE DISATTIVATA digitalWrite(5, LOW); //VENTILAZIONE DISATTIVATA Serial.print ("Ventola Spenta "); Serial.println (Ventola, DEC); } else if ((tempAria > 27) and (tempAria < 100)){ Serial.println ("Aria calda"); digitalWrite(9, HIGH); //VENTILAZIONE ATTIVATA digitalWrite(5, HIGH); //VENTILAZIONE ATTIVATA Serial.print ("Ventola Accesa "); Serial.println (Ventola,DEC); }
otherSensorReadingAria = Ventola; dataString += "\nsensor2,"; dataString += otherSensorReadingAria; // you can append multiple readings to this String if your // Cosm feed is set up to handle multiple values: tempAcqua = (analogRead(A3) * 0.5) + 4; // sensore di temperatura LM35DZ ACQUA otherSensorReading = tempAcqua; Serial.print("Temp. Acqua: "); Serial.println(tempAcqua);
dataString += "\nsensor3,"; dataString += otherSensorReading;
if ((tempAcqua > 0) and (tempAcqua < 42)) { Serial.println("Acqua fredda"); digitalWrite(8, HIGH); // ACCENSIONA CALDAIA digitalWrite(6, HIGH); //USCITA IN PARALLELO AL SEGNALE CALDAIA PER TELECONTROLLO Serial.print("Caldaia Accesa "); Serial.println(caldaiaON, DEC); } else if ((tempAcqua > 42) and (tempAcqua < 100)){ delay(2000); Serial.println ("Acqua Calda"); digitalWrite(8, LOW); //SPEGNIMENTO CALDAIA digitalWrite(6, LOW); //USCITA IN PARALLELO AL SEGNALE CALDAIA PER TELECONTROLLO Serial.print("Caldaia Spenta "); Serial.println(caldaiaON, DEC); }
otherSensorReadingCaldaia = caldaiaON; dataString += "\nsensor4,"; dataString += otherSensorReadingCaldaia; // if there's incoming data from the net connection. // send it out the serial port. This is for debugging // purposes only: if (client.available()) { char c = client.read(); Serial.print(c); }
// if there's no net connection, but there was one last time // through the loop, then stop the client: if (!client.connected() && lastConnected) { Serial.println(); Serial.println("disconnecting..."); client.stop(); }
// if you're not connected, and ten seconds have passed since // your last connection, then connect again and send data:
if (millis() < lastConnectionTime) lastConnectionTime = millis(); // evita il blocco dopo 50gg poiché millis() si azzera
if (!client.connected() && (millis() - lastConnectionTime > postingInterval)) { sendData(dataString); } // store the state of the connection for next time through // the loop: lastConnected = client.connected(); }
// this method makes a HTTP connection to the server:
void sendData(String thisData) { // if there's a successful connection: if (client.connect(server, 80)) { Serial.println("connecting..."); // send the HTTP PUT request: client.print("PUT /v2/feeds/"); client.print(FEEDID); client.println(".csv HTTP/1.1"); client.println("Host: api.cosm.com"); client.print("X-ApiKey: "); client.println(APIKEY); client.print("User-Agent: "); client.println(USERAGENT); client.print("Content-Length: "); client.println(thisData.length());
// last pieces of the HTTP PUT request: client.println("Content-Type: text/csv"); client.println("Connection: close"); client.println();
// here's the actual content of the PUT request: client.println(thisData); Serial.println(thisData); } else { // if you couldn't make a connection: Serial.println("connection failed"); Serial.println(); Serial.println("disconnecting..."); client.stop(); } // note the time that the connection was made or attempted: lastConnectionTime = millis(); }
Accade che se l'acqua all'interno del bollitore è ad una temperatura <42°C va tutto bene perché non è impostato nessun delay, se invece l'acqua ha una temperatura >42°C il codice è: else if ((tempAcqua > 42) and (tempAcqua < 100)){ delay(2000); Serial.println ("Acqua Calda"); digitalWrite(8, LOW); //SPEGNIMENTO CALDAIA digitalWrite(6, LOW); //USCITA IN PARALLELO AL SEGNALE CALDAIA PER TELECONTROLLO Serial.print("Caldaia Spenta "); Serial.println(caldaiaON, DEC); }
ed il delay(2000) sullo spegnimento della caldaia mi manda in crisi tutto e per non so quale motivo il refresh avviene ogni 20/30min circa. Il delay per lo spegnimento della caldaia è molto importante per evitare che per differenze di 0,5°C o di 1°C la caldaia si accenda e si spenga in continuazione. A questo punto non ho la minima idea di come risolvere la cosa... P.S. x Ettore: Ho provato il nuovo codice ma continuo ad avere letture di temperatura completamente sballate. Io però non sono in grado di capirne il motivo...
|
|
|
|
|
Logged
|
|
|
|
|
Selvazzano Dentro - Padova
Offline
God Member
Karma: 17
Posts: 546
"Chi sa fa, chi non sa insegna"
|
 |
« Reply #38 on: August 13, 2012, 11:59:09 am » |
Cominciamo dall'inizio. Non so dove tu abbia trovato quello schema, ma è errato! Infatti il resistore sull'uscita del LM35DZ deve essere in SERIE e, per sicurezza, deve avere un valore di 2k. Non deve essere posto a massa. Se il cavo (rigorosamente schermato) è molto lungo, potrebbe essere necessario un filtro composto da un condensatore da 1uF con in serie un resistore da 75 ohm verso massa, entrambi posti vicino all'integrato (vedi Typical Applications http://www.ti.com/lit/ds/symlink/lm35.pdf). Nessuno si è accorto (me compreso) che l'istruzione if ((TempAcqua > 0) and (TempAcqua <= 42)) è sbagliata! L'istruzione corretta è if ((TempAcqua > 0.0) && (TempAcqua <= 42.0)). Non è necessario un secondo else if, ma può bastare un semplice else. Non è necessario usare per Ventola e Caldaia gli ingressi analogici per COSM, quindi lo sketch e lo schema risultano molto più semplici. Questa volta spero che ci siamo. Ettore Massimo Albani /* IDE 1.01 Ethernet shield attached to pins 10, 11, 12, 13 Arduino uno This code is in the public domain. */
#include <SPI.h> #include <Ethernet.h>
#define APIKEY "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // replace your Cosm api key here #define FEEDID XXXXX // replace your feed ID #define USERAGENT "TEST 2" // user agent is the project name
byte mac[] = { // MAC address of Ethernet controller 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xBD};
IPAddress ip(192, 168, 11, 12); // IP address on your network here
EthernetClient client; // initialize the library instance:
// if you don't want to use DNS (and reduce your sketch size) // use the numeric IP instead of the name for the server:
// IPAddress server(216, 52, 233, 121); // numeric IP for api.cosm.com char server[] = "api.cosm.com"; // name address for Cosm API
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds boolean lastConnected = false; // state of the connection last time through the main loop const unsigned long postingInterval = 10000; // delay between updates to Cosm.com
String DataString = ""; // stringa per invio dati char Buffer[10]; // buffer per dftostr()
boolean Caldaia = LOW; // stato caldaia (LED Caldaia) boolean Ventola = LOW; // stato ventola (LED Ventola)
int Valore = 0; // valore sensori analogici float TempAria = 0.0; // sensore di temperatura LM35DZ aria float TempAcqua = 0.0; // sensore di temperatura LM35DZ acqua
void setup() {
analogReference(DEFAULT); // DEFAULT (5V), INTERNAL (1,1V), EXTERNAL (0÷5V)
pinMode(A1, INPUT); // sensore di temperatura aria (LM35DZ) alim. 5V, out con res. 2k in serie pinMode(A3, INPUT); // sensore di temperatura acqua (LM35DZ) alim. 5V, out con res. 2k in serie
pinMode(8, OUTPUT); // relay caldaia ON/OFF pinMode(9, OUTPUT); // relay ventola ON/OFF
Serial.begin(9600);
if (Ethernet.begin(mac) == 0) { // start the Ethernet connection Ethernet.begin(mac, ip); // DHCP failed, so use a fixed IP address Serial.println("Failed to configure Ethernet using DHCP"); } }
void loop() {
Valore = analogRead(A1); // valore sensore aria TempAria = float(Valore * 5 * (100 - 2) / 1024 + 2); // temperatura aria in °C (10 mV/°C, range +2°C - +100°C) delayMicroseconds(120); // 120 µs (min time reading = 100 µs x channel)
Valore = analogRead(A3); // valore sensore acqua TempAcqua = float(Valore * 5 * (100 - 2) / 1024 + 2); // temperatura acqua in °C (10 mV/°C, range +2°C - +100°C) delayMicroseconds(120); // 120 µs (min time reading = 100 µs x channel)
Serial.print("Temp. Aria: "); Serial.println(TempAria, 1);
Serial.print("Temp. Acqua: "); Serial.println(TempAcqua, 1);
if ((TempAria > 0.0) && (TempAria <= 35.0)) { digitalWrite(9, LOW); // relay ventola OFF Serial.println("Aria fredda - Ventola OFF"); } else { digitalWrite(9, HIGH); // relay ventola ON Serial.println("Aria calda - Ventola ON"); }
if ((TempAcqua > 0.0) && (TempAcqua <= 42.0)) { digitalWrite(8, HIGH); // relay caldaia ON Serial.println("Acqua fredda - Caldaia ON"); } else { digitalWrite(8, LOW); // relay caldaia OFF Serial.println("Acqua Calda - Caldaia OFF"); }
DataString = "TempAria,"; DataString += FloatFormat(TempAria, 10, 1, false, true); DataString += "\nVentola,"; DataString += String(Ventola, DEC); DataString += "\nTempAcqua,"; DataString += FloatFormat(TempAcqua, 10, 1, false, true); DataString += "\nCaldaia,"; DataString += String(Caldaia, DEC);
if (client.available()) { char c = client.read(); Serial.print(c); }
// if there's no net connection, but there was one last time // through the loop, then stop the client: if (!client.connected() && lastConnected) { Serial.println(); Serial.println("disconnecting..."); client.stop(); }
// if you're not connected, and ten seconds have passed since // your last connection, then connect again and send data:
if (millis() < lastConnectionTime) lastConnectionTime = millis(); // evita il blocco dopo 50gg poiché millis() si azzera
if (!client.connected() && (millis() - lastConnectionTime > postingInterval)) { sendData(DataString); } // store the state of the connection for next time through // the loop: lastConnected = client.connected(); }
// this method makes a HTTP connection to the server:
void sendData(String thisData) { // if there's a successful connection: if (client.connect(server, 80)) { Serial.println("connecting..."); // send the HTTP PUT request: client.print("PUT /v2/feeds/"); client.print(FEEDID); client.println(".csv HTTP/1.1"); client.println("Host: api.cosm.com"); client.print("X-ApiKey: "); client.println(APIKEY); client.print("User-Agent: "); client.println(USERAGENT); client.print("Content-Length: "); client.println(thisData.length());
// last pieces of the HTTP PUT request: client.println("Content-Type: text/csv"); client.println("Connection: close"); client.println();
// here's the actual content of the PUT request: client.println(thisData); Serial.println(thisData); } else { // if you couldn't make a connection: Serial.println("connection failed"); Serial.println(); Serial.println("disconnecting..."); client.stop(); } // note the time that the connection was made or attempted: lastConnectionTime = millis(); }
String FloatFormat(float X, char Size, unsigned char Decimal, boolean Plus, boolean AutoReduce) {
char Buffer[Size + 1];
String Z = dtostrf(X, Size, Decimal, Buffer); if (Plus && X > 0) Z[Z.lastIndexOf(' ')] = '+'; if (AutoReduce) Z.trim();
return Z; }
|
|
|
|
« Last Edit: August 13, 2012, 12:07:35 pm by cyberhs »
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 57
|
 |
« Reply #39 on: August 13, 2012, 02:30:33 pm » |
PERFETTO! Ettore, so che oramai ne ho approfittato un po' troppo della tua pazienza... ma come faccio ad inserire un delay per lo spegnimento della caldaia senza che questo mi blocchi il refresh ogni 60sec?
|
|
|
|
|
Logged
|
|
|
|
|
Selvazzano Dentro - Padova
Offline
God Member
Karma: 17
Posts: 546
"Chi sa fa, chi non sa insegna"
|
 |
« Reply #40 on: August 13, 2012, 09:38:47 pm » |
Sono contento che tutto ora funzioni (ne avevo fatto un punto d'onore!)  La soluzione al problema non è un ritardo, ma l'inserimento di un'isteresi (diciamo 0,5 - 1 °C). Ora non ho tempo, ma se hai un po' di pazienza ti risolvo il problema. Ettore Massimo Albani
|
|
|
|
|
Logged
|
|
|
|
|
Selvazzano Dentro - Padova
Offline
God Member
Karma: 17
Posts: 546
"Chi sa fa, chi non sa insegna"
|
 |
« Reply #41 on: August 13, 2012, 11:51:30 pm » |
Ogni promessa è debito... Modificato il codice aggiungendo, tra le altre cose, l'isteresi settata ad 1°C, ma modicabile come tutti gli altri parametri. Perciò la caldaia si accenderà con una temperatura superiore a 42+1 °C e si spegnerà con una temperatura di 42-1 °C. Lo stesso vale per la ventola. I sensori, avendo solo una tensione positiva, non possono misurare meno di 2°C Ettore Massimo Albani PS: mi devi da bere...  /* IDE 1.0.1 Ethernet shield attached to pins 10, 11, 12, 13 Arduino uno modified by Ettore Massimo Albani */
#include <SPI.h> #include <Ethernet.h>
#define APIKEY "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // replace your Cosm api key here #define FEEDID XXXXX // replace your feed ID #define USERAGENT "TEST 2" // user agent is the project name
byte mac[] = { // MAC address of Ethernet controller 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xBD};
IPAddress ip(192, 168, 11, 12); // IP address on your network here
EthernetClient client; // initialize the library instance:
// if you don't want to use DNS (and reduce your sketch size) // use the numeric IP instead of the name for the server:
// IPAddress server(216, 52, 233, 121); // numeric IP for api.cosm.com char server[] = "api.cosm.com"; // name address for Cosm API
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds boolean lastConnected = false; // state of the connection last time through the main loop const unsigned long postingInterval = 10000; // delay between updates to Cosm.com (10 s)
String DataString = ""; // stringa per invio dati char Buffer[10]; // buffer per dftostr()
boolean Caldaia = LOW; // stato caldaia (LED Caldaia) boolean Ventola = LOW; // stato ventola (LED Ventola)
const float AnaRef = 5.0; // valore tensione (5V) const unsigned int Risoluzione = 1024; // risoluzione (10 bit)
const float RangeMin = 2.0; // temperatura minima °C sensore LM35DZ (alim. 5V, out con res. 2k in serie) const float RangeMax = 100.0; // temperatura massima °C sensore LM35DZ (alim. 5V, out con res. 2k in serie) const float Incremento = 0.01; // incremento (10 mV/°C)
float Volt = 0; // valore sensori analogici in volt
const float Isteresi = 1.0; // isteresi (1 °C)
float TempAria = 0.0; // temperatura aria float TempAriaMin = 2.0; // soglia inferiore temperatura aria (min 2 °C = RangeMin) float TempAriaMax = 35.0; // soglia superiore temperatura aria (max 100 °C = RangeMax)
float TempAcqua = 0.0; // temperatura acqua float TempAcquaMin = 2.0; // soglia inferiore temperatura acqua (min 2 °C) float TempAcquaMax = 42.0; // soglia superiore temperatura acqua (max 100 °C)
void setup() {
analogReference(DEFAULT); // DEFAULT (5V), INTERNAL (1,1V), EXTERNAL (0÷5V)
pinMode(A1, INPUT); // sensore di temperatura aria pinMode(A3, INPUT); // sensore di temperatura acqua
pinMode(8, OUTPUT); // relay caldaia ON/OFF pinMode(9, OUTPUT); // relay ventola ON/OFF
Serial.begin(9600);
if (Ethernet.begin(mac) == 0) { // start the Ethernet connection Ethernet.begin(mac, ip); // DHCP failed, so use a fixed IP address Serial.println("Failed to configure Ethernet using DHCP"); } }
void loop() {
Volt = analogRead(A1) * AnaRef / Risoluzione; // valore sensore aria in volt TempAria = Volt / Incremento + RangeMin; // temperatura aria in °C delayMicroseconds(120); // 120 µs (min time reading = 100 µs x channel)
Volt = analogRead(A3) * AnaRef / Risoluzione; // valore sensore acqua in volt TempAcqua = Volt / Incremento + RangeMin; // temperatura acqua in °C delayMicroseconds(120); // 120 µs (min time reading = 100 µs x channel)
Serial.print("Temp. Aria: "); Serial.println(TempAria, 1);
Serial.print("Temp. Acqua: "); Serial.println(TempAcqua, 1);
if (TempAria > (TempAriaMax + Isteresi)) { digitalWrite(9, HIGH); // relay ventola ON Serial.println("Aria calda - Ventola ON"); } else if (TempAria < (TempAriaMax - Isteresi)) { digitalWrite(9, LOW); // relay ventola OFF Serial.println("Aria fredda - Ventola OFF"); }
if (TempAcqua > (TempAcquaMax + Isteresi)) { digitalWrite(8, LOW); // relay caldaia OFF Serial.println("Acqua Calda - Caldaia OFF"); } else if (TempAcqua < (TempAcquaMax - Isteresi)) { digitalWrite(8, HIGH); // relay caldaia ON Serial.println("Acqua fredda - Caldaia ON"); }
if (client.available()) { char c = client.read(); Serial.print(c); }
// if there's no net connection, but there was one last time // through the loop, then stop the client: if (!client.connected() && lastConnected) { Serial.println(); Serial.println("disconnecting..."); client.stop(); }
// if you're not connected, and ten seconds have passed since // your last connection, then connect again and send data:
if (millis() < lastConnectionTime) lastConnectionTime = millis(); // evita il blocco dopo 50gg poiché millis() si azzera
if (!client.connected() && (millis() - lastConnectionTime > postingInterval)) { DataString = "TempAria,"; DataString += FloatFormat(TempAria, 10, 1, false, true); DataString += "\nVentola,"; DataString += String(Ventola, DEC); DataString += "\nTempAcqua,"; DataString += FloatFormat(TempAcqua, 10, 1, false, true); DataString += "\nCaldaia,"; DataString += String(Caldaia, DEC); sendData(DataString); } // store the state of the connection for next time through // the loop: lastConnected = client.connected(); }
// this method makes a HTTP connection to the server:
void sendData(String thisData) { // if there's a successful connection: if (client.connect(server, 80)) { Serial.println("connecting..."); // send the HTTP PUT request: client.print("PUT /v2/feeds/"); client.print(FEEDID); client.println(".csv HTTP/1.1"); client.println("Host: api.cosm.com"); client.print("X-ApiKey: "); client.println(APIKEY); client.print("User-Agent: "); client.println(USERAGENT); client.print("Content-Length: "); client.println(thisData.length());
// last pieces of the HTTP PUT request: client.println("Content-Type: text/csv"); client.println("Connection: close"); client.println();
// here's the actual content of the PUT request: client.println(thisData); Serial.println(thisData); } else { // if you couldn't make a connection: Serial.println("connection failed"); Serial.println(); Serial.println("disconnecting..."); client.stop(); } // note the time that the connection was made or attempted: lastConnectionTime = millis(); }
String FloatFormat(float X, char Size, unsigned char Decimal, boolean Plus, boolean AutoReduce) {
char Buffer[Size + 1];
String Z = dtostrf(X, Size, Decimal, Buffer); if (Plus && X > 0) Z[Z.lastIndexOf(' ')] = '+'; if (AutoReduce) Z.trim();
return Z; }
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Tesla Member
Karma: 87
Posts: 8501
:(){:|:&};:
|
 |
« Reply #42 on: August 14, 2012, 03:01:22 am » |
se posso dire la mia, elvis non imparerà mai nulla finchè gli scrivi il codice.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 57
|
 |
« Reply #43 on: August 14, 2012, 04:33:13 am » |
Caro Lesto, tu hai anche ragione, ma da autodidatta e con i capelli oramai biancastri non è così facile...
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 57
|
 |
« Reply #44 on: August 14, 2012, 09:19:58 am » |
Ciao Ettore, spero veramente di riuscir ad offrirti una birra!
Alla fine ho fatto un mix del tuo codice e di quello che avevo originalmente perché anche con caldaia e ventola accesa il valore su pachube rimaneva a 0. Adesso lo testo per un paio di giorni e poi lo installo sul sistema termosolare. Veramente grazie ancora, non essendo un programmatore, ma solo un elettricista, confrontare i codici e trovarne gli errori mi aiuta ad imparare tanto.
|
|
|
|
|
Logged
|
|
|
|
|
|