-
Notifications
You must be signed in to change notification settings - Fork 118
/
RAK12008_CO2_Detection.ino
131 lines (113 loc) · 3.91 KB
/
RAK12008_CO2_Detection.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* @file RAK12008_CO2_Detection.ino
* @author rakwireless.com
* @brief use MG812 sensor detect CO2 example.
* @version 0.1
* @date 2021-06-08
* @copyright Copyright (c) 2021
*/
#include <Wire.h>
#include "ADC121C021.h" // Click to install library: http://librarymanager/All#MQx
#include <U8g2lib.h> // Click to install library: http://librarymanager/All#u8g2
#define EN_PIN WB_IO6 //Logic high enables the device. Logic low disables the device
#define ALERT_PIN WB_IO5 //a high indicates that the respective limit has been violated.
#define MG812_ADDRESS 0x50 //the device i2c address
#define V_RATIO 3.0 // voltage amplification factor
float constantA = 0.027; //log(y) = constantA*log(x) + constantB, y:sensor voltage, x:gas concentration ppm
float constantB = 0.4524; //log(y) = constantA*log(x) + constantB, y:sensor voltage, x:gas concentration ppm
ADC121C021 MG812;
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0);
uint16_t result;
char displayData[32]; //OLED dispaly datas
//Function declaration
void firstDisplay();
void setup()
{
pinMode(ALERT_PIN,INPUT);
pinMode(EN_PIN,OUTPUT);
digitalWrite(EN_PIN,HIGH); //power on RAK12008
delay(500);
time_t timeout = millis();
Serial.begin(115200);
while (!Serial)
{
if ((millis() - timeout) < 5000)
{
delay(100);
}
else
{
break;
}
}
//***********OLED display init *****************************************
u8g2.begin();
u8g2.clearDisplay();
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_ncenB10_tr); // choose a suitable font
memset(displayData, 0, sizeof(displayData));
sprintf(displayData, "RAK12008 Test");
u8g2.drawStr(3, 15, displayData);
sprintf(displayData, "MG812 checking...");
u8g2.drawStr(3, 45, displayData);
u8g2.sendBuffer();
//********ADC121C021 ADC convert init ********************************
while(!(MG812.begin(MG812_ADDRESS,Wire)))
{
Serial.println("please check device!!!");
delay(200);
}
Serial.println("RAK12008 test Example");
//**************init MG812********************************************
firstDisplay();
delay(3000);
}
void loop()
{
float sensorPPM;
float PPMpercentage;
float sensorVoltage;
Serial.println("Getting Conversion Readings from ADC121C021");
Serial.println(" ");
sensorVoltage = MG812.getSensorVoltage()/V_RATIO;
double ppm_log = (constantB-sensorVoltage)/constantA;
sensorPPM = pow(M_E, ppm_log);
Serial.printf("sensor voltage Value is: %3.2f\r\n",sensorVoltage);
Serial.printf("sensor PPM Value is: %3.2f\r\n",sensorPPM);
PPMpercentage = sensorPPM/10000;
Serial.printf("PPM percentage Value is:%3.2f%%\r\n",PPMpercentage);
Serial.println(" ");
Serial.println(" *************************** ");
Serial.println(" ");
u8g2.clearDisplay();
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_ncenB10_tr); // choose a suitable font
memset(displayData, 0, sizeof(displayData));
sprintf(displayData, "RAK12008 Test");
u8g2.drawStr(3, 15, displayData);
memset(displayData, 0, sizeof(displayData));
sprintf(displayData, "CO2:");
u8g2.drawStr(3, 30, displayData);
memset(displayData, 0, sizeof(displayData));
sprintf(displayData, "%3.2f PPM",sensorPPM);
u8g2.drawStr(3, 45, displayData);
memset(displayData, 0, sizeof(displayData));
sprintf(displayData, "%3.2f %%",PPMpercentage);
u8g2.drawStr(3, 60, displayData);
u8g2.sendBuffer();
delay(1000);
}
void firstDisplay()
{
u8g2.clearDisplay();
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_ncenB10_tr); // choose a suitable font
memset(displayData, 0, sizeof(displayData));
sprintf(displayData, "RAK12008 Test");
u8g2.drawStr(3, 15, displayData);
u8g2.sendBuffer();
float voltage = MG812.getSensorVoltage();
sprintf(displayData, "voltage:%3.3f",voltage);
u8g2.drawStr(3, 45, displayData);
u8g2.sendBuffer();
}