-
Notifications
You must be signed in to change notification settings - Fork 1
/
ATDFiWareSonda.ino
295 lines (245 loc) · 11.3 KB
/
ATDFiWareSonda.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#define _DEBUG_;
#include "MHZ19.h"
#include <SoftwareSerial.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <WiFiManager.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
// Portal Cautivo
WiFiManager wm;
//WiFiManagerParameter custom_sonda_id("sonda", "Sonda ID", "", 40);
#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, 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xe0, 0x3f, 0xff, 0xfe, 0x07, 0xff, 0xe0, 0xff,
0xe0, 0x0f, 0xf8, 0x3f, 0xf8, 0x3f, 0xf8, 0x07, 0xc0, 0x7f, 0x81, 0xff, 0x00, 0xff, 0x01, 0xff,
0xe0, 0x3f, 0xf8, 0x3f, 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 Config UL IoTAgent
const String host = "<FiWare Ultralight Server>";
const String port = "<Ultralight Server Port>";
const String apik = "<FiWare Group Tocken>";
const String deid = "<FiWare Device ID>";
// Preheating time
const unsigned long MHZ19_PREHEATING_TIME = 2L * 60L * 1000L;
// FiWare POST URL
String urlpost = "http://"+host+":"+port+"/iot/d?k="+apik+"&i="+deid+"&getCmd=0";
// Peticiones WEB
HTTPClient http;
//
// Conexion Pantalla
//
#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-Z19 Serial baudrate (should not be changed)
MHZ19 myMHZ19; // Constructor for library
SoftwareSerial mySerial(RX_PIN, TX_PIN); // (Uno example) create device to MH-Z19 serial
// Data Timer
unsigned long getDataTimer = 0;
unsigned long DataTimer = 2 * 1000;
// Data Post FiWare
unsigned long getDataPost = 0;
unsigned long DataPost = 300 * 1000;
int val = 0;
void setup() {
Serial.begin(9600);
// 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
}
WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
//reset settings - wipe credentials for testing
//wm.resetSettings();
wm.setAPCallback(configModeCallback);
//automatically connect using saved credentials if they exist
//If connection fails it starts an access point with the specified name
if(wm.autoConnect("ATDFiWareAP")){
#ifdef _DEBUG_
Serial.println("Connected...:)");
#endif
// Logo
testdrawbitmap();
delay(2000);
display.display();
// Sensores
mySerial.begin(BAUDRATE); // (Uno example) device to MH-Z19 serial start
myMHZ19.begin(mySerial); // *Serial(Stream) refence must be passed to library begin().
myMHZ19.autoCalibration(); // Turn auto calibration ON (OFF autoCalibration(false))
// PreHeating
if (isPreHeating()) {
#ifdef _DEBUG_
Serial.print("Preheating");
#endif
boolean color = false;
while (isPreHeating()) {
#ifdef _DEBUG_
Serial.print(".");
#endif
color = !color;
display.invertDisplay(color);
display.display();
delay(1000);
}
Serial.println();
}
//
// Set invertDisplay FALSE
//
display.invertDisplay(false);
}
else {
Serial.println("Configportal running");
}
}
void loop() {
// Process WiFiManager
wm.process();
// Read Data Sensor in DataTimer
if (millis() - getDataTimer >= DataTimer)
{
int CO2;
double Temp;
double Temp_1;
/* note: getCO2() default is command "CO2 Unlimited". This returns the correct CO2 reading even
if below background CO2 levels or above range (useful to validate sensor). You can use the
usual documented command with getCO2(false) */
CO2 = myMHZ19.getCO2(); // Request CO2 (as ppm)
Temp = myMHZ19.getTemperature(true,true); // Request Temperature (as Celsius)
Temp_1 = myMHZ19.getTemperature(true); // Request Temperature (as Celsius)
// Display Frame
display.clearDisplay();
display.drawRoundRect(0,0,128,32,1,SSD1306_WHITE);
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
// Display CO2 value + sufix
display.setCursor(20,3);
display.print(CO2);
display.println(" PPm");
// Display TEMP value + sufix
display.setTextSize(1);
display.setCursor(67,22);
display.print(Temp_1);
display.print(" ");
display.write(167);
display.print("C");
// Draw Signal Blocks (bottom left border)
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);
display.drawRect(5*i,25,4,5,WHITE);
} else {
// display.fillRect(5*i,25-i,4,5+i,WHITE);
display.fillRect(5*i,25,4,5,WHITE);
}
}
// ReDraw. Send to OLED
display.display();
#ifdef _DEBUG_
Serial.print("CO2 (ppm): ");
Serial.println(CO2);
Serial.print("Temperature (Cº): ");
Serial.println(Temp_1);
#endif
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-ServicePath>"); //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
// Obtener datos Meteorológicos
// obtenerDatosOpenWeather();
}
//
getDataPost = millis();
}
}
}
// Save extra config parameters
void saveParamsCallback () {
Serial.println("Get Params:");
//Serial.print(custom_sonda_id.getID());
Serial.print(" : ");
//Serial.println(custom_sonda_id.getValue());
}
// Draw Logo
void testdrawbitmap(void) {
display.clearDisplay();
display.drawBitmap(0,0,myBitmap, 128, 32, 1);
display.display();
delay(1000);
}
// Preheating Sensor
boolean isPreHeating() {
return millis() < (MHZ19_PREHEATING_TIME);
}
// Config Mode to Display
void configModeCallback (WiFiManager *wm) {
#ifdef _DEBUG_
Serial.println("Entered config mode.");
Serial.println(WiFi.softAPIP());
Serial.println(wm->getConfigPortalSSID());
#endif
// OLED Display Portal warning
display.clearDisplay();
display.setCursor(0,0);
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.println("Portal Running...");
display.println("");
display.print(wm->getConfigPortalSSID());
display.display();
}