-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathm2x.c
315 lines (261 loc) · 10.6 KB
/
m2x.c
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/* =====================================================================
Copyright © 2016, Avnet (R)
www.avnet.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
either express or implied. See the License for the specific
language governing permissions and limitations under the License.
@file m2x.c
@version 1.0
@date Sept 2017
======================================================================== */
#include <stdint.h>
#include <string.h>
#include <nettle/nettle-stdint.h>
#include "iot_monitor.h"
#include "http.h"
#include "m2x.h"
#include "HTS221.hpp"
#include "mytimer.h"
#include "lis2dw12.h"
#include "mal.hpp"
size_t m2x_sensor_timer;
int hts221_iterations;
int lis2dw12_iterations;
int adc_iterations;
void sensor_hts221(int interval, int iterations);
void sensor_lis2dw12(int interval, int iterations);
void sensor_adc(int interval, int iterations);
void do_lis2dw_temp(int f, void *val);
void do_lis2dw2m2x(void);
M2XFUNC m2xfunctions[] = {
// name, rate, func(), *tmr
"HTS221", 0, sensor_hts221, NULL,
"LIS2DW12", 0, sensor_lis2dw12, NULL,
"ADC", 0, sensor_adc, NULL,
"ENDTAB", 0, NULL, NULL,
};
#define _MAX_M2XFUNCTIONS (sizeof(m2xfunctions)/sizeof(M2XFUNC))
const int _max_m2xfunctions = _MAX_M2XFUNCTIONS;
void tx2m2x_timer(size_t timer_id, void * user_data)
{
float adc_voltage;;
char str_adc_voltage[16];
const time_t t = time(0);
if( hts221_iterations ) {
if( dbg_flag & DBG_MYTIMER )
printf("\n-MYTIMER: Read HTS221 sensor and send data %d more times (%s)\n",
hts221_iterations, asctime(localtime(&t)));
do_hts2m2x();
--hts221_iterations;
if( !hts221_iterations )
sensor_hts221(0,0); //cancel the calls, we are done.
}
if( lis2dw12_iterations ) {
if( dbg_flag & DBG_MYTIMER )
printf("\n-MYTIMER: Read LSW12D2 sensor and send data %d more times (%s)\n",
lis2dw12_iterations, asctime(localtime(&t)));
do_lis2dw2m2x();
--lis2dw12_iterations;
if( !lis2dw12_iterations )
sensor_lis2dw12(0,0); //cancel the calls, we are done.
}
if( adc_iterations ) {
if( dbg_flag & DBG_MYTIMER )
printf("\n-MYTIMER: Read ADC sensor and send data %d more times (%s)\n",
adc_iterations, asctime(localtime(&t)));
do_adc2m2x();
--adc_iterations;
if( !adc_iterations )
sensor_adc(0,0); //cancel the calls, we are done.
}
}
void do_hts2m2x(void)
{
void doNewLine();
float adc_voltage;
char str_adc_voltage[16];
if( dbg_flag & DBG_M2X )
printf("-M2X: Tx HTS221 M2X data to:\n-DeviceID = [%s]\n-API Key=[%s]\n-Stream Name=[%s]\n",
device_id, api_key, temp_stream_name);
m2x_create_stream(device_id, api_key, temp_stream_name);
while( !hts221_getTemperature() ) /* make sure we have a good Temp reading */;
adc_voltage = hts221_readTemperature();
memset(str_adc_voltage, 0, sizeof(str_adc_voltage));
sprintf(str_adc_voltage, "%f", adc_voltage);
m2x_update_stream_value(device_id, api_key, temp_stream_name, str_adc_voltage);
if( dbg_flag & DBG_M2X )
printf("-M2X: Tx HTS221 M2X data to:\n-DeviceID = [%s]\n-API Key=[%s]\n-Stream Name=[%s]\n",
device_id, api_key, "humid");
m2x_create_stream(device_id, api_key, "humid");
while( !hts221_getHumidity() ) /* make sure we have a good Humidity reading */;
adc_voltage = hts221_readHumidity();
memset(str_adc_voltage, 0, sizeof(str_adc_voltage));
sprintf(str_adc_voltage, "%f", adc_voltage);
m2x_update_stream_value(device_id, api_key, "humid", str_adc_voltage);
printf("HTS221 Data sent.\n");
doNewLine();
}
void do_adc2m2x(void)
{
adc_handle_t my_adc=(adc_handle_t)NULL;
float adc_voltage;
char str_adc_voltage[16];
if( dbg_flag & DBG_M2X )
printf("-M2x: Tx (adc) M2X data to:\n-DeviceID = [%s]\n-API Key=[%s]\n-Stream Name=[%s]\n",
device_id, api_key, adc_stream_name);
m2x_create_stream(device_id, api_key, adc_stream_name);
adc_init(&my_adc);
adc_read(my_adc, &adc_voltage);
memset(str_adc_voltage, 0, sizeof(str_adc_voltage));
sprintf(str_adc_voltage, "%f", adc_voltage);
m2x_update_stream_value(device_id, api_key, adc_stream_name, str_adc_voltage);
adc_deinit(&my_adc);
printf("ADC Data sent.\n");
doNewLine();
}
void do_lis2dw2m2x(void)
{
char str_val[16];
sprintf(str_val, "%f", lis2dw12_readTemp12());
if( dbg_flag & DBG_M2X )
printf("-M2x: Tx (lis2dw temp12)M2X data to:\n-DeviceID = [%s]\n-API Key=[%s]\n-Stream Name=[%s]\n",
device_id, api_key, "LIS2DW12_TEMP");
m2x_create_stream(device_id, api_key, "LIS2DW12_TEMP");
m2x_update_stream_value(device_id, api_key, "LIS2DW12_TEMP", str_val);
printf("LIS2DW12 Data sent.\n");
doNewLine();
}
void do_lis2dw_xyz(char **ptr)
{
char * Xstream_name = "LIS2DW12_x";
char * Ystream_name = "LIS2DW12_y";
char * Zstream_name = "LIS2DW12_z";
if( dbg_flag & DBG_M2X ) {
printf("-M2x: (xyz) Tx M2X data to:\n-DeviceID = [%s]\n-API Key=[%s]\n-Stream Name=[%s]\n",
device_id, api_key, Xstream_name);
printf("-M2x: (xyz) Tx M2X data to:\n-DeviceID = [%s]\n-API Key=[%s]\n-Stream Name=[%s]\n",
device_id, api_key, Ystream_name);
printf("-M2x: (xyz) Tx M2X data to:\n-DeviceID = [%s]\n-API Key=[%s]\n-Stream Name=[%s]\n",
device_id, api_key, Zstream_name);
}
m2x_create_stream(device_id, api_key, Xstream_name);
m2x_create_stream(device_id, api_key, Ystream_name);
m2x_create_stream(device_id, api_key, Zstream_name);
m2x_update_stream_value(device_id, api_key, Xstream_name, ptr[0]);
m2x_update_stream_value(device_id, api_key, Ystream_name, ptr[1]);
m2x_update_stream_value(device_id, api_key, Zstream_name, ptr[2]);
printf("\n");
doNewLine();
}
//
// These routines control starting/stopping the timer for multiple sensor readings
//
void sensor_adc(int interval, int iterations)
{
if( dbg_flag & DBG_HTS221 )
printf("-ADC: sending data %d times, every %d seconds.\n",iterations, interval);
if( m2x_sensor_timer != 0 ) { //currently have a timer running
if( interval ) { //just want to change the rate of samples
adc_iterations = iterations;
delete_IoTtimer(m2x_sensor_timer);
m2x_sensor_timer = create_IoTtimer(interval, tx2m2x_timer, TIMER_PERIODIC, NULL);
if( dbg_flag & DBG_HTS221 )
printf("-ADC: changed timer\n");
}
else { //want to kill the timer
adc_iterations = 0;
delete_IoTtimer(m2x_sensor_timer);
stop_IoTtimers();
m2x_sensor_timer = 0;
if( dbg_flag & DBG_HTS221 )
printf("-ADC: stopped timer\n");
}
}
else { //don't have a timer currently running, start one up
adc_iterations = iterations;
start_IoTtimers();
m2x_sensor_timer = create_IoTtimer(interval, tx2m2x_timer, TIMER_PERIODIC, NULL);
if( dbg_flag & DBG_HTS221 )
printf("-ADC: started timer\n");
start_data_service();
}
}
void sensor_hts221(int interval, int iterations)
{
if( dbg_flag & DBG_HTS221 )
printf("-HTS221: sending data %d times, every %d seconds.\n",iterations, interval);
if( m2x_sensor_timer != 0 ) { //currently have a timer running
if( interval ) { //just want to change the rate of samples
hts221_iterations = iterations;
delete_IoTtimer(m2x_sensor_timer);
m2x_sensor_timer = create_IoTtimer(interval, tx2m2x_timer, TIMER_PERIODIC, NULL);
if( dbg_flag & DBG_HTS221 )
printf("-HTS221: changed timer\n");
}
else { //want to kill the timer
hts221_iterations = 0;
delete_IoTtimer(m2x_sensor_timer);
stop_IoTtimers();
m2x_sensor_timer = 0;
if( dbg_flag & DBG_HTS221 )
printf("-HTS221: stopped timer\n");
}
}
else { //don't have a timer currently running, start one up
hts221_iterations = iterations;
start_IoTtimers();
m2x_sensor_timer = create_IoTtimer(interval, tx2m2x_timer, TIMER_PERIODIC, NULL);
if( dbg_flag & DBG_HTS221 )
printf("-HTS221: started timer\n");
start_data_service();
}
}
void sensor_lis2dw12(int interval, int iterations)
{
if( dbg_flag & DBG_LIS2DW12 )
printf("-LIS2DW12: sending ADC data %d times, every %d seconds.\n",iterations, interval);
if( m2x_sensor_timer != 0 ) { //currently have a timer running
if( interval ) { //just want to change the rate of samples
lis2dw12_iterations = iterations;
delete_IoTtimer(m2x_sensor_timer);
m2x_sensor_timer = create_IoTtimer(interval, tx2m2x_timer, TIMER_PERIODIC, NULL);
if( dbg_flag & DBG_LIS2DW12 )
printf("-LIS2DW12: changed timer\n");
}
else { //want to kill the timer
lis2dw12_iterations = 0;
delete_IoTtimer(m2x_sensor_timer);
stop_IoTtimers();
m2x_sensor_timer = 0;
if( dbg_flag & DBG_LIS2DW12 )
printf("-LIS2DW12: stopped timer\n");
}
}
else { //don't have a timer currently running, start one up
lis2dw12_iterations = iterations;
start_IoTtimers();
m2x_sensor_timer = create_IoTtimer(interval, tx2m2x_timer, TIMER_PERIODIC, NULL);
if( dbg_flag & DBG_LIS2DW12 )
printf("-LIS2DW12: started timer\n");
}
}
//
// curl -i -X PUT http://api-m2x.att.com/v2/devices/2ac9dc89132469eb809bea6e3a95d675/streaX-KEY: 6cd9c60f4a4e5d91d0ec4cc79536c661"
// -H "Content-Type: application/json" -d "{ \"value\": \"RED\" }"
//
void set_m2xColor(char *color)
{
if( dbg_flag & DBG_M2X )
printf("-M2x: Tx M2X (color) data to:\n-DeviceID = [%s]\n-API Key=[%s]\n-Stream Name=[%s]\n",
device_id, api_key, "rgb");
m2x_create_stream(device_id, api_key, "rgb");
m2x_update_color_value (device_id, api_key, "rgb", color);
printf("\n");
doNewLine();
}