Commit c0584650085c552f4e644f0f402514b7ac353ba2
1 parent
bd3fb56a
--no commit message
Showing
1 changed file
with
251 additions
and
0 deletions
Project/applications/smartcities/main2.c
0 → 100644
1 | +#include <string.h> | |
2 | +#include "libwismart.h" | |
3 | +#include "libwismart_irqs.h" /* implement irq handlers */ | |
4 | +#include "lwip/inet.h" | |
5 | +#include "globals.h" | |
6 | +#include "httpClient.h" | |
7 | +#include "callbacks.h" | |
8 | +#include "buffer.h" | |
9 | +#include "i2c.h" | |
10 | +#include "configServer.h" | |
11 | + | |
12 | +#define WIFI_MODE WIFI_MODE_CLIENT | |
13 | +#define NETWORK_SSID_AP "modularsens" | |
14 | +#define NETWORK_KEY_AP NULL | |
15 | +#define NETWORK_CHANNEL_AP 1 | |
16 | + | |
17 | +#define HARD_LIMIT_WAIT_TIME 10*1000 //5*60*1000 | |
18 | +#define NUMSENSORS 4 | |
19 | + | |
20 | +wismart_registryKey_t geo; | |
21 | + | |
22 | + | |
23 | +void initLibwismart(void) | |
24 | +{ | |
25 | + wismart_hwif_t hwif = libwismart_GetDefaultHWIF(); | |
26 | + libwismart_Init(hwif); | |
27 | +} | |
28 | + | |
29 | +uint8_t connected=0; | |
30 | +uint8_t timeout=0; | |
31 | +uint8_t retries=0; | |
32 | + | |
33 | +void init_registry(void) | |
34 | +{ | |
35 | + config_params_t config; | |
36 | + | |
37 | + strcpy(config.localization,"none"); | |
38 | + libwismart_RegistryCreateKey(&geo, 1, sizeof(config)); | |
39 | + libwismart_RegistryOpen(1); | |
40 | + if(!libwismart_RegistryIsValueEmpty(&geo)) | |
41 | + { | |
42 | + libwismart_RegistryGet(&geo,&config); | |
43 | + } | |
44 | + | |
45 | + printf("SSID = %s\r\n",config.ssid); | |
46 | + printf("Wep key = %s\r\n",config.wepkey); | |
47 | + printf("Passphrase = %s\r\n", config.passphrase); | |
48 | + printf("User = %s\r\n",config.user); | |
49 | + printf("Password = %s\r\n",config.password); | |
50 | + printf("Encryption type = %d\r\n",config.security); | |
51 | + printf("Geo Localization = %s\r\n", config.localization); | |
52 | + | |
53 | + strcpy(mod.geoloc,config.localization); | |
54 | + if(config.security == PROFILE_SECURITY_OPEN) | |
55 | + { | |
56 | + printf("open detected\r\n"); | |
57 | + libwismart_WiFiConnect(config.ssid,NULL,wifi_connect_result_cb); | |
58 | + } | |
59 | + else if(config.security == PROFILE_SECURITY_WPA_WPA2) | |
60 | + { | |
61 | + if(!strcmp(config.user,"")) | |
62 | + { | |
63 | + printf("wpa detected\r\n"); | |
64 | + libwismart_WiFiConnect(config.ssid,config.passphrase,wifi_connect_result_cb); | |
65 | + } | |
66 | + else | |
67 | + { | |
68 | + printf("wpa Enterprise detected\r\n"); | |
69 | + struct wpa_param wpa; | |
70 | + wpa.eap_method = WISMART_EAP_METHOD_TTLS; | |
71 | + wpa.u.ttls.identity = config.user; | |
72 | + wpa.u.ttls.password = config.password; | |
73 | + wpa.u.ttls.ca_cert=NULL; | |
74 | + libwismart_WiFiConnectEnterprise(config.ssid, &wpa, wifi_connect_result_cb); | |
75 | + } | |
76 | + } | |
77 | + else | |
78 | + { | |
79 | + printf("wep detected\r\n"); | |
80 | + //Is WEP | |
81 | + libwismart_WiFiConnect(config.ssid,config.wepkey,wifi_connect_result_cb); | |
82 | + } | |
83 | + while(connected == 0 && timeout != 1 ) | |
84 | + {chThdSleepMilliseconds(500);} | |
85 | + | |
86 | +} | |
87 | +int main(void) | |
88 | +{ | |
89 | + initLibwismart(); | |
90 | + libwismart_PowerSave_Enable(); | |
91 | + libwismart_PowerSave_HigherProfile(TRUE); | |
92 | + libwismart_RegisterDhcpCB(dhcp_connect_result_cb); | |
93 | + libwismart_WiFiInit(); | |
94 | + libwismart_SetScanRunsForConnTimeout(4); //Edit a 4 | |
95 | + libwismart_EnableBsdSocketAPI(); | |
96 | + | |
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 | |
98 | + uint8_t sensors[TOTAL_SENSORS]; | |
99 | + char valueSensors[TOTAL_SENSORS]; | |
100 | + I2C_scan(sensors); | |
101 | + | |
102 | + int i = 1; | |
103 | + //unsigned int time = getNTPTime(); | |
104 | + unsigned int time = getSecsSince1900(); | |
105 | + | |
106 | + unsigned int timestamp = 0; | |
107 | + unsigned int delay = getSystemTime(); | |
108 | + unsigned int delay2 = 0; | |
109 | + sleep_thread(SHORT_PERIOD - time%SHORT_PERIOD); | |
110 | + | |
111 | + while(1){ | |
112 | + 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); | |
114 | + | |
115 | + delay = getSystemTime(); | |
116 | + /* Collect data from sensors */ | |
117 | + //collect_data(/*a_rawData*/); | |
118 | + collect_data(valueSensors, sensors); | |
119 | + | |
120 | + | |
121 | + time += getElapsedTime(delay); | |
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); | |
123 | + | |
124 | + delay = getSystemTime(); | |
125 | + if (i == LONG_PERIOD/SHORT_PERIOD ){ | |
126 | + /* Wi-Fi connect */ | |
127 | + wifi_connect(); | |
128 | + /* Send data to server, empty the buffer */ | |
129 | + //time = getNTPTime(); | |
130 | + 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); | |
132 | + i = 0; | |
133 | + } | |
134 | + delay2 = getElapsedTime(delay); | |
135 | + | |
136 | + delay = getSystemTime(); | |
137 | + timestamp = time - delay2; | |
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); | |
139 | + | |
140 | + /* Add data to the buffer with timestamp*/ | |
141 | + //a_cookedData = format_data(a_rawData, timestamp); | |
142 | + store_data(/* data */); | |
143 | + | |
144 | + 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); | |
146 | + | |
147 | + delay = getSystemTime(); | |
148 | + sleep_thread(SHORT_PERIOD - time%SHORT_PERIOD); | |
149 | + i++; | |
150 | + } | |
151 | + | |
152 | + | |
153 | + | |
154 | + | |
155 | + | |
156 | + | |
157 | +//A partir de aquí se debe integrar arriba, preferiblemente con funciones María | |
158 | + uint32_t ind[4]={0}; | |
159 | + char** buffers[4]; | |
160 | + uint32_t sizes[4]={0}; | |
161 | + char* sensor_id[4]; | |
162 | + int i; | |
163 | + int num_sensors; | |
164 | + | |
165 | + init_registry(); | |
166 | + | |
167 | + if(timeout==1) | |
168 | + { | |
169 | + printf("Creating AP\r\n"); | |
170 | + //corroborar los parametros del AP | |
171 | + configServer_start(1); | |
172 | + libwismart_WiFi_SoftAP_Start(NETWORK_SSID_AP,NETWORK_CHANNEL_AP,NULL,softapMode_apStartedCb, softapMode_clientIndicationCb); | |
173 | + for(;;) | |
174 | + { | |
175 | + chThdSleepMilliseconds(1000); | |
176 | + } | |
177 | + } | |
178 | + for(;;) | |
179 | + { | |
180 | + // i2c gets the data and combines it with the time stamp | |
181 | + char* data="message,0"; | |
182 | + char* data2="message,1"; | |
183 | + char* data3="message,2"; | |
184 | + for(i=0;i<NUMSENSORS;i++){ | |
185 | + | |
186 | + printf("------------------BUFFER %d ----------------------\r\n",i); | |
187 | + printf("index=%d\r\n",ind[i]); | |
188 | + | |
189 | + // 3 missatges de prova a cada buffer | |
190 | + buffers[i]=put_message(data, buffers[i] ,&ind[i],&sizes[i]); | |
191 | + buffers[i]=put_message(data2, buffers[i] ,&ind[i],&sizes[i]); | |
192 | + buffers[i]=put_message(data3, buffers[i] ,&ind[i],&sizes[i]); | |
193 | + printf("mirant memoria\r\n"); | |
194 | + int res=check_memory(); | |
195 | + /*if(res==SOFT_REACHED){ | |
196 | + printf("--------------soft limit-------------\r\n"); | |
197 | + int j; | |
198 | + for(j=0;j<NUMSENSORS;j++) | |
199 | + { | |
200 | + printf(" enviant buffer %d\r\n",j); | |
201 | + | |
202 | + // fem servir 085 de prova, però haurem de fer servir sensor_id[j] | |
203 | + int ok=send(buffers[j],&ind[j],&sizes[j], "bmp", "085"); | |
204 | + if(ok==JSON_COMM_ERROR) | |
205 | + { | |
206 | + printf("wismart is not connected\r\n"); | |
207 | + } | |
208 | + else if( ok==JSON_OTHER_ERROR){ | |
209 | + printf("some error ocurred\r\n"); | |
210 | + } | |
211 | + else if(ok ==JSON_POST_OK){ | |
212 | + printf(" send OK \r\n"); | |
213 | + } | |
214 | + } | |
215 | + } | |
216 | + else if(res==HARD_REACHED){ | |
217 | + printf("--------------hard limit-------------\r\n"); | |
218 | + | |
219 | + // fem servir 085 de prova, però haurem de fer servir sensor_id[0] | |
220 | + while( send(buffers[0],&ind[0],&sizes[0], "bmp", "085") != JSON_POST_OK ) | |
221 | + { | |
222 | + // El servidor no ens sap dir si tenim permisos o no sense registrar una mostra. | |
223 | + // Intentem enviar un buffer sencer, a veure si ja podem buidar. | |
224 | + // No podem enviar només una mostra perquè json és rígid en aquest sentit. | |
225 | + | |
226 | + printf("hard_reached and unable to sentd.\r\nLa biblia en vers: i Jesús digué: Aixeca't, Llàtzer!!\r\n"); | |
227 | + chThdSleepMilliseconds(HARD_LIMIT_WAIT_TIME); | |
228 | + } | |
229 | + int j; | |
230 | + for(j=1;j<NUMSENSORS;j++) | |
231 | + { | |
232 | + printf(" enviant buffer %d\r\n",j); | |
233 | + | |
234 | + // fem servir 085 de prova, però haurem de fer servir sensor_id[j] | |
235 | + int ok=send(buffers[j],&ind[j],&sizes[j], "bmp", "085"); | |
236 | + if(ok==JSON_COMM_ERROR) | |
237 | + { | |
238 | + printf("wismart is not connected\r\n"); | |
239 | + } | |
240 | + else if( ok==JSON_OTHER_ERROR){ | |
241 | + printf("some error ocurred\r\n"); | |
242 | + } | |
243 | + else if(ok ==JSON_POST_OK){ | |
244 | + printf(" send OK \r\n"); | |
245 | + } | |
246 | + } | |
247 | + }*/ | |
248 | + } | |
249 | + //chThdSleepMilliseconds(100); | |
250 | + } | |
251 | +} | ... | ... |