From 3a569d6c88cd09af00bbeb0a0bc02c427abde7d7 Mon Sep 17 00:00:00 2001 From: Kai Nguyen Date: Fri, 17 Nov 2023 19:20:42 +0700 Subject: [PATCH] deploy IoT --- iot/arduino/arduino.ino | 54 +++++++++++++++++++++++++++++++++++++++-- iot/esp8266/esp8266.ino | 11 +++++++-- 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/iot/arduino/arduino.ino b/iot/arduino/arduino.ino index 70adec4..479c70c 100644 --- a/iot/arduino/arduino.ino +++ b/iot/arduino/arduino.ino @@ -4,6 +4,8 @@ #include "MQ135.h" #include "MQ131.h" #include "Adafruit_CCS811.h" +#include "SharpGP2Y10.h" +#include // CAM BIEN DHT11 const int DHTPIN = 2; //Đọc dữ liệu từ DHT11 ở chân 2 trên mạch Arduino @@ -33,9 +35,50 @@ int tvoc; // ESP Serial SoftwareSerial espSerial(10, 11); // RX, TX +// Optical Dust Sensor (Sharp GP2Y10) +int voPin = A5; +int dustSensorLedPin = 13; + +int samplingTime = 280; +int deltaTime = 40; +int sleepTime = 9680; +float voMeasured = 0; +float voltage = 0; + +float pm25 = 0; + +// SharpGP2Y10 dustSensor(voPin, ledPin); + +void measureOpticalDustSensor() { + digitalWrite(dustSensorLedPin,LOW); // Bật IR LED + + delayMicroseconds(samplingTime); //Delay 0.28ms + + voMeasured = analogRead(voPin); // Đọc giá trị ADC V0 + + delayMicroseconds(deltaTime); //Delay 0.04ms + + digitalWrite(dustSensorLedPin,HIGH); // Tắt LED + + delayMicroseconds(sleepTime); //Delay 9.68ms + + // Tính điện áp từ giá trị ADC Vo + voltage = voMeasured * (5.0 / 1024.0); + Serial.print("PM2.5 voltage: "); + Serial.println(voltage); + + pm25 = 170 * voltage - 0.1; // unit: ug/m3 + + Serial.print("PM2.5: "); + Serial.print(pm25); + Serial.println(" ug/m3"); +} + void warmUpSensor() { + Serial.println(".....Warm up....."); + digitalWrite(6, HIGH); // Ozone sensor - delay(60000); //1min for warm heater + delay(1 * 30 * 1000); // 5 min for warm heater digitalWrite(6, LOW); } @@ -97,6 +140,9 @@ void setup() { espSerial.begin(9600); pinMode(DPIN_MQ7, INPUT); + // dust sensor + pinMode(dustSensorLedPin, OUTPUT); + pinMode(6, OUTPUT); dht.begin(); @@ -157,7 +203,10 @@ void loop() { Serial.print(o3); Serial.println(" ppm"); - Serial.println("=========================="); // Xuong dong + // Đo PM2.5 + measureOpticalDustSensor(); + + Serial.println("============================="); // Xuong dong // SERIAL JSON DynamicJsonDocument doc(1024); @@ -169,6 +218,7 @@ void loop() { doc["co2"] = co2; doc["tvoc"] = tvoc; doc["o3"] = o3; + doc["pm25"] = round(pm25 * 10.0) / 10.0; // round to 2 decimal places serializeJson(doc, espSerial); diff --git a/iot/esp8266/esp8266.ino b/iot/esp8266/esp8266.ino index 8f44835..75436e6 100644 --- a/iot/esp8266/esp8266.ino +++ b/iot/esp8266/esp8266.ino @@ -10,7 +10,7 @@ const char *ssid = "ANHKIET"; // Enter SSID const char *password = "family@123"; // Enter Password -#define mqtt_host "airchecker.online" +#define mqtt_host "66.42.63.123" #define mqtt_topic "/airchecker" #define mqtt_topic_noti "/airchecker/noti" #define mqtt_user "admin" @@ -23,7 +23,8 @@ PubSubClient client(espClient); // Wiring for ESP8266 NodeMCU boards: VDD to 3V3, GND to GND, SDA to D2, SCL to D1, nWAKE to D3 (or GND) CCS811 ccs811(D3); // nWAKE on D3 -float co2, tvoc; +float co2 = 0; +float tvoc = 0; void ccs811_init() { Serial.print("Setup: ccs811 lib version: "); @@ -68,6 +69,12 @@ void ccs811_read() { Serial.print("TVOC: "); Serial.print(tvoc); Serial.println("ppb"); + + } else { + Serial.print("CCS811: errstat="); + Serial.print(errstat, HEX); + Serial.print("="); + Serial.println(ccs811.errstat_str(errstat)); } }