Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Blynk
- #define BLYNK_PRINT DebugSerial
- #include <SoftwareSerial.h>
- SoftwareSerial DebugSerial(0, 1); // RX, TX
- char auth[] = "5b343a8730db451b810a0916fcc8317e";
- #include <BlynkSimpleStream.h>
- BlynkTimer timer;
- #define BLYNK_PRINT Serial
- WidgetTerminal terminal(V4);
- // DHT
- #include <DHT.h>
- #include "DHT.h"
- #define DHTPIN 8
- #define DHTTYPE DHT11
- // Trasmettitore IR
- #include <IRremote.h>
- IRsend irsend;
- // LCD
- #include <LiquidCrystal.h>
- const int rs = 12, en = 11, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
- LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
- // Carattere °
- byte simbologradi[8] =
- {
- B00111,
- B00101,
- B00111,
- B00000,
- B00000,
- B00000,
- B00000,
- B00000,
- };
- // Carattere %
- byte simbolopercentuale[9] =
- {
- B00000,
- B00000,
- B11001,
- B11010,
- B00100,
- B01011,
- B10011
- };
- // Carattere :)
- byte simbolosmile[10] =
- {
- B00000,
- B01010,
- B00000,
- B10001,
- B01110,
- B00000,
- B00000
- };
- // Carattere :(
- byte simbolosad[11] =
- {
- B00000,
- B01010,
- B00000,
- B01110,
- B10001,
- B00000,
- B00000
- };
- // Carattere :|
- byte simboloneut[12] =
- {
- B00000,
- B01010,
- B00000,
- B00000,
- B11111,
- B00000,
- B00000
- };
- // Contatore pagine
- int pageCounter = 1;
- // Igrometro
- int dryValueRaw = 800; // Valore raw Igrometro -> Umidità 0
- int wetValueRaw = 480; // Valore raw Igrometro -> Umidità 100
- int pDryValue = 0; // Valore percentuale umidità 0
- int pWetValue = 100; // Valore percentuale umidità 100
- int statoMessErrore = 0;
- // DHT11
- DHT dht (DHTPIN, DHTTYPE); // Pin DHT e tipo DHT
- // Pulsante e Debounce
- int pinPulsanteDown = 10;
- int pinPulsanteUp = 9;
- int pinPulsanteLed = 13;
- // Debounce Pulsante Led
- int statoPulsanteLed = LOW;
- int attesaDebounce = 50;
- int ultimaLetturaPulsanteLed = LOW;
- unsigned long ultimoTempoDebounce = 0;
- bool current_up = LOW;
- bool last_up = LOW;
- bool last_down = LOW;
- bool current_down = LOW;
- // Illuminazione
- int statoLed = 0;
- // Blynk Data
- #define PIN_Temp V0
- /*BLYNK_READ(PIN_Temp)
- {
- Blynk.virtualWrite(PIN_Temp, millis() / 1000);
- }*/
- void sendSensor()
- {
- int h = dht.readHumidity();
- int t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
- int ur = analogRead(V0);
- int u = map(ur, dryValueRaw, wetValueRaw, pDryValue, pWetValue);
- Blynk.virtualWrite(V1, h);
- Blynk.virtualWrite(V0, t);
- Blynk.virtualWrite(V2, u);
- }
- BLYNK_WRITE(V3){
- int pinValue = param.asInt();
- if (pinValue = 1){
- irsend.sendNEC(0xFF02FD, 32);
- statoLed = !statoLed;
- }
- else{
- irsend.sendNEC(0xFF02FD, 32);
- statoLed = !statoLed;
- }
- }
- void setup() {
- Serial.begin(9600); // Inizia la comunicazione seriale con il computer
- lcd.begin(16,2); // Inizializza lcd, 16 caratteri x 2 righe
- lcd.createChar(0, simbologradi);
- lcd.createChar(1, simbolopercentuale);
- lcd.createChar(2, simbolosmile);
- lcd.createChar(3, simbolosad);
- lcd.createChar(4, simboloneut);
- pinMode(pinPulsanteDown,INPUT);
- pinMode(pinPulsanteUp, INPUT);
- Blynk.begin(Serial, auth);
- dht.begin();
- timer.setInterval(1000L, sendSensor);
- terminal.clear();
- for(int i = 0; i < 5; i++){
- terminal.println((" "));
- }
- terminal.println(("Benvenuto nella tua Serrina!"));
- terminal.println(("-------------------------------"));
- }
- // Debounce
- bool debounce(bool last, int pin)
- {
- bool current = digitalRead(pin);
- if(last != current){
- delay(5);
- current = digitalRead(pin);
- }
- return current;
- }
- void loop() {
- Blynk.run();
- timer.run();
- // Umidità terreno
- int rawUmidita = analogRead(0); // Legge l'umidità
- int percentualeUmidita = map(rawUmidita, dryValueRaw, wetValueRaw, pDryValue, pWetValue); // Converte l'umidità in percentuale
- // DHT 11 - Temperatura e umidità
- int temperatura = dht.readTemperature();
- int umidita = dht.readHumidity();
- // Fotocellula
- int luminosita = analogRead(1);
- /*if(luminosita < 550 and statoLed == 0){
- irsend.sendNEC(0xFF02FD, 32);
- statoLed = !statoLed;
- }*/
- int letturaPulsanteLed = digitalRead(pinPulsanteLed);
- if(letturaPulsanteLed != ultimaLetturaPulsanteLed){
- ultimoTempoDebounce = millis();
- }
- if((millis() - ultimoTempoDebounce) > attesaDebounce){
- if(letturaPulsanteLed != statoPulsanteLed and letturaPulsanteLed == HIGH){
- irsend.sendNEC(0xFF02FD, 32);
- statoLed = !statoLed;
- delay(100);
- }
- statoPulsanteLed = letturaPulsanteLed;
- }
- ultimaLetturaPulsanteLed = letturaPulsanteLed;
- // Pagine LCD
- current_up = debounce(last_up, pinPulsanteUp);
- current_down = debounce(last_down, pinPulsanteDown);
- //Page Up
- if (last_up == LOW && current_up == HIGH){
- lcd.clear();
- if(pageCounter <4){
- pageCounter= pageCounter +1;
- }
- else{
- pageCounter= 4;
- }
- }
- last_up = current_up;
- //Page Down
- if (last_down == LOW && current_down == HIGH){
- lcd.clear();
- if(pageCounter >1){
- pageCounter= pageCounter -1;
- }
- else{
- pageCounter= 1;
- }
- }
- last_down = current_down;
- //------- Switch pagine---//
- switch (pageCounter) {
- // Pagina 1 - Temperatura Aria
- case 1:{
- lcd.setCursor(14,1);
- lcd.print("p1");
- lcd.setCursor(0,0);
- lcd.print("Temperatura Aria");
- lcd.setCursor(0,1);
- lcd.print(temperatura);
- lcd.setCursor(2,1);
- lcd.write(byte(0));
- lcd.print("C");
- }
- break;
- //Pagina 2 - Umidità Aria
- case 2: {
- lcd.setCursor(14,1);
- lcd.print("p2");
- lcd.setCursor(0,0);
- lcd.print("Umidita' Aria");
- lcd.setCursor(0,1);
- lcd.print(umidita);
- lcd.setCursor(2,1);
- lcd.write(byte(1));
- }
- break;
- //Pagina 3 - Umidità terreno
- case 3: {
- lcd.setCursor(0,0);
- lcd.print("Umidita' Terreno");
- lcd.setCursor(14,1);
- lcd.print("p3");
- if(percentualeUmidita < 30){
- lcd.setCursor(4,1);
- lcd.write(byte(3));
- }
- else if(percentualeUmidita < 50){
- lcd.setCursor(4,1);
- lcd.write(byte(4));
- }
- else if(percentualeUmidita < 85){
- lcd.setCursor(4,1);
- lcd.write(byte(2));
- }
- else{
- lcd.setCursor(4,1);
- lcd.write(byte(4));
- }
- if(percentualeUmidita < 10){
- lcd.setCursor(0,1);
- lcd.print(percentualeUmidita);
- lcd.setCursor(1,1);
- lcd.write(byte(1));
- lcd.setCursor(2,1);
- lcd.print(" ");
- }
- else if(percentualeUmidita < 0){
- lcd.setCursor(0,1);
- lcd.print(percentualeUmidita);
- lcd.setCursor(2,1);
- lcd.write(byte(1));
- }
- else {
- lcd.setCursor(0,1);
- lcd.print(percentualeUmidita);
- lcd.setCursor(2,1);
- lcd.write(byte(1));
- }
- }
- break;
- //Pagina 4 - Intensità luce
- case 4: {
- lcd.setCursor(14,1);
- lcd.print("p4");
- if(luminosita <1000){
- lcd.setCursor(0,0);
- lcd.print("Luminosita' amb.");
- lcd.setCursor(0,1);
- lcd.print(luminosita);
- lcd.setCursor(3,1);
- lcd.print(" ");
- }
- else {
- lcd.setCursor(0,0);
- lcd.print("Luminosita' amb.");
- lcd.setCursor(0,1);
- lcd.print(luminosita);
- }
- }
- break;
- delay(1000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement