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