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;
}