This shows you the differences between two versions of the page.
|
iothings:proiecte:2023sric:alcoholbreath [2024/05/30 08:33] dumitru_razvan.dinu |
iothings:proiecte:2023sric:alcoholbreath [2024/05/30 08:38] (current) dumitru_razvan.dinu |
||
|---|---|---|---|
| Line 49: | Line 49: | ||
| {{:iothings:proiecte:2023sric:app_mob.png?450|}} | {{:iothings:proiecte:2023sric:app_mob.png?450|}} | ||
| + | |||
| + | <code> | ||
| + | #include <DHT.h> | ||
| + | #include <Wire.h> | ||
| + | #include "BluetoothSerial.h" | ||
| + | #include <LiquidCrystal.h> | ||
| + | |||
| + | |||
| + | #define MQ3_PIN 35 | ||
| + | #define DHT_PIN 4 | ||
| + | #define DHT_TYPE DHT11 | ||
| + | #define BUZZER_PIN 14 | ||
| + | |||
| + | #define rs 16 | ||
| + | #define E 17 | ||
| + | #define D4 18 | ||
| + | #define D5 19 | ||
| + | #define D6 21 | ||
| + | #define D7 22 | ||
| + | #define V0 23 | ||
| + | |||
| + | LiquidCrystal lcd(rs, E, D4, D5, D6, D7); | ||
| + | |||
| + | BluetoothSerial SerialBT; | ||
| + | DHT dht(DHT_PIN, DHT_TYPE); | ||
| + | |||
| + | char received; | ||
| + | const float RL = 10.0; | ||
| + | const int NUM_READINGS = 100; | ||
| + | const int COLLECTION_PERIOD = 10000; | ||
| + | |||
| + | float R0; | ||
| + | |||
| + | bool startCollection = false; | ||
| + | |||
| + | void setup() { | ||
| + | SerialBT.begin("ESP32_Alcohol_Test"); | ||
| + | Serial.begin(115200); | ||
| + | pinMode(MQ3_PIN, INPUT); | ||
| + | pinMode(BUZZER_PIN, OUTPUT); | ||
| + | digitalWrite(BUZZER_PIN, LOW); | ||
| + | dht.begin(); | ||
| + | |||
| + | lcd.begin(16, 2); | ||
| + | lcd.print("Initializing..."); | ||
| + | |||
| + | pinMode(V0, OUTPUT); | ||
| + | analogWrite(V0, 100); | ||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | if (SerialBT.available()) { | ||
| + | received = SerialBT.read(); | ||
| + | if (received == 'C') { | ||
| + | R0 = calibrateMQ3(); | ||
| + | } else if (received == 'S') { | ||
| + | startCollection = true; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | if (startCollection) { | ||
| + | collectAndSendSensorData(); | ||
| + | startCollection = false; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | float calibrateMQ3() { | ||
| + | R0 = readRS(); | ||
| + | Serial.print("R0 in clean air: "); | ||
| + | Serial.print(R0); | ||
| + | Serial.println(" kOhm"); | ||
| + | |||
| + | lcd.clear(); | ||
| + | lcd.print("Calibrated"); | ||
| + | |||
| + | SerialBT.println("Ready to use!"); | ||
| + | return R0; | ||
| + | } | ||
| + | |||
| + | float readRS() { | ||
| + | long totalReadings = 0; | ||
| + | |||
| + | for (int i = 0; i < NUM_READINGS; i++) { | ||
| + | totalReadings += analogRead(MQ3_PIN); | ||
| + | delay(100); | ||
| + | } | ||
| + | |||
| + | int avgReading = totalReadings / NUM_READINGS; | ||
| + | float sensorVoltage = avgReading * (3.3 / 4095.0); | ||
| + | float RS = ((3.3 * RL) / sensorVoltage) - RL; | ||
| + | |||
| + | return RS; | ||
| + | } | ||
| + | |||
| + | void collectAndSendSensorData() { | ||
| + | digitalWrite(BUZZER_PIN, HIGH); | ||
| + | float temperature = dht.readTemperature(); | ||
| + | float humidity = dht.readHumidity(); | ||
| + | |||
| + | Serial.println("temperature: " + String(temperature)); | ||
| + | Serial.println("humidity: " + String(humidity)); | ||
| + | | ||
| + | float mq3_value = readRS(); | ||
| + | |||
| + | if (isnan(temperature) || isnan(humidity)) { | ||
| + | Serial.println("Failed to read from DHT sensor!"); | ||
| + | return; | ||
| + | } | ||
| + | | ||
| + | float alcohol_concentration; | ||
| + | |||
| + | float dif = R0 - mq3_value; | ||
| + | |||
| + | if (dif < 2) { | ||
| + | alcohol_concentration = 0; | ||
| + | } else { | ||
| + | float compensated = dif / (1 + (0.02 * (temperature - 20)) + (0.05 * (humidity - 65))); | ||
| + | alcohol_concentration = calculateAlcoholConcentration(compensated); | ||
| + | Serial.println("compensated: " + String(compensated)); | ||
| + | |||
| + | } | ||
| + | | ||
| + | Serial.println("R0: " + String(R0)); | ||
| + | Serial.println("Mq-3: " + String(mq3_value)); | ||
| + | Serial.println("dif: " + String(dif)); | ||
| + | | ||
| + | |||
| + | String alcohol_concentration_str = "Alcohol Concentration: " + String(alcohol_concentration) + " mg/L"; | ||
| + | |||
| + | Serial.println(alcohol_concentration_str); | ||
| + | |||
| + | lcd.clear(); | ||
| + | lcd.print(String(alcohol_concentration) + " mg/L"); | ||
| + | |||
| + | SerialBT.println(alcohol_concentration_str); | ||
| + | digitalWrite(BUZZER_PIN, LOW); | ||
| + | |||
| + | } | ||
| + | |||
| + | float calculateAlcoholConcentration(float ratio) { | ||
| + | float m = 45; | ||
| + | float b = -0.77; | ||
| + | double alcohol_ppm = (log10(ratio) - b) / m ; | ||
| + | double alcohol_mg_per_l = alcohol_ppm * 1.25; | ||
| + | return alcohol_mg_per_l; | ||
| + | } | ||
| + | </code> | ||
| ===== Mod de functionare ===== | ===== Mod de functionare ===== | ||