diff --git a/Project/applications/smartcities/json.c b/Project/applications/smartcities/json.c index b679bc5..b659d51 100644 --- a/Project/applications/smartcities/json.c +++ b/Project/applications/smartcities/json.c @@ -26,7 +26,7 @@ char* prepare_json_register_statement(module mod, sensor sens) char ID[MODULE_ID_LENGTH+3]; length = 23; strcpy(ID,mod.ID); - sprintf(ID+MODULE_ID_LENGTH,"%d",sens.ID); + sprintf(ID+MODULE_ID_LENGTH,"%X",sens.ID); str_aux = join_strings("{\"sensors\":[{\"sensor\":\"",ID,length,MODULE_ID_LENGTH+2,JOIN_NO_FREE); length += MODULE_ID_LENGTH+2; str_aux2 = join_strings(str_aux,"\",\"description\":\"",length,17,JOIN_NO_FREE); @@ -98,23 +98,21 @@ uint8_t send_json(char* statement, uint32_t length, char* provider_ID, char* sen if(sensor_ID == NULL) //Register sensor { URL = (char*) chHeapAlloc(NULL,8+strlen(SERVER_HOSTNAME)); - strcpy(URL,"http://"); - strcpy(URL+7,SERVER_HOSTNAME); + strcpy(URL,SERVER_HOSTNAME); PATH = (char*) chHeapAlloc(NULL,10+strlen(provider_ID)); strcpy(PATH,"/catalog/"); strcpy(PATH+9,provider_ID); } else //Post data { - URL = (char*) chHeapAlloc(NULL,8+strlen(SERVER_HOSTNAME)); - strcpy(URL,"http://"); - strcpy(URL+7,SERVER_HOSTNAME); - URL[7+strlen(SERVER_HOSTNAME)] = '\0'; - PATH = (char*) chHeapAlloc(NULL,8+strlen(provider_ID)+strlen(sensor_ID)); - strcpy(PATH,"/data/"); - strcpy(PATH+6,provider_ID); - strcpy(PATH+6+strlen(provider_ID),sensor_ID); - PATH[7+strlen(provider_ID)+strlen(sensor_ID)] = '\0'; + URL = (char*) chHeapAlloc(NULL,1+strlen(SERVER_HOSTNAME)); + strcpy(URL,SERVER_HOSTNAME); + URL[strlen(SERVER_HOSTNAME)] = '\0'; + PATH = (char*) chHeapAlloc(NULL,22+strlen(provider_ID)+strlen(sensor_ID)); + 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'; } struct httpHeaders server = { put,PATH,strlen(PATH),URL,strlen(URL)}; response_code = httpRequest(server, statement, length); diff --git a/Project/applications/smartcities/main_demo.c b/Project/applications/smartcities/main_demo.c new file mode 100644 index 0000000..db4a68f --- /dev/null +++ b/Project/applications/smartcities/main_demo.c @@ -0,0 +1,145 @@ +#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 "json.h" +#include "ntp.h" +#include "i2c.h" + +#define WIFI_MODE WIFI_MODE_CLIENT +#define NETWORK_SSID "linksys" +#define NETWORK_KEY "" +#define WPA_USER "smartcities" +#define WPA_PASS "superpass" + +uint8_t connected = 0; + +void initLibwismart(void) +{ + wismart_hwif_t hwif = libwismart_GetDefaultHWIF(); + libwismart_Init(hwif); +} + +int main(void) +{ + 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; + initLibwismart(); + libwismart_PowerSave_Enable(); + libwismart_PowerSave_HigherProfile(TRUE); + libwismart_RegisterDhcpCB(dhcp_connect_result_cb); + libwismart_WiFiInit(); + libwismart_WiFiConnectEnterprise(NETWORK_SSID, &wpa, wifi_connect_result_cb); + + printf("WiSmart initialized\r\n"); + + while(!connected) + {chThdSleepMilliseconds(500);} + + uint16_t received_data; + char answer[10]; + + I2C_init(); + printf("I2C Initialized\r\n"); + /*I2C_start(I2C1,0x77 << 1, I2C_Direction_Transmitter); + I2C_write(I2C1, 0xF4); + I2C_write(I2C1, 0x2E); + I2C_stop(I2C1); + printf("I2C Temperature measurment started\r\n"); + + I2C_start(I2C1,0x77 << 1, I2C_Direction_Transmitter); + I2C_write(I2C1, 0xF6); + I2C_stop(I2C1); + printf("I2C Temperature measurment request sent\r\n"); + + I2C_start(I2C1, 0x77<<1, I2C_Direction_Receiver); + printf("I2C Read request sent\r\n"); + received_data = I2C_read_ack(I2C1); + received_data = received_data << 8; + received_data = received_data | I2C_read_ack(I2C1); + printf("I2C data received\r\n"); + + sprintf(answer,"%d",received_data); + printf("Data: %s\r\n",answer);*/ + + I2C_start(I2C1,0x39 << 1, I2C_Direction_Transmitter); + I2C_write(I2C1, 0x0C); + I2C_stop(I2C1); + printf("I2C Temperature measurment request sent\r\n"); + + I2C_start(I2C1, 0x39<<1, I2C_Direction_Receiver); + printf("I2C Read request sent\r\n"); + received_data = I2C_read_ack(I2C1); + received_data = received_data << 8; + received_data = received_data | I2C_read_ack(I2C1); + printf("I2C data received\r\n"); + + sprintf(answer,"%d",received_data); + printf("Data: %s\r\n",answer); + + char **buffer; + buffer = chHeapAlloc(NULL,sizeof (char *) * 1); + + udpNTP_Setup(); + Date time=getDate(); + + libwismart_EnableBsdSocketAPI(); + + buffer[0] = timestamp_data(answer,time); + + printf("Init JSON statement\r\n"); + char* statement = prepare_json_observation_statement(buffer,1); + chHeapFree(buffer[0]); + printf("Statement: "); + printf("%s\r\n",statement); + uint8_t result = send_json(statement, strlen(statement), "bmp", "085"); + if(result == JSON_POST_OK) + { + printf("JSON_POST_OK\r\n"); + } + else if(result == JSON_COMM_ERROR) + { + printf("JSON_COMM_ERROR\r\n"); + } + else if(result == JSON_OTHER_ERROR) + { + printf("JSON_OTHER_ERROR\r\n"); + } + else + { + printf("WTF I don't even\r\n"); + } + + 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); + } +} +