This shows you the differences between two versions of the page.
pm:prj2023:dene:cosmin-duta [2023/05/24 19:03] cosmin_mihail.duta [Introducere] |
pm:prj2023:dene:cosmin-duta [2023/05/30 14:31] (current) cosmin_mihail.duta [Software Design - Descrierea codului aplicatiei] |
||
---|---|---|---|
Line 12: | Line 12: | ||
ducând la probleme de sănătate și la distrugerea mediului înconjurător. | ducând la probleme de sănătate și la distrugerea mediului înconjurător. | ||
Un sistem de înregistrare al fumului, temperaturii și umidității poate fi deosebit de util în lupta împotriva poluării. | Un sistem de înregistrare al fumului, temperaturii și umidității poate fi deosebit de util în lupta împotriva poluării. | ||
- | Acest sistem poate monitoriza si detectarapid nivelul de poluare dintr-o anumită zonă in doar cateva secunde. | + | Acest sistem poate monitoriza si detecta rapid nivelul de poluare dintr-o anumită zonă in doar cateva secunde. |
De asemenea, poate fi folosit pentru a evalua impactul unui anumit proces sau eveniment asupra mediului înconjurător. | De asemenea, poate fi folosit pentru a evalua impactul unui anumit proces sau eveniment asupra mediului înconjurător. | ||
Într-o lume în care mediul înconjurător este tot mai vulnerabil, aceste sisteme sunt mai importante ca niciodată. | Într-o lume în care mediul înconjurător este tot mai vulnerabil, aceste sisteme sunt mai importante ca niciodată. | ||
Line 109: | Line 109: | ||
</note> | </note> | ||
- | ===== Software Design: Libraries ===== | + | ===== Software Design - Descrierea codului aplicatiei ===== |
+ | |||
<note tip> | <note tip> | ||
- | Librarii folosite: | ||
- | * Dht11.h | ||
- | * Wire.h | ||
- | * LiquidCrystal_I2C.h | ||
- | * SPI.h | ||
- | * Ethernet.h | ||
- | * SD.h | ||
- | </note> | + | /*Importam librariile necesare*/ |
+ | #include <dht11.h> | ||
+ | #include <Wire.h> | ||
+ | #include <LiquidCrystal_I2C.h> | ||
+ | #include <SPI.h> | ||
+ | #include <Ethernet.h> | ||
+ | #include <SD.h> | ||
- | ===== Rezultate obtinute ===== | + | LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); |
- | ===== Concluzii ===== | + | dht11 DHT11; |
+ | #define DHT11PIN 3 | ||
+ | int sensorValue; | ||
+ | int alb = 1; // led alb pe pin digital 1 | ||
+ | int rosu = 2; // led alb pe pin digital 2 | ||
+ | int t; | ||
+ | int h; | ||
+ | String aer = "Normal"; // consideram in momentul de start ca aerul este normal | ||
+ | // Introducem o adresa MAC unica si o adresa IP | ||
+ | byte mac[] = { | ||
+ | 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED | ||
+ | }; | ||
+ | IPAddress ip( 192, 168, 0, 123 ); | ||
+ | IPAddress gateway( 192, 168, 0, 1 ); | ||
+ | IPAddress subnet( 255, 255, 255, 0 ); | ||
+ | IPAddress DNSserver(192,168,0,1); | ||
+ | |||
+ | char server[] = "mail.smtp.ro"; //aici trebuie schimbat cu serverul vostru de smtp - gmail | ||
+ | int port = 587; // porturile 25, 8025 sau 587. | ||
+ | |||
+ | File myFile; | ||
+ | |||
+ | EthernetClient client; | ||
+ | int c = 1; //contor de intarziere | ||
+ | int k=0; //contor de verificare timp alarma | ||
+ | |||
+ | void setup() | ||
+ | { | ||
+ | lcd.begin(16,2); // initializam LCD-ul | ||
+ | |||
+ | pinMode(alb, OUTPUT); | ||
+ | pinMode(rosu, OUTPUT); | ||
+ | |||
+ | digitalWrite(rosu, HIGH); | ||
+ | digitalWrite(alb, HIGH); | ||
+ | |||
+ | Serial.begin(115200); | ||
+ | pinMode(4,OUTPUT); | ||
+ | digitalWrite(4,HIGH); | ||
+ | Ethernet.begin(mac, ip, gateway, DNSserver, subnet); | ||
+ | delay(2000); | ||
+ | Serial.println(Ethernet.localIP()); | ||
+ | Serial.println(F("Gata! Apăsati 'e' pentru a trimite mail")); // metoda manuala de trimis mail de test din Serial Monitor | ||
+ | | ||
+ | } | ||
+ | |||
+ | void loop() | ||
+ | { | ||
+ | | ||
+ | byte inChar; | ||
+ | |||
+ | inChar = Serial.read(); | ||
+ | |||
+ | if(inChar == 'e') | ||
+ | { | ||
+ | if(sendEmail()) Serial.println(F("Email trimis")); | ||
+ | else Serial.println(F("Eroare trimitere Email")); | ||
+ | } | ||
+ | | ||
+ | | ||
+ | afisare_info(); // apelam functia de afisare a informatiilor pe LCD | ||
+ | | ||
+ | } | ||
+ | |||
+ | void afisare_info(){ | ||
+ | DHT11.read(DHT11PIN); // citim valorile senzorului DHT11 | ||
+ | sensorValue = analogRead(0); // se citeste valoarea senzorului de fum | ||
+ | // //aer normal in jur de 120-150 | ||
+ | // //peste 150 posibil sa existe fum | ||
+ | |||
+ | t = (float)DHT11.temperature, 2; | ||
+ | h = (float)DHT11.humidity, 2; | ||
+ | |||
+ | //afisam temperatura pe LCD | ||
+ | lcd.clear(); | ||
+ | lcd.setCursor(0, 0); | ||
+ | lcd.print("Temp: "); | ||
+ | lcd.print(t); | ||
+ | lcd.print((char)223); | ||
+ | lcd.print("C"); | ||
+ | | ||
+ | //afisam umiditatea pe LCD | ||
+ | lcd.setCursor(0, 1); | ||
+ | lcd.print("Umid: "); | ||
+ | lcd.print((float)DHT11.humidity, 2); | ||
+ | lcd.print(" %"); | ||
+ | |||
+ | delay(2000); | ||
+ | //afisam nivelul de fum pe LCD | ||
+ | lcd.clear(); | ||
+ | lcd.setCursor(0, 0); | ||
+ | lcd.print("Nivel fum: "); | ||
+ | lcd.print(sensorValue); | ||
+ | |||
+ | //si afisam Normal sau FUM pe LCD | ||
+ | lcd.setCursor(0, 1); | ||
+ | lcd.print("Aer: "); | ||
+ | if(sensorValue>150){ | ||
+ | lcd.print("FUM!"); | ||
+ | aer = "FUM!"; // daca senzorul a detectat FUM, variabila "aer" devine FUM | ||
+ | }else{ | ||
+ | lcd.print("Normal"); | ||
+ | aer = "Normal"; | ||
+ | } | ||
+ | |||
+ | //Aici se stabilesc valorile de alerta: | ||
+ | |||
+ | //verificam daca unul din parametri a depasit limita maxima sau daca a fost detectat deja FUM | ||
+ | if(aer == "FUM!" || t >= 25 || h >= 80){ | ||
+ | digitalWrite(rosu, HIGH); | ||
+ | digitalWrite(alb, LOW); | ||
+ | }else{ | ||
+ | digitalWrite(alb, HIGH); | ||
+ | digitalWrite(rosu, LOW); | ||
+ | } | ||
+ | |||
+ | |||
+ | if(c>10){ // la pornire lasam 20 de secunde sa se stabilizeze senzorii, dupa care trimitem alerte daca sunt probleme | ||
+ | // am pus 10 pentru ca mai jos este delay de 2000 (10*2000=20secunde) | ||
+ | if(aer == "FUM!" || t >= 25 || h >= 80){ | ||
+ | k++; // contor ce numara de cate ori se inregistreaza eroare. | ||
+ | if(k>=30){ //daca timp de 60 secunde este alarma, se trimite mail | ||
+ | if(sendEmail_alerta()) Serial.println(F("Email trimis")); | ||
+ | else Serial.println(F("Eroare trimitere Email")); | ||
+ | k=0; // resetam contorul daca s-a trimis mail | ||
+ | } | ||
+ | } | ||
+ | }else{ | ||
+ | c++; | ||
+ | } | ||
+ | delay(2000); | ||
+ | } | ||
+ | |||
+ | //functia de mai jos trimite mail la cerere din Serial Monitor, apasand tasta 'e' | ||
+ | byte sendEmail() | ||
+ | { | ||
+ | byte thisByte = 0; | ||
+ | byte respCode; | ||
+ | |||
+ | if(sensorValue>150){ | ||
+ | aer = "FUM!"; | ||
+ | }else{ | ||
+ | aer = "Normal"; | ||
+ | } | ||
+ | //Codul de conectare la serverul de smtp si trimitere mail este preluat de aici: | ||
+ | // https://www.smtp2go.com/setupguide/arduino/ | ||
+ | |||
+ | if(client.connect(server,port) == 1) { | ||
+ | Serial.println(F("connected")); | ||
+ | } else { | ||
+ | Serial.println(F("connection failed")); | ||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | if(!eRcv()) return 0; | ||
+ | |||
+ | Serial.println(F("Sending hello")); | ||
+ | // replace 1.2.3.4 with your Arduino's ip | ||
+ | client.println("EHLO 1.2.3.4"); | ||
+ | if(!eRcv()) return 0; | ||
+ | |||
+ | Serial.println(F("Sending auth login")); | ||
+ | client.println("auth login"); | ||
+ | if(!eRcv()) return 0; | ||
+ | |||
+ | Serial.println(F("Sending User")); | ||
+ | // Change to your base64 encoded user | ||
+ | client.println(F("user criptat Base64 aici")); | ||
+ | |||
+ | |||
+ | if(!eRcv()) return 0; | ||
+ | |||
+ | Serial.println(F("Sending Password")); | ||
+ | // change to your base64 encoded password | ||
+ | client.println(F("parola criptata Base64 aici")); | ||
+ | |||
+ | |||
+ | if(!eRcv()) return 0; | ||
+ | |||
+ | // change to your email address (sender) | ||
+ | Serial.println(F("Sending From")); | ||
+ | client.println("MAIL From: <expeditor@cevreitu.ro>"); | ||
+ | if(!eRcv()) return 0; | ||
+ | |||
+ | // change to recipient address | ||
+ | Serial.println(F("Sending To")); | ||
+ | client.println("RCPT To: <destinatar@cevreitu.ro>"); | ||
+ | if(!eRcv()) return 0; | ||
+ | |||
+ | Serial.println(F("Sending DATA")); | ||
+ | client.println("DATA"); | ||
+ | if(!eRcv()) return 0; | ||
+ | |||
+ | Serial.println(F("Sending email")); | ||
+ | |||
+ | // change to recipient address | ||
+ | client.println("To: Destinatar <destinatar@cevreitu.ro>"); | ||
+ | |||
+ | // change to your address | ||
+ | client.println("From: Camera Server <cameraserver@cevreitu.ro>"); | ||
+ | |||
+ | client.println("Subject: Date termice Camera Server"); | ||
+ | |||
+ | client.print("Date termice Camera Server:"); | ||
+ | client.println(""); | ||
+ | client.print("Temperatura: "); | ||
+ | client.print(t); | ||
+ | client.print(" C"); | ||
+ | client.println(""); | ||
+ | client.print("Umiditatea: "); | ||
+ | client.print((float)DHT11.humidity, 2); | ||
+ | client.println(""); | ||
+ | client.print("Nivel fum: "); | ||
+ | client.print(sensorValue); | ||
+ | client.println(""); | ||
+ | client.print("Aer: "); | ||
+ | client.print(aer); | ||
+ | client.println(""); | ||
+ | | ||
+ | client.println("."); | ||
+ | |||
+ | if(!eRcv()) return 0; | ||
+ | |||
+ | Serial.println(F("Sending QUIT")); | ||
+ | client.println("QUIT"); | ||
+ | if(!eRcv()) return 0; | ||
+ | |||
+ | client.stop(); | ||
+ | |||
+ | Serial.println(F("disconnected")); | ||
+ | |||
+ | return 1; | ||
+ | } | ||
+ | |||
+ | //functia de mai jos trimite mail cand este detectata o alarma | ||
+ | byte sendEmail_alerta() | ||
+ | { | ||
+ | byte thisByte = 0; | ||
+ | byte respCode; | ||
+ | |||
+ | if(sensorValue>150){ | ||
+ | aer = "FUM!"; | ||
+ | }else{ | ||
+ | aer = "Normal"; | ||
+ | } | ||
+ | | ||
+ | if(client.connect(server,port) == 1) { | ||
+ | Serial.println(F("connected")); | ||
+ | } else { | ||
+ | Serial.println(F("connection failed")); | ||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | if(!eRcv()) return 0; | ||
+ | |||
+ | Serial.println(F("Sending hello")); | ||
+ | // replace 1.2.3.4 with your Arduino's ip | ||
+ | client.println("EHLO 1.2.3.4"); | ||
+ | if(!eRcv()) return 0; | ||
+ | |||
+ | Serial.println(F("Sending auth login")); | ||
+ | client.println("auth login"); | ||
+ | if(!eRcv()) return 0; | ||
+ | |||
+ | Serial.println(F("Sending User")); | ||
+ | // Change to your base64 encoded user | ||
+ | client.println(F("user criptat Base64 aici")); | ||
+ | |||
+ | |||
+ | if(!eRcv()) return 0; | ||
+ | |||
+ | Serial.println(F("Sending Password")); | ||
+ | // change to your base64 encoded password | ||
+ | client.println(F("parola criptata Base64 aici")); | ||
+ | |||
+ | |||
+ | if(!eRcv()) return 0; | ||
+ | |||
+ | // change to your email address (sender) | ||
+ | Serial.println(F("Sending From")); | ||
+ | client.println("MAIL From: <cameraserver@cevreitu.ro>"); | ||
+ | if(!eRcv()) return 0; | ||
+ | |||
+ | // change to recipient address | ||
+ | Serial.println(F("Sending To")); | ||
+ | client.println("RCPT To: <destinatar@cevreitu.ro>"); | ||
+ | if(!eRcv()) return 0; | ||
+ | |||
+ | Serial.println(F("Sending DATA")); | ||
+ | client.println("DATA"); | ||
+ | if(!eRcv()) return 0; | ||
+ | |||
+ | Serial.println(F("Sending email")); | ||
+ | |||
+ | // change to recipient address | ||
+ | client.println("To: Destinatar <destinatar@cevreitu.ro>"); | ||
+ | |||
+ | // change to your address | ||
+ | client.println("From: Camera Server <cameraserver@cevreitu.ro>"); | ||
+ | |||
+ | client.println("Subject: ALERTA CAMERA SERVER!!!"); | ||
+ | |||
+ | client.print("Date termice Camera Server:"); | ||
+ | client.println(""); | ||
+ | client.print("Temperatura: "); | ||
+ | client.print(t); | ||
+ | client.print(" C"); | ||
+ | client.println(""); | ||
+ | client.print("Umiditatea: "); | ||
+ | client.print((float)DHT11.humidity, 2); | ||
+ | client.println(""); | ||
+ | client.print("Nivel fum: "); | ||
+ | client.print(sensorValue); | ||
+ | client.println(""); | ||
+ | client.print("Aer: "); | ||
+ | client.print(aer); | ||
+ | client.println(""); | ||
+ | | ||
+ | client.println("."); | ||
+ | |||
+ | if(!eRcv()) return 0; | ||
+ | |||
+ | Serial.println(F("Sending QUIT")); | ||
+ | client.println("QUIT"); | ||
+ | if(!eRcv()) return 0; | ||
+ | |||
+ | client.stop(); | ||
+ | |||
+ | Serial.println(F("disconnected")); | ||
+ | |||
+ | return 1; | ||
+ | } | ||
+ | |||
+ | byte eRcv() | ||
+ | { | ||
+ | byte respCode; | ||
+ | byte thisByte; | ||
+ | int loopCount = 0; | ||
+ | |||
+ | while(!client.available()) { | ||
+ | delay(1); | ||
+ | loopCount++; | ||
+ | |||
+ | // if nothing received for 10 seconds, timeout | ||
+ | if(loopCount > 10000) { | ||
+ | client.stop(); | ||
+ | Serial.println(F("\r\nTimeout")); | ||
+ | return 0; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | respCode = client.peek(); | ||
+ | |||
+ | while(client.available()) | ||
+ | { | ||
+ | thisByte = client.read(); | ||
+ | Serial.write(thisByte); | ||
+ | } | ||
+ | |||
+ | if(respCode >= '4') | ||
+ | { | ||
+ | efail(); | ||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | return 1; | ||
+ | } | ||
+ | |||
+ | |||
+ | void efail() | ||
+ | { | ||
+ | byte thisByte = 0; | ||
+ | int loopCount = 0; | ||
+ | |||
+ | client.println(F("QUIT")); | ||
+ | |||
+ | while(!client.available()) { | ||
+ | delay(1); | ||
+ | loopCount++; | ||
+ | |||
+ | // if nothing received for 10 seconds, timeout | ||
+ | if(loopCount > 10000) { | ||
+ | client.stop(); | ||
+ | Serial.println(F("\r\nTimeout")); | ||
+ | return; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | while(client.available()) | ||
+ | { | ||
+ | thisByte = client.read(); | ||
+ | Serial.write(thisByte); | ||
+ | } | ||
+ | |||
+ | client.stop(); | ||
+ | |||
+ | Serial.println(F("disconnected")); | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | </note> |