Monitor Kualitas Udara ( AIR QUALITY ) MICS-6814 dan DEBU (DUST) Winsen ZH03 ARDUINO IOT BLYNK 2.0
Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang dapat memonitor kualitas udara meliputi CO, NH3, NO2, dan Debu. alat ini menggunakan fitur IOT Blynk sehingga dapat memonitor udara dengan jarak jauh baik itu melalui handphone atau website. untuk lebih jelasnya berikut adalah koding skemanya.
a. Skema Alat
#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <SD.h>
#include <WinsenZH03.h>
WinsenZH03 sensorZH;
#define DS3231_I2C_ADDRESS 0x68
LiquidCrystal_I2C lcd(0x27, 20, 4);
//mics-6814
const float max_volts = 5.0;
const float max_analog_steps = 1023.0;
int co, nh3, no2;
int cox, nh3x, no2x;
int debu;
int cacah;
File myFile;
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return( (val/16*10) + (val%16) );
}
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year)
{
// sets time and date data to DS3231
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set next input to start at the seconds register
Wire.write(decToBcd(second)); // set seconds
Wire.write(decToBcd(minute)); // set minutes
Wire.write(decToBcd(hour)); // set hours
Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
Wire.write(decToBcd(month)); // set month
Wire.write(decToBcd(year)); // set year (0 to 99)
Wire.endTransmission();
}
void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set DS3231 register pointer to 00h
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
// request seven bytes of data from DS3231 starting from register 00h
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f);
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
void displayTime()
{
// retrieve data from DS3231
readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
&year);
/*
lcd.setCursor(0,0);
// send it to the serial monitor
lcd.print(hour, DEC);
// convert the byte variable to a decimal number when displayed
lcd.print(":");
if (minute<10)
{
lcd.print("0");
}
lcd.print(minute, DEC);
lcd.print(":");
if (second<10)
{
lcd.print("0");
}
lcd.print(second, DEC);
*/
}
void setup() {
Serial.begin(9600); //UART to pc
Serial1.begin(9600); //UART to sensor
sensorZH.begin(&Serial1);//configure the serial that the sensor will use
sensorZH.setAs(QA);//configure the sensor QA mode
delay(1000);
//Serial.println("Setup initializing");
Wire.begin();
Serial.begin(9600);
// DS3231 seconds, minutes, hours, day, date, month, year
// setDS3231time(0,1,18,6,19,4,24);
lcd.begin();
lcd.clear();
lcd.noCursor();
}
void loop() {
displayTime();
co = analogRead(A0);
nh3 = analogRead(A1);
no2 = analogRead(A2);
cox = map (analogRead(A0), 0, 1023, 1, 1000); // Calcolo Monossido di Carbonio
nh3x = map (analogRead(A1), 0, 1023, 1, 500); // Calcolo Ammoniaca
no2x = (map (analogRead(A2), 0, 1023, 5, 1000)) / 100.0 ; // Calcolo Dios
sensorZH.readOnce();//Every time a sample is taken, this function should be set, only in QA.
debu = sensorZH.readPM2_5();
/*
Serial.print("PM1.0: ");
Serial.print(sensorZH.readPM1_0());//
Serial.println(" ug/m3");
Serial.print("PM2.5: ");
Serial.print(sensorZH.readPM2_5());
Serial.println(" ug/m3");
Serial.print("PM10: ");
Serial.print(sensorZH.readPM10());
Serial.println(" ug/m3");
delay(1000);
*/
lcd.setCursor(0,0);
lcd.print("CO: ");
lcd.print(cox);
lcd.print(" ppm ");
lcd.setCursor(0,1);
lcd.print("NH3: ");
lcd.print(nh3x);
lcd.print(" ppm ");
lcd.setCursor(0,2);
lcd.print("NO2: ");
lcd.print(no2x);
lcd.print(" ppm ");
lcd.setCursor(0,3);
lcd.print("PM2.5: ");
lcd.print(debu);
lcd.print(" ug/m3 ");
Serial.print("*");
Serial.print(cox);
Serial.print(",");
Serial.print(nh3x);
Serial.print(",");
Serial.print(no2x);
Serial.print(",");
Serial.print(debu);
Serial.println("#");
delay(1000);
cacah++;
if(cacah >= 3){
simpan();
cacah = 0;
}
}
void simpan(){
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(53, OUTPUT);
if (!SD.begin(53)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
myFile.print("Tanggal:");
myFile.print(dayOfMonth);
myFile.print("/");
myFile.print(month);
myFile.print("/");
myFile.println(year);
myFile.print("Jam:");
myFile.print(hour);
myFile.print(":");
myFile.print(minute);
myFile.print(":");
myFile.println(second);
myFile.print("CO: ");
myFile.println(cox);
myFile.print("NH3: ");
myFile.println(nh3x);
myFile.print("NO2: ");
myFile.println(no2x);
myFile.print("PM2.5: ");
myFile.println(debu);
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
c. Program NODEMCU ESP8266
#define BLYNK_TEMPLATE_ID "TMPL6wOC--xxx"
#define BLYNK_TEMPLATE_NAME "monitor daya"
#define BLYNK_AUTH_TOKEN "VOK0cWiFN5ycHj3SV_snEDXTfTfeixxx"
#define BLYNK_PRINT Serial
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
int temp;
int x = 5;
int y;
int value1;
int value2;
int co = 0;
int nh3 = 0;
int no2 = 0;
int ppm = 0;
float datain1;
float datain2;
float datain3;
float datain4;
float datain5;
float datain6;
String dataIn;
String dt[10];
int i;
boolean parsing=false;
int pinValue;
int pinValue2;
int pinValue3;
BlynkTimer timer;
char ssid[] = "hotspothpku";
char pass[] = "123456789";
void sendSensor()
{
Blynk.virtualWrite(V0, co);
Blynk.virtualWrite(V1, nh3);
Blynk.virtualWrite(V2, no2);
Blynk.virtualWrite(V3, ppm);
delay(1000);
}
void setup()
{
dataIn="";
// Debug console
Serial.begin(9600);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
timer.setInterval(1000L, sendSensor);
pinMode(D6,OUTPUT);
pinMode(D7,OUTPUT);
pinMode(D8,OUTPUT);
}
void loop()
{
while(Serial.available()>0) {
// dataIn="";
char inChar = (char)Serial.read();
dataIn += inChar;
if (inChar == '\n') {
parsing = true;
}
}
if(parsing){
parsingData();
Blynk.run();
timer.run();
}
}
void parsingData(){
int j=0;
//kirim data yang telah diterima sebelumnya
//Serial.print("data masuk : ");
//Serial.print(dataIn);
//Serial.print("\n");
//inisialisasi variabel, (reset isi variabel)
dt[j]="";
//proses parsing data
for(i=1;i<dataIn.length();i++){
//pengecekan tiap karakter dengan karakter (#) dan (,)
if ((dataIn[i] == '#') || (dataIn[i] == ','))
{
//increment variabel j, digunakan untuk merubah index array penampung
j++;
dt[j]=""; //inisialisasi variabel array dt[j]
}
else
{
//proses tampung data saat pengecekan karakter selesai.
dt[j] = dt[j] + dataIn[i];
}
}
datain1 = dt[0].toInt();
datain2 = dt[1].toInt();
datain3 = dt[2].toInt();
datain4 = dt[3].toInt();
//kirim data hasil parsing
Serial.print("data 1 : ");
Serial.print(datain1);
Serial.print("\n");
Serial.print("data 2 : ");
Serial.print(datain2);
Serial.print("\n");
Serial.print("data 3 : ");
Serial.print(datain3);
Serial.print("\n");
Serial.print("data 4 : ");
Serial.print(datain4);
Serial.print("\n");
//Serial.print("data 3 : ");
//Serial.print(dt[2].toInt());
//Serial.print("\n\n");
co = datain1 / 1;
nh3 = datain2 / 1;
no2 = datain3 / 1;
ppm = datain4 / 1;
}
d. VIDEO HASILNYA
No comments:
Post a Comment