From a24d841b15ac8e848be80e790fe777b62ee7347e Mon Sep 17 00:00:00 2001 From: Imanol-Mikel Barba Sabariego Date: Sat, 31 May 2014 04:33:58 +0000 Subject: [PATCH] --- Project/applications/smartcities/example-code/httpClient/test_httpClient.c | 133 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Project/applications/smartcities/example-code/test_timer-loop.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Project/applications/smartcities/include/buffer.h | 2 +- Project/applications/smartcities/include/globals.h | 6 ++++++ Project/applications/smartcities/include/sensors.h | 4 ++-- Project/applications/smartcities/json.c | 2 -- Project/applications/smartcities/main.c | 55 +++++++++++++++++++++---------------------------------- Project/applications/smartcities/ntp.c | 28 ++++++++++++---------------- Project/applications/smartcities/sensors.c | 168 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------------------------------------------------- Project/applications/smartcities/test_httpClient.c | 133 ------------------------------------------------------------------------------------------------------------------------------------- Project/applications/smartcities/test_timer-loop.c | 97 ------------------------------------------------------------------------------------------------- Project/applications/smartcities/timer-loop.c | 9 ++++++--- 12 files changed, 363 insertions(+), 371 deletions(-) create mode 100644 Project/applications/smartcities/example-code/httpClient/test_httpClient.c create mode 100644 Project/applications/smartcities/example-code/test_timer-loop.c delete mode 100644 Project/applications/smartcities/test_httpClient.c delete mode 100644 Project/applications/smartcities/test_timer-loop.c diff --git a/Project/applications/smartcities/example-code/httpClient/test_httpClient.c b/Project/applications/smartcities/example-code/httpClient/test_httpClient.c new file mode 100644 index 0000000..493000a --- /dev/null +++ b/Project/applications/smartcities/example-code/httpClient/test_httpClient.c @@ -0,0 +1,133 @@ +#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" + +#define WIFI_MODE WIFI_MODE_CLIENT +#define NETWORK_SSID "linksys" +#define NETWORK_KEY "" +#define WPA_USER "smartcities" +#define WPA_PASS "superpass" + +void initLibwismart(void) +{ + wismart_hwif_t hwif = libwismart_GetDefaultHWIF(); + libwismart_Init(hwif); +} + +int main(void) +{ + //WPA (RADIUS) + /* + struct wpa_param wpa; + wpa.eap_method = WISMART_EAP_METHOD_TTLS; + wpa.u.ttls.identity=WPA_USER; + wpa.u.ttls.password=WPA_PASS; + wpa.u.ttls.ca_cert=NULL; + */ + + //NO SECURITY (HOTSPOT) + /**/ + + /*GET / HTTP/1.1 + Host: www.upc.edu*/ + struct httpHeaders head200 = { get, "/", 1, "www.upc.edu", 11 }; + + /*GET / HTTP/1.1 + Host: upc.edu*/ + struct httpHeaders head301 = { get, "/", 1, "upc.edu", 7 }; + + /*GET /errorerror HTTP/1.1 + Host: www.upc.edu*/ + struct httpHeaders head404 = { get, "/errorerrorerror", 15, "www.upc.edu", 11 }; + + initLibwismart(); + libwismart_PowerSave_Enable(); + libwismart_PowerSave_HigherProfile(TRUE); + + printf("\r\n\r\n,------------------.\r\n"); + printf("| GRUP ARM DE PAET |\r\n"); + printf("`------------------'\r\n"); + + libwismart_RegisterDhcpCB(dhcp_connect_result_cb); + libwismart_WiFiInit(); + //falta definir les variables de la xarxa + + /* // WPA (RADIUS) + libwismart_WiFiConnectEnterprise(NETWORK_SSID, &wpa, wifi_connect_result_cb); + */ + // NO Security (hotspot) + libwismart_WiFiConnect("ARMwifi",NULL, wifi_connect_result_cb); + + libwismart_EnableBsdSocketAPI(); + //chThdSleepMilliseconds(5000); + + DBG("HTTP Request expected to receive code 200 OK"); + httpRequest(head200, NULL, 0); + DBG("HTTP Request expected to receive code 301 OK"); + httpRequest(head301, NULL, 0); + DBG("HTTP Request expected to receive code 404 OK"); + httpRequest(head404, NULL, 0); + /* + for(;;) + { + // i2c get info= data + char* data=NULL; + put_message(data, buffer ,&ind); + int res=check_memory(); + if(res==SOFT_REACHED){ + + int ok=send(buffer, ind, "", ""); + if(ok==JSON_COMM_ERROR) + { + printf("wismart is not connected\r\n"); + } + else if( ok==JSON_OTHER_ERROR){ + printf("some error ocurred\r\n"); + } + else if(ok ==JSON_POST_OK){ + printf(" send OK \r\n"); + } + } + else if(res==HARD_REACHED){ + destroy(buffer); + } + chThdSleepMilliseconds(500); + }*/ + return 0; +} +/* + # + # WiSmart Applications Makefile + # + # Copyright 2012 eConais S.A + # + + + # Set the sdk root folder + SDK_ROOT = ../../ + PROJECT_OUT = smartcities + + USER_SRC = main.c httpClient.c callbacks.c module.c sensors.c json.c i2c.c buffer.c + USER_INCDIR = include/ + + # if you need to add build Defines options add to USER_DEFS define + #USER_DEFS += -D + + # select SDK's version: + SDK_VERSION = SDK_REV_9_2 + + # select the MCU type e.g. STM32F1, STM32F4 + PLAT = STM32F1 + + # select the MCU flash/ram size e.g. MCU_386K_64K, MCU_768K_96K, MCU_1M_96K + MCU_SIZE = MCU_768K_96K + + USER_DEFS += -D$(SDK_VERSION) + + # this must be the last include + include $(SDK_ROOT)/applications/rules.mk +*/ \ No newline at end of file diff --git a/Project/applications/smartcities/example-code/test_timer-loop.c b/Project/applications/smartcities/example-code/test_timer-loop.c new file mode 100644 index 0000000..1031574 --- /dev/null +++ b/Project/applications/smartcities/example-code/test_timer-loop.c @@ -0,0 +1,97 @@ +#include +#include "timer-loop.h" + +/* Funcions que simulen comportament real */ +void testf(unsigned long delay, const char* premesg, const char* postmesg) +{ + printf("%s\r\n",premesg); + sleep_thread(delay); + printf("%s\r\n",postmesg); +} +unsigned long getNTPTime() +{ + testf(1,"Asking for NTP time...", "NTP time received"); + return getSystemTime(); +} +void collect_data() +{ + testf(5, "Collecting data...", "Data collected"); +} +void store_data() +{ + testf(1, "Storing data...", "Data stored"); +} +void wifi_connect() +{ + testf(7, "Connecting via Wi-Fi...", "Wi-Fi connected"); +} + +/* Main de testing */ +void main(void) +{ + // Init wismart + initLibwismart(); + libwismart_PowerSave_Enable(); + libwismart_PowerSave_HigherProfile(TRUE); + + // Code + + long i = 1; + printf(",----------------.\r\n"); + printf("| TIME LOOP TEST |\r\n"); + printf("'----------------'\r\n\r\n"); + + printf("--------\r\n"); + printf("Loop #0\r\n\r\n"); + + unsigned long time = getNTPTime(); + unsigned long timestamp = 0; + unsigned long delay = getSystemTime(); + unsigned long delay2 = 0; + sleep_thread(SHORT_PERIOD - time%SHORT_PERIOD); + + //int* a_rawData; + //char* a_cookedData; + + while(1) + { + printf("\r\n\r\n--------\r\n"); + printf("Loop #%i\r\n\r\n",i); + + 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*/); + 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(); + 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++; + } +} \ No newline at end of file diff --git a/Project/applications/smartcities/include/buffer.h b/Project/applications/smartcities/include/buffer.h index 14bca06..7768d8d 100644 --- a/Project/applications/smartcities/include/buffer.h +++ b/Project/applications/smartcities/include/buffer.h @@ -4,7 +4,7 @@ #include "libwismart.h" #include "json.h" -#define SOFT_LIMIT 8192 /* en bytes -> 10k * 1024 */ +#define SOFT_LIMIT 8192 /* en bytes -> 8k * 1024 */ #define HARD_LIMIT 10240 #define MEMORY_OK 0 #define SOFT_REACHED 1 diff --git a/Project/applications/smartcities/include/globals.h b/Project/applications/smartcities/include/globals.h index 2088933..e9a9d50 100644 --- a/Project/applications/smartcities/include/globals.h +++ b/Project/applications/smartcities/include/globals.h @@ -7,5 +7,11 @@ #define BUFFER_LENGTH 30 #define MAX_RAM 30000 #define MODULE_ID "123456" +#define WIFI_MODE WIFI_MODE_CLIENT +#define NETWORK_SSID_AP "modularsense" +#define NETWORK_KEY_AP NULL +#define NETWORK_CHANNEL_AP 1 +#define HARD_LIMIT_WAIT_TIME 10*1000 //5*60*1000 + #endif diff --git a/Project/applications/smartcities/include/sensors.h b/Project/applications/smartcities/include/sensors.h index ad2af33..e1713d8 100644 --- a/Project/applications/smartcities/include/sensors.h +++ b/Project/applications/smartcities/include/sensors.h @@ -67,13 +67,13 @@ typedef struct { uint16_t MD; } bmp085_callibration; +void init_pressure(void); +void init_pressure_temperature(void); uint16_t get_pressure_data(void); uint16_t get_pressure_temperature_data(void); bmp085_callibration get_pressure_callibration_data(void); char* callibration_pressure_data_csv(bmp085_callibration parameters); char* pressure_value(uint16_t pressure,uint16_t temperature); -void init_pressure(void); -void init_pressure_temperature(void); //HUMIDITY AND TEMPERATURE SENSOR diff --git a/Project/applications/smartcities/json.c b/Project/applications/smartcities/json.c index d5c0c30..c87abf9 100644 --- a/Project/applications/smartcities/json.c +++ b/Project/applications/smartcities/json.c @@ -157,14 +157,12 @@ uint8_t send_json(char* statement, uint32_t length, char* provider_ID, char* sen strcpy(PATH,"/data/modularupload/"); strcpy(PATH+20,provider_ID); strcpy(PATH+20+strlen(provider_ID),sensor_ID);*/ - //PATH[21+strlen(provider_ID)+strlen(sensor_ID)] = '\0'; //FUCKING LEWIS... PATH = (char*) chHeapAlloc(NULL,54+strlen(provider_ID)+strlen(sensor_ID)); strcpy(PATH,"/modularsense/wordpress/opendata/data/modularupload/"); strcpy(PATH+52,provider_ID); strcpy(PATH+52+strlen(provider_ID),sensor_ID); - //PATH[53+strlen(provider_ID)+strlen(sensor_ID)] = '\0'; } struct httpHeaders server = { put,PATH,strlen(PATH),URL,strlen(URL)}; response_code = httpRequest(server, statement, length); diff --git a/Project/applications/smartcities/main.c b/Project/applications/smartcities/main.c index 4e951f1..0631106 100644 --- a/Project/applications/smartcities/main.c +++ b/Project/applications/smartcities/main.c @@ -12,15 +12,13 @@ #include "ntp.h" #include "sensors.h" -#define WIFI_MODE WIFI_MODE_CLIENT -#define NETWORK_SSID_AP "modularsense" -#define NETWORK_KEY_AP NULL -#define NETWORK_CHANNEL_AP 1 - -#define HARD_LIMIT_WAIT_TIME 10*1000 //5*60*1000 - +uint8_t connected=0; +uint8_t timeout=0; +uint8_t retries=0; +uint8_t sensors_length=0; +uint8_t registry_opened=0; wismart_registryKey_t geo; - +module mod; void initLibwismart(void) { @@ -28,13 +26,6 @@ void initLibwismart(void) libwismart_Init(hwif); } -uint8_t connected=0; -uint8_t timeout=0; -uint8_t retries=0; -uint8_t sensors_length=0; -uint8_t registry_opened=0; - -module mod; void update_time(unsigned long *time) { @@ -114,8 +105,6 @@ void send_data(char** buffers[], uint32_t ind[], uint32_t sizes[], uint8_t senso for(j=0;j 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 //Escanea y registra I2C_scan(I2C1,sensors); sensors_length=strlen((char*)sensors); @@ -289,7 +279,8 @@ int main(void) printf("Putting data in buffer...\r\n"); put_buffers(buffers,ind,sizes,values,sensors); printf("Data is now in buffer...\n\r"); - if (i == LONG_PERIOD/SHORT_PERIOD ){ + if (i == LONG_PERIOD/SHORT_PERIOD ) + { printf("Programmed Send cycle...\r\n"); /* Wi-Fi connect */ printf("Connecting to wifi...\r\n"); @@ -299,10 +290,8 @@ int main(void) printf("Data sent!\r\n"); //Now sending battery level send_battery_level(timestamp); - //time = getNTPTime(); update_time(&time); delay = getSystemTime(); - //desconectar wifi wifi_disconnect(); printf("new ntp 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); i = 0; @@ -310,16 +299,17 @@ int main(void) printf("mirant memoria\r\n"); int res=check_memory(); - if(res==SOFT_REACHED){ + if(res==SOFT_REACHED) + { printf("--------------soft limit-------------\r\n"); wifi_connect(); send_data(buffers, ind, sizes, sensors); //Now sending battery level send_battery_level(timestamp); - //disconect_wifi() wifi_disconnect(); } - else if(res==HARD_REACHED){ + else if(res==HARD_REACHED) + { printf("--------------hard limit-------------\r\n"); wifi_connect(); char id_0[3]; @@ -351,7 +341,6 @@ int main(void) else if(ok ==JSON_POST_OK){ printf(" send OK \r\n"); } - } //Now sending battery level send_battery_level(timestamp); @@ -370,8 +359,6 @@ int main(void) /* * TO-DO * - * - Test distance and humidity sensors - * - Test ADC - * - Test register, data send and battery send + * - Test distance * - Reset timer-related defines */ \ No newline at end of file diff --git a/Project/applications/smartcities/ntp.c b/Project/applications/smartcities/ntp.c index 9d152a7..6b733db 100644 --- a/Project/applications/smartcities/ntp.c +++ b/Project/applications/smartcities/ntp.c @@ -1,8 +1,8 @@ #include "ntp.h" -Date getDate(unsigned long secsSince1900){ +Date getDate(unsigned long secsSince1900) +{ unsigned long daysPerMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; - unsigned long epoch = secsSince1900 - DIFF_SEC_1900_1970; int second = epoch % 60; @@ -14,7 +14,8 @@ Date getDate(unsigned long secsSince1900){ int year = 0; unsigned long days = 0; - while((unsigned)(days += (LEAP_YEAR(year) ? 366 : 365)) <= epoch){ + while((unsigned)(days += (LEAP_YEAR(year) ? 366 : 365)) <= epoch) + { year++; } days -= LEAP_YEAR(year) ? 366 : 365; @@ -23,14 +24,17 @@ Date getDate(unsigned long secsSince1900){ int j=0; int cont=0; int aux=epoch; - if(LEAP_YEAR(year)){ + if(LEAP_YEAR(year)) + { daysPerMonth[1]=29; } - while (daysPerMonth[j] -#include "timer-loop.h" - -/* Funcions que simulen comportament real */ -void testf(unsigned long delay, const char* premesg, const char* postmesg) -{ - printf("%s\r\n",premesg); - sleep_thread(delay); - printf("%s\r\n",postmesg); -} -unsigned long getNTPTime() -{ - testf(1,"Asking for NTP time...", "NTP time received"); - return getSystemTime(); -} -void collect_data() -{ - testf(5, "Collecting data...", "Data collected"); -} -void store_data() -{ - testf(1, "Storing data...", "Data stored"); -} -void wifi_connect() -{ - testf(7, "Connecting via Wi-Fi...", "Wi-Fi connected"); -} - -/* Main de testing */ -void main(void) -{ - // Init wismart - initLibwismart(); - libwismart_PowerSave_Enable(); - libwismart_PowerSave_HigherProfile(TRUE); - - // Code - - long i = 1; - printf(",----------------.\r\n"); - printf("| TIME LOOP TEST |\r\n"); - printf("'----------------'\r\n\r\n"); - - printf("--------\r\n"); - printf("Loop #0\r\n\r\n"); - - unsigned long time = getNTPTime(); - unsigned long timestamp = 0; - unsigned long delay = getSystemTime(); - unsigned long delay2 = 0; - sleep_thread(SHORT_PERIOD - time%SHORT_PERIOD); - - //int* a_rawData; - //char* a_cookedData; - - while(1) - { - printf("\r\n\r\n--------\r\n"); - printf("Loop #%i\r\n\r\n",i); - - 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*/); - 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(); - 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++; - } -} \ No newline at end of file diff --git a/Project/applications/smartcities/timer-loop.c b/Project/applications/smartcities/timer-loop.c index 61d38c5..927241e 100644 --- a/Project/applications/smartcities/timer-loop.c +++ b/Project/applications/smartcities/timer-loop.c @@ -19,13 +19,16 @@ * - milisegons:ans/1000 = 4.294.967,295 segons // 49 days 17 hours 2 minutes 47.3 seconds */ unsigned long getSystemTime() -{ return (long)libwismart_GetTime()/1000; } +{ + return (long)libwismart_GetTime()/1000; +} unsigned long getElapsedTime(unsigned long t) -{ return (long)libwismart_ElapsedTime(t*1000)/1000; } +{ + return (long)libwismart_ElapsedTime(t*1000)/1000; +} void sleep_thread(unsigned long seconds) { chThdSleepMilliseconds( (unsigned int)seconds*1000 ); - //sleep(seconds); //for testing in linux machine } \ No newline at end of file -- libgit2 0.22.2