-
Notifications
You must be signed in to change notification settings - Fork 1
/
BasicUsageMHZ14A.ino
257 lines (205 loc) · 9.17 KB
/
BasicUsageMHZ14A.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#define _DEBUG_;
#include <Arduino.h>
#include <SoftwareSerial.h> // Remove if using HardwareSerial
//#include <SPI.h>
//#include <Wire.h>
//#include <Adafruit_GFX.h>
//#include <Adafruit_SSD1306.h>
//#define SCREEN_WIDTH 128 // OLED display width, in pixels
//#define SCREEN_HEIGHT 32 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library.
// On an arduino UNO: A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO: 2(SDA), 3(SCL), ...
//#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
//Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// 'atd', 128x32px
const unsigned char myBitmap [] PROGMEM = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xe0, 0x3f, 0xfc, 0x00, 0x00, 0x7f, 0xf0, 0x1c, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0x80, 0x0f, 0xf8, 0x00, 0x00, 0x3f, 0xc0, 0x04, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0x00, 0x03, 0xf8, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xfc, 0x00, 0x01, 0xf8, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xf9, 0xcf, 0xff, 0xff,
0xfc, 0x00, 0x00, 0xfc, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x1f, 0xff, 0xff, 0x39, 0xc0, 0xff, 0xff,
0xf8, 0x00, 0x00, 0xfe, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x1f, 0xff, 0xff, 0x31, 0xc0, 0x1f, 0xff,
0xf0, 0x0f, 0xc0, 0x7f, 0xf8, 0x3f, 0xf8, 0x0f, 0xc0, 0x1f, 0xff, 0xfe, 0x71, 0xc0, 0x0f, 0xff,
0xf0, 0x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 0xf8, 0x3f, 0xfc, 0x00, 0x00, 0x7f, 0x81, 0xff, 0x80, 0x00, 0x03, 0xff,
0xe0, 0x7f, 0xf8, 0x3f, 0xf8, 0x3f, 0xfe, 0x00, 0x00, 0xff, 0x81, 0xff, 0xc0, 0x00, 0x0f, 0xff,
0xe0, 0x7f, 0xf8, 0x3f, 0xf8, 0x3f, 0xff, 0x00, 0x01, 0xff, 0x81, 0xff, 0xf8, 0x00, 0x7f, 0xff,
0xe0, 0xff, 0xf8, 0x3f, 0xf8, 0x3f, 0xff, 0x80, 0x07, 0xff, 0x81, 0xff, 0xff, 0xdf, 0xff, 0xff,
0xf0, 0xff, 0xfc, 0x7f, 0xfc, 0x7f, 0xff, 0xe0, 0x1f, 0xff, 0x81, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};
//
// FiWare
//
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
const char* ssid = "<ssid>";
const char* password = "<passwd>";
// FiWare Config UL IoTAgent
const String host = "<Ultralight Fiware Server>";
const String port = "<por>";
const String apik = "<group token>";
const String deid = "<device_id>";
// FiWare POST URL
String urlpost = "http://"+host+":"+port+"/iot/d?k="+apik+"&i="+deid+"&getCmd=0";
// Peticiones WEB
HTTPClient http;
//
// Declaración pinout Serial Comunication
//
#define RX_PIN 14 // Rx pin which the MHZ19 Tx pin is attached to
#define TX_PIN 12 // Tx pin which the MHZ19 Rx pin is attached to
#define BAUDRATE 9600 // Device to MH-Z14a Serial baudrate (should not be changed)
// Sensor MHZ14A + Comando
SoftwareSerial mySerial(RX_PIN, TX_PIN);
const unsigned long MHZ14A_PREHEATING_TIME = 3L * 60L * 1000L;
const byte leerCO2[] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79};
// Variables
unsigned long getDataTimer = 0;
unsigned long DataTimer = 2 * 1000;
unsigned long getDataPost = 0;
unsigned long DataPost = 300 * 1000;
int val = 0;
int CO2 = 0;
void setup()
{
Serial.begin(9600); // Device to serial monitor feedback
mySerial.begin(BAUDRATE);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
//if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
// Serial.println(F("SSD1306 allocation failed"));
// for(;;); // Don't proceed, loop forever
//}
// Show initial display buffer contents on the screen --
// the library initializes this with an Adafruit splash screen.
// display.display();
//testdrawbitmap();
//delay(2000); // Pause for 2 seconds
// Enviar a pantalla
// display.display();
if (isPreHeating()) {
Serial.print("Preheating");
while (isPreHeating()) {
Serial.print(".");
delay(5000);
}
Serial.println();
}
// Conectar WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
delay(500);
}
void loop()
{
if (millis() - getDataTimer >= DataTimer)
{
// Read Sensor Buffer
byte bufferCO2[9];
mySerial.write(leerCO2, 9);
mySerial.readBytes(bufferCO2, 9);
// Get/convert values from buffer
int CO2 = bufferCO2[2] * 256 + bufferCO2[3];
float Temp_1 = bufferCO2[4] - 44;
// Callibrate con UrsaLink
//Temp_1 = Temp_1 + 4;
//CO2 = CO2 + 205;
#ifdef _DEBUG_
Serial.print("PPMuart: ");
if (CO2 > 0) {
Serial.print(CO2);
} else {
Serial.print("n/a");
}
#endif
#ifdef _DEBUG_
Serial.print(", Temperature: ");
if (Temp_1 > 0) {
Serial.println(Temp_1);
} else {
Serial.println("n/a");
}
#endif
// Display
//display.clearDisplay();
//display.drawRoundRect(0,0,128,32,1,SSD1306_WHITE);
//display.setTextSize(2);
//display.setTextColor(SSD1306_WHITE);
//display.setCursor(20,3);
//display.print(CO2);
//display.println(" PPm");
//display.setTextSize(1);
//display.setCursor(67,22);
//display.print(Temp_1);
//display.print(" ");
//display.write(167);
//display.print("C");
// Draw Signal Blocks
//val = map(CO2, 400, 1500, 1, 5);
//for(int16_t i=1; i<=5; i+=1) {
// if (val <= i){
// display.drawRect(5*i,25-i,4,5+i,WHITE);
// } else {
// display.fillRect(5*i,25-i,4,5+i,WHITE);
// }
//}
// Enviar a pantalla
// display.display();
getDataTimer = millis();
// POST Fiware
if (millis() - getDataPost >= DataPost){
if(WiFi.status()== WL_CONNECTED){
http.begin(urlpost); //Specify request destination
http.addHeader("Content-Type", "text/plain"); //Specify content-type header
http.addHeader("Fiware-Service", "<fiware service>"); //Specify content-type header
http.addHeader("Fiware-ServicePath", "<fiware service-path>"); //Specify content-type header
int httpCode = http.POST("ppm|"+String(CO2)+"|temp|"+String(Temp_1)); //Send the request
String payload = http.getString();
http.end();
// DEBUG
#ifdef _DEBUG_
Serial.print("POST Fiware: ");
Serial.println(urlpost);
Serial.print("HTTP POST Code: ");
Serial.println(httpCode);
#endif
}
//
getDataPost = millis();
}
}
}
//void testdrawbitmap(void) {
// display.clearDisplay();
// display.drawBitmap(0,0,myBitmap, 128, 32, 1);
// display.display();
// delay(1000);
//}
//
// Preheating Sensor
//
boolean isPreHeating() {
return millis() < (MHZ14A_PREHEATING_TIME);
}