-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensorhub.c
361 lines (279 loc) · 9.85 KB
/
sensorhub.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
"THE BEER/MATE-WARE LICENSE":
<xil@xil.se> wrote this file. As long as you retain this notice you
can do whatever you want with this stuff. If we meet some day, and you think
this stuff is worth it, you can buy us a ( > 0 ) beer/mate in return - The Xil TEAM
*/
#include "osapi.h"
#include "os_type.h"
#include "ets_sys.h"
#include "user_interface.h"
#include "espconn.h"
#include "gpio.h"
#include "network_config.h"
#include "drivers/drivers.h"
#define SENSORHUB_VERSION 4
#define SENSOR_TASK_PRIO 2
#define SENSOR_TASK_QUEUE_SIZE 1
static const uint8_t RECEIVER[4] = RECEIVER_IP;
#define POLL_TIME 60000 // ms
#define MAX_RESPONSE_SIZE 256 // bytes
#define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
#define NETWORK_MODE_POLLING
//#define NETWORK_MODE_PUSHING
static struct {
struct espconn conn_tcp;
struct espconn conn_udp;
esp_tcp tcp;
esp_udp udp;
} network;
// Data bus configuration
static driver_bus driver_buses[] = {
{ .bus = &bus_i2c, .params.i2c.gpio_sda = 13, .params.i2c.gpio_scl = 12, },
{ .bus = &bus_i2c, .params.i2c.gpio_sda = 5, .params.i2c.gpio_scl = 4, },
};
// Sensor driver configuration
static driver driver_sensors[] = {
{ .bus = &driver_buses[0], .sensor = &sensor_bmp180,
.params.bmp180.pressure_sampling_accuracy = bmp180_pressure_sampling_accuracy_high_resolution, },
{ .bus = &driver_buses[1], .sensor = &sensor_bmp280,
.params.bmp280.sdo_high = false,
.params.bmp280.temp_oversampling = 1,
.params.bmp280.pressure_oversampling = 2, },
};
static struct {
int cnt;
os_event_t queue[SENSOR_TASK_QUEUE_SIZE];
volatile os_timer_t timer;
} sensor;
static void ICACHE_FLASH_ATTR write_value(char* string, void* value, int size)
{
uint8_t* v = (uint8_t*)value;
int i = 0;
while (i < size) {
switch (v[size - i - 1] & 0xf0) {
case 0x00: string[(i<<1) + 0] = '0'; break;
case 0x10: string[(i<<1) + 0] = '1'; break;
case 0x20: string[(i<<1) + 0] = '2'; break;
case 0x30: string[(i<<1) + 0] = '3'; break;
case 0x40: string[(i<<1) + 0] = '4'; break;
case 0x50: string[(i<<1) + 0] = '5'; break;
case 0x60: string[(i<<1) + 0] = '6'; break;
case 0x70: string[(i<<1) + 0] = '7'; break;
case 0x80: string[(i<<1) + 0] = '8'; break;
case 0x90: string[(i<<1) + 0] = '9'; break;
case 0xA0: string[(i<<1) + 0] = 'A'; break;
case 0xB0: string[(i<<1) + 0] = 'B'; break;
case 0xC0: string[(i<<1) + 0] = 'C'; break;
case 0xD0: string[(i<<1) + 0] = 'D'; break;
case 0xE0: string[(i<<1) + 0] = 'E'; break;
case 0xF0: string[(i<<1) + 0] = 'F'; break;
default: string[(i<<1) + 0] = ' '; break;
}
switch (v[size - i - 1] & 0x0f) {
case 0x00: string[(i<<1) + 1] = '0'; break;
case 0x01: string[(i<<1) + 1] = '1'; break;
case 0x02: string[(i<<1) + 1] = '2'; break;
case 0x03: string[(i<<1) + 1] = '3'; break;
case 0x04: string[(i<<1) + 1] = '4'; break;
case 0x05: string[(i<<1) + 1] = '5'; break;
case 0x06: string[(i<<1) + 1] = '6'; break;
case 0x07: string[(i<<1) + 1] = '7'; break;
case 0x08: string[(i<<1) + 1] = '8'; break;
case 0x09: string[(i<<1) + 1] = '9'; break;
case 0x0A: string[(i<<1) + 1] = 'A'; break;
case 0x0B: string[(i<<1) + 1] = 'B'; break;
case 0x0C: string[(i<<1) + 1] = 'C'; break;
case 0x0D: string[(i<<1) + 1] = 'D'; break;
case 0x0E: string[(i<<1) + 1] = 'E'; break;
case 0x0F: string[(i<<1) + 1] = 'F'; break;
default: string[(i<<1) + 1] = ' '; break;
}
i++;
}
}
static bool ICACHE_FLASH_ATTR add_value(char* data, uint16_t* index, int i, const char* string, void* value, int size)
{
int length;
length = os_strlen(string);
memcpy(&data[*index], string, length);
(*index) += length;
data[*index] = '.';
(*index)++;
length = sizeof(uint8_t);
write_value(&data[*index], &i, length);
(*index) += length*2;
data[*index] = ':';
(*index)++;
data[*index] = ' ';
(*index)++;
length = size;
write_value(&data[*index], value, length);
(*index) += length*2;
data[*index] = '\n';
(*index)++;
return true;
}
static bool ICACHE_FLASH_ATTR add_string(char* data, uint16_t* index, int i, const char* string, const char* value)
{
int length;
length = os_strlen(string);
memcpy(&data[*index], string, length);
(*index) += length;
data[*index] = '.';
(*index)++;
length = sizeof(uint8_t);
write_value(&data[*index], &i, length);
(*index) += length*2;
data[*index] = ':';
(*index)++;
data[*index] = ' ';
(*index)++;
length = os_strlen(value);
memcpy(&data[*index], value, length);
(*index) += length;
data[*index] = '\n';
(*index)++;
return true;
}
static void ICACHE_FLASH_ATTR build_result(char* data, int* length)
{
uint16_t index;
uint8_t i;
uint16_t version;
driver_print_data print_data;
print_data.data = data;
print_data.index = &index;
print_data.print_value = add_value;
print_data.print_string = add_string;
index = 0;
version = SENSORHUB_VERSION;
add_value(data, &index, 0, "version", &version, sizeof(version));
for (i = 0; i < ARRAY_SIZE(driver_buses); i++) {
print_data.i = i;
driver_buses[i].bus->print(&driver_buses[i].params, &print_data);
}
for (i = 0; i < ARRAY_SIZE(driver_sensors); i++) {
print_data.i = i;
driver_sensors[i].sensor->print(&driver_sensors[i].params, &print_data);
}
if (index > *length)
{
index = 0;
add_value(data, &index, 0, "ERROR: Buffer too small", length, sizeof(*length));
}
*length = index;
}
void ICACHE_FLASH_ATTR send_data(void)
{
int length;
static char data[MAX_RESPONSE_SIZE] = "";
length = sizeof(data);
build_result(data, &length);
// Set ip
os_memcpy(network.conn_udp.proto.udp->remote_ip, RECEIVER, sizeof(network.conn_udp.proto.udp->remote_ip));
// Set port
network.conn_udp.proto.udp->remote_port = RECEIVER_PORT;
espconn_sendto(&network.conn_udp, data, length);
}
void ICACHE_FLASH_ATTR init_udp(void)
{
network.conn_udp.proto.udp = &network.udp;
// TCP connection
network.conn_udp.type = ESPCONN_UDP;
// No state
network.conn_udp.state = ESPCONN_NONE;
// Set ip
os_memcpy(network.conn_udp.proto.udp->remote_ip, RECEIVER, sizeof(network.conn_udp.proto.udp->remote_ip));
// Set port
network.conn_udp.proto.udp->remote_port = RECEIVER_PORT;
network.conn_udp.proto.udp->local_port = espconn_port();
espconn_create(&network.conn_udp);
}
static void ICACHE_FLASH_ATTR tcp_connect_cb(void* arg)
{
struct espconn* conn;
int length;
static char data[MAX_RESPONSE_SIZE] = "";
length = sizeof(data);
build_result(data, &length);
conn = (struct espconn*)arg;
espconn_send(conn, data, length);
espconn_disconnect(conn);
}
static void ICACHE_FLASH_ATTR start_network_services(void)
{
sint8 rc;
network.conn_tcp.proto.tcp = &network.tcp;
// TCP connection
network.conn_tcp.type = ESPCONN_TCP;
// No state
network.conn_tcp.state = ESPCONN_NONE;
// Set port
network.conn_tcp.proto.tcp->local_port = LISTENING_PORT;
// Register callbacks
espconn_regist_connectcb(&network.conn_tcp, tcp_connect_cb);
// espconn_regist_reconcb(&network.conn_tcp, tcp_reconnect_cb);
// espconn_regist_disconcb(&network.conn_tcp, tcp_disconnect_cb);
rc = espconn_accept(&network.conn_tcp);
if (rc != ESPCONN_OK) {
// handle error
os_printf("Error opening listening socket: %d\n", rc);
}
}
static void ICACHE_FLASH_ATTR set_wifi_config(void)
{
struct station_config config;
os_memset(&config, 0, sizeof(config));
// Connect to any AP
config.bssid_set = 0;
os_memcpy(&config.ssid, WIFI_SSID, os_strlen(WIFI_SSID));
os_memcpy(&config.password, WIFI_PSK, os_strlen(WIFI_PSK));
wifi_station_set_config(&config);
}
static void ICACHE_FLASH_ATTR sensor_timer(void *arg)
{
system_os_post(SENSOR_TASK_PRIO, 0, (os_param_t)NULL);
}
static void ICACHE_FLASH_ATTR sensor_task(os_event_t* event)
{
int i;
sensor.cnt++;
// Read all sensors
for (i = 0; i < ARRAY_SIZE(driver_sensors); i++) {
driver_sensors[i].sensor->read(&driver_sensors[i].params);
}
#ifdef NETWORK_MODE_PUSHING
send_data();
#endif // NETWORK_MODE_PUSHING
}
void ICACHE_FLASH_ATTR user_init(void)
{
int i;
os_memset(&sensor, 0, sizeof(sensor));
wifi_set_opmode(STATION_MODE);
set_wifi_config();
#ifdef NETWORK_MODE_POLLING
start_network_services();
#endif // NETWORK_MODE_POLLING
#ifdef NETWORK_MODE_PUSHING
init_udp();
#endif // NETWORK_MODE_PUSHING
// configure busses
for (i = 0; i < ARRAY_SIZE(driver_buses); i++) {
driver_buses[i].bus->init(&driver_buses[i].params);
}
// initialize sensors
for (i = 0; i < ARRAY_SIZE(driver_sensors); i++) {
driver_sensors[i].sensor->init(&driver_sensors[i].params, driver_sensors[i].bus);
}
// task for sensors
system_os_task(sensor_task, SENSOR_TASK_PRIO, sensor.queue, SENSOR_TASK_QUEUE_SIZE);
system_os_post(SENSOR_TASK_PRIO, 0, (os_param_t)NULL);
// timer for sensors
os_timer_setfn(&sensor.timer, (os_timer_func_t *)sensor_timer, NULL);
os_timer_arm(&sensor.timer, POLL_TIME, 1);
system_set_os_print(1);
system_print_meminfo();
system_set_os_print(0);
}