forked from jcheca/co2ATDFiware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BasicUsageCCS811.ino
132 lines (102 loc) · 4.36 KB
/
BasicUsageCCS811.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
#define _DEBUG_;
#include <Wire.h> // I2C library
#include "ccs811.h" // CCS811 library
// 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
//
// FiWare
//
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
const char* ssid = "SSID";
const char* password = "password";
// FiWare Config UL IoTAgent
const String host = "Stack FiWare Server UL";
const String port = "Stack FiWare Port UL";
const String apik = "Api Key FiWare Device Group";
const String deid = "Provision Device.id";
// FiWare POST URL
String urlpost = "http://"+host+":"+port+"/iot/d?k="+apik+"&i="+deid+"&getCmd=0";
// Peticiones WEB
HTTPClient http;
unsigned long getDataTimer = 0;
unsigned long DataTimer = 2 * 1000;
unsigned long getDataPost = 0;
unsigned long DataPost = 300 * 1000;
void setup()
{
Serial.begin(115200); // Device to serial monitor feedback
#ifdef _DEBUG_
Serial.println("");
Serial.println("setup: Starting CCS811 basic demo");
Serial.print("setup: ccs811 lib version: ");
Serial.println(CCS811_VERSION);
#endif
// Enable I2C
Wire.begin();
// Enable CCS811
ccs811.set_i2cdelay(50); // Needed for ESP8266 because it doesn't handle I2C clock stretch correctly
bool ok= ccs811.begin();
if( !ok ) Serial.println("setup: CCS811 begin FAILED");
// Print CCS811 versions
#ifdef _DEBUG_
Serial.print("setup: hardware version: "); Serial.println(ccs811.hardware_version(),HEX);
Serial.print("setup: bootloader version: "); Serial.println(ccs811.bootloader_version(),HEX);
Serial.print("setup: application version: "); Serial.println(ccs811.application_version(),HEX);
#endif
// Start measuring
ok= ccs811.start(CCS811_MODE_1SEC);
if( !ok ) Serial.println("setup: CCS811 start FAILED");
// Conectar WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
delay(500);
}
void loop()
{
if (millis() - getDataTimer >= DataTimer)
{
// Read
uint16_t eco2, etvoc, errstat, raw;
ccs811.read(&eco2,&etvoc,&errstat,&raw);
// Print measurement results based on status
if( errstat==CCS811_ERRSTAT_OK ) {
Serial.print("CCS811: ");
Serial.print("eco2="); Serial.print(eco2); Serial.print(" ppm ");
Serial.print("etvoc="); Serial.print(etvoc); Serial.print(" ppb ");
Serial.print("mA="); Serial.print(raw/1024); Serial.print(" uuA ");
Serial.print("adc="); Serial.print(raw%1024); Serial.print(" ADC ");
Serial.print("ohm="); Serial.print((1650*1000L/1023)*(raw%1024)/(raw/1024)); Serial.print(" ohm");
Serial.println();
} else if( errstat==CCS811_ERRSTAT_OK_NODATA ) {
Serial.println("CCS811: waiting for (new) data");
} else if( errstat & CCS811_ERRSTAT_I2CFAIL ) {
Serial.println("CCS811: I2C error");
} else {
Serial.print("CCS811: errstat="); Serial.print(errstat,HEX);
Serial.print("="); Serial.println( ccs811.errstat_str(errstat) );
}
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("eco2|"+String(eco2)+"|etvoc|"+String(etvoc)+"|mcramp|"+String(raw/1024)+"|adc|"+String(raw%1024)+"|ohm|"+String((1650*1000L/1023)*(raw%1024)/(raw/1024))); //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();
}
}
}