Commit 1a74fac0b68676600c82e5e956dabedf713f3cce

Authored by Imanol-Mikel Barba Sabariego
1 parent 8fbcd94f

--no commit message

Project/applications/smartcities/callbacks.c
@@ -13,18 +13,12 @@ void dhcp_connect_result_cb(int result) @@ -13,18 +13,12 @@ void dhcp_connect_result_cb(int result)
13 else if(result==LIBWISMART_DHCP_TIMEOUT) 13 else if(result==LIBWISMART_DHCP_TIMEOUT)
14 { 14 {
15 printf("DHCP timeout\r\n"); 15 printf("DHCP timeout\r\n");
16 - if(++retries == MAX_RETRIES)  
17 - {  
18 - timeout = 1;  
19 - } 16 + timeout = 1;
20 } 17 }
21 else 18 else
22 { 19 {
23 printf("DHCP error\r\n"); 20 printf("DHCP error\r\n");
24 - if(++retries == MAX_RETRIES)  
25 - {  
26 - timeout = 1;  
27 - } 21 + timeout = 1;
28 } 22 }
29 23
30 } 24 }
Project/applications/smartcities/configServer.c
@@ -61,6 +61,7 @@ void configServer_setClientParameters() @@ -61,6 +61,7 @@ void configServer_setClientParameters()
61 strcpy(config.password,arg_radPassStr); 61 strcpy(config.password,arg_radPassStr);
62 strcpy(config.localization,arg_geoLocalizationStr); 62 strcpy(config.localization,arg_geoLocalizationStr);
63 strcpy(config.ssid,arg_networkNameStr); 63 strcpy(config.ssid,arg_networkNameStr);
  64 + strcpy(config.wepkey,arg_passphraseStr);
64 strcpy(config.passphrase,arg_passphraseStr); 65 strcpy(config.passphrase,arg_passphraseStr);
65 66
66 /* Value validation */ 67 /* Value validation */
@@ -105,6 +106,7 @@ void configServer_setClientParameters() @@ -105,6 +106,7 @@ void configServer_setClientParameters()
105 if(ret == 0) 106 if(ret == 0)
106 { 107 {
107 //successful conversion 108 //successful conversion
  109 + printf("%d,%d,%d,%d,%d\r\n",asciiBuf[0],asciiBuf[1],asciiBuf[2],asciiBuf[3],asciiBuf[4]);
108 memset(arg_passphraseStr, 0, sizeof(arg_passphraseStr)); 110 memset(arg_passphraseStr, 0, sizeof(arg_passphraseStr));
109 memcpy(arg_passphraseStr, asciiBuf, 5); 111 memcpy(arg_passphraseStr, asciiBuf, 5);
110 } 112 }
@@ -143,8 +145,7 @@ void configServer_setClientParameters() @@ -143,8 +145,7 @@ void configServer_setClientParameters()
143 } 145 }
144 146
145 strcpy(config.passphrase, (char*)arg_passphraseStr); 147 strcpy(config.passphrase, (char*)arg_passphraseStr);
146 - //strcpy(config.wepkey, (char*)arg_passphraseStr);  
147 - strcpy(config.wepkey, "0009031991"); 148 + strcpy(config.wepkey, (char*)arg_passphraseStr);
148 } 149 }
149 libwismart_RegistrySet(&geo,&config); 150 libwismart_RegistrySet(&geo,&config);
150 } 151 }
@@ -166,8 +167,6 @@ void configServer_setClientParameters() @@ -166,8 +167,6 @@ void configServer_setClientParameters()
166 uint32_t configServer_dynamicCb(char* varName, char** varValue, uint8_t* varAllocType) 167 uint32_t configServer_dynamicCb(char* varName, char** varValue, uint8_t* varAllocType)
167 { 168 {
168 char* value; 169 char* value;
169 - uint16_t profile_enabled;  
170 -  
171 config_params_t config; 170 config_params_t config;
172 171
173 if(libwismart_RegistryIsValueEmpty(&geo)) 172 if(libwismart_RegistryIsValueEmpty(&geo))
@@ -181,8 +180,6 @@ uint32_t configServer_dynamicCb(char* varName, char** varValue, uint8_t* varAllo @@ -181,8 +180,6 @@ uint32_t configServer_dynamicCb(char* varName, char** varValue, uint8_t* varAllo
181 libwismart_RegistryGet(&geo,&config); 180 libwismart_RegistryGet(&geo,&config);
182 181
183 CONFIG_SERVER_DBG("Quering configuration server for variable '%s'", varName); 182 CONFIG_SERVER_DBG("Quering configuration server for variable '%s'", varName);
184 -  
185 - libwismart_ProfileGet_Int("profile_enabled", &profile_enabled);  
186 183
187 if(strcmp((char*)varName,"WIFI_SECURITY") == 0){ 184 if(strcmp((char*)varName,"WIFI_SECURITY") == 0){
188 uint16_t security; 185 uint16_t security;
@@ -244,7 +241,7 @@ uint32_t configServer_dynamicCb(char* varName, char** varValue, uint8_t* varAllo @@ -244,7 +241,7 @@ uint32_t configServer_dynamicCb(char* varName, char** varValue, uint8_t* varAllo
244 if(value == NULL){ 241 if(value == NULL){
245 return WISMART_SERVER_ERR_MEM; 242 return WISMART_SERVER_ERR_MEM;
246 } 243 }
247 - 244 +
248 strcpy(value,config.passphrase); 245 strcpy(value,config.passphrase);
249 246
250 /* Display only visible characters */ 247 /* Display only visible characters */
@@ -339,20 +336,23 @@ HEX2BIN conversion @@ -339,20 +336,23 @@ HEX2BIN conversion
339 * into "111" (byte array [49][49][49]). This is needed because WEP keys can be 336 * into "111" (byte array [49][49][49]). This is needed because WEP keys can be
340 * givent either as HEX strigs or as ASCII strings. 337 * givent either as HEX strigs or as ASCII strings.
341 */ 338 */
342 -int configServer_hex2bin(char *hex, char *buf, size_t hexLen){  
343 -  
344 - size_t i;  
345 - char *ipos = hex;  
346 - char *opos = buf;  
347 -  
348 - if( (hexLen%2) != 0){ 339 +int configServer_hex2bin(char *hex, char *buf, size_t hexLen)
  340 +{
  341 + size_t i, j = 0;
  342 + uint8_t byte;
  343 + /*if( (hexLen%2) != 0){
349 return -1; 344 return -1;
350 - } 345 + }*
351 346
352 for (i = 0; i < hexLen; i += 2) { 347 for (i = 0; i < hexLen; i += 2) {
353 *opos++ = (ipos[i] << 4) | (ipos[i + 1]); 348 *opos++ = (ipos[i] << 4) | (ipos[i + 1]);
  349 + }*/
  350 +
  351 + for(i = 0; i < strlen(hex); i += 2)
  352 + {
  353 + byte = (hex[i]-48)*16 + (hex[i+1]-48);
  354 + buf[j++] = byte;
354 } 355 }
355 -  
356 return 0; 356 return 0;
357 } 357 }
358 358
Project/applications/smartcities/i2c.c
1 #include "i2c.h" 1 #include "i2c.h"
  2 +#include "json.h"
2 3
3 void I2C_init(void) 4 void I2C_init(void)
4 { 5 {
  6 + printf("Initializing I2C...\r\n");
5 GPIO_InitTypeDef GPIO_InitStruct; 7 GPIO_InitTypeDef GPIO_InitStruct;
6 I2C_InitTypeDef I2C_InitStruct; 8 I2C_InitTypeDef I2C_InitStruct;
7 9
@@ -38,6 +40,7 @@ void I2C_init(void) @@ -38,6 +40,7 @@ void I2C_init(void)
38 40
39 void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction) 41 void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction)
40 { 42 {
  43 + printf("Sending I2C Start...\r\n");
41 while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY)) 44 while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY))
42 { 45 {
43 // wait until I2C1 is not busy any more 46 // wait until I2C1 is not busy any more
@@ -66,6 +69,7 @@ void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction) @@ -66,6 +69,7 @@ void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction)
66 69
67 void I2C_write(I2C_TypeDef* I2Cx, uint8_t data) 70 void I2C_write(I2C_TypeDef* I2Cx, uint8_t data)
68 { 71 {
  72 + printf("Sending I2C byte %x...\r\n",data);
69 /* WARNING 73 /* WARNING
70 * 74 *
71 * OJITO QUE ESTร ESPERANDO EL EV8 Y NO EL EV8_2 75 * OJITO QUE ESTร ESPERANDO EL EV8 Y NO EL EV8_2
@@ -81,6 +85,7 @@ void I2C_write(I2C_TypeDef* I2Cx, uint8_t data) @@ -81,6 +85,7 @@ void I2C_write(I2C_TypeDef* I2Cx, uint8_t data)
81 85
82 uint8_t I2C_read_ack(I2C_TypeDef* I2Cx) 86 uint8_t I2C_read_ack(I2C_TypeDef* I2Cx)
83 { 87 {
  88 + printf("Listening for byte in I2C (ACK)\r\n");
84 I2C_AcknowledgeConfig(I2Cx, ENABLE); 89 I2C_AcknowledgeConfig(I2Cx, ENABLE);
85 while( !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED)) 90 while( !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED))
86 { 91 {
@@ -91,8 +96,9 @@ uint8_t I2C_read_ack(I2C_TypeDef* I2Cx) @@ -91,8 +96,9 @@ uint8_t I2C_read_ack(I2C_TypeDef* I2Cx)
91 96
92 uint8_t I2C_read_nack(I2C_TypeDef* I2Cx) 97 uint8_t I2C_read_nack(I2C_TypeDef* I2Cx)
93 { 98 {
  99 + printf("Listening for byte in I2C (NACK)\r\n");
94 I2C_AcknowledgeConfig(I2Cx, DISABLE); 100 I2C_AcknowledgeConfig(I2Cx, DISABLE);
95 - I2C_GenerateSTOP(I2Cx, ENABLE); 101 + //I2C_GenerateSTOP(I2Cx, ENABLE);
96 while( !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED)) 102 while( !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED))
97 { 103 {
98 // wait until one byte has been received 104 // wait until one byte has been received
@@ -102,8 +108,9 @@ uint8_t I2C_read_nack(I2C_TypeDef* I2Cx) @@ -102,8 +108,9 @@ uint8_t I2C_read_nack(I2C_TypeDef* I2Cx)
102 108
103 void I2C_stop(I2C_TypeDef* I2Cx) 109 void I2C_stop(I2C_TypeDef* I2Cx)
104 { 110 {
  111 + printf("Sending I2C Stop...\r\n");
105 I2C_GenerateSTOP(I2Cx, ENABLE); 112 I2C_GenerateSTOP(I2Cx, ENABLE);
106 - while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) 113 + while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTING))
107 { 114 {
108 // wait for I2C1 EV8_2 --> byte has been transmitted 115 // wait for I2C1 EV8_2 --> byte has been transmitted
109 } 116 }
@@ -122,7 +129,7 @@ uint8_t I2C_check(I2C_TypeDef* I2Cx, uint8_t address) @@ -122,7 +129,7 @@ uint8_t I2C_check(I2C_TypeDef* I2Cx, uint8_t address)
122 } 129 }
123 printf("Probing address %x ...\r\n",address); 130 printf("Probing address %x ...\r\n",address);
124 I2C_Send7bitAddress(I2Cx, address, I2C_Direction_Transmitter); 131 I2C_Send7bitAddress(I2Cx, address, I2C_Direction_Transmitter);
125 - chThdSleepMilliseconds(SCAN_TIMEOUT); 132 + chThdSleepMilliseconds(I2C_TIMEOUT);
126 if(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) 133 if(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
127 { 134 {
128 return 1; 135 return 1;
@@ -133,21 +140,29 @@ uint8_t I2C_check(I2C_TypeDef* I2Cx, uint8_t address) @@ -133,21 +140,29 @@ uint8_t I2C_check(I2C_TypeDef* I2Cx, uint8_t address)
133 return 0; 140 return 0;
134 } 141 }
135 142
  143 +void I2C_reset()
  144 +{
  145 + printf("Resetting I2C...\r\n");
  146 + I2C_SoftwareResetCmd(I2C1, ENABLE);
  147 + I2C_DeInit(I2C1);
  148 + I2C_init();
  149 +
  150 +}
  151 +
136 void I2C_scan(uint8_t *addresses) 152 void I2C_scan(uint8_t *addresses)
137 { 153 {
138 - uint8_t sensors[TOTAL_SENSORS] = {DISTANCE_ADDR,LIGHT_ADDR,PRESSURE_ADDR,HUMIDITY_TEMP_ADDR};  
139 uint8_t i, j = 0; 154 uint8_t i, j = 0;
  155 + I2C_init();
140 for(i = 0; i < TOTAL_SENSORS; i++) 156 for(i = 0; i < TOTAL_SENSORS; i++)
141 { 157 {
142 - if(!I2C_check(I2C1,sensors[i] << 1)) 158 + if(!I2C_check(I2C1,sensors[i]->ID << 1))
143 { 159 {
144 - addresses[j++] = sensors[i]; 160 + addresses[j++] = sensors[i]->ID;
  161 + register_sensor(*sensors[i]);
145 } 162 }
146 else 163 else
147 { 164 {
148 - I2C_SoftwareResetCmd(I2C1, ENABLE);  
149 - I2C_DeInit(I2C1);  
150 - I2C_init(); 165 + I2C_reset();
151 } 166 }
152 } 167 }
153 } 168 }
154 \ No newline at end of file 169 \ No newline at end of file
Project/applications/smartcities/include/i2c.h
@@ -7,7 +7,7 @@ @@ -7,7 +7,7 @@
7 #include "sensors.h" 7 #include "sensors.h"
8 #include "libwismart.h" 8 #include "libwismart.h"
9 9
10 -#define SCAN_TIMEOUT 100 10 +#define I2C_TIMEOUT 100
11 11
12 void I2C_init(void); 12 void I2C_init(void);
13 void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction); 13 void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction);
@@ -17,6 +17,7 @@ uint8_t I2C_read_ack(I2C_TypeDef* I2Cx); @@ -17,6 +17,7 @@ uint8_t I2C_read_ack(I2C_TypeDef* I2Cx);
17 uint8_t I2C_read_nack(I2C_TypeDef* I2Cx); 17 uint8_t I2C_read_nack(I2C_TypeDef* I2Cx);
18 void I2C_scan(uint8_t *addresses); 18 void I2C_scan(uint8_t *addresses);
19 uint8_t I2C_check(I2C_TypeDef* I2Cx, uint8_t address); 19 uint8_t I2C_check(I2C_TypeDef* I2Cx, uint8_t address);
  20 +void I2C_reset(void);
20 21
21 22
22 #endif 23 #endif
Project/applications/smartcities/include/sensors.h
@@ -13,7 +13,8 @@ @@ -13,7 +13,8 @@
13 #define PRESSURE_ADDR 0x77 //Cambiar a MPL115A 13 #define PRESSURE_ADDR 0x77 //Cambiar a MPL115A
14 #define HUMIDITY_TEMP_ADDR 0x27 14 #define HUMIDITY_TEMP_ADDR 0x27
15 #define SOUND_ADDR 0x72 15 #define SOUND_ADDR 0x72
16 -//REMEMBER TO UPDATE I2C_scan ROUTINE WITH EACH NEW SENSOR!!!! 16 +//REMEMBER TO UPDATE collectData ROUTINE WITH EACH NEW SENSOR!!!!
  17 +//REMEMBER TO UPDATE I2C sensors in sensor definitions in this header and sensors.c WITH EACH NEW SENSOR!!!!
17 #define TOTAL_SENSORS 5 18 #define TOTAL_SENSORS 5
18 19
19 #define BATTERY_ADDR 0x00 //SIEMPRE PRESENTE, NUNCA BUSCAR EN i2c_scan 20 #define BATTERY_ADDR 0x00 //SIEMPRE PRESENTE, NUNCA BUSCAR EN i2c_scan
@@ -26,7 +27,7 @@ typedef struct { @@ -26,7 +27,7 @@ typedef struct {
26 char* additional_info; 27 char* additional_info;
27 } sensor; 28 } sensor;
28 29
29 -void wakeup(uint8_t logic_address); 30 +void wakeup_sensors(uint8_t logic_address);
30 31
31 //SENSOR DEFINITIONS 32 //SENSOR DEFINITIONS
32 extern sensor light_sensor; 33 extern sensor light_sensor;
@@ -36,6 +37,8 @@ extern sensor humidity_temp_sensor; @@ -36,6 +37,8 @@ extern sensor humidity_temp_sensor;
36 extern sensor sound_sensor; 37 extern sensor sound_sensor;
37 extern sensor battery; 38 extern sensor battery;
38 39
  40 +extern sensor* sensors[TOTAL_SENSORS+1];
  41 +
39 //SENSOR FUNCTIONS 42 //SENSOR FUNCTIONS
40 43
41 //LIGHT SENSOR 44 //LIGHT SENSOR
Project/applications/smartcities/json.c
@@ -2,19 +2,26 @@ @@ -2,19 +2,26 @@
2 2
3 uint8_t register_sensor(sensor sens) 3 uint8_t register_sensor(sensor sens)
4 { 4 {
5 - uint8_t result;  
6 - if(sens.ID == PRESSURE_ADDR)  
7 - {  
8 - sens.additional_info = callibration_pressure_data_csv(get_pressure_callibration_data());  
9 - }  
10 - char *statement = prepare_json_register_statement(mod,sens);  
11 - chHeapFree(sens.additional_info);  
12 - sens.additional_info = NULL; 5 + printf("Registering sensor: %s\r\n",sens.description);
  6 + uint8_t result;
  7 + char *statement;
  8 + if(sens.ID == PRESSURE_ADDR)
  9 + {
  10 + sens.additional_info = callibration_pressure_data_csv(get_pressure_callibration_data());
  11 + statement = prepare_json_register_statement(mod,sens);
  12 + chHeapFree(sens.additional_info);
  13 + sens.additional_info = NULL;
  14 + }
  15 + else
  16 + {
  17 + statement = prepare_json_register_statement(mod,sens);
  18 + }
  19 + printf("%s\r\n",statement);
13 char sensor_ID[3]; 20 char sensor_ID[3];
14 sprintf(sensor_ID,"%d",sens.ID); 21 sprintf(sensor_ID,"%d",sens.ID);
15 - result = send_json(statement,strlen(statement),mod.ID,sensor_ID);  
16 - chHeapFree(statement);  
17 - return result; 22 + result = send_json(statement,strlen(statement),mod.ID,sensor_ID);
  23 + chHeapFree(statement);
  24 + return result;
18 } 25 }
19 26
20 char* prepare_json_observation_statement(char** data, uint32_t nObservations) 27 char* prepare_json_observation_statement(char** data, uint32_t nObservations)
@@ -39,6 +46,7 @@ char* prepare_json_observation_statement(char** data, uint32_t nObservations) @@ -39,6 +46,7 @@ char* prepare_json_observation_statement(char** data, uint32_t nObservations)
39 46
40 char* prepare_json_register_statement(module mod, sensor sens) 47 char* prepare_json_register_statement(module mod, sensor sens)
41 { 48 {
  49 + printf("Registering sensor: %s in: %s\r\n",sens.description,mod.geoloc);
42 unsigned int length; 50 unsigned int length;
43 char *json_statement, *str_aux, *str_aux2; 51 char *json_statement, *str_aux, *str_aux2;
44 char ID[MODULE_ID_LENGTH+3]; 52 char ID[MODULE_ID_LENGTH+3];
@@ -85,13 +93,6 @@ char* prepare_json_register_statement(module mod, sensor sens) @@ -85,13 +93,6 @@ char* prepare_json_register_statement(module mod, sensor sens)
85 char* prepare_observation(char* observation, uint32_t length) 93 char* prepare_observation(char* observation, uint32_t length)
86 { 94 {
87 char *json_data, *str_aux, *str_aux2; 95 char *json_data, *str_aux, *str_aux2;
88 - /* TOPKEK  
89 - *  
90 - * ยฟGestiรณn de errores?  
91 - * ยฟPA QUร‰?  
92 - *  
93 - * --le trole  
94 - */  
95 int value_length = find_next_index(observation,length,','); //EXPECTS FORMATTING!!! 96 int value_length = find_next_index(observation,length,','); //EXPECTS FORMATTING!!!
96 int timestamp_length = length - (value_length + 1); 97 int timestamp_length = length - (value_length + 1);
97 char *value = (char*) chHeapAlloc(NULL,value_length+1); 98 char *value = (char*) chHeapAlloc(NULL,value_length+1);
Project/applications/smartcities/main2.c
@@ -70,6 +70,7 @@ void init_registry(void) @@ -70,6 +70,7 @@ void init_registry(void)
70 printf("Geo Localization = %s\r\n", config.localization); 70 printf("Geo Localization = %s\r\n", config.localization);
71 71
72 strcpy(mod.geoloc,config.localization); 72 strcpy(mod.geoloc,config.localization);
  73 + printf("%s\r\n", mod.geoloc);
73 if(config.security == PROFILE_SECURITY_OPEN) 74 if(config.security == PROFILE_SECURITY_OPEN)
74 { 75 {
75 printf("open detected\r\n"); 76 printf("open detected\r\n");
@@ -98,6 +99,7 @@ void init_registry(void) @@ -98,6 +99,7 @@ void init_registry(void)
98 printf("wep detected\r\n"); 99 printf("wep detected\r\n");
99 //Is WEP 100 //Is WEP
100 libwismart_WiFiConnect(config.ssid,config.wepkey,wifi_connect_result_cb); 101 libwismart_WiFiConnect(config.ssid,config.wepkey,wifi_connect_result_cb);
  102 + libwismart_WiFiSetWep(config.wepkey,1);
101 } 103 }
102 while(connected == 0 && timeout != 1 ) 104 while(connected == 0 && timeout != 1 )
103 {chThdSleepMilliseconds(500);} 105 {chThdSleepMilliseconds(500);}
@@ -201,16 +203,21 @@ int main(void) @@ -201,16 +203,21 @@ int main(void)
201 203
202 char* valueSensors[TOTAL_SENSORS]; 204 char* valueSensors[TOTAL_SENSORS];
203 memset (valueSensors, 0, TOTAL_SENSORS); 205 memset (valueSensors, 0, TOTAL_SENSORS);
  206 +
  207 + printf("Connecting to wifi...\r\n");
  208 + wifi_connect();
  209 +
  210 + wakeup_sensors(0x15);
204 printf("Scanning sensors...\r\n"); 211 printf("Scanning sensors...\r\n");
205 - //I2C_scan(sensors);  
206 - sensors[0] = LIGHT_ADDR; 212 + //Escanea y registra
  213 + I2C_scan(sensors);
207 sensors_length=strlen((char*)sensors); 214 sensors_length=strlen((char*)sensors);
208 printf("%d sensor detected...\r\n",sensors_length); 215 printf("%d sensor detected...\r\n",sensors_length);
209 - //registrar sensores  
210 - //Esta funcion esta mas arriba para no repetir cรณdigo luego -- Imanol  
211 - printf("Connecting to wifi...\r\n");  
212 - wifi_connect();  
213 - //unsigned int time = getNTPTime(); 216 +
  217 + I2C_init();
  218 + printf("%s\r\n",light_value(get_light_data()));
  219 + printf("%s\r\n",light_value(get_light_data()));
  220 +
214 unsigned long time; 221 unsigned long time;
215 update_time(&time); 222 update_time(&time);
216 //desconectarwifi 223 //desconectarwifi
@@ -219,13 +226,11 @@ int main(void) @@ -219,13 +226,11 @@ int main(void)
219 226
220 unsigned long timestamp = 0; 227 unsigned long timestamp = 0;
221 unsigned long delay = getSystemTime(); 228 unsigned long delay = getSystemTime();
222 - unsigned long delay2 = 0;  
223 sleep_thread(SHORT_PERIOD - time%SHORT_PERIOD); 229 sleep_thread(SHORT_PERIOD - time%SHORT_PERIOD);
224 230
225 uint32_t ind[TOTAL_SENSORS]={0}; 231 uint32_t ind[TOTAL_SENSORS]={0};
226 char** buffers[TOTAL_SENSORS]; 232 char** buffers[TOTAL_SENSORS];
227 uint32_t sizes[TOTAL_SENSORS]={0}; 233 uint32_t sizes[TOTAL_SENSORS]={0};
228 - //char* cooked_data[TOTAL_SENSORS];  
229 int i = 1; 234 int i = 1;
230 235
231 while(1){ 236 while(1){
@@ -234,29 +239,15 @@ int main(void) @@ -234,29 +239,15 @@ int main(void)
234 239
235 delay = getSystemTime(); 240 delay = getSystemTime();
236 /* Collect data from sensors */ 241 /* Collect data from sensors */
237 - //collect_data(/*a_rawData*/);  
238 printf("Collecting data...\r\n"); 242 printf("Collecting data...\r\n");
239 - //collectData(valueSensors, sensors);  
240 - valueSensors[0] = chHeapAlloc(NULL,6);  
241 - strcpy(valueSensors[0],"123456"); 243 + collectData(valueSensors, sensors);
242 printf("Data collected...\r\n"); 244 printf("Data collected...\r\n");
243 245
244 time += getElapsedTime(delay); 246 time += getElapsedTime(delay);
245 printf("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); 247 printf("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);
246 -  
247 - //delay = getSystemTime();  
248 - //cada mitja hora  
249 -  
250 - //delay2 = getElapsedTime(delay);  
251 -  
252 - //delay = getSystemTime();  
253 - //timestamp = time - delay2;  
254 timestamp = time; 248 timestamp = time;
255 printf("timestamp (absolute):\t%ul\r\ntimestamp mod LONG_PERIOD:\t%ul\r\ntimestamp mod SHORT_PERIOD:\t%ul\r\n",timestamp,timestamp%LONG_PERIOD,timestamp%SHORT_PERIOD); 249 printf("timestamp (absolute):\t%ul\r\ntimestamp mod LONG_PERIOD:\t%ul\r\ntimestamp mod SHORT_PERIOD:\t%ul\r\n",timestamp,timestamp%LONG_PERIOD,timestamp%SHORT_PERIOD);
256 delay = getSystemTime(); 250 delay = getSystemTime();
257 - /* Add data to the buffer with timestamp*/  
258 -  
259 - //a_cookedData = format_data(a_rawData, timestamp);  
260 printf("Timestamping data...\r\n"); 251 printf("Timestamping data...\r\n");
261 char** values=timestamp_datas(valueSensors,timestamp,sensors); 252 char** values=timestamp_datas(valueSensors,timestamp,sensors);
262 printf("Putting data in buffer...\r\n"); 253 printf("Putting data in buffer...\r\n");
@@ -292,7 +283,6 @@ int main(void) @@ -292,7 +283,6 @@ int main(void)
292 else if(res==HARD_REACHED){ 283 else if(res==HARD_REACHED){
293 printf("--------------hard limit-------------\r\n"); 284 printf("--------------hard limit-------------\r\n");
294 wifi_connect(); 285 wifi_connect();
295 - // fem servir 085 de prova, perรฒ haurem de fer servir sensor_id[0]  
296 char id_0[3]; 286 char id_0[3];
297 sprintf(id_0,"%x",sensors[0]); 287 sprintf(id_0,"%x",sensors[0]);
298 while( send(buffers[0],&ind[0],&sizes[0], mod.ID, id_0) != JSON_POST_OK ) 288 while( send(buffers[0],&ind[0],&sizes[0], mod.ID, id_0) != JSON_POST_OK )
@@ -309,7 +299,6 @@ int main(void) @@ -309,7 +299,6 @@ int main(void)
309 { 299 {
310 300
311 printf(" enviant buffer %d\r\n",j); 301 printf(" enviant buffer %d\r\n",j);
312 - // fem servir 085 de prova, perรฒ haurem de fer servir sensor_id[j]  
313 char id[3]; 302 char id[3];
314 sprintf(id,"%x",sensors[j]); 303 sprintf(id,"%x",sensors[j]);
315 int ok=send(buffers[j],&ind[j],&sizes[j], mod.ID, id); 304 int ok=send(buffers[j],&ind[j],&sizes[j], mod.ID, id);
Project/applications/smartcities/sensors.c
@@ -7,7 +7,9 @@ sensor humidity_temp_sensor = {HUMIDITY_TEMP_ADDR, &quot;Humidity and Temperature&quot;,&quot;ร‚ @@ -7,7 +7,9 @@ sensor humidity_temp_sensor = {HUMIDITY_TEMP_ADDR, &quot;Humidity and Temperature&quot;,&quot;ร‚
7 sensor sound_sensor = {SOUND_ADDR, "Sound sensor","mV",NULL}; 7 sensor sound_sensor = {SOUND_ADDR, "Sound sensor","mV",NULL};
8 sensor battery = {BATTERY_ADDR, "Battery Level","mV",NULL}; 8 sensor battery = {BATTERY_ADDR, "Battery Level","mV",NULL};
9 9
10 -void wakeup(uint8_t logic_address) 10 +sensor* sensors[TOTAL_SENSORS+1] = {&light_sensor,&ultrasound_sensor,&pressure_sensor,&humidity_temp_sensor,&sound_sensor,&battery};
  11 +
  12 +void wakeup_sensors(uint8_t logic_address)
11 { 13 {
12 /* Aquรƒยญ habrรƒยญa que implementar un control 14 /* Aquรƒยญ habrรƒยญa que implementar un control
13 * inteligente de la alimentaciรƒยณn de los 15 * inteligente de la alimentaciรƒยณn de los
@@ -78,9 +80,10 @@ uint32_t get_light_data(void) @@ -78,9 +80,10 @@ uint32_t get_light_data(void)
78 uint32_t data = 0; 80 uint32_t data = 0;
79 81
80 data = get_light_ch1(); 82 data = get_light_ch1();
  83 + printf("LIGHT: Got ch1\r\n");
81 data = data << 16; 84 data = data << 16;
82 data = data | get_light_ch0(); 85 data = data | get_light_ch0();
83 - 86 + printf("LIGHT: Got ch0\r\n");
84 return data; 87 return data;
85 } 88 }
86 uint16_t get_light_ch0(void) 89 uint16_t get_light_ch0(void)
@@ -88,21 +91,17 @@ uint16_t get_light_ch0(void) @@ -88,21 +91,17 @@ uint16_t get_light_ch0(void)
88 uint16_t data = 0; 91 uint16_t data = 0;
89 92
90 I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter); 93 I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter);
91 - I2C_write(I2C1, 0x0D);  
92 - I2C_stop(I2C1);  
93 -  
94 - I2C_start(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver);  
95 - data = I2C_read_ack(I2C1);  
96 - data = data << 8;  
97 -  
98 - I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter);  
99 I2C_write(I2C1, 0x0C); 94 I2C_write(I2C1, 0x0C);
100 I2C_stop(I2C1); 95 I2C_stop(I2C1);
  96 + printf("LIGHT: Sent fetch command for low byte for CH1\r\n");
101 97
102 I2C_start(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver); 98 I2C_start(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver);
103 - data = data | I2C_read_ack(I2C1);  
104 - I2C_stop(I2C1);  
105 - 99 + data = I2C_read_ack(I2C1);
  100 + printf("LIGHT: Got low byte for CH1\r\n");
  101 + data = data | (I2C_read_ack(I2C1) << 8);
  102 + printf("LIGHT: Got high byte for CH1\r\n");
  103 + I2C_stop(I2C1);
  104 +
106 return data; 105 return data;
107 } 106 }
108 uint16_t get_light_ch1(void) 107 uint16_t get_light_ch1(void)
@@ -110,30 +109,29 @@ uint16_t get_light_ch1(void) @@ -110,30 +109,29 @@ uint16_t get_light_ch1(void)
110 uint16_t data = 0; 109 uint16_t data = 0;
111 110
112 I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter); 111 I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter);
113 - I2C_write(I2C1, 0x0F);  
114 - I2C_stop(I2C1);  
115 -  
116 - I2C_start(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver);  
117 - data = I2C_read_ack(I2C1);  
118 - data = data << 8;  
119 -  
120 - I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter);  
121 I2C_write(I2C1, 0x0E); 112 I2C_write(I2C1, 0x0E);
122 I2C_stop(I2C1); 113 I2C_stop(I2C1);
  114 + printf("LIGHT: Sent fetch command for low byte for CH1\r\n");
123 115
124 I2C_start(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver); 116 I2C_start(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver);
125 - data = data | I2C_read_ack(I2C1); 117 + data = I2C_read_ack(I2C1);
  118 + printf("LIGHT: Got low byte for CH1\r\n");
  119 + data = data | (I2C_read_ack(I2C1) << 8);
  120 + printf("LIGHT: Got high byte for CH1\r\n");
126 I2C_stop(I2C1); 121 I2C_stop(I2C1);
127 - 122 +
128 return data; 123 return data;
129 } 124 }
130 125
131 uint16_t get_distance_data(void) 126 uint16_t get_distance_data(void)
132 { 127 {
133 init_ultrasound(); 128 init_ultrasound();
  129 + printf("DISTANCE: Initialized\r\n");
134 uint16_t data = get_distance_high(); 130 uint16_t data = get_distance_high();
  131 + printf("Got distance high byte\r\n");
135 data = data << 8; 132 data = data << 8;
136 data = data | get_distance_low(); 133 data = data | get_distance_low();
  134 + printf("Got distance low byte\r\n");
137 return data; 135 return data;
138 } 136 }
139 void init_ultrasound(void) 137 void init_ultrasound(void)
@@ -177,15 +175,17 @@ uint16_t get_pressure_data(void) @@ -177,15 +175,17 @@ uint16_t get_pressure_data(void)
177 { 175 {
178 uint16_t data; 176 uint16_t data;
179 init_pressure(); 177 init_pressure();
180 - 178 + printf("PRESSURE: Initialized\r\n");
181 I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter); 179 I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter);
182 I2C_write(I2C1, 0xF6); 180 I2C_write(I2C1, 0xF6);
183 I2C_stop(I2C1); 181 I2C_stop(I2C1);
184 182
185 I2C_start(I2C1, DISTANCE_ADDR << 1, I2C_Direction_Receiver); 183 I2C_start(I2C1, DISTANCE_ADDR << 1, I2C_Direction_Receiver);
186 data = I2C_read_ack(I2C1); 184 data = I2C_read_ack(I2C1);
  185 + printf("PRESSURE: Got high byte\r\n");
187 data = data << 8; 186 data = data << 8;
188 data = data | I2C_read_nack(I2C1); 187 data = data | I2C_read_nack(I2C1);
  188 + printf("PRESSURE: Got low byte\r\n");
189 return data; 189 return data;
190 } 190 }
191 191
@@ -193,20 +193,23 @@ uint16_t get_pressure_temperature_data(void) @@ -193,20 +193,23 @@ uint16_t get_pressure_temperature_data(void)
193 { 193 {
194 uint16_t data; 194 uint16_t data;
195 init_pressure_temperature(); 195 init_pressure_temperature();
196 - 196 + printf("PRESSURE: Initialized temperature\r\n");
197 I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter); 197 I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter);
198 I2C_write(I2C1, 0xF6); 198 I2C_write(I2C1, 0xF6);
199 I2C_stop(I2C1); 199 I2C_stop(I2C1);
200 200
201 I2C_start(I2C1, DISTANCE_ADDR << 1, I2C_Direction_Receiver); 201 I2C_start(I2C1, DISTANCE_ADDR << 1, I2C_Direction_Receiver);
202 data = I2C_read_ack(I2C1); 202 data = I2C_read_ack(I2C1);
  203 + printf("PRESSURE: Got temperature high byte\r\n");
203 data = data << 8; 204 data = data << 8;
204 data = data | I2C_read_nack(I2C1); 205 data = data | I2C_read_nack(I2C1);
  206 + printf("PRESSURE: Got temperature low byte\r\n");
205 return data; 207 return data;
206 } 208 }
207 209
208 bmp085_callibration get_pressure_callibration_data(void) 210 bmp085_callibration get_pressure_callibration_data(void)
209 { 211 {
  212 + printf("PRESSURE: Fetching callibration data\r\n");
210 bmp085_callibration calib_data; 213 bmp085_callibration calib_data;
211 214
212 I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); 215 I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
@@ -285,13 +288,13 @@ bmp085_callibration get_pressure_callibration_data(void) @@ -285,13 +288,13 @@ bmp085_callibration get_pressure_callibration_data(void)
285 I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); 288 I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
286 calib_data.MD = I2C_read_ack(I2C1); 289 calib_data.MD = I2C_read_ack(I2C1);
287 I2C_stop(I2C1); 290 I2C_stop(I2C1);
288 - 291 + printf("PRESSURE: Got callibration data\r\n");
289 return calib_data; 292 return calib_data;
290 } 293 }
291 char* callibration_pressure_data_csv(bmp085_callibration parameters) 294 char* callibration_pressure_data_csv(bmp085_callibration parameters)
292 { 295 {
293 - char *str = chHeapAlloc(NULL,sizeof(char)*(4*11-1));  
294 - memset(str,0x00,sizeof(char)*(4*11-1)); 296 + char *str = chHeapAlloc(NULL,sizeof(char)*(11*11-1));
  297 + memset(str,0x00,sizeof(char)*(11*11-1));
295 sprintf(str + strlen(str),"%d,",parameters.AC1); 298 sprintf(str + strlen(str),"%d,",parameters.AC1);
296 sprintf(str + strlen(str),"%d,",parameters.AC2); 299 sprintf(str + strlen(str),"%d,",parameters.AC2);
297 sprintf(str + strlen(str),"%d,",parameters.AC3); 300 sprintf(str + strlen(str),"%d,",parameters.AC3);
@@ -334,24 +337,32 @@ void init_humidity_temp(void) @@ -334,24 +337,32 @@ void init_humidity_temp(void)
334 uint16_t get_humidity_data(void) 337 uint16_t get_humidity_data(void)
335 { 338 {
336 init_humidity_temp(); 339 init_humidity_temp();
  340 + printf("HUMIDITY_TEMP: Initialized humidity\r\n");
337 uint16_t data = 0; 341 uint16_t data = 0;
338 I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Receiver); 342 I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Receiver);
339 data = I2C_read_ack(I2C1) & 0x3F; 343 data = I2C_read_ack(I2C1) & 0x3F;
  344 + printf("HUMIDITY_TEMP: Got humidity high byte\r\n");
340 data = data << 8; 345 data = data << 8;
341 data |= I2C_read_nack(I2C1); 346 data |= I2C_read_nack(I2C1);
  347 + printf("HUMIDITY_TEMP: Got humidity low byte\r\n");
342 I2C_stop(I2C1); 348 I2C_stop(I2C1);
343 return data; 349 return data;
344 } 350 }
345 uint16_t get_temperature_data(void) 351 uint16_t get_temperature_data(void)
346 { 352 {
347 init_humidity_temp(); 353 init_humidity_temp();
  354 + printf("HUMIDITY_TEMP: Initialized temperature\r\n");
348 uint16_t data = 0; 355 uint16_t data = 0;
349 I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Receiver); 356 I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Receiver);
350 I2C_read_ack(I2C1); 357 I2C_read_ack(I2C1);
  358 + printf("HUMIDITY_TEMP: Discarded first humidity byte\r\n");
351 I2C_read_ack(I2C1); 359 I2C_read_ack(I2C1);
  360 + printf("HUMIDITY_TEMP: Discarded second humidity byte\r\n");
352 data = I2C_read_ack(I2C1); 361 data = I2C_read_ack(I2C1);
  362 + printf("HUMIDITY_TEMP: Got temperature high byte\r\n");
353 data = data << 8; 363 data = data << 8;
354 data |= I2C_read_nack(I2C1) & 0xFC ; 364 data |= I2C_read_nack(I2C1) & 0xFC ;
  365 + printf("HUMIDITY_TEMP: Got temperature low byte\r\n");
355 data = data >> 2; 366 data = data >> 2;
356 I2C_stop(I2C1); 367 I2C_stop(I2C1);
357 return data; 368 return data;
@@ -401,28 +412,42 @@ char* sound_value(uint32_t sound) @@ -401,28 +412,42 @@ char* sound_value(uint32_t sound)
401 412
402 void collectData(char* valueSensors[], uint8_t* sensors){ 413 void collectData(char* valueSensors[], uint8_t* sensors){
403 uint8_t i; 414 uint8_t i;
404 - for(i=0;i<strlen((char*)sensors);i++){ 415 + for(i=0;i<strlen((char*)sensors);i++)
  416 + {
405 417
406 if(valueSensors[i]!=NULL) 418 if(valueSensors[i]!=NULL)
  419 + {
  420 + printf("Freeing memory from previous entry\r\n");
407 chHeapFree(valueSensors[i]); 421 chHeapFree(valueSensors[i]);
  422 + }
408 423
409 - else if(sensors[i]==LIGHT_ADDR){ 424 + if(sensors[i]==LIGHT_ADDR)
  425 + {
  426 + printf("Fetching data from light sensor\r\n");
410 valueSensors[i]=light_value(get_light_data()); 427 valueSensors[i]=light_value(get_light_data());
411 continue; 428 continue;
412 } 429 }
413 - else if(sensors[i]==DISTANCE_ADDR){ 430 + else if(sensors[i]==DISTANCE_ADDR)
  431 + {
  432 + printf("Fetching data from distance sensor\r\n");
414 valueSensors[i]=distance_value(get_distance_data()); 433 valueSensors[i]=distance_value(get_distance_data());
415 continue; 434 continue;
416 } 435 }
417 - else if(sensors[i]==SOUND_ADDR){ 436 + else if(sensors[i]==SOUND_ADDR)
  437 + {
  438 + printf("Fetching data from sound sensor\r\n");
418 valueSensors[i]=sound_value(get_sound_data()); 439 valueSensors[i]=sound_value(get_sound_data());
419 continue; 440 continue;
420 } 441 }
421 - else if(sensors[i]==PRESSURE_ADDR){ 442 + else if(sensors[i]==PRESSURE_ADDR)
  443 + {
  444 + printf("Fetching data from pressure sensor\r\n");
422 valueSensors[i]=pressure_value(get_pressure_data(), get_pressure_temperature_data()); 445 valueSensors[i]=pressure_value(get_pressure_data(), get_pressure_temperature_data());
423 continue; 446 continue;
424 } 447 }
425 - else if(sensors[i]==HUMIDITY_TEMP_ADDR){ 448 + else if(sensors[i]==HUMIDITY_TEMP_ADDR)
  449 + {
  450 + printf("Fetching data from humidity and temperature sensor\r\n");
426 valueSensors[i]=temp_humidity_value(get_temperature_data(),get_humidity_data()); 451 valueSensors[i]=temp_humidity_value(get_temperature_data(),get_humidity_data());
427 continue; 452 continue;
428 } 453 }