Commit 60c50eb4d147a39e39d0c42bd1c143462e96bf21
1 parent
c99ad75c
--no commit message
Showing
1 changed file
with
50 additions
and
63 deletions
Project/applications/smartcities/main2.c
@@ -31,7 +31,7 @@ void initLibwismart(void) | @@ -31,7 +31,7 @@ void initLibwismart(void) | ||
31 | uint8_t connected=0; | 31 | uint8_t connected=0; |
32 | uint8_t timeout=0; | 32 | uint8_t timeout=0; |
33 | uint8_t retries=0; | 33 | uint8_t retries=0; |
34 | -uint8_t first_sensor=0; | 34 | +uint8_t sensors_length=0; |
35 | 35 | ||
36 | void init_registry(void) | 36 | void init_registry(void) |
37 | { | 37 | { |
@@ -91,64 +91,50 @@ void init_registry(void) | @@ -91,64 +91,50 @@ void init_registry(void) | ||
91 | void send_data(char** buffers[], uint32_t ind[], uint32_t sizes[], uint8_t sensors[]) | 91 | void send_data(char** buffers[], uint32_t ind[], uint32_t sizes[], uint8_t sensors[]) |
92 | { | 92 | { |
93 | int j; | 93 | int j; |
94 | - for(j=0;j<TOTAL_SENSORS;j++) | 94 | + for(j=0;j<sensors_length;j++) |
95 | { | 95 | { |
96 | - if(sensors[j]!=0) | ||
97 | - { | ||
98 | - printf(" enviant buffer %d\r\n",j); | ||
99 | - | ||
100 | - // fem servir 085 de prova, però haurem de fer servir sensor_id[j] | ||
101 | - char id[3]; | ||
102 | - sprintf(id,"%x",sensors[j]); | ||
103 | - int ok=send(buffers[j],&ind[j],&sizes[j], mod.ID, id); | 96 | + printf(" enviant buffer %d\r\n",j); |
97 | + | ||
98 | + // fem servir 085 de prova, però haurem de fer servir sensor_id[j] | ||
99 | + char id[3]; | ||
100 | + sprintf(id,"%x",sensors[j]); | ||
101 | + int ok=send(buffers[j],&ind[j],&sizes[j], mod.ID, id); | ||
104 | 102 | ||
105 | - if(ok==JSON_COMM_ERROR) | ||
106 | - { | ||
107 | - printf("wismart is not connected\r\n"); | ||
108 | - } | ||
109 | - else if( ok==JSON_OTHER_ERROR){ | ||
110 | - printf("some error ocurred\r\n"); | ||
111 | - } | ||
112 | - else if(ok ==JSON_POST_OK){ | ||
113 | - printf(" send OK \r\n"); | ||
114 | - } | 103 | + if(ok==JSON_COMM_ERROR) |
104 | + { | ||
105 | + printf("wismart is not connected\r\n"); | ||
106 | + } | ||
107 | + else if( ok==JSON_OTHER_ERROR){ | ||
108 | + printf("some error ocurred\r\n"); | ||
109 | + } | ||
110 | + else if(ok ==JSON_POST_OK){ | ||
111 | + printf(" send OK \r\n"); | ||
115 | } | 112 | } |
116 | } | 113 | } |
117 | } | 114 | } |
118 | 115 | ||
119 | void put_buffers(char** buffers[],uint32_t ind[],uint32_t sizes[],char** cooked, uint8_t* sensors){ | 116 | void put_buffers(char** buffers[],uint32_t ind[],uint32_t sizes[],char** cooked, uint8_t* sensors){ |
120 | int i; | 117 | int i; |
121 | - for(i=0;i<TOTAL_SENSORS;i++){ | ||
122 | - if(sensors[i]!=0){ | ||
123 | - put_message(cooked[i], buffers[i] ,&ind[i],&sizes[i]); | ||
124 | - chHeapFree(cooked[i]); | ||
125 | - } | 118 | + for(i=0;i<sensors_length;i++){ |
119 | + put_message(cooked[i], buffers[i] ,&ind[i],&sizes[i]); | ||
120 | + chHeapFree(cooked[i]); | ||
126 | } | 121 | } |
127 | 122 | ||
128 | } | 123 | } |
129 | char** timestamp_datas(char* value[],unsigned long timestamp, uint8_t* sensors){ | 124 | char** timestamp_datas(char* value[],unsigned long timestamp, uint8_t* sensors){ |
130 | char** cooked_data; | 125 | char** cooked_data; |
131 | - cooked_data = (char**) chHeapAlloc(NULL,TOTAL_SENSORS * sizeof(char*)); | 126 | + cooked_data = (char**) chHeapAlloc(NULL,sensors_length * sizeof(char*)); |
132 | Date t=getDate(timestamp); | 127 | Date t=getDate(timestamp); |
133 | int i; | 128 | int i; |
134 | - for(i=0;i<TOTAL_SENSORS;i++){ | ||
135 | - if(sensors[i]!=0){ | ||
136 | - cooked_data[i]=timestamp_data(value[i], t); | ||
137 | - chHeapFree(value[i]); | ||
138 | - } | 129 | + for(i=0;i<sensors_length;i++){ |
130 | + | ||
131 | + cooked_data[i]=timestamp_data(value[i], t); | ||
132 | + chHeapFree(value[i]); | ||
133 | + | ||
139 | } | 134 | } |
140 | return cooked_data; | 135 | return cooked_data; |
141 | } | 136 | } |
142 | 137 | ||
143 | -void first_sens(uint8_t* sensors){ | ||
144 | - int i; | ||
145 | - for(i=0;i<TOTAL_SENSORS;i++){ | ||
146 | - if(sensors[i]!=0){ | ||
147 | - first_sensor=i; | ||
148 | - break; | ||
149 | - } | ||
150 | - } | ||
151 | -} | ||
152 | 138 | ||
153 | void wifi_connect(void) | 139 | void wifi_connect(void) |
154 | { | 140 | { |
@@ -185,10 +171,11 @@ int main(void) | @@ -185,10 +171,11 @@ int main(void) | ||
185 | libwismart_EnableBsdSocketAPI(); | 171 | libwismart_EnableBsdSocketAPI(); |
186 | 172 | ||
187 | //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 | 173 | //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 |
174 | + | ||
188 | uint8_t sensors[TOTAL_SENSORS]; | 175 | uint8_t sensors[TOTAL_SENSORS]; |
189 | char* valueSensors[TOTAL_SENSORS]; | 176 | char* valueSensors[TOTAL_SENSORS]; |
190 | I2C_scan(sensors); | 177 | I2C_scan(sensors); |
191 | - first_sens(sensors); | 178 | + sensors_length=strlen(sensors); |
192 | //registrar sensores | 179 | //registrar sensores |
193 | //Esta funcion esta mas arriba para no repetir código luego -- Imanol | 180 | //Esta funcion esta mas arriba para no repetir código luego -- Imanol |
194 | wifi_connect(); | 181 | wifi_connect(); |
@@ -202,9 +189,9 @@ int main(void) | @@ -202,9 +189,9 @@ int main(void) | ||
202 | unsigned long delay2 = 0; | 189 | unsigned long delay2 = 0; |
203 | sleep_thread(SHORT_PERIOD - time%SHORT_PERIOD); | 190 | sleep_thread(SHORT_PERIOD - time%SHORT_PERIOD); |
204 | 191 | ||
205 | - uint32_t ind[TOTAL_SENSORS]={0}; | ||
206 | - char** buffers[TOTAL_SENSORS]; | ||
207 | - uint32_t sizes[TOTAL_SENSORS]={0}; | 192 | + uint32_t ind[sensors_length]={0}; |
193 | + char** buffers[sensors_length]; | ||
194 | + uint32_t sizes[sensors_length]={0}; | ||
208 | //char* cooked_data[TOTAL_SENSORS]; | 195 | //char* cooked_data[TOTAL_SENSORS]; |
209 | int i = 1; | 196 | int i = 1; |
210 | 197 | ||
@@ -261,8 +248,8 @@ int main(void) | @@ -261,8 +248,8 @@ int main(void) | ||
261 | wifi_connect(); | 248 | wifi_connect(); |
262 | // fem servir 085 de prova, però haurem de fer servir sensor_id[0] | 249 | // fem servir 085 de prova, però haurem de fer servir sensor_id[0] |
263 | char id_0[3]; | 250 | char id_0[3]; |
264 | - sprintf(id_0,"%x",sensors[first_sensor]); | ||
265 | - while( send(buffers[first_sensor],&ind[first_sensor],&sizes[first_sensor], mod.ID, id_0) != JSON_POST_OK ) | 251 | + sprintf(id_0,"%x",sensors[0]); |
252 | + while( send(buffers[0],&ind[0],&sizes[0], mod.ID, id_0) != JSON_POST_OK ) | ||
266 | { | 253 | { |
267 | // El servidor no ens sap dir si tenim permisos o no sense registrar una mostra. | 254 | // El servidor no ens sap dir si tenim permisos o no sense registrar una mostra. |
268 | // Intentem enviar un buffer sencer, a veure si ja podem buidar. | 255 | // Intentem enviar un buffer sencer, a veure si ja podem buidar. |
@@ -272,25 +259,25 @@ int main(void) | @@ -272,25 +259,25 @@ int main(void) | ||
272 | chThdSleepMilliseconds(HARD_LIMIT_WAIT_TIME); | 259 | chThdSleepMilliseconds(HARD_LIMIT_WAIT_TIME); |
273 | } | 260 | } |
274 | int j; | 261 | int j; |
275 | - for(j=first_sensor;j<TOTAL_SENSORS;j++) | 262 | + for(j=1;j<sensors_length;j++) |
276 | { | 263 | { |
277 | - if(sensors[j]!=0){ | ||
278 | - printf(" enviant buffer %d\r\n",j); | ||
279 | - // fem servir 085 de prova, però haurem de fer servir sensor_id[j] | ||
280 | - char id[3]; | ||
281 | - sprintf(id,"%x",sensors[j]); | ||
282 | - int ok=send(buffers[j],&ind[j],&sizes[j], mod.ID, id); | ||
283 | - if(ok==JSON_COMM_ERROR) | ||
284 | - { | ||
285 | - printf("wismart is not connected\r\n"); | ||
286 | - } | ||
287 | - else if( ok==JSON_OTHER_ERROR){ | ||
288 | - printf("some error ocurred\r\n"); | ||
289 | - } | ||
290 | - else if(ok ==JSON_POST_OK){ | ||
291 | - printf(" send OK \r\n"); | ||
292 | - } | 264 | + |
265 | + printf(" enviant buffer %d\r\n",j); | ||
266 | + // fem servir 085 de prova, però haurem de fer servir sensor_id[j] | ||
267 | + char id[3]; | ||
268 | + sprintf(id,"%x",sensors[j]); | ||
269 | + int ok=send(buffers[j],&ind[j],&sizes[j], mod.ID, id); | ||
270 | + if(ok==JSON_COMM_ERROR) | ||
271 | + { | ||
272 | + printf("wismart is not connected\r\n"); | ||
273 | + } | ||
274 | + else if( ok==JSON_OTHER_ERROR){ | ||
275 | + printf("some error ocurred\r\n"); | ||
293 | } | 276 | } |
277 | + else if(ok ==JSON_POST_OK){ | ||
278 | + printf(" send OK \r\n"); | ||
279 | + } | ||
280 | + | ||
294 | } | 281 | } |
295 | wifi_disconnect(); | 282 | wifi_disconnect(); |
296 | } | 283 | } |