Commit 737124c7b279634d86422ea3e121de5d7d5ba5fb

Authored by Imanol-Mikel Barba Sabariego
1 parent 79b57a0c

--no commit message

Project/applications/smartcities/include/timer-loop.h
@@ -6,8 +6,10 @@ @@ -6,8 +6,10 @@
6 6
7 #define HALF_HOUR 60*30 // time in seconds 7 #define HALF_HOUR 60*30 // time in seconds
8 #define FIVE_MIN 60*5 // time in seconds 8 #define FIVE_MIN 60*5 // time in seconds
9 -#define LONG_PERIOD HALF_HOUR //60*3 // for testing  
10 -#define SHORT_PERIOD FIVE_MIN //60*1 // for testing 9 +//#define LONG_PERIOD HALF_HOUR //60*3 // for testing
  10 +//#define SHORT_PERIOD FIVE_MIN //60*1 // for testing
  11 +#define LONG_PERIOD 60*3 //60*3 // for testing
  12 +#define SHORT_PERIOD 60*1 //60*1 // for testing
11 13
12 unsigned long getSystemTime(void); 14 unsigned long getSystemTime(void);
13 15
Project/applications/smartcities/main2.c
@@ -115,7 +115,8 @@ void send_data(char** buffers[], uint32_t ind[], uint32_t sizes[], uint8_t senso @@ -115,7 +115,8 @@ void send_data(char** buffers[], uint32_t ind[], uint32_t sizes[], uint8_t senso
115 115
116 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){
117 int i; 117 int i;
118 - for(i=0;i<sensors_length;i++){ 118 + for(i=0;i<sensors_length;i++){
  119 + printf("For %d and %d\n\r", ind[i],sizes[i]);
119 put_message(cooked[i], buffers[i] ,&ind[i],&sizes[i]); 120 put_message(cooked[i], buffers[i] ,&ind[i],&sizes[i]);
120 chHeapFree(cooked[i]); 121 chHeapFree(cooked[i]);
121 } 122 }
@@ -124,14 +125,18 @@ void put_buffers(char** buffers[],uint32_t ind[],uint32_t sizes[],char** cooked, @@ -124,14 +125,18 @@ void put_buffers(char** buffers[],uint32_t ind[],uint32_t sizes[],char** cooked,
124 char** timestamp_datas(char* value[],unsigned long timestamp, uint8_t* sensors){ 125 char** timestamp_datas(char* value[],unsigned long timestamp, uint8_t* sensors){
125 char** cooked_data; 126 char** cooked_data;
126 cooked_data = (char**) chHeapAlloc(NULL,sensors_length * sizeof(char*)); 127 cooked_data = (char**) chHeapAlloc(NULL,sensors_length * sizeof(char*));
  128 + printf("Data allocated...\r\n");
127 Date t=getDate(timestamp); 129 Date t=getDate(timestamp);
128 int i; 130 int i;
129 for(i=0;i<sensors_length;i++){ 131 for(i=0;i<sensors_length;i++){
130 - 132 + printf("Calling timestamp_data...\r\n");
131 cooked_data[i]=timestamp_data(value[i], t); 133 cooked_data[i]=timestamp_data(value[i], t);
  134 + printf("Finished timestamp_data...\r\n");
  135 + printf("Data is: %s",cooked_data[i]);
132 chHeapFree(value[i]); 136 chHeapFree(value[i]);
133 137
134 } 138 }
  139 + printf("Returning timestamped data...\r\n");
135 return cooked_data; 140 return cooked_data;
136 } 141 }
137 142
@@ -177,15 +182,19 @@ int main(void) @@ -177,15 +182,19 @@ int main(void)
177 182
178 char* valueSensors[TOTAL_SENSORS]; 183 char* valueSensors[TOTAL_SENSORS];
179 memset (valueSensors, 0, TOTAL_SENSORS); 184 memset (valueSensors, 0, TOTAL_SENSORS);
180 -  
181 - I2C_scan(sensors); 185 + printf("Scanning sensors...\r\n");
  186 + //I2C_scan(sensors);
  187 + sensors[0] = LIGHT_ADDR;
182 sensors_length=strlen((char*)sensors); 188 sensors_length=strlen((char*)sensors);
  189 + printf("%d sensor detected...\r\n",sensors_length);
183 //registrar sensores 190 //registrar sensores
184 //Esta funcion esta mas arriba para no repetir código luego -- Imanol 191 //Esta funcion esta mas arriba para no repetir código luego -- Imanol
  192 + printf("Connecting to wifi...\r\n");
185 wifi_connect(); 193 wifi_connect();
186 //unsigned int time = getNTPTime(); 194 //unsigned int time = getNTPTime();
187 unsigned int time = getSecsSince1900(); 195 unsigned int time = getSecsSince1900();
188 //desconectarwifi 196 //desconectarwifi
  197 + printf("Disconecting wifi...\r\n");
189 wifi_disconnect(); 198 wifi_disconnect();
190 199
191 unsigned long timestamp = 0; 200 unsigned long timestamp = 0;
@@ -201,21 +210,42 @@ int main(void) @@ -201,21 +210,42 @@ int main(void)
201 210
202 while(1){ 211 while(1){
203 time += getElapsedTime(delay); 212 time += getElapsedTime(delay);
204 - 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); 213 + 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);
205 214
206 delay = getSystemTime(); 215 delay = getSystemTime();
207 /* Collect data from sensors */ 216 /* Collect data from sensors */
208 //collect_data(/*a_rawData*/); 217 //collect_data(/*a_rawData*/);
209 - collectData(valueSensors, sensors);  
210 - 218 + printf("Collecting data...\r\n");
  219 + //collectData(valueSensors, sensors);
  220 + valueSensors[0] = chHeapAlloc(NULL,6);
  221 + strcpy(valueSensors[0],"123456");
  222 + printf("Data collected...\r\n");
211 223
212 time += getElapsedTime(delay); 224 time += getElapsedTime(delay);
213 - 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); 225 + 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);
214 226
215 delay = getSystemTime(); 227 delay = getSystemTime();
216 //cada mitja hora 228 //cada mitja hora
217 - if (i == LONG_PERIOD/SHORT_PERIOD ){ 229 +
  230 + //delay2 = getElapsedTime(delay);
  231 +
  232 + delay = getSystemTime();
  233 + //timestamp = time - delay2;
  234 + timestamp = time - delay;
  235 + 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);
  236 +
  237 + /* Add data to the buffer with timestamp*/
  238 +
  239 + //a_cookedData = format_data(a_rawData, timestamp);
  240 + printf("Timestamping data...\r\n");
  241 + char** values=timestamp_datas(valueSensors,timestamp,sensors);
  242 + printf("Putting data in buffer...\r\n");
  243 + put_buffers(buffers,ind,sizes,values,sensors);
  244 + printf("Data is now in buffer...\n\r");
  245 + if (i == LONG_PERIOD/SHORT_PERIOD ){
  246 + printf("Programmed Send cycle...\r\n");
218 /* Wi-Fi connect */ 247 /* Wi-Fi connect */
  248 + printf("Connecting to wifi...\r\n");
219 wifi_connect(); 249 wifi_connect();
220 /* Send data to server, empty the buffer */ 250 /* Send data to server, empty the buffer */
221 send_data(buffers, ind, sizes, sensors); 251 send_data(buffers, ind, sizes, sensors);
@@ -223,20 +253,9 @@ int main(void) @@ -223,20 +253,9 @@ int main(void)
223 time = getSecsSince1900(); 253 time = getSecsSince1900();
224 //desconectar wifi 254 //desconectar wifi
225 wifi_disconnect(); 255 wifi_disconnect();
226 - 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); 256 + 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);
227 i = 0; 257 i = 0;
228 } 258 }
229 - delay2 = getElapsedTime(delay);  
230 -  
231 - delay = getSystemTime();  
232 - timestamp = time - delay2;  
233 - printf("timestamp (absolute):\t%d\r\ntimestamp mod LONG_PERIOD:\t%d\r\ntimestamp mod SHORT_PERIOD:\t%d\r\n",timestamp,timestamp%LONG_PERIOD,timestamp%SHORT_PERIOD);  
234 -  
235 - /* Add data to the buffer with timestamp*/  
236 -  
237 - //a_cookedData = format_data(a_rawData, timestamp);  
238 - char** values=timestamp_datas(valueSensors,timestamp,sensors);  
239 - put_buffers(buffers,ind,sizes,values,sensors);  
240 259
241 printf("mirant memoria\r\n"); 260 printf("mirant memoria\r\n");
242 int res=check_memory(); 261 int res=check_memory();
@@ -286,9 +305,10 @@ int main(void) @@ -286,9 +305,10 @@ int main(void)
286 wifi_disconnect(); 305 wifi_disconnect();
287 } 306 }
288 time += getElapsedTime(delay); 307 time += getElapsedTime(delay);
289 - 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); 308 + 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);
290 309
291 delay = getSystemTime(); 310 delay = getSystemTime();
  311 + printf("Time to sleep! for %d seconds\r\n", SHORT_PERIOD - time%SHORT_PERIOD);
292 sleep_thread(SHORT_PERIOD - time%SHORT_PERIOD); 312 sleep_thread(SHORT_PERIOD - time%SHORT_PERIOD);
293 i++; 313 i++;
294 } 314 }
Project/applications/smartcities/test_timer-loop.c
@@ -9,7 +9,7 @@ void testf(unsigned long delay, const char* premesg, const char* postmesg) @@ -9,7 +9,7 @@ void testf(unsigned long delay, const char* premesg, const char* postmesg)
9 printf("%s\r\n",postmesg); 9 printf("%s\r\n",postmesg);
10 } 10 }
11 unsigned long getNTPTime() 11 unsigned long getNTPTime()
12 -{ 12 +{
13 testf(1,"Asking for NTP time...", "NTP time received"); 13 testf(1,"Asking for NTP time...", "NTP time received");
14 return getSystemTime(); 14 return getSystemTime();
15 } 15 }