Commit b4b1b5c42c271e26f9bbdceab6d61fbf80414409

Authored by Maria Jorba Brosa
1 parent bf3c8796

--no commit message

Project/applications/smartcities/include/globals.h
@@ -5,7 +5,6 @@ @@ -5,7 +5,6 @@
5 #define SERVER_HOSTNAME SERVER_IP 5 #define SERVER_HOSTNAME SERVER_IP
6 #define MODULE_ID_LENGTH 6 6 #define MODULE_ID_LENGTH 6
7 #define BUFFER_LENGTH 30 7 #define BUFFER_LENGTH 30
8 -  
9 #define MAX_RAM 30000 8 #define MAX_RAM 30000
10 9
11 #endif 10 #endif
12 \ No newline at end of file 11 \ No newline at end of file
Project/applications/smartcities/main2.c
@@ -8,6 +8,7 @@ @@ -8,6 +8,7 @@
8 #include "buffer.h" 8 #include "buffer.h"
9 #include "i2c.h" 9 #include "i2c.h"
10 #include "configServer.h" 10 #include "configServer.h"
  11 +#include "timer-loop.h"
11 12
12 #define WIFI_MODE WIFI_MODE_CLIENT 13 #define WIFI_MODE WIFI_MODE_CLIENT
13 #define NETWORK_SSID_AP "modularsens" 14 #define NETWORK_SSID_AP "modularsens"
@@ -15,7 +16,6 @@ @@ -15,7 +16,6 @@
15 #define NETWORK_CHANNEL_AP 1 16 #define NETWORK_CHANNEL_AP 1
16 17
17 #define HARD_LIMIT_WAIT_TIME 10*1000 //5*60*1000 18 #define HARD_LIMIT_WAIT_TIME 10*1000 //5*60*1000
18 -#define NUMSENSORS 4  
19 19
20 wismart_registryKey_t geo; 20 wismart_registryKey_t geo;
21 21
@@ -29,6 +29,7 @@ void initLibwismart(void) @@ -29,6 +29,7 @@ void initLibwismart(void)
29 uint8_t connected=0; 29 uint8_t connected=0;
30 uint8_t timeout=0; 30 uint8_t timeout=0;
31 uint8_t retries=0; 31 uint8_t retries=0;
  32 +uint8_t total_sensors=0;
32 33
33 void init_registry(void) 34 void init_registry(void)
34 { 35 {
@@ -88,7 +89,7 @@ void init_registry(void) @@ -88,7 +89,7 @@ void init_registry(void)
88 void send_data(char** buffers[j], uint32_t ind[], uint32_t sizes[j], uint8_t sensors[]); 89 void send_data(char** buffers[j], uint32_t ind[], uint32_t sizes[j], uint8_t sensors[]);
89 { 90 {
90 int j; 91 int j;
91 - for(j=0;j<TOTAL_SENSORS;j++) 92 + for(j=0;j<total_sensors;j++)
92 { 93 {
93 printf(" enviant buffer %d\r\n",j); 94 printf(" enviant buffer %d\r\n",j);
94 95
@@ -110,7 +111,7 @@ void send_data(char** buffers[j], uint32_t ind[], uint32_t sizes[j], uint8_t sen @@ -110,7 +111,7 @@ void send_data(char** buffers[j], uint32_t ind[], uint32_t sizes[j], uint8_t sen
110 111
111 void put_buffers(char** buffers,uint32_t ind[],uint32_t sizes[],char** cooked){ 112 void put_buffers(char** buffers,uint32_t ind[],uint32_t sizes[],char** cooked){
112 int i; 113 int i;
113 - for(i=0;i<TOTAL_SENSORS;i++){ 114 + for(i=0;i<total_sensors;i++){
114 put_message(cooked[i], buffers[i] ,&ind[i],&sizes[i]); 115 put_message(cooked[i], buffers[i] ,&ind[i],&sizes[i]);
115 chHeapFree(cooked[i]); 116 chHeapFree(cooked[i]);
116 } 117 }
@@ -121,13 +122,22 @@ char** timestamp_datas(char* value[],unsigned long timestamp){ @@ -121,13 +122,22 @@ char** timestamp_datas(char* value[],unsigned long timestamp){
121 char** cooked_data; 122 char** cooked_data;
122 Date t=getDate(timestamp); 123 Date t=getDate(timestamp);
123 int i; 124 int i;
124 - for(i=0;i<TOTAL_SENSORS;i++){ 125 + for(i=0;i<total_sensors;i++){
125 cooked_data[i]=timestamp_data(value[i], t); 126 cooked_data[i]=timestamp_data(value[i], t);
126 chHeapFree(value[i]); 127 chHeapFree(value[i]);
127 } 128 }
128 return cooked_data; 129 return cooked_data;
129 } 130 }
130 131
  132 +void sensor_scan(){
  133 + I2C_scan(sensors);
  134 + int i;
  135 + for(i=0;i<TOTAL_SENSORS;i++){
  136 + if(sensors[i]!=0){
  137 + total_sensors++;
  138 + }
  139 + }
  140 +}
131 141
132 int main(void) 142 int main(void)
133 { 143 {
@@ -141,12 +151,14 @@ int main(void) @@ -141,12 +151,14 @@ int main(void)
141 151
142 //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 152 //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
143 uint8_t sensors[TOTAL_SENSORS]; 153 uint8_t sensors[TOTAL_SENSORS];
144 - char* valueSensors[TOTAL_SENSORS]; 154 + sensor_scan();
  155 + char* valueSensors[total_sensors];
145 I2C_scan(sensors); 156 I2C_scan(sensors);
146 157
147 - 158 + //conectar wifi
148 //unsigned int time = getNTPTime(); 159 //unsigned int time = getNTPTime();
149 unsigned int time = getSecsSince1900(); 160 unsigned int time = getSecsSince1900();
  161 + //desconectarwifi
150 162
151 unsigned long timestamp = 0; 163 unsigned long timestamp = 0;
152 unsigned long delay = getSystemTime(); 164 unsigned long delay = getSystemTime();
@@ -156,7 +168,7 @@ int main(void) @@ -156,7 +168,7 @@ int main(void)
156 uint32_t ind[4]={0}; 168 uint32_t ind[4]={0};
157 char** buffers[4]; 169 char** buffers[4];
158 uint32_t sizes[4]={0}; 170 uint32_t sizes[4]={0};
159 - char* cooked_data[TOTAL_SENSORS]; 171 + char* cooked_data[total_sensors];
160 172
161 init_registry(); 173 init_registry();
162 174
@@ -194,6 +206,7 @@ int main(void) @@ -194,6 +206,7 @@ int main(void)
194 send_data(); 206 send_data();
195 //time = getNTPTime(); 207 //time = getNTPTime();
196 time = getSecsSince1900(); 208 time = getSecsSince1900();
  209 + //desconectar wifi
197 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); 210 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);
198 i = 0; 211 i = 0;
199 } 212 }
@@ -228,7 +241,7 @@ int main(void) @@ -228,7 +241,7 @@ int main(void)
228 chThdSleepMilliseconds(HARD_LIMIT_WAIT_TIME); 241 chThdSleepMilliseconds(HARD_LIMIT_WAIT_TIME);
229 } 242 }
230 int j; 243 int j;
231 - for(j=1;j<TOTAL_SENSORS;j++) 244 + for(j=1;j<total_sensors;j++)
232 { 245 {
233 printf(" enviant buffer %d\r\n",j); 246 printf(" enviant buffer %d\r\n",j);
234 247
@@ -252,105 +265,5 @@ int main(void) @@ -252,105 +265,5 @@ int main(void)
252 delay = getSystemTime(); 265 delay = getSystemTime();
253 sleep_thread(SHORT_PERIOD - time%SHORT_PERIOD); 266 sleep_thread(SHORT_PERIOD - time%SHORT_PERIOD);
254 i++; 267 i++;
255 - }  
256 -  
257 -  
258 -  
259 -  
260 -  
261 -  
262 -//A partir de aquí se debe integrar arriba, preferiblemente con funciones María  
263 - uint32_t ind[4]={0};  
264 - char** buffers[4];  
265 - uint32_t sizes[4]={0};  
266 - char* sensor_id[4];  
267 - int i;  
268 -  
269 -  
270 - init_registry();  
271 -  
272 - if(timeout==1)  
273 - {  
274 - printf("Creating AP\r\n");  
275 - //corroborar los parametros del AP  
276 - configServer_start(1);  
277 - libwismart_WiFi_SoftAP_Start(NETWORK_SSID_AP,NETWORK_CHANNEL_AP,NULL,softapMode_apStartedCb, softapMode_clientIndicationCb);  
278 - for(;;)  
279 - {  
280 - chThdSleepMilliseconds(1000);  
281 - }  
282 - }  
283 - for(;;)  
284 - {  
285 - // i2c gets the data and combines it with the time stamp  
286 - char* data="message,0";  
287 - char* data2="message,1";  
288 - char* data3="message,2";  
289 - for(i=0;i<NUMSENSORS;i++){  
290 -  
291 - printf("------------------BUFFER %d ----------------------\r\n",i);  
292 - printf("index=%d\r\n",ind[i]);  
293 -  
294 - // 3 missatges de prova a cada buffer  
295 - buffers[i]=put_message(data, buffers[i] ,&ind[i],&sizes[i]);  
296 - buffers[i]=put_message(data2, buffers[i] ,&ind[i],&sizes[i]);  
297 - buffers[i]=put_message(data3, buffers[i] ,&ind[i],&sizes[i]);  
298 - printf("mirant memoria\r\n");  
299 - int res=check_memory();  
300 - /*if(res==SOFT_REACHED){  
301 - printf("--------------soft limit-------------\r\n");  
302 - int j;  
303 - for(j=0;j<NUMSENSORS;j++)  
304 - {  
305 - printf(" enviant buffer %d\r\n",j);  
306 -  
307 - // fem servir 085 de prova, però haurem de fer servir sensor_id[j]  
308 - int ok=send(buffers[j],&ind[j],&sizes[j], "bmp", "085");  
309 - if(ok==JSON_COMM_ERROR)  
310 - {  
311 - printf("wismart is not connected\r\n");  
312 - }  
313 - else if( ok==JSON_OTHER_ERROR){  
314 - printf("some error ocurred\r\n");  
315 - }  
316 - else if(ok ==JSON_POST_OK){  
317 - printf(" send OK \r\n");  
318 - }  
319 - }  
320 - }  
321 - else if(res==HARD_REACHED){  
322 - printf("--------------hard limit-------------\r\n");  
323 -  
324 - // fem servir 085 de prova, però haurem de fer servir sensor_id[0]  
325 - while( send(buffers[0],&ind[0],&sizes[0], "bmp", "085") != JSON_POST_OK )  
326 - {  
327 - // El servidor no ens sap dir si tenim permisos o no sense registrar una mostra.  
328 - // Intentem enviar un buffer sencer, a veure si ja podem buidar.  
329 - // No podem enviar només una mostra perquè json és rígid en aquest sentit.  
330 -  
331 - printf("hard_reached and unable to sentd.\r\nLa biblia en vers: i Jesús digué: Aixeca't, Llàtzer!!\r\n");  
332 - chThdSleepMilliseconds(HARD_LIMIT_WAIT_TIME);  
333 - }  
334 - int j;  
335 - for(j=1;j<NUMSENSORS;j++)  
336 - {  
337 - printf(" enviant buffer %d\r\n",j);  
338 -  
339 - // fem servir 085 de prova, però haurem de fer servir sensor_id[j]  
340 - int ok=send(buffers[j],&ind[j],&sizes[j], "bmp", "085");  
341 - if(ok==JSON_COMM_ERROR)  
342 - {  
343 - printf("wismart is not connected\r\n");  
344 - }  
345 - else if( ok==JSON_OTHER_ERROR){  
346 - printf("some error ocurred\r\n");  
347 - }  
348 - else if(ok ==JSON_POST_OK){  
349 - printf(" send OK \r\n");  
350 - }  
351 - }  
352 - }*/  
353 - }  
354 - //chThdSleepMilliseconds(100);  
355 - } 268 + }
356 } 269 }