prova codice locale
Dependencies: mbed ESP8266 Servo
main.cpp
- Committer:
- vidica94
- Date:
- 2016-10-09
- Revision:
- 6:3bb8c63c1561
- Parent:
- 5:f2e73e0562d0
- Child:
- 7:c79c53363102
File content as of revision 6:3bb8c63c1561:
#include "mbed.h" #include "com.h" #include "Servo.h" #define TOT_UMIDITA 3300 #define TOT_RUGIADA 3300 #define SEND_TIME 50 Servo myservo(D5); // test programma piantina AnalogIn umidit(A0); AnalogIn lux(A1); AnalogIn rugi(A2); AnalogIn temp(A3); Timer timer; DigitalOut irriga(PA_7);// irriga DigitalOut rosso(PA_9);// terreno secco DigitalOut verde(PB_6);// terreno mediamente umido DigitalOut blu(PC_7);// terreno innaffiato da poco o molto umido Serial pc(SERIAL_TX, SERIAL_RX); ESP8266 esp(PB_10, PB_11,115200); int main() { pc.baud(9600); float tempo;//tempo trascorso float umid;//umidità float luce;// luce float rugiada;// rugiada float temperatura;// temperarura amb #define priority 1 // ciascuna pianta ha una propria priorità // priorità massima =1 #define tmax 5 // tempo massimo di attesa prima di far partire l'irrigazione int tmaxr=tmax*priority ;// reale tempo da attendere tenendo conto della priorità int ti=0; uint8_t time_to_send_data = 0;//ogni 5 di questo timer significa che è passato un secondo, all'incirca, tramite la costante SEND_TIME decidiamo ogni quando inviare il dato // test funzionamento led rosso=1; wait(1); rosso=0; blu=1; wait(1); blu=0; verde=1; wait(1); verde=0; myservo.calibrate(0.0014, 45.0); // calibrazione servo myservo =0.5; pc.printf("servo calibrato "); while(1) { // Letture dei sensori if(ti){ tempo = timer.read(); // Converts and read the analog input value (value from 0.0 to 1.0) il livello di restituzione è normalizzato al massimo valore if( tempo>=tmaxr) { myservo = 1.0;// irriga timer.stop(); timer.reset(); // lampeggio led rosso 4 volte per segnalare che ha dovuto irrigare forzatamente for( int i=0;i<4;i++){ rosso=1; blu=0; verde=0; wait(1); rosso=0; blu=0; verde=0; } } } umid = umidit.read(); // Converts and read the analog input value (value from 0.0 to 1.0) il livello di restituzione è normalizzato al massimo valore umid = umid * 3300; // Change the value to be in the 0 to 3300 range luce = lux.read(); // Converts and read the analog input value (value from 0.0 to 1.0) il livello di restituzione è normalizzato al massimo valore luce = luce * 3300; // Change the value to be in the 0 to 3300 range rugiada = rugi.read(); // Converts and read the analog input value (value from 0.0 to 1.0) il livello di restituzione è normalizzato al massimo valore rugiada = rugiada * 3300; // Change the value to be in the 0 to 3300 range temperatura = temp.read(); // Converts and read the analog input value (value from 0.0 to 1.0) il livello di restituzione è normalizzato al massimo valore temperatura*=(3.685503686*100); temperatura-=10; // ragionamenti pc.printf("luce è %f\n",luce); // pc.printf("umid = %.0f mV \n", umid); // pc.printf("luce = %.0f mV \n", luce); if (luce<2000) { // se la luce è bassa ( notte) pc.printf("Il sole e' tramontato\n"); if (umid>1500) { // se il terreno è secco pc.printf("terreno secco"); rosso=0; blu=1; verde=0; timer.start(); ti=1; } else if(umid<1500) { // se èmolto umido pc.printf("terreno umido"); rosso = 0; blu=0; verde=1; timer.stop(); timer.reset(); ti=0; } } else { pc.printf("e' ancora giorno"); // se è giorno non ha senso illuminare i vasi if (umid>1500) { // se il terreno è secco <10% pc.printf("terreno secco"); timer.start(); ti=1; } else if(umid<1500) { // se umido pc.printf("terreno umido"); timer.stop(); timer.reset(); ti=0; } } wait(0.2); // 200 ms // pc.printf("la temperatura ambiente è %f\n",temperatura); /* if(++time_to_send_data == SEND_TIME)//Invio i dagtiì {//è arrivato finalmente il tempo di inviare! time_to_send_data^=time_to_send_data;//=0; connect(esp); uint8_t perc = (int)(TOT_UMIDITA*umid/100.0); send_data(esp,1,perc);//idx 1 è umidità terreno, 3 è bagnatura fogliare perc = (int)(TOT_RUGIADA*umid/100.0); send_data(esp,3,perc); disconnect(esp); }*/ } }