From c0584650085c552f4e644f0f402514b7ac353ba2 Mon Sep 17 00:00:00 2001 From: Emilio Soca Herrera Date: Thu, 22 May 2014 06:41:08 +0000 Subject: [PATCH] --- Project/applications/smartcities/main2.c | 251 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 251 insertions(+), 0 deletions(-) create mode 100644 Project/applications/smartcities/main2.c diff --git a/Project/applications/smartcities/main2.c b/Project/applications/smartcities/main2.c new file mode 100644 index 0000000..05e2596 --- /dev/null +++ b/Project/applications/smartcities/main2.c @@ -0,0 +1,251 @@ +#include +#include "libwismart.h" +#include "libwismart_irqs.h" /* implement irq handlers */ +#include "lwip/inet.h" +#include "globals.h" +#include "httpClient.h" +#include "callbacks.h" +#include "buffer.h" +#include "i2c.h" +#include "configServer.h" + +#define WIFI_MODE WIFI_MODE_CLIENT +#define NETWORK_SSID_AP "modularsens" +#define NETWORK_KEY_AP NULL +#define NETWORK_CHANNEL_AP 1 + +#define HARD_LIMIT_WAIT_TIME 10*1000 //5*60*1000 +#define NUMSENSORS 4 + +wismart_registryKey_t geo; + + +void initLibwismart(void) +{ + wismart_hwif_t hwif = libwismart_GetDefaultHWIF(); + libwismart_Init(hwif); +} + +uint8_t connected=0; +uint8_t timeout=0; +uint8_t retries=0; + +void init_registry(void) +{ + config_params_t config; + + strcpy(config.localization,"none"); + libwismart_RegistryCreateKey(&geo, 1, sizeof(config)); + libwismart_RegistryOpen(1); + if(!libwismart_RegistryIsValueEmpty(&geo)) + { + libwismart_RegistryGet(&geo,&config); + } + + printf("SSID = %s\r\n",config.ssid); + printf("Wep key = %s\r\n",config.wepkey); + printf("Passphrase = %s\r\n", config.passphrase); + printf("User = %s\r\n",config.user); + printf("Password = %s\r\n",config.password); + printf("Encryption type = %d\r\n",config.security); + printf("Geo Localization = %s\r\n", config.localization); + + strcpy(mod.geoloc,config.localization); + if(config.security == PROFILE_SECURITY_OPEN) + { + printf("open detected\r\n"); + libwismart_WiFiConnect(config.ssid,NULL,wifi_connect_result_cb); + } + else if(config.security == PROFILE_SECURITY_WPA_WPA2) + { + if(!strcmp(config.user,"")) + { + printf("wpa detected\r\n"); + libwismart_WiFiConnect(config.ssid,config.passphrase,wifi_connect_result_cb); + } + else + { + printf("wpa Enterprise detected\r\n"); + struct wpa_param wpa; + wpa.eap_method = WISMART_EAP_METHOD_TTLS; + wpa.u.ttls.identity = config.user; + wpa.u.ttls.password = config.password; + wpa.u.ttls.ca_cert=NULL; + libwismart_WiFiConnectEnterprise(config.ssid, &wpa, wifi_connect_result_cb); + } + } + else + { + printf("wep detected\r\n"); + //Is WEP + libwismart_WiFiConnect(config.ssid,config.wepkey,wifi_connect_result_cb); + } + while(connected == 0 && timeout != 1 ) + {chThdSleepMilliseconds(500);} + +} +int main(void) +{ + initLibwismart(); + libwismart_PowerSave_Enable(); + libwismart_PowerSave_HigherProfile(TRUE); + libwismart_RegisterDhcpCB(dhcp_connect_result_cb); + libwismart_WiFiInit(); + libwismart_SetScanRunsForConnTimeout(4); //Edit a 4 + libwismart_EnableBsdSocketAPI(); + + //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 + uint8_t sensors[TOTAL_SENSORS]; + char valueSensors[TOTAL_SENSORS]; + I2C_scan(sensors); + + int i = 1; + //unsigned int time = getNTPTime(); + unsigned int time = getSecsSince1900(); + + unsigned int timestamp = 0; + unsigned int delay = getSystemTime(); + unsigned int delay2 = 0; + sleep_thread(SHORT_PERIOD - time%SHORT_PERIOD); + + while(1){ + time += getElapsedTime(delay); + 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); + + delay = getSystemTime(); + /* Collect data from sensors */ + //collect_data(/*a_rawData*/); + collect_data(valueSensors, sensors); + + + time += getElapsedTime(delay); + 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); + + delay = getSystemTime(); + if (i == LONG_PERIOD/SHORT_PERIOD ){ + /* Wi-Fi connect */ + wifi_connect(); + /* Send data to server, empty the buffer */ + //time = getNTPTime(); + time = getSecsSince1900(); + 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); + i = 0; + } + delay2 = getElapsedTime(delay); + + delay = getSystemTime(); + timestamp = time - delay2; + 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); + + /* Add data to the buffer with timestamp*/ + //a_cookedData = format_data(a_rawData, timestamp); + store_data(/* data */); + + time += getElapsedTime(delay); + 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); + + delay = getSystemTime(); + sleep_thread(SHORT_PERIOD - time%SHORT_PERIOD); + i++; + } + + + + + + +//A partir de aquí se debe integrar arriba, preferiblemente con funciones María + uint32_t ind[4]={0}; + char** buffers[4]; + uint32_t sizes[4]={0}; + char* sensor_id[4]; + int i; + int num_sensors; + + init_registry(); + + if(timeout==1) + { + printf("Creating AP\r\n"); + //corroborar los parametros del AP + configServer_start(1); + libwismart_WiFi_SoftAP_Start(NETWORK_SSID_AP,NETWORK_CHANNEL_AP,NULL,softapMode_apStartedCb, softapMode_clientIndicationCb); + for(;;) + { + chThdSleepMilliseconds(1000); + } + } + for(;;) + { + // i2c gets the data and combines it with the time stamp + char* data="message,0"; + char* data2="message,1"; + char* data3="message,2"; + for(i=0;i