From ad23cb4c6dc8673b239e416c41253478927736c6 Mon Sep 17 00:00:00 2001 From: Imanol-Mikel Barba Sabariego Date: Sun, 25 May 2014 23:47:58 +0000 Subject: [PATCH] --- Project/applications/smartcities/Makefile | 2 +- Project/applications/smartcities/i2c.c | 1 + Project/applications/smartcities/include/globals.h | 1 + Project/applications/smartcities/include/module.h | 6 ++++-- Project/applications/smartcities/json.c | 5 +++-- Project/applications/smartcities/main2.c | 23 +++++++++++++++++++++++ Project/applications/smartcities/module.c | 3 --- Project/applications/smartcities/sensors.c | 12 ++++++------ 8 files changed, 39 insertions(+), 14 deletions(-) delete mode 100644 Project/applications/smartcities/module.c diff --git a/Project/applications/smartcities/Makefile b/Project/applications/smartcities/Makefile index f516dd7..f71930f 100644 --- a/Project/applications/smartcities/Makefile +++ b/Project/applications/smartcities/Makefile @@ -10,7 +10,7 @@ SDK_ROOT = ../../ PROJECT_OUT = smartcities -USER_SRC = main2.c configServer.c httpClient.c callbacks.c module.c sensors.c json.c i2c.c ntp.c buffer.c timer-loop.c adc.c +USER_SRC = main2.c configServer.c httpClient.c callbacks.c sensors.c json.c i2c.c ntp.c buffer.c timer-loop.c adc.c USER_INCDIR = include/ makefsdata/ # if you need to add build Defines options add to USER_DEFS define diff --git a/Project/applications/smartcities/i2c.c b/Project/applications/smartcities/i2c.c index 6e329ef..c872bb2 100644 --- a/Project/applications/smartcities/i2c.c +++ b/Project/applications/smartcities/i2c.c @@ -165,4 +165,5 @@ void I2C_scan(uint8_t *addresses) I2C_reset(); } } + register_sensor(battery); } \ No newline at end of file diff --git a/Project/applications/smartcities/include/globals.h b/Project/applications/smartcities/include/globals.h index 3feaee7..d41b141 100644 --- a/Project/applications/smartcities/include/globals.h +++ b/Project/applications/smartcities/include/globals.h @@ -6,5 +6,6 @@ #define MODULE_ID_LENGTH 6 #define BUFFER_LENGTH 30 #define MAX_RAM 30000 +#define MODULE_ID "123456" #endif \ No newline at end of file diff --git a/Project/applications/smartcities/include/module.h b/Project/applications/smartcities/include/module.h index ede47e5..71d5c4d 100644 --- a/Project/applications/smartcities/include/module.h +++ b/Project/applications/smartcities/include/module.h @@ -1,9 +1,11 @@ #ifndef MODULE_H #define MODULE_H +#include "globals.h" + typedef struct { - char* ID; - char* geoloc; + char ID[MODULE_ID_LENGTH +1]; + char geoloc[20]; } module; extern module mod; diff --git a/Project/applications/smartcities/json.c b/Project/applications/smartcities/json.c index cdba6cd..23150ff 100644 --- a/Project/applications/smartcities/json.c +++ b/Project/applications/smartcities/json.c @@ -19,7 +19,8 @@ uint8_t register_sensor(sensor sens) printf("%s\r\n",statement); char sensor_ID[3]; sprintf(sensor_ID,"%d",sens.ID); - result = send_json(statement,strlen(statement),mod.ID,sensor_ID); + printf("REMOVE COMMENTARY IN send_json ON json.c LINE 23!!!!!!!\r\n"); + //result = send_json(statement,strlen(statement),mod.ID,sensor_ID); chHeapFree(statement); return result; } @@ -52,7 +53,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,"%X",sens.ID); + sprintf(ID+MODULE_ID_LENGTH,"%02X",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); diff --git a/Project/applications/smartcities/main2.c b/Project/applications/smartcities/main2.c index 4b8acc5..97ca8e6 100644 --- a/Project/applications/smartcities/main2.c +++ b/Project/applications/smartcities/main2.c @@ -34,6 +34,8 @@ uint8_t retries=0; uint8_t sensors_length=0; uint8_t registry_opened=0; +module mod; + void update_time(unsigned long *time) { printf("Requesting new NTP time...\r\n"); @@ -70,6 +72,7 @@ void init_registry(void) printf("Geo Localization = %s\r\n", config.localization); strcpy(mod.geoloc,config.localization); + strcpy(mod.ID,MODULE_ID); printf("%s\r\n", mod.geoloc); if(config.security == PROFILE_SECURITY_OPEN) { @@ -178,6 +181,19 @@ void wifi_connect(void) } } +void send_battery_level(unsigned long timestamp) +{ + char *batt_level = battery_value(get_battery_data()); + char *batt_data = timestamp_data(batt_level,getDate(timestamp)); + chHeapFree(batt_level); + char *statement = prepare_json_observation_statement(&batt_data,1); + chHeapFree(batt_data); + char id[3]; + sprintf(id,"%d",battery.ID); + send_json(statement,strlen(statement),id,mod.ID); + chHeapFree(statement); +} + void wifi_disconnect(void){ uint8_t res=libwismart_WiFiDisconnect(); if(res) @@ -204,6 +220,7 @@ int main(void) char* valueSensors[TOTAL_SENSORS]; memset (valueSensors, 0, TOTAL_SENSORS); + printf("Connecting to wifi...\r\n"); wifi_connect(); @@ -262,6 +279,8 @@ int main(void) /* Send data to server, empty the buffer */ send_data(buffers, ind, sizes, sensors); printf("Data sent!\r\n"); + //Now sending battery level + send_battery_level(timestamp); //time = getNTPTime(); update_time(&time); delay = getSystemTime(); @@ -277,6 +296,8 @@ int main(void) 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(); } @@ -314,6 +335,8 @@ int main(void) } } + //Now sending battery level + send_battery_level(timestamp); wifi_disconnect(); } time += getElapsedTime(delay); diff --git a/Project/applications/smartcities/module.c b/Project/applications/smartcities/module.c deleted file mode 100644 index 527afc7..0000000 --- a/Project/applications/smartcities/module.c +++ /dev/null @@ -1,3 +0,0 @@ -#include "module.h" - -module mod = {"123456", "41.39479 2.148768"}; /* append 0/1 flag for read write */ \ No newline at end of file diff --git a/Project/applications/smartcities/sensors.c b/Project/applications/smartcities/sensors.c index f93bb0d..4d849b0 100644 --- a/Project/applications/smartcities/sensors.c +++ b/Project/applications/smartcities/sensors.c @@ -1,11 +1,11 @@ #include "sensors.h" -sensor light_sensor = {LIGHT_ADDR, "Light sensor", "illumination", "lux",NULL}; -sensor ultrasound_sensor = {DISTANCE_ADDR, "Ultrasound sensor", "distance", "cm",NULL}; -sensor pressure_sensor = {PRESSURE_ADDR, "Pressure sensor", "pressure", "hPa",NULL}; -sensor humidity_temp_sensor = {HUMIDITY_TEMP_ADDR, "Humidity and Temperature","ºC,RH",NULL}; -sensor sound_sensor = {SOUND_ADDR, "Sound sensor","mV",NULL}; -sensor battery = {BATTERY_ADDR, "Battery Level","mV",NULL}; +sensor light_sensor = {LIGHT_ADDR, "Light sensor", "illumination", "lux", NULL}; +sensor ultrasound_sensor = {DISTANCE_ADDR, "Ultrasound sensor", "distance", "cm", NULL}; +sensor pressure_sensor = {PRESSURE_ADDR, "Pressure sensor", "pressure", "hPa", NULL}; +sensor humidity_temp_sensor = {HUMIDITY_TEMP_ADDR, "Temperature and humidity sensor", "temperature,humidity", "ºC,RH",NULL}; +sensor sound_sensor = {SOUND_ADDR, "Sound sensor", "sound", "mV", NULL}; +sensor battery = {BATTERY_ADDR, "Battery Level", "power", "mV", NULL}; sensor* sensors[TOTAL_SENSORS+1] = {&light_sensor,&ultrasound_sensor,&pressure_sensor,&humidity_temp_sensor,&sound_sensor,&battery}; -- libgit2 0.22.2