-
Notifications
You must be signed in to change notification settings - Fork 27
/
Dev_THSensor.cpp
213 lines (195 loc) · 5.16 KB
/
Dev_THSensor.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* @file Dev_THSensor.cpp
*
* THSensor API of IoTgo (iotgo.iteadstudio.com)
*
* @author Wu Pengfei (email:<[email protected]>)
* @date 2014/11/17
* @copyright
* Copyright (C) 2013-2014 ITEAD Intelligent Systems Co., Ltd. \n
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*/
#include "Dev_THSensor.h"
/**
* Constructor.
*
* @param net - the pointer to object of subclass of NetInterface.
* @param sensor - the pointer to an object of classes which implement
* THSensorInterface.
*/
THSensor::THSensor(NetInterface *net, THSensorInterface *sensor)
:IoTgo(net)
{
if (sensor)
{
this->sensor = sensor;
}
else
{
DebugSerial.println("ERROR: Parameter sensor is NULL!");
}
}
/**
* Initialize Temperature & Humidity Sensor.
*
* @retval 0 - success.
* @retval ERR_INIT_DEVICE_FAILED - if initialization failed!
*/
int32_t THSensor::begin(void)
{
return sensor->begin();
}
/**
* Close Temperature & Humidity Sensor.
*
* @retval 0 - success.
* @retval ERR_CLOSE_DEVICE_FAILED - if device cannot be closed.
*/
int32_t THSensor::end(void)
{
return sensor->end();
}
/**
* Read temperature and humidity from device then push them to server.
*
* @retval 0 - success
* @retval ERR_NO_RESPONSE - if no response from server.
* @retval ERR_NO_EXPECT - if no content expected in response.
* @retval ERR_NO_DEVICES_AVAILABLE - if no devices available.
*/
int32_t THSensor::sync(void)
{
char str_temp_c[8] = {'\0'};
char str_hum[8] = {'\0'};
char str_tmp[8] = {'\0'};
const char *params[] = {"temperature", "humidity", NULL};
const char *values[] = {str_temp_c, str_hum, NULL};
const char *response;
float temp_c;
float temp_f;
float hum;
int32_t ret;
char *str_error;
int32_t len;
ret = getAll(&temp_c, &temp_f, &hum);
if (ret)
{
#ifdef DEBUG
DebugSerial.print("getAll() =");
DebugSerial.println(ret);
#endif
return ERR_NO_DEVICES_AVAILABLE;
}
DebugSerial.print("temp_c=");
DebugSerial.print(temp_c, 2);
DebugSerial.print(", hum=");
DebugSerial.println(hum, 2);
temp_c += 0.005;
hum += 0.005;
snprintf(str_tmp, sizeof(str_tmp), "%d", (int)(temp_c*100));
len = strlen(str_tmp);
strncpy(str_temp_c, str_tmp, len-2);
strcat(str_temp_c, ".");
strcat(str_temp_c, &str_tmp[len-2]);
snprintf(str_tmp, sizeof(str_tmp), "%d", (int)(hum*100));
len = strlen(str_tmp);
strncpy(str_hum, str_tmp, len-2);
strcat(str_hum, ".");
strcat(str_hum, &str_tmp[len-2]);
#ifdef DEBUG
DebugSerial.print("str_temp_c=");
DebugSerial.println(str_temp_c);
DebugSerial.print("str_hum=");
DebugSerial.println(str_hum);
#endif
response = update(params, values);
if(response)
{
str_error = strstr(response, "\"error\":0");
if (str_error)
{
return 0;
}
else
{
return ERR_NO_EXPECT;
}
}
else
{
return ERR_NO_RESPONSE;
}
}
/**
* Read temperature from device by Celsius.
*
* @param temp_c - the pointer to store temperature by Celsius.
*
* @retval 0 - success.
* @retval ERR_INVALID_PARAMETER - if temp_c is NULL.
* @retval ERR_READ_DEVICE_FAILED - if device cannot be read.
*/
int32_t THSensor::getTemperatureC(float *temp_c)
{
return temp_c
?
sensor->getData(temp_c, NULL, NULL)
:
ERR_INVALID_PARAMETER;
}
/**
* Read temperature from device by Fahrenheit.
*
* @param temp_f - the pointer to store temperature by Fahrenheit.
*
* @retval 0 - success.
* @retval ERR_INVALID_PARAMETER - if temp_c is NULL.
* @retval ERR_READ_DEVICE_FAILED - if device cannot be read.
*/
int32_t THSensor::getTemperatureF(float *temp_f)
{
return temp_f
?
sensor->getData(NULL, temp_f, NULL)
:
ERR_INVALID_PARAMETER;
}
/**
* Read humidity from device.
*
* @param hum - the pointer to store humidity.
*
* @retval 0 - success
* @retval ERR_INVALID_PARAMETER - if hum is NULL.
* @retval ERR_READ_DEVICE_FAILED - if device cannot be read.
*/
int32_t THSensor::getHumidity(float *hum)
{
return hum
?
sensor->getData(NULL, NULL, hum)
:
ERR_INVALID_PARAMETER;
}
/**
* Read temperature and humidity from device.
*
* @param temp_c - the pointer to store temperature by Celsius.
* @param temp_f - the pointer to store temperature by Fahrenheit.
* @param hum - the pointer to store humidity.
*
* @retval 0 - success
* @retval ERR_INVALID_PARAMETER - if any parameter is NULL.
* @retval ERR_READ_DEVICE_FAILED - if device cannot be read.
*/
int32_t THSensor::getAll(float *temp_c, float *temp_f, float *hum)
{
return temp_c && temp_f && hum
?
sensor->getData(temp_c, temp_f, hum)
:
ERR_INVALID_PARAMETER;
}