diff --git a/Project/applications/smartcities/httpClient.c b/Project/applications/smartcities/httpClient.c index 1e3f7ca..d66768d 100644 --- a/Project/applications/smartcities/httpClient.c +++ b/Project/applications/smartcities/httpClient.c @@ -19,8 +19,7 @@ int httpRequest(struct httpHeaders head, char* content, int content_size) char* request; char* response; - //LE PARCHE - initialize_IP(&remote_ip,SERVER_IP); + ip4_addr_set_u32(&remote_ip,ipaddr_addr(SERVER_IP)); // Check or default params printf("httpRequest: Checking params\r\n"); @@ -108,29 +107,6 @@ int httpRequest(struct httpHeaders head, char* content, int content_size) return http_response; } -void initialize_IP(struct ip_addr* addr,char* IP) -{ - uint8_t b[4],i,j = 0,k = 0,l; - char byte[4]; - for(i = 0; i < (strlen(IP)+1); i++) - { - if(IP[i] == '.' || IP[i] == '\0') - { - for(l = 0; l < (3-j); l++) - { - byte[2] = byte[1]; - byte[1] = byte[0]; - byte[0] = '0'; - } - b[k++] = response2int(byte); - j = 0; - continue; - } - byte[j++] = IP[i]; - } - IP4_ADDR(addr,b[0],b[1],b[2],b[3]); -} - /* err_t netconn_write ( struct netconn * aNetConn, const void * aData, size_t aSize, u8_t aApiFlags ); diff --git a/Project/applications/smartcities/include/httpClient.h b/Project/applications/smartcities/include/httpClient.h index fd728ea..17b923f 100644 --- a/Project/applications/smartcities/include/httpClient.h +++ b/Project/applications/smartcities/include/httpClient.h @@ -47,7 +47,6 @@ const char* reqMethod2text(enum reqMethod method); char* int2string(int num); int numberofdigits(int number); int response2int(char* chars); -void initialize_IP(struct ip_addr* addr,char* IP); #endif diff --git a/Project/applications/smartcities/include/ntp.h b/Project/applications/smartcities/include/ntp.h index d2a8dcc..4b4ed1a 100644 --- a/Project/applications/smartcities/include/ntp.h +++ b/Project/applications/smartcities/include/ntp.h @@ -8,9 +8,17 @@ #include "lwip/sys.h" #include "lwip/api.h" #include "ch.h" +#include +#include +#include +#include +#include +#include +#include + #define NTP_PACKET_LENGTH 48 -//char* remoteIpString ="81.184.154.182"; +#define NTP_SERVER "81.184.154.182" //const uint16_t remotePort = 123; @@ -40,6 +48,7 @@ void udpNtp_dataReceivedCb(void * arg, struct udp_pcb * upcb,struct pbuf * first uint32_t udpNtp_setupConnection(void); uint32_t udpNtp_setupConnectionL(void); uint32_t read32(char* buffer, int offset); +char* timestamp_data(char* value,Date time); #endif diff --git a/Project/applications/smartcities/json.c b/Project/applications/smartcities/json.c index 1ce006c..b679bc5 100644 --- a/Project/applications/smartcities/json.c +++ b/Project/applications/smartcities/json.c @@ -116,8 +116,6 @@ uint8_t send_json(char* statement, uint32_t length, char* provider_ID, char* sen strcpy(PATH+6+strlen(provider_ID),sensor_ID); PATH[7+strlen(provider_ID)+strlen(sensor_ID)] = '\0'; } - printf("%s: %d\n\r",PATH,strlen(PATH)); - printf("%s: %d\n\r",URL,strlen(URL)); struct httpHeaders server = { put,PATH,strlen(PATH),URL,strlen(URL)}; response_code = httpRequest(server, statement, length); chHeapFree(PATH); diff --git a/Project/applications/smartcities/ntp.c b/Project/applications/smartcities/ntp.c index 13b4ba5..bab8240 100644 --- a/Project/applications/smartcities/ntp.c +++ b/Project/applications/smartcities/ntp.c @@ -1,12 +1,3 @@ -#include -#include -#include -#include -#include -#include -#include -#include - #include "ntp.h" #define DBG(fmt,...) if(1){printf("[NTP] "fmt"\r\n", ##__VA_ARGS__);}else{({});} @@ -52,7 +43,6 @@ UDP TRANSMISSION HANDLING FUNCTIONS * Sends a new message to the remote peer */ void udpNtp_send(){ - char* remoteIpString ="81.184.154.182";/*"192.168.43.83";"165.193.126.229";*/ uint16_t remotePort = 123; char packetBuffer[NTP_PACKET_LENGTH]; static uint32_t messageCount = 0; @@ -73,7 +63,7 @@ void udpNtp_send(){ err_t err; //Convert the IP address from string to lwip format - ipaddr = ipaddr_addr(remoteIpString); + ipaddr = ipaddr_addr(NTP_SERVER); ip4_addr_set_u32((&addr), ipaddr); /* @@ -302,6 +292,36 @@ void udpNTP_Setup(){ udpNtp_process(); } +char* timestamp_data(char* value,Date time) +{ + uint8_t length = strlen(value) + strlen(",00/00/0000T00:00:00") + 1; + char str_day[3],str_month[3],str_year[5],str_hour[3],str_minute[3],str_second[3]; + char* data = chHeapAlloc(NULL,length*sizeof(char)); + + sprintf(str_day,"%d",time.day); + sprintf(str_month,"%d",time.month); + sprintf(str_year,"%d",time.year); + sprintf(str_hour,"%d",time.hour); + sprintf(str_minute,"%d",time.minute); + sprintf(str_second,"%d",time.second); + + strcpy(data,value); + strcat(data,","); + strcat(data, str_day); + strcat(data,"/"); + strcat(data, str_month); + strcat(data,"/"); + strcat(data, str_year); + strcat(data,"T"); + strcat(data, str_hour); + strcat(data,":"); + strcat(data, str_minute); + strcat(data,":"); + strcat(data, str_second); + data[length-1] = '\0'; + return data; +} + /*Funcion para testeo del resto de funciones relacionadas con NTP. En el main() el orden de ejecuciĆ³n debe ser el siguiente. 1ro: udpNTP_Setup diff --git a/Project/applications/smartcities/test_httpClient.c b/Project/applications/smartcities/test_httpClient.c deleted file mode 100644 index 493000a..0000000 --- a/Project/applications/smartcities/test_httpClient.c +++ /dev/null @@ -1,133 +0,0 @@ -#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