-
Notifications
You must be signed in to change notification settings - Fork 0
/
edison-thingspeak.cpp
198 lines (159 loc) · 5.31 KB
/
edison-thingspeak.cpp
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
#include <aio.hpp>
#include <bmpx8x.h>
#include <common.hpp>
#include <curl/curl.h>
#include <curl/easy.h>
#include <lm35.h>
#include <string.h>
#include <types.hpp>
#include <unistd.h>
#include <cstdio>
#include <iostream>
/*
* Description: An Intel Edison project, where we, simply, post data from various sensors to a ThingsSpeak channel every 15 seconds.
*
*
* Hardware: 1 x Gravity - Analog LM35 Temperature Sensor
* 1 x Gravity - Analog Ambient Light Sensor
* 1 x HIH4030 Humidity Sensor
* 1 x BMP180 Barometer Module
* Resting on a Gravity - GPIO Shield
*
* Additional linker flags: upm-bmpx8x upm-lm35 curl
*/
#define THINGSPEAK_HOST "https://api.thingspeak.com"
#define API_KEY "YOUR_API_KEY"
#define TIMEOUT_IN_SECS 15 // Delay between requests
enum SensorPinMap {
BUS_BMP180,
PIN_PT550,
PIN_LM35,
PIN_HIH4030
};
// ------------ Global variables -----------------
// I2C interface for the BMP180 barometer module.
upm::BMPX8X *i2c_BMP180;
// Analog pin for the HIH4030 humidity sensor.
mraa::Aio *aio_HIH4030;
// Analog pin for the PT550 light sensor.
mraa::Aio *aio_PT550;
// Analog pin for the LM35 temperature sensor.
upm::LM35 *aio_LM35;
// Not really necessary since there is no exit mechanism, but there may be in the future.
bool APP_RUNNING = true;
// Voltage value for calculating relative humidity.
float supply_voltage = 5;
// ------------ Global functions -----------------
float getTemperature();
int getHumidity(int temp);
int getLightLevel();
int getPressure();
int main()
{
// ---------------------- Platform Verification --------------------------
mraa::Platform platform = mraa::getPlatformType();
if (platform != mraa::INTEL_EDISON_FAB_C) {
std::cerr << "Unsupported platform, exiting" << std::endl;
return mraa::ERROR_INVALID_PLATFORM;
}
// ---------------------- Sensor Initialization --------------------------
aio_HIH4030 = new mraa::Aio(PIN_HIH4030);
if (aio_HIH4030 == NULL) {
std::cerr << "HIH4030 initialization failed, exiting" << std::endl;
return mraa::ERROR_UNSPECIFIED;
}
aio_PT550 = new mraa::Aio(PIN_PT550);
if (aio_PT550 == NULL) {
std::cerr << "PT550 initialization failed, exiting" << std::endl;
return mraa::ERROR_UNSPECIFIED;
}
aio_LM35 = new upm::LM35(PIN_LM35);
if (aio_LM35 == NULL) {
std::cerr << "LM35 initialization failed, exiting" << std::endl;
return mraa::ERROR_UNSPECIFIED;
}
i2c_BMP180 = new upm::BMPX8X(BUS_BMP180);
if (i2c_BMP180 == NULL) {
std::cerr << "BMP180 initialization failed, exiting" << std::endl;
return mraa::ERROR_UNSPECIFIED;
}
// ------------------------- Curl Initialization ---------------------------
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if (res != CURLE_OK) {
std::cerr << "Curl global initialization failed, exiting" << std::endl;
return res;
}
// Initialize a curl object.
CURL *curl = curl_easy_init();
// If the curl initialization was successful, we can start our endless loop!
if (curl) {
// Don't verify SSL certificates. We could probably set some HTTP headers here for a more sophisticated request.
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
char url[128];
int humidity, pressure, light_level;
float temperature;
while (APP_RUNNING) {
temperature = getTemperature();
humidity = getHumidity(temperature);
pressure = getPressure();
light_level = getLightLevel();
sprintf(url, "%s/update?api_key=%s&field1=%i&field2=%i&field3=%i&field4=%.2f", THINGSPEAK_HOST, API_KEY,
humidity, pressure, light_level, temperature);
// Set the URL for the curl object.
curl_easy_setopt(curl, CURLOPT_URL, url);
// Perform the request.
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
}
// Clear bytes
memset(url, 0, sizeof(url));
sleep(TIMEOUT_IN_SECS);
}
curl_easy_cleanup(curl);
} else {
fprintf(stderr, "curl_easy_init() failed\n");
}
curl_global_cleanup();
delete aio_LM35;
delete aio_HIH4030;
delete aio_PT550;
delete i2c_BMP180;
return mraa::SUCCESS;
}
float getTemperature()
{
float raw_value = aio_LM35->getTemperature();
float calibrated_value = raw_value * 5.0/10.24;
int rounded_value = round(calibrated_value);
std::cout << "\nTemperature: " << rounded_value << std::endl;
return rounded_value;
}
int getHumidity(int temp)
{
int current_value = aio_HIH4030->read();
std::cout << "Read value: " << current_value << std::endl;
float voltage = supply_voltage*(0.0062*current_value + 0.16); // convert to voltage value
std::cout << "Voltage: " << voltage << "V" << std::endl;
// The following calculations derive from the HIH-4030 datasheet.
float sensorRH = (voltage - 0.958)/0.0307;
std::cout << "sensorRH: " << sensorRH << "%" << std::endl;
float trueRH = sensorRH / (1.0546 - (0.00216 * temp)); //temperature adjustment
// Trim to two digits.
trueRH = round(trueRH * 100.) / 100.;
std::cout << "trueRH: " << trueRH << "%"<< std::endl;
return trueRH;
}
int getPressure()
{
int current_value = i2c_BMP180->getPressure();
std::cout << "Pressure: " << current_value << std::endl;
return current_value;
}
int getLightLevel()
{
int current_value = aio_PT550->read();
std::cout << "Light voltage signal: " << current_value << std::endl;
return current_value;
}