Rieccomi qua, col mio sistema di controllo termosolare…
Vorrei ritardare di 30 sec. l’andata in LOW dell’uscita digitale 8.
Con delay non si può fare in quanto mi blocca l’esecuzione del programma ed in refresh dei dati su pachube avviene ogni 3 ore circa, allora mi è stato consigliato di usare la funzione millis… ho però chiaramente qualche problema…
il codice è:
#include <SPI.h>
#include <Ethernet.h>
#define APIKEY "XXXXXXXXXXXXXXXXX"
#define FEEDID XXXX
#define USERAGENT "TEST 2"
byte mac[] = {
0xDE, 0xAD, 0xBC, 0xAF, 0xFE, 0xBD};
IPAddress ip(192, 168, 11, 22);
EthernetClient client;
char server[] = "api.cosm.com";
unsigned long lastConnectionTime = 0;
boolean lastConnected = false;
const unsigned long postingInterval = 10000;
String DataString = "";
char Buffer[10];
int otherSensorReadingAria = 0;
int otherSensorReadingCaldaia = 0;
int caldaiaON = 0; //*INGRESSO ANALOGICO PER CONTROLLO WEB STATO CALDAIA
int Ventola = 0; //*INGRESSO ANALOGICO PER CONTROLLO WEB STATO 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 = 26.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 = 30.0; // soglia superiore temperatura acqua (max 100 °C)
///-----------------------------------------------------------------------------
const int relePin = 8;
int relePinState = HIGH;
long previousMillis = 0;
long interval = 30*1000;
///-----------------------------------------------------------------------------
void setup() {
analogReference(DEFAULT); //
pinMode(A1, INPUT); // sensore di temperatura aria
pinMode(A3, INPUT); // sensore di temperatura acqua
pinMode(5, OUTPUT); //*USCITA IN PARALLELO AL RELE CALDAIA PER VISUALIZZAZIONE WEB STATO VENTOLA
pinMode(6, OUTPUT); //*USCITA IN PARALLELO AL RELE CALDAIA PER VISUALIZZAZIONE WEB STATO CALDAIA
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() {
//----------------------------------------------------------------------------
unsigned long currentMillis = millis();
if(currentMillis - previousMillis < interval) {
previousMillis = currentMillis;
if (relePinState == LOW)
relePinState= HIGH;
else
relePinState = LOW;
digitalWrite(relePin, relePinState);
}
//--------------------------------------------------------------------------
caldaiaON = analogRead(A0); //*INGRESSO ANALOGICO PER CONTROLLO WEB STATO CALDAIA
Ventola = analogRead(A2); //*INGRESSO ANALOGICO PER CONTROLLO WEB STATO VENTOLA
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)+0); // temperatura acqua in °C +X°C PER COMPENSAZIONE TERMOMETRO TRIVALVOLA
delayMicroseconds(120); // 120 µs (min time reading = 100 µs x channel)
Serial.print("Temp. Aria: ");
Serial.println(TempAria, 1);
otherSensorReadingAria = Ventola;
Serial.print("Temp. Acqua: ");
Serial.println(TempAcqua, 1);
otherSensorReadingCaldaia = caldaiaON;
if (TempAria > (TempAriaMax + Isteresi)) {
digitalWrite(9, HIGH); // relay ventola ON
digitalWrite(5, HIGH); //*VENTILAZIONE ATTIVATA
Serial.println("Aria calda - Ventola ON");
}
else if (TempAria < (TempAriaMax - Isteresi)) {
digitalWrite(9, LOW); // relay ventola OFF
digitalWrite(5, LOW); //*VENTILAZIONE DISATTIVATA
Serial.println("Aria fredda - Ventola OFF");
}
if (TempAcqua > (TempAcquaMax + Isteresi)) {
digitalWrite(8, LOW); // relay caldaia OFF
digitalWrite(6, LOW); //*USCITA IN PARALLELO AL SEGNALE CALDAIA PER TELECONTROLLO
Serial.println("Acqua Calda - Caldaia OFF");
}
else if (TempAcqua < (TempAcquaMax - Isteresi)){
digitalWrite(8, HIGH); // relay caldaia ON
digitalWrite(6, HIGH); //*USCITA IN PARALLELO AL SEGNALE CALDAIA PER TELECONTROLLO
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 += otherSensorReadingAria;
DataString += "\nTempAcqua,";
DataString += FloatFormat(TempAcqua, 10, 1, false, true);
DataString += "\nCaldaia,";
DataString += otherSensorReadingCaldaia;
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;
}
Sono sulla buona strada?