main2.c
11.9 KB
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
#include <string.h>
#include "libwismart.h"
#include "libwismart_irqs.h" /* implement irq handlers */
#include "lwip/inet.h"
#include "globals.h"
#include "httpClient.h"
#include "callbacks.h"
#include "buffer.h"
#include "i2c.h"
#include "configServer.h"
#define WIFI_MODE WIFI_MODE_CLIENT
#define NETWORK_SSID_AP "modularsens"
#define NETWORK_KEY_AP NULL
#define NETWORK_CHANNEL_AP 1
#define HARD_LIMIT_WAIT_TIME 10*1000 //5*60*1000
#define NUMSENSORS 4
wismart_registryKey_t geo;
void initLibwismart(void)
{
wismart_hwif_t hwif = libwismart_GetDefaultHWIF();
libwismart_Init(hwif);
}
uint8_t connected=0;
uint8_t timeout=0;
uint8_t retries=0;
void init_registry(void)
{
config_params_t config;
strcpy(config.localization,"none");
libwismart_RegistryCreateKey(&geo, 1, sizeof(config));
libwismart_RegistryOpen(1);
if(!libwismart_RegistryIsValueEmpty(&geo))
{
libwismart_RegistryGet(&geo,&config);
}
printf("SSID = %s\r\n",config.ssid);
printf("Wep key = %s\r\n",config.wepkey);
printf("Passphrase = %s\r\n", config.passphrase);
printf("User = %s\r\n",config.user);
printf("Password = %s\r\n",config.password);
printf("Encryption type = %d\r\n",config.security);
printf("Geo Localization = %s\r\n", config.localization);
strcpy(mod.geoloc,config.localization);
if(config.security == PROFILE_SECURITY_OPEN)
{
printf("open detected\r\n");
libwismart_WiFiConnect(config.ssid,NULL,wifi_connect_result_cb);
}
else if(config.security == PROFILE_SECURITY_WPA_WPA2)
{
if(!strcmp(config.user,""))
{
printf("wpa detected\r\n");
libwismart_WiFiConnect(config.ssid,config.passphrase,wifi_connect_result_cb);
}
else
{
printf("wpa Enterprise detected\r\n");
struct wpa_param wpa;
wpa.eap_method = WISMART_EAP_METHOD_TTLS;
wpa.u.ttls.identity = config.user;
wpa.u.ttls.password = config.password;
wpa.u.ttls.ca_cert=NULL;
libwismart_WiFiConnectEnterprise(config.ssid, &wpa, wifi_connect_result_cb);
}
}
else
{
printf("wep detected\r\n");
//Is WEP
libwismart_WiFiConnect(config.ssid,config.wepkey,wifi_connect_result_cb);
}
while(connected == 0 && timeout != 1 )
{chThdSleepMilliseconds(500);}
}
void send_data()
{
int j;
for(j=0;j<TOTAL_SENSORS;j++)
{
printf(" enviant buffer %d\r\n",j);
// fem servir 085 de prova, però haurem de fer servir sensor_id[j]
int ok=send(buffers[j],&ind[j],&sizes[j], mod.ID, sensors[j]);
if(ok==JSON_COMM_ERROR)
{
printf("wismart is not connected\r\n");
}
else if( ok==JSON_OTHER_ERROR){
printf("some error ocurred\r\n");
}
else if(ok ==JSON_POST_OK){
printf(" send OK \r\n");
}
}
}
void put_buffers(char** buffers,uint32_t ind[],uint32_t sizes[],char* valueSensors[]){
int i;
for(i=0;i<TOTAL_SENSORS;i++){
put_message(valueSensors[i], buffers[i] ,&ind[i],&sizes[i]);
}
}
int main(void)
{
initLibwismart();
libwismart_PowerSave_Enable();
libwismart_PowerSave_HigherProfile(TRUE);
libwismart_RegisterDhcpCB(dhcp_connect_result_cb);
libwismart_WiFiInit();
libwismart_SetScanRunsForConnTimeout(4); //Edit a 4
libwismart_EnableBsdSocketAPI();
//Escanea los sensores -> retorna un vector con las direcciones en cada posición del vector, si la posición del vector retorna un cero -> no existe el sensor
uint8_t sensors[TOTAL_SENSORS];
char* valueSensors[TOTAL_SENSORS];
I2C_scan(sensors);
//unsigned int time = getNTPTime();
unsigned int time = getSecsSince1900();
unsigned int timestamp = 0;
unsigned int delay = getSystemTime();
unsigned int delay2 = 0;
sleep_thread(SHORT_PERIOD - time%SHORT_PERIOD);
uint32_t ind[4]={0};
char** buffers[4];
uint32_t sizes[4]={0};
init_registry();
if(timeout==1)
{
printf("Creating AP\r\n");
//corroborar los parametros del AP
configServer_start(1);
libwismart_WiFi_SoftAP_Start(NETWORK_SSID_AP,NETWORK_CHANNEL_AP,NULL,softapMode_apStartedCb, softapMode_clientIndicationCb);
for(;;)
{
chThdSleepMilliseconds(1000);
}
int i = 1;
while(1){
time += getElapsedTime(delay);
printf("time (absolute):\t%d\r\ntime mod LONG_PERIOD:\t%d\r\ntime mod SHORT_PERIOD:\t%d\r\n",time,time%LONG_PERIOD,time%SHORT_PERIOD);
delay = getSystemTime();
/* Collect data from sensors */
//collect_data(/*a_rawData*/);
collect_data(valueSensors, sensors);
time += getElapsedTime(delay);
printf("time (absolute):\t%d\r\ntime mod LONG_PERIOD:\t%d\r\ntime mod SHORT_PERIOD:\t%d\r\n",time,time%LONG_PERIOD,time%SHORT_PERIOD);
delay = getSystemTime();
//cada mitja hora
if (i == LONG_PERIOD/SHORT_PERIOD ){
/* Wi-Fi connect */
wifi_connect();
/* Send data to server, empty the buffer */
send_data();
//time = getNTPTime();
time = getSecsSince1900();
printf("time (absolute):\t%d\r\ntime mod LONG_PERIOD:\t%d\r\ntime mod SHORT_PERIOD:\t%d\r\n",time,time%LONG_PERIOD,time%SHORT_PERIOD);
i = 0;
}
delay2 = getElapsedTime(delay);
delay = getSystemTime();
timestamp = time - delay2;
printf("timestamp (absolute):\t%d\r\ntimestamp mod LONG_PERIOD:\t%d\r\ntimestamp mod SHORT_PERIOD:\t%d\r\n",timestamp,timestamp%LONG_PERIOD,timestamp%SHORT_PERIOD);
/* Add data to the buffer with timestamp*/
put_buffers(buffers,ind,sizes,valueSensors);
printf("mirant memoria\r\n");
int res=check_memory();
if(res==SOFT_REACHED){
printf("--------------soft limit-------------\r\n");
int j;
for(j=0;j<TOTAL_SENSORS;j++)
{
printf(" enviant buffer %d\r\n",j);
// fem servir 085 de prova, però haurem de fer servir sensor_id[j]
int ok=send(buffers[j],&ind[j],&sizes[j], mod.ID, sensor_id[j]);
if(ok==JSON_COMM_ERROR)
{
printf("wismart is not connected\r\n");
}
else if( ok==JSON_OTHER_ERROR){
printf("some error ocurred\r\n");
}
else if(ok ==JSON_POST_OK){
printf(" send OK \r\n");
}
}
}
else if(res==HARD_REACHED){
printf("--------------hard limit-------------\r\n");
// fem servir 085 de prova, però haurem de fer servir sensor_id[0]
while( send(buffers[0],&ind[0],&sizes[0], mod.ID, sensors[j]) != JSON_POST_OK )
{
// El servidor no ens sap dir si tenim permisos o no sense registrar una mostra.
// Intentem enviar un buffer sencer, a veure si ja podem buidar.
// No podem enviar només una mostra perquè json és rígid en aquest sentit.
printf("hard_reached and unable to sentd.\r\nLa biblia en vers: i Jesús digué: Aixeca't, Llàtzer!!\r\n");
chThdSleepMilliseconds(HARD_LIMIT_WAIT_TIME);
}
int j;
for(j=1;j<TOTAL_SENSORS;j++)
{
printf(" enviant buffer %d\r\n",j);
// fem servir 085 de prova, però haurem de fer servir sensor_id[j]
int ok=send(buffers[j],&ind[j],&sizes[j], mod.ID, sensors[j]);
if(ok==JSON_COMM_ERROR)
{
printf("wismart is not connected\r\n");
}
else if( ok==JSON_OTHER_ERROR){
printf("some error ocurred\r\n");
}
else if(ok ==JSON_POST_OK){
printf(" send OK \r\n");
}
}
}
//a_cookedData = format_data(a_rawData, timestamp);
time += getElapsedTime(delay);
printf("time (absolute):\t%d\r\ntime mod LONG_PERIOD:\t%d\r\ntime mod SHORT_PERIOD:\t%d\r\n",time,time%LONG_PERIOD,time%SHORT_PERIOD);
delay = getSystemTime();
sleep_thread(SHORT_PERIOD - time%SHORT_PERIOD);
i++;
}
//A partir de aquí se debe integrar arriba, preferiblemente con funciones María
uint32_t ind[4]={0};
char** buffers[4];
uint32_t sizes[4]={0};
char* sensor_id[4];
int i;
init_registry();
if(timeout==1)
{
printf("Creating AP\r\n");
//corroborar los parametros del AP
configServer_start(1);
libwismart_WiFi_SoftAP_Start(NETWORK_SSID_AP,NETWORK_CHANNEL_AP,NULL,softapMode_apStartedCb, softapMode_clientIndicationCb);
for(;;)
{
chThdSleepMilliseconds(1000);
}
}
for(;;)
{
// i2c gets the data and combines it with the time stamp
char* data="message,0";
char* data2="message,1";
char* data3="message,2";
for(i=0;i<NUMSENSORS;i++){
printf("------------------BUFFER %d ----------------------\r\n",i);
printf("index=%d\r\n",ind[i]);
// 3 missatges de prova a cada buffer
buffers[i]=put_message(data, buffers[i] ,&ind[i],&sizes[i]);
buffers[i]=put_message(data2, buffers[i] ,&ind[i],&sizes[i]);
buffers[i]=put_message(data3, buffers[i] ,&ind[i],&sizes[i]);
printf("mirant memoria\r\n");
int res=check_memory();
/*if(res==SOFT_REACHED){
printf("--------------soft limit-------------\r\n");
int j;
for(j=0;j<NUMSENSORS;j++)
{
printf(" enviant buffer %d\r\n",j);
// fem servir 085 de prova, però haurem de fer servir sensor_id[j]
int ok=send(buffers[j],&ind[j],&sizes[j], "bmp", "085");
if(ok==JSON_COMM_ERROR)
{
printf("wismart is not connected\r\n");
}
else if( ok==JSON_OTHER_ERROR){
printf("some error ocurred\r\n");
}
else if(ok ==JSON_POST_OK){
printf(" send OK \r\n");
}
}
}
else if(res==HARD_REACHED){
printf("--------------hard limit-------------\r\n");
// fem servir 085 de prova, però haurem de fer servir sensor_id[0]
while( send(buffers[0],&ind[0],&sizes[0], "bmp", "085") != JSON_POST_OK )
{
// El servidor no ens sap dir si tenim permisos o no sense registrar una mostra.
// Intentem enviar un buffer sencer, a veure si ja podem buidar.
// No podem enviar només una mostra perquè json és rígid en aquest sentit.
printf("hard_reached and unable to sentd.\r\nLa biblia en vers: i Jesús digué: Aixeca't, Llàtzer!!\r\n");
chThdSleepMilliseconds(HARD_LIMIT_WAIT_TIME);
}
int j;
for(j=1;j<NUMSENSORS;j++)
{
printf(" enviant buffer %d\r\n",j);
// fem servir 085 de prova, però haurem de fer servir sensor_id[j]
int ok=send(buffers[j],&ind[j],&sizes[j], "bmp", "085");
if(ok==JSON_COMM_ERROR)
{
printf("wismart is not connected\r\n");
}
else if( ok==JSON_OTHER_ERROR){
printf("some error ocurred\r\n");
}
else if(ok ==JSON_POST_OK){
printf(" send OK \r\n");
}
}
}*/
}
//chThdSleepMilliseconds(100);
}
}