#include <SPI.h>
#include <SD.h> // SparkFun //
#include "DHT.h" // - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
#define DHTPIN 7 // Digital pin connected to the DHT sensor // Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
#define DHTTYPE DHT22 //#define DHTTYPE DHT21 // DHT 21 (AM2301) //#define DHTTYPE DHT11 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
float _TEMPERATURA;
float _HUMIDITA;
#define SD_pin_CS 10 // Slave Select en pin digital 10
String filesd = "datos.txt";
File _file_log_; // oggetto di tipo file
#include <Arduino.h>
#include <DS3232RTC.h> // https://github.com/JChristensen/DS3232RTC
#include <Streaming.h> // https://github.com/janelia-arduino/Streaming
DS3232RTC myRTC;
#include "ventola.h"
#include "pulsante.h"
pulsante puls1(2); //non ha funzionato dentro del setup...
int _ventole_pins[3] = { 3, 5, 6 }; //{ 3, 6, 11 };
//Ventola v1; //(_ventole_pins, 1); //Ventola v1; // se non si passano i parametri non serve () vuote
Ventola v1(_ventole_pins, 1);
/*
SCRIPT SI BLOCCA QUANDO SI ATTIVA: Ventola v1
*/
int ora_allarm1 = 9; int min_allarm1 = 0; int ora_allarm2 = 23; int min_allarm2 = 10;
void setup() {
Serial.begin(9600);
delay(1000);
myRTC_setup();
DHT22_setup();
SD_setup();
};
void loop() {
/*
if (puls1.sePremuto()) {
v1.nextStato();
};
*/
myRTC_loop();
delay(1000);
Serial.print("DHT22_1 ");
DHT22_loop();
SD_loop();
};
void DHT22_setup() {
dht.begin();
};
void DHT22_loop() {
// Wait a few seconds between measurements. // delay(2000);
float _HUMIDITA = dht.readHumidity();
float _TEMPERATURA = dht.readTemperature(); // Read temperature as Celsius (the default)
// float _HeatIndex = dht.computeHeatIndex(_TEMPERATURA, _HUMIDITA, false); // Compute heat index in Celsius
// Reading temperature or humidity takes about 250 milliseconds!
delay(1000);
Serial.print("Umid.: ");
Serial.print(String(_HUMIDITA,2));
Serial.print(" - Temp.: ");
Serial.print(String(_TEMPERATURA,2));
Serial.println(" C ");
Serial.print("Umid.: ");
// Serial.print(_HUMIDITA);
// Serial.print(" - Temp.: ");
// Serial.print(_TEMPERATURA);
// Serial.println(" C ");
}
void SD_setup() {
while (!SD.begin(SD_pin_CS)) {
Serial.println("Errore: SD non avviato!");
delay(3000);
}
Serial.println("SD avviato correttamente!"); // if (!SD.begin(SD_pin_CS)) { Serial.println("Errore: SD non avviato!"); } else { Serial.println("SD avviato correttamente!"); };
}
void SD_loop() {
Serial.println("Loop SD ");
_file_log_ = SD.open(filesd, FILE_WRITE);
if (_file_log_) {
for (int i = 1; i < 11; i++) {
_TEMPERATURA = dht.readTemperature();
_HUMIDITA = dht.readHumidity();
char buf1[25];
sprintf(buf1, "(%d) Temp.: %d °C Humid.: %d",
i, _TEMPERATURA, _HUMIDITA);
Serial.println(buf1);
_file_log_.println(buf1);
// delay(1000);
}
_file_log_.close();
Serial.println("Scrittura effettuata");
} else {
Serial.println("Errore SD");
/* char error[25];
sprintf(error, "Errore nell'apertura %s.", filesd);
Serial.println(error);*/
}
}
////////////////////////
void myRTC_loop() {
Serial.println("Loop RTC ");
if (myRTC.alarm(DS3232RTC::ALARM_1)) { // check alarm flag, clear it if set
Serial.println("Esegue ALARM_1!");
Serial << millis() << " ALARM_1 ";
printDateTime(myRTC.get());
Serial << endl;
se_allarme1();
}
if (myRTC.alarm(DS3232RTC::ALARM_2)) { // check alarm flag, clear it if set
Serial.println("Esegue ALARM_2!");
Serial << millis() << " ALARM_2 ";
printDateTime(myRTC.get());
Serial << endl;
se_allarme2();
}
}
void se_allarme1() {
//v1.setStato(2);
Serial.println("Chiamato se_allarme1()");
}
void se_allarme2() {
// v1.setStato(1);
Serial.println("Chiamato se_allarme2()");
}
void myRTC_setup() {
myRTC.begin(); // initialize the alarms to known values, clear the alarm flags, clear the alarm interrupt flags
bool forza_set_ora = false; //bool forza_set_ora = true;
if (forza_set_ora || myRTC.oscStopped(false)) { //
myRTC.set(compileTime()); // assegna ora RTC solo quando resettato
Serial.println("##################");
Serial.println("### updated RTC ##");
Serial.println("##################");
} else {
Serial.println("### RTC non aggiornato ##");
}
myRTC.setAlarm(DS3232RTC::ALM1_MATCH_HOURS, 0, min_allarm1, ora_allarm1, 1); //08:30
myRTC.alarm(DS3232RTC::ALARM_1); // clear the alarm flag
// allarme DUE
myRTC.setAlarm(DS3232RTC::ALM2_MATCH_HOURS, 0, min_allarm2, ora_allarm2, 1); // 23:10
myRTC.alarm(DS3232RTC::ALARM_2); // clear the alarm flag
Serial << millis() << " Start ";
printDateTime(myRTC.get());
Serial << endl;
}
void set_ventole_all_accensione_arduino() {
time_t t = myRTC.get();
int hour_now = hour(t);
int minute_now = minute(t);
Serial.print("hour_now:");
Serial.println(hour_now);
if (hour_now >= ora_allarm1 && hour_now <= ora_allarm2) { // if( hour_now >= ora_allarm1 && minute_now >= min_allarm1){
Serial.println("Forza come allarme 1");
se_allarme1();
} else {
Serial.println("Forza come allarme 2");
se_allarme2();
}
}
// format and print a time_t value
void printTime(time_t t) {
char buf[25];
char m[4]; // temporary storage for month string (DateStrings.cpp uses shared buffer)
strcpy(m, monthShortStr(month(t)));
sprintf(buf, "%.2d:%.2d:%.2d %s %.2d %s %d",
hour(t), minute(t), second(t), dayShortStr(weekday(t)), day(t), m, year(t));
Serial.println(buf);
}
// function to return the compile date and time as a time_t value
time_t compileTime() {
//myRTC.set(now()); // fa 01Jan2066 00:00:01 write
const time_t FUDGE(10); //fudge factor to allow for upload time, etc. (seconds, YMMV)
const char *compDate = __DATE__, *compTime = __TIME__, *months = "JanFebMarAprMayJunJulAugSepOctNovDec";
char compMon[4], *m;
strncpy(compMon, compDate, 3);
compMon[3] = '\0';
m = strstr(months, compMon);
tmElements_t tm;
tm.Month = ((m - months) / 3 + 1);
tm.Day = atoi(compDate + 4);
tm.Year = atoi(compDate + 7) - 1970;
tm.Hour = atoi(compTime);
tm.Minute = atoi(compTime + 3);
tm.Second = atoi(compTime + 6);
time_t t = makeTime(tm);
return t + FUDGE; //add fudge factor to allow for compile time
}
void printDateTime(time_t t) {
Serial << ((day(t) < 10) ? "0" : "") << _DEC(day(t));
Serial << monthShortStr(month(t)) << _DEC(year(t)) << ' ';
Serial << ((hour(t) < 10) ? "0" : "") << _DEC(hour(t)) << ':';
Serial << ((minute(t) < 10) ? "0" : "") << _DEC(minute(t)) << ':';
Serial << ((second(t) < 10) ? "0" : "") << _DEC(second(t));
}
/* dal Loop. da capire
enum states_t { wait, alarm, ledOn,setAlarm };
static states_t state{ setAlarm };
static uint32_t msLED;
switch (state) {
case wait:
break;
case alarm:
state = ledOn;
Serial << F("Alarm at ");
printTime(myRTC.get());
myRTC.alarm(DS3232RTC::ALARM_1); // clear alarm flag
// myRTC.alarmInterrupt(DS3232RTC::ALARM_1, false);
noInterrupts();
// rtcAlarm = false;
interrupts();
msLED = millis();
digitalWrite(LED_BUILTIN, HIGH);
break;
case ledOn:
if (millis() - msLED >= ledInterval * 1000) {
state = setAlarm;
digitalWrite(LED_BUILTIN, LOW);
}
break;
case setAlarm:
state = wait;
time_t utc = myRTC.get();
time_t alm = utc; // - utc % wake + wake;
Serial << F("Setting alarm for ");
printTime(alm);
// myRTC.setAlarm(DS3232RTC::ALM1_MATCH_DATE, second(alm), minute(alm), hour(alm), day(alm));
// myRTC.alarmInterrupt(DS3232RTC::ALARM_1, true);
break;
}
*/