Commit a24d841b15ac8e848be80e790fe777b62ee7347e

Authored by Imanol-Mikel Barba Sabariego
1 parent f352470c

--no commit message

Project/applications/smartcities/test_httpClient.c renamed to Project/applications/smartcities/example-code/httpClient/test_httpClient.c
Project/applications/smartcities/test_timer-loop.c renamed to Project/applications/smartcities/example-code/test_timer-loop.c
Project/applications/smartcities/include/buffer.h
... ... @@ -4,7 +4,7 @@
4 4 #include "libwismart.h"
5 5 #include "json.h"
6 6  
7   -#define SOFT_LIMIT 8192 /* en bytes -> 10k * 1024 */
  7 +#define SOFT_LIMIT 8192 /* en bytes -> 8k * 1024 */
8 8 #define HARD_LIMIT 10240
9 9 #define MEMORY_OK 0
10 10 #define SOFT_REACHED 1
... ...
Project/applications/smartcities/include/globals.h
... ... @@ -7,5 +7,11 @@
7 7 #define BUFFER_LENGTH 30
8 8 #define MAX_RAM 30000
9 9 #define MODULE_ID "123456"
  10 +#define WIFI_MODE WIFI_MODE_CLIENT
  11 +#define NETWORK_SSID_AP "modularsense"
  12 +#define NETWORK_KEY_AP NULL
  13 +#define NETWORK_CHANNEL_AP 1
  14 +#define HARD_LIMIT_WAIT_TIME 10*1000 //5*60*1000
  15 +
10 16  
11 17 #endif
... ...
Project/applications/smartcities/include/sensors.h
... ... @@ -67,13 +67,13 @@ typedef struct {
67 67 uint16_t MD;
68 68 } bmp085_callibration;
69 69  
  70 +void init_pressure(void);
  71 +void init_pressure_temperature(void);
70 72 uint16_t get_pressure_data(void);
71 73 uint16_t get_pressure_temperature_data(void);
72 74 bmp085_callibration get_pressure_callibration_data(void);
73 75 char* callibration_pressure_data_csv(bmp085_callibration parameters);
74 76 char* pressure_value(uint16_t pressure,uint16_t temperature);
75   -void init_pressure(void);
76   -void init_pressure_temperature(void);
77 77  
78 78 //HUMIDITY AND TEMPERATURE SENSOR
79 79  
... ...
Project/applications/smartcities/json.c
... ... @@ -157,14 +157,12 @@ uint8_t send_json(char* statement, uint32_t length, char* provider_ID, char* sen
157 157 strcpy(PATH,"/data/modularupload/");
158 158 strcpy(PATH+20,provider_ID);
159 159 strcpy(PATH+20+strlen(provider_ID),sensor_ID);*/
160   - //PATH[21+strlen(provider_ID)+strlen(sensor_ID)] = '\0';
161 160  
162 161 //FUCKING LEWIS...
163 162 PATH = (char*) chHeapAlloc(NULL,54+strlen(provider_ID)+strlen(sensor_ID));
164 163 strcpy(PATH,"/modularsense/wordpress/opendata/data/modularupload/");
165 164 strcpy(PATH+52,provider_ID);
166 165 strcpy(PATH+52+strlen(provider_ID),sensor_ID);
167   - //PATH[53+strlen(provider_ID)+strlen(sensor_ID)] = '\0';
168 166 }
169 167 struct httpHeaders server = { put,PATH,strlen(PATH),URL,strlen(URL)};
170 168 response_code = httpRequest(server, statement, length);
... ...
Project/applications/smartcities/main.c
... ... @@ -12,15 +12,13 @@
12 12 #include "ntp.h"
13 13 #include "sensors.h"
14 14  
15   -#define WIFI_MODE WIFI_MODE_CLIENT
16   -#define NETWORK_SSID_AP "modularsense"
17   -#define NETWORK_KEY_AP NULL
18   -#define NETWORK_CHANNEL_AP 1
19   -
20   -#define HARD_LIMIT_WAIT_TIME 10*1000 //5*60*1000
21   -
  15 +uint8_t connected=0;
  16 +uint8_t timeout=0;
  17 +uint8_t retries=0;
  18 +uint8_t sensors_length=0;
  19 +uint8_t registry_opened=0;
22 20 wismart_registryKey_t geo;
23   -
  21 +module mod;
24 22  
25 23 void initLibwismart(void)
26 24 {
... ... @@ -28,13 +26,6 @@ void initLibwismart(void)
28 26 libwismart_Init(hwif);
29 27 }
30 28  
31   -uint8_t connected=0;
32   -uint8_t timeout=0;
33   -uint8_t retries=0;
34   -uint8_t sensors_length=0;
35   -uint8_t registry_opened=0;
36   -
37   -module mod;
38 29  
39 30 void update_time(unsigned long *time)
40 31 {
... ... @@ -114,8 +105,6 @@ void send_data(char** buffers[], uint32_t ind[], uint32_t sizes[], uint8_t senso
114 105 for(j=0;j<sensors_length;j++)
115 106 {
116 107 printf(" enviant buffer %d\r\n",j);
117   -
118   - // fem servir 085 de prova, perรฒ haurem de fer servir sensor_id[j]
119 108 char id[3];
120 109 sprintf(id,"%x",sensors[j]);
121 110 int ok=send(buffers[j],&ind[j],&sizes[j], mod.ID, id);
... ... @@ -124,10 +113,12 @@ void send_data(char** buffers[], uint32_t ind[], uint32_t sizes[], uint8_t senso
124 113 {
125 114 printf("wismart is not connected\r\n");
126 115 }
127   - else if( ok==JSON_OTHER_ERROR){
  116 + else if( ok==JSON_OTHER_ERROR)
  117 + {
128 118 printf("some error ocurred\r\n");
129 119 }
130   - else if(ok ==JSON_POST_OK){
  120 + else if(ok ==JSON_POST_OK)
  121 + {
131 122 printf(" send OK \r\n");
132 123 }
133 124 }
... ... @@ -136,7 +127,8 @@ void send_data(char** buffers[], uint32_t ind[], uint32_t sizes[], uint8_t senso
136 127 void put_buffers(char** buffers[],uint32_t ind[],uint32_t sizes[],char** cooked, uint8_t* sensors)
137 128 {
138 129 int i;
139   - for(i=0;i<sensors_length;i++){
  130 + for(i=0;i<sensors_length;i++)
  131 + {
140 132 printf("For %d and %d\n\r", ind[i],sizes[i]);
141 133 printf("Putting data: %s\n\r",cooked[i]);
142 134 buffers[i] = put_message(cooked[i], buffers[i] ,&ind[i],&sizes[i]);
... ... @@ -173,7 +165,6 @@ void wifi_connect(void)
173 165 if(timeout==1)
174 166 {
175 167 printf("Creating AP\r\n");
176   - //corroborar los parametros del AP
177 168 configServer_start(1);
178 169 libwismart_WiFi_SoftAP_Start(NETWORK_SSID_AP,NETWORK_CHANNEL_AP,NULL,softapMode_apStartedCb, softapMode_clientIndicationCb);
179 170 for(;;)
... ... @@ -209,7 +200,8 @@ void send_battery_level(unsigned long timestamp)
209 200 chHeapFree(statement);
210 201 }
211 202  
212   -void wifi_disconnect(void){
  203 +void wifi_disconnect(void)
  204 +{
213 205 connected = 0;
214 206 timeout = 0;
215 207 uint8_t res=libwismart_WiFiDisconnect();
... ... @@ -235,7 +227,6 @@ int main(void)
235 227 memset (sensors, 0, TOTAL_SENSORS);
236 228  
237 229 char* valueSensors[TOTAL_SENSORS];
238   - //memset (valueSensors, 0, TOTAL_SENSORS);
239 230  
240 231 for(i = 0; i < TOTAL_SENSORS; i++)
241 232 {
... ... @@ -247,7 +238,6 @@ int main(void)
247 238  
248 239 wakeup_sensors(0xFF);
249 240 printf("Scanning sensors...\r\n");
250   - //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
251 241 //Escanea y registra
252 242 I2C_scan(I2C1,sensors);
253 243 sensors_length=strlen((char*)sensors);
... ... @@ -289,7 +279,8 @@ int main(void)
289 279 printf("Putting data in buffer...\r\n");
290 280 put_buffers(buffers,ind,sizes,values,sensors);
291 281 printf("Data is now in buffer...\n\r");
292   - if (i == LONG_PERIOD/SHORT_PERIOD ){
  282 + if (i == LONG_PERIOD/SHORT_PERIOD )
  283 + {
293 284 printf("Programmed Send cycle...\r\n");
294 285 /* Wi-Fi connect */
295 286 printf("Connecting to wifi...\r\n");
... ... @@ -299,10 +290,8 @@ int main(void)
299 290 printf("Data sent!\r\n");
300 291 //Now sending battery level
301 292 send_battery_level(timestamp);
302   - //time = getNTPTime();
303 293 update_time(&time);
304 294 delay = getSystemTime();
305   - //desconectar wifi
306 295 wifi_disconnect();
307 296 printf("new ntp time (absolute):\t%ul\r\ntime mod LONG_PERIOD:\t%ul\r\ntime mod SHORT_PERIOD:\t%ul\r\n",time,time%LONG_PERIOD,time%SHORT_PERIOD);
308 297 i = 0;
... ... @@ -310,16 +299,17 @@ int main(void)
310 299  
311 300 printf("mirant memoria\r\n");
312 301 int res=check_memory();
313   - if(res==SOFT_REACHED){
  302 + if(res==SOFT_REACHED)
  303 + {
314 304 printf("--------------soft limit-------------\r\n");
315 305 wifi_connect();
316 306 send_data(buffers, ind, sizes, sensors);
317 307 //Now sending battery level
318 308 send_battery_level(timestamp);
319   - //disconect_wifi()
320 309 wifi_disconnect();
321 310 }
322   - else if(res==HARD_REACHED){
  311 + else if(res==HARD_REACHED)
  312 + {
323 313 printf("--------------hard limit-------------\r\n");
324 314 wifi_connect();
325 315 char id_0[3];
... ... @@ -351,7 +341,6 @@ int main(void)
351 341 else if(ok ==JSON_POST_OK){
352 342 printf(" send OK \r\n");
353 343 }
354   -
355 344 }
356 345 //Now sending battery level
357 346 send_battery_level(timestamp);
... ... @@ -370,8 +359,6 @@ int main(void)
370 359 /*
371 360 * TO-DO
372 361 *
373   - * - Test distance and humidity sensors
374   - * - Test ADC
375   - * - Test register, data send and battery send
  362 + * - Test distance
376 363 * - Reset timer-related defines
377 364 */
378 365 \ No newline at end of file
... ...
Project/applications/smartcities/ntp.c
1 1 #include "ntp.h"
2 2  
3   -Date getDate(unsigned long secsSince1900){
  3 +Date getDate(unsigned long secsSince1900)
  4 +{
4 5 unsigned long daysPerMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
5   -
6 6 unsigned long epoch = secsSince1900 - DIFF_SEC_1900_1970;
7 7  
8 8 int second = epoch % 60;
... ... @@ -14,7 +14,8 @@ Date getDate(unsigned long secsSince1900){
14 14 int year = 0;
15 15 unsigned long days = 0;
16 16  
17   - while((unsigned)(days += (LEAP_YEAR(year) ? 366 : 365)) <= epoch){
  17 + while((unsigned)(days += (LEAP_YEAR(year) ? 366 : 365)) <= epoch)
  18 + {
18 19 year++;
19 20 }
20 21 days -= LEAP_YEAR(year) ? 366 : 365;
... ... @@ -23,14 +24,17 @@ Date getDate(unsigned long secsSince1900){
23 24 int j=0;
24 25 int cont=0;
25 26 int aux=epoch;
26   - if(LEAP_YEAR(year)){
  27 + if(LEAP_YEAR(year))
  28 + {
27 29 daysPerMonth[1]=29;
28 30 }
29 31  
30   - while (daysPerMonth[j]<sizeof(daysPerMonth)){
  32 + while (daysPerMonth[j]<sizeof(daysPerMonth))
  33 + {
31 34  
32 35 cont+=daysPerMonth[j];
33   - if(epoch<cont){
  36 + if(epoch<cont)
  37 + {
34 38 break;
35 39 }
36 40 aux-=daysPerMonth[j];
... ... @@ -165,16 +169,8 @@ unsigned long getSecsSince1900 (void)
165 169  
166 170 /*Funcion para testeo del resto de funciones relacionadas con NTP.
167 171 -) getSecsSince1900() solo devuelve los segundos desde 1900, esta funciรณn abre y cierra los sockets de forma que puede ser llamada de forma continuada
168   - -) a getDate() se le deve pasar el resultado de la funciรณn anterior
  172 + -) a getDate() se le debe pasar el resultado de la funciรณn anterior
169 173 */
170   -void udpNtp_test(){
171   -
172   - libwismart_EnableBsdSocketAPI();
173   - Date var2=getDate(getSecsSince1900());
174   -
175   - printf("Hora:%i:%i:%i\r\n", var2.hour, var2.minute, var2.second);
176   - printf("Fecha:%i|%i|%i\r\n", var2.day,var2.month,var2.year);
177   -}
178 174  
179 175 char* timestamp_data(char* value,Date time)
180 176 {
... ... @@ -204,4 +200,4 @@ char* timestamp_data(char* value,Date time)
204 200 strcat(data, str_second);
205 201 data[length-1] = '\0';
206 202 return data;
207 203 -}
  204 +}
208 205 \ No newline at end of file
... ...
Project/applications/smartcities/sensors.c
... ... @@ -115,6 +115,23 @@ uint16_t get_light_ch1(void)
115 115 return data;
116 116 }
117 117  
  118 +char* light_value(uint32_t light)
  119 +{
  120 + char *value = chHeapAlloc(NULL,10 + 1);
  121 + sprintf(value,"%d",(unsigned int)light);
  122 + return value;
  123 +}
  124 +
  125 +void init_ultrasound(void)
  126 +{
  127 + I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter);
  128 + I2C_write(I2C1, 0x00);
  129 + I2C_write(I2C1, 0x50);
  130 + I2C_stop(I2C1);
  131 +
  132 + chThdSleepMilliseconds(70);
  133 +}
  134 +
118 135 uint16_t get_distance_data(void)
119 136 {
120 137 init_ultrasound();
... ... @@ -134,50 +151,31 @@ uint16_t get_distance_data(void)
134 151 return data;
135 152 }
136 153  
137   -void init_ultrasound(void)
  154 +char* distance_value(uint16_t distance)
138 155 {
139   - I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter);
140   - I2C_write(I2C1, 0x00);
141   - I2C_write(I2C1, 0x50);
142   - I2C_stop(I2C1);
143   -
144   - chThdSleepMilliseconds(70);
  156 + char *value = chHeapAlloc(NULL,5 + 1);
  157 + sprintf(value,"%d",distance);
  158 + return value;
145 159 }
146 160  
147   -uint16_t get_pressure_data(void)
  161 +void init_pressure(void)
148 162 {
149   - uint16_t data;
150   - init_pressure();
151   - printf("PRESSURE: Initialized\r\n");
152 163 I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
153   - I2C_write(I2C1, 0xF6);
  164 + I2C_write(I2C1, 0xF4);
  165 + I2C_write(I2C1, 0x34);
  166 + I2C_stop(I2C1);
154 167  
155   - I2C_restart(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
156   - data = I2C_read_ack(I2C1);
157   - printf("PRESSURE: Got high byte\r\n");
158   - data = data << 8;
159   - data = data | I2C_read_nack(I2C1);
160   - printf("PRESSURE: Got low byte\r\n");
161   - I2C_stop(I2C1);
162   - return data;
  168 + chThdSleepMilliseconds(5);
163 169 }
164 170  
165   -uint16_t get_pressure_temperature_data(void)
  171 +void init_pressure_temperature(void)
166 172 {
167   - uint16_t data;
168   - init_pressure_temperature();
169   - printf("PRESSURE: Initialized temperature\r\n");
170 173 I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
171   - I2C_write(I2C1, 0xF6);
172   -
173   - I2C_restart(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
174   - data = I2C_read_ack(I2C1);
175   - printf("PRESSURE: Got temperature high byte\r\n");
176   - data = data << 8;
177   - data = data | I2C_read_nack(I2C1);
178   - printf("PRESSURE: Got temperature low byte\r\n");
179   - I2C_stop(I2C1);
180   - return data;
  174 + I2C_write(I2C1, 0xF4);
  175 + I2C_write(I2C1, 0x2E);
  176 + I2C_stop(I2C1);
  177 +
  178 + chThdSleepMilliseconds(5);
181 179 }
182 180  
183 181 bmp085_callibration get_pressure_callibration_data(void)
... ... @@ -265,6 +263,7 @@ bmp085_callibration get_pressure_callibration_data(void)
265 263 printf("PRESSURE: Got callibration data\r\n");
266 264 return calib_data;
267 265 }
  266 +
268 267 char* callibration_pressure_data_csv(bmp085_callibration parameters)
269 268 {
270 269 char *str = chHeapAlloc(NULL,sizeof(char)*(11*11-1));
... ... @@ -283,24 +282,47 @@ char* callibration_pressure_data_csv(bmp085_callibration parameters)
283 282 return str;
284 283 }
285 284  
286   -void init_pressure(void)
  285 +uint16_t get_pressure_data(void)
287 286 {
  287 + uint16_t data;
  288 + init_pressure();
  289 + printf("PRESSURE: Initialized\r\n");
288 290 I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
289   - I2C_write(I2C1, 0xF4);
290   - I2C_write(I2C1, 0x34);
291   - I2C_stop(I2C1);
  291 + I2C_write(I2C1, 0xF6);
292 292  
293   - chThdSleepMilliseconds(5);
  293 + I2C_restart(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
  294 + data = I2C_read_ack(I2C1);
  295 + printf("PRESSURE: Got high byte\r\n");
  296 + data = data << 8;
  297 + data = data | I2C_read_nack(I2C1);
  298 + printf("PRESSURE: Got low byte\r\n");
  299 + I2C_stop(I2C1);
  300 + return data;
294 301 }
295 302  
296   -void init_pressure_temperature(void)
  303 +uint16_t get_pressure_temperature_data(void)
297 304 {
  305 + uint16_t data;
  306 + init_pressure_temperature();
  307 + printf("PRESSURE: Initialized temperature\r\n");
298 308 I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
299   - I2C_write(I2C1, 0xF4);
300   - I2C_write(I2C1, 0x2E);
301   - I2C_stop(I2C1);
302   -
303   - chThdSleepMilliseconds(5);
  309 + I2C_write(I2C1, 0xF6);
  310 +
  311 + I2C_restart(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
  312 + data = I2C_read_ack(I2C1);
  313 + printf("PRESSURE: Got temperature high byte\r\n");
  314 + data = data << 8;
  315 + data = data | I2C_read_nack(I2C1);
  316 + printf("PRESSURE: Got temperature low byte\r\n");
  317 + I2C_stop(I2C1);
  318 + return data;
  319 +}
  320 +
  321 +char* pressure_value(uint16_t pressure, uint16_t temperature)
  322 +{
  323 + char *value = chHeapAlloc(NULL,11 + 1);
  324 + sprintf(value,"%d,%d",pressure,temperature);
  325 + return value;
304 326 }
305 327  
306 328 void init_humidity_temp(void)
... ... @@ -308,7 +330,6 @@ void init_humidity_temp(void)
308 330 printf("HUMIDITY: Initializing sensor\r\n");
309 331 I2C_start(I2C1,HUMIDITY_TEMP_ADDR << 1, I2C_Direction_Transmitter);
310 332 I2C_stop(I2C1);
311   - //Reset I2C?
312 333 }
313 334 uint16_t get_humidity_data(void)
314 335 {
... ... @@ -344,24 +365,6 @@ uint16_t get_temperature_data(void)
344 365 return data;
345 366 }
346 367  
347   -char* light_value(uint32_t light)
348   -{
349   - char *value = chHeapAlloc(NULL,10 + 1);
350   - sprintf(value,"%d",(unsigned int)light);
351   - return value;
352   -}
353   -char* distance_value(uint16_t distance)
354   -{
355   - char *value = chHeapAlloc(NULL,5 + 1);
356   - sprintf(value,"%d",distance);
357   - return value;
358   -}
359   -char* pressure_value(uint16_t pressure, uint16_t temperature)
360   -{
361   - char *value = chHeapAlloc(NULL,11 + 1);
362   - sprintf(value,"%d,%d",pressure,temperature);
363   - return value;
364   -}
365 368 char* temp_humidity_value(uint16_t temp, uint16_t humidity)
366 369 {
367 370 char *value = chHeapAlloc(NULL,11 + 1);
... ... @@ -371,7 +374,6 @@ char* temp_humidity_value(uint16_t temp, uint16_t humidity)
371 374  
372 375 void init_sound(void)
373 376 {
374   - //DARLE CHICHA AL INTERRUPTOR I2C
375 377 adc_sound_init();
376 378 }
377 379 uint32_t get_sound_data(void)
... ... @@ -386,6 +388,24 @@ char* sound_value(uint32_t sound)
386 388 return value;
387 389 }
388 390  
  391 +void init_battery(void)
  392 +{
  393 + adc_batt_init();
  394 +}
  395 +
  396 +uint32_t get_battery_data(void)
  397 +{
  398 + init_battery();
  399 + return adc_batt_process();
  400 +}
  401 +
  402 +char* battery_value(uint32_t battery)
  403 +{
  404 + char *value = chHeapAlloc(NULL,11 + 1);
  405 + sprintf(value,"%4u",(unsigned int)battery);
  406 + return value;
  407 +}
  408 +
389 409 void collectData(char* valueSensors[], uint8_t* sensors){
390 410 uint8_t i;
391 411 for(i=0;i<strlen((char*)sensors);i++)
... ... @@ -427,22 +447,4 @@ void collectData(char* valueSensors[], uint8_t* sensors){
427 447 valueSensors[i]=temp_humidity_value(get_temperature_data(),get_humidity_data());
428 448 }
429 449 }
430   -}
431   -
432   -void init_battery(void)
433   -{
434   - adc_batt_init();
435   -}
436   -
437   -uint32_t get_battery_data(void)
438   -{
439   - init_battery();
440   - return adc_batt_process();
441   -}
442   -
443   -char* battery_value(uint32_t battery)
444   -{
445   - char *value = chHeapAlloc(NULL,11 + 1);
446   - sprintf(value,"%4u",(unsigned int)battery);
447   - return value;
448 450 }
449 451 \ No newline at end of file
... ...
Project/applications/smartcities/timer-loop.c
... ... @@ -19,13 +19,16 @@
19 19 * - milisegons:ans/1000 = 4.294.967,295 segons // 49 days 17 hours 2 minutes 47.3 seconds
20 20 */
21 21 unsigned long getSystemTime()
22   -{ return (long)libwismart_GetTime()/1000; }
  22 +{
  23 + return (long)libwismart_GetTime()/1000;
  24 +}
23 25  
24 26 unsigned long getElapsedTime(unsigned long t)
25   -{ return (long)libwismart_ElapsedTime(t*1000)/1000; }
  27 +{
  28 + return (long)libwismart_ElapsedTime(t*1000)/1000;
  29 +}
26 30  
27 31 void sleep_thread(unsigned long seconds)
28 32 {
29 33 chThdSleepMilliseconds( (unsigned int)seconds*1000 );
30   - //sleep(seconds); //for testing in linux machine
31 34 }
32 35 \ No newline at end of file
... ...