Commit 626579214fa75723c349260631a950df357ba214
1 parent
1cbe8c09
--no commit message
Showing
1 changed file
with
112 additions
and
4 deletions
Project/applications/smartcities/main2.c
@@ -84,6 +84,38 @@ void init_registry(void) | @@ -84,6 +84,38 @@ void init_registry(void) | ||
84 | {chThdSleepMilliseconds(500);} | 84 | {chThdSleepMilliseconds(500);} |
85 | 85 | ||
86 | } | 86 | } |
87 | + | ||
88 | +void send_data() | ||
89 | +{ | ||
90 | + int j; | ||
91 | + for(j=0;j<TOTAL_SENSORS;j++) | ||
92 | + { | ||
93 | + printf(" enviant buffer %d\r\n",j); | ||
94 | + | ||
95 | + // fem servir 085 de prova, però haurem de fer servir sensor_id[j] | ||
96 | + int ok=send(buffers[j],&ind[j],&sizes[j], mod.ID, sensors[j]); | ||
97 | + | ||
98 | + if(ok==JSON_COMM_ERROR) | ||
99 | + { | ||
100 | + printf("wismart is not connected\r\n"); | ||
101 | + } | ||
102 | + else if( ok==JSON_OTHER_ERROR){ | ||
103 | + printf("some error ocurred\r\n"); | ||
104 | + } | ||
105 | + else if(ok ==JSON_POST_OK){ | ||
106 | + printf(" send OK \r\n"); | ||
107 | + } | ||
108 | + } | ||
109 | +} | ||
110 | + | ||
111 | +void put_buffers(char** buffers,uint32_t ind[],uint32_t sizes[],char* valueSensors[]){ | ||
112 | +int i; | ||
113 | + for(i=0;i<TOTAL_SENSORS;i++){ | ||
114 | + put_message(valueSensors[i], buffers[i] ,&ind[i],&sizes[i]); | ||
115 | + } | ||
116 | + | ||
117 | +} | ||
118 | + | ||
87 | int main(void) | 119 | int main(void) |
88 | { | 120 | { |
89 | initLibwismart(); | 121 | initLibwismart(); |
@@ -96,10 +128,10 @@ int main(void) | @@ -96,10 +128,10 @@ int main(void) | ||
96 | 128 | ||
97 | //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 | 129 | //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 |
98 | uint8_t sensors[TOTAL_SENSORS]; | 130 | uint8_t sensors[TOTAL_SENSORS]; |
99 | - char valueSensors[TOTAL_SENSORS]; | 131 | + char* valueSensors[TOTAL_SENSORS]; |
100 | I2C_scan(sensors); | 132 | I2C_scan(sensors); |
101 | 133 | ||
102 | - int i = 1; | 134 | + |
103 | //unsigned int time = getNTPTime(); | 135 | //unsigned int time = getNTPTime(); |
104 | unsigned int time = getSecsSince1900(); | 136 | unsigned int time = getSecsSince1900(); |
105 | 137 | ||
@@ -108,6 +140,24 @@ int main(void) | @@ -108,6 +140,24 @@ int main(void) | ||
108 | unsigned int delay2 = 0; | 140 | unsigned int delay2 = 0; |
109 | sleep_thread(SHORT_PERIOD - time%SHORT_PERIOD); | 141 | sleep_thread(SHORT_PERIOD - time%SHORT_PERIOD); |
110 | 142 | ||
143 | + uint32_t ind[4]={0}; | ||
144 | + char** buffers[4]; | ||
145 | + uint32_t sizes[4]={0}; | ||
146 | + | ||
147 | + init_registry(); | ||
148 | + | ||
149 | + if(timeout==1) | ||
150 | + { | ||
151 | + printf("Creating AP\r\n"); | ||
152 | + //corroborar los parametros del AP | ||
153 | + configServer_start(1); | ||
154 | + libwismart_WiFi_SoftAP_Start(NETWORK_SSID_AP,NETWORK_CHANNEL_AP,NULL,softapMode_apStartedCb, softapMode_clientIndicationCb); | ||
155 | + for(;;) | ||
156 | + { | ||
157 | + chThdSleepMilliseconds(1000); | ||
158 | + } | ||
159 | + | ||
160 | + int i = 1; | ||
111 | while(1){ | 161 | while(1){ |
112 | time += getElapsedTime(delay); | 162 | time += getElapsedTime(delay); |
113 | 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); | 163 | 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); |
@@ -122,10 +172,12 @@ int main(void) | @@ -122,10 +172,12 @@ int main(void) | ||
122 | 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); | 172 | 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); |
123 | 173 | ||
124 | delay = getSystemTime(); | 174 | delay = getSystemTime(); |
175 | + //cada mitja hora | ||
125 | if (i == LONG_PERIOD/SHORT_PERIOD ){ | 176 | if (i == LONG_PERIOD/SHORT_PERIOD ){ |
126 | /* Wi-Fi connect */ | 177 | /* Wi-Fi connect */ |
127 | wifi_connect(); | 178 | wifi_connect(); |
128 | /* Send data to server, empty the buffer */ | 179 | /* Send data to server, empty the buffer */ |
180 | + send_data(); | ||
129 | //time = getNTPTime(); | 181 | //time = getNTPTime(); |
130 | time = getSecsSince1900(); | 182 | time = getSecsSince1900(); |
131 | 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); | 183 | 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); |
@@ -138,8 +190,64 @@ int main(void) | @@ -138,8 +190,64 @@ int main(void) | ||
138 | 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); | 190 | 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); |
139 | 191 | ||
140 | /* Add data to the buffer with timestamp*/ | 192 | /* Add data to the buffer with timestamp*/ |
193 | + put_buffers(buffers,ind,sizes,valueSensors); | ||
194 | + printf("mirant memoria\r\n"); | ||
195 | + int res=check_memory(); | ||
196 | + if(res==SOFT_REACHED){ | ||
197 | + printf("--------------soft limit-------------\r\n"); | ||
198 | + int j; | ||
199 | + for(j=0;j<TOTAL_SENSORS;j++) | ||
200 | + { | ||
201 | + printf(" enviant buffer %d\r\n",j); | ||
202 | + | ||
203 | + // fem servir 085 de prova, però haurem de fer servir sensor_id[j] | ||
204 | + int ok=send(buffers[j],&ind[j],&sizes[j], mod.ID, sensor_id[j]); | ||
205 | + | ||
206 | + if(ok==JSON_COMM_ERROR) | ||
207 | + { | ||
208 | + printf("wismart is not connected\r\n"); | ||
209 | + } | ||
210 | + else if( ok==JSON_OTHER_ERROR){ | ||
211 | + printf("some error ocurred\r\n"); | ||
212 | + } | ||
213 | + else if(ok ==JSON_POST_OK){ | ||
214 | + printf(" send OK \r\n"); | ||
215 | + } | ||
216 | + } | ||
217 | + } | ||
218 | + else if(res==HARD_REACHED){ | ||
219 | + printf("--------------hard limit-------------\r\n"); | ||
220 | + | ||
221 | + // fem servir 085 de prova, però haurem de fer servir sensor_id[0] | ||
222 | + while( send(buffers[0],&ind[0],&sizes[0], mod.ID, sensors[j]) != JSON_POST_OK ) | ||
223 | + { | ||
224 | + // El servidor no ens sap dir si tenim permisos o no sense registrar una mostra. | ||
225 | + // Intentem enviar un buffer sencer, a veure si ja podem buidar. | ||
226 | + // No podem enviar només una mostra perquè json és rígid en aquest sentit. | ||
227 | + | ||
228 | + printf("hard_reached and unable to sentd.\r\nLa biblia en vers: i Jesús digué: Aixeca't, Llàtzer!!\r\n"); | ||
229 | + chThdSleepMilliseconds(HARD_LIMIT_WAIT_TIME); | ||
230 | + } | ||
231 | + int j; | ||
232 | + for(j=1;j<TOTAL_SENSORS;j++) | ||
233 | + { | ||
234 | + printf(" enviant buffer %d\r\n",j); | ||
235 | + | ||
236 | + // fem servir 085 de prova, però haurem de fer servir sensor_id[j] | ||
237 | + int ok=send(buffers[j],&ind[j],&sizes[j], mod.ID, sensors[j]); | ||
238 | + if(ok==JSON_COMM_ERROR) | ||
239 | + { | ||
240 | + printf("wismart is not connected\r\n"); | ||
241 | + } | ||
242 | + else if( ok==JSON_OTHER_ERROR){ | ||
243 | + printf("some error ocurred\r\n"); | ||
244 | + } | ||
245 | + else if(ok ==JSON_POST_OK){ | ||
246 | + printf(" send OK \r\n"); | ||
247 | + } | ||
248 | + } | ||
249 | + } | ||
141 | //a_cookedData = format_data(a_rawData, timestamp); | 250 | //a_cookedData = format_data(a_rawData, timestamp); |
142 | - store_data(/* data */); | ||
143 | 251 | ||
144 | time += getElapsedTime(delay); | 252 | time += getElapsedTime(delay); |
145 | 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); | 253 | 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); |
@@ -160,7 +268,7 @@ int main(void) | @@ -160,7 +268,7 @@ int main(void) | ||
160 | uint32_t sizes[4]={0}; | 268 | uint32_t sizes[4]={0}; |
161 | char* sensor_id[4]; | 269 | char* sensor_id[4]; |
162 | int i; | 270 | int i; |
163 | - int num_sensors; | 271 | + |
164 | 272 | ||
165 | init_registry(); | 273 | init_registry(); |
166 | 274 |