From ce796b71c09b9ba20752a5aa836d432aa93ebc83 Mon Sep 17 00:00:00 2001 From: Imanol-Mikel Barba Sabariego Date: Tue, 29 Apr 2014 12:05:15 +0000 Subject: [PATCH] --- Project/applications/smartcities/include/sensors.h | 47 ++++++++++++++++++++++++++++++++++++++++++----- Project/applications/smartcities/json.c | 6 ++++++ Project/applications/smartcities/sensors.c | 259 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------- 3 files changed, 273 insertions(+), 39 deletions(-) diff --git a/Project/applications/smartcities/include/sensors.h b/Project/applications/smartcities/include/sensors.h index 2d301a1..25b8771 100644 --- a/Project/applications/smartcities/include/sensors.h +++ b/Project/applications/smartcities/include/sensors.h @@ -5,22 +5,59 @@ #include #include "i2c.h" #include "libwismart.h" +#include "module.h" +#include "json.h" +#define LIGHT_ADDR 0x39 +#define DISTANCE_ADDR 0x01 +#define PRESSURE_ADDR 0x77 typedef struct { uint8_t ID; char* description; char* type; char* unit; + char* additional_data; } sensor; -// PRESSURE SENSOR FUNCTIONS +uint8_t register_sensor(sensor sens); +//SENSOR DEFINITIONS +extern sensor light_sensor; +extern sensor ultrasound_sensor; extern sensor pressure_sensor; + +//SENSOR FUNCTIONS + +//LIGHT SENSOR +uint32_t get_light_data(void); +uint16_t get_light_ch0(void); +uint16_t get_light_ch1(void); + +//ULTRASONIC SENSOR +uint16_t get_distance_data(void); +void init_ultrasound(void); +uint8_t get_distance_low(void); +uint8_t get_distance_high(void); + +//PRESSURE SENSOR +typedef struct { + uint16_t AC1; + uint16_t AC2; + uint16_t AC3; + uint16_t AC4; + uint16_t AC5; + uint16_t AC6; + uint16_t B1; + uint16_t B2; + uint16_t MB; + uint16_t MC; + uint16_t MD; +} bmp085_callibration; + uint16_t get_pressure_data(void); -void pressure_start_conversion(void); -void pressure_get_coefficients(void); -uint16_t pressure_read_pressure_data(void); -uint16_t pressure_process_data(uint16_t data); +bmp085_callibration get_callibration_data(void); +char* callibration_data_csv(bmp085_callibration parameters); +void init_pressure(void); #endif \ No newline at end of file diff --git a/Project/applications/smartcities/json.c b/Project/applications/smartcities/json.c index b659d51..bac3d42 100644 --- a/Project/applications/smartcities/json.c +++ b/Project/applications/smartcities/json.c @@ -53,6 +53,12 @@ char* prepare_json_register_statement(module mod, sensor sens) str_aux = join_strings(str_aux2,mod.geoloc,length,strlen(mod.geoloc),JOIN_NO_FREE); chHeapFree(str_aux2); length += strlen(mod.geoloc); + if(sens.additional_info != NULL) + { + str_aux2 = join_strings(str_aux,sens.additional_info,length,strlen(sens.additional_info),JOIN_NO_FREE); + chHeapFree(str_aux2); + length += strlen(sens.additional_info); + } json_statement = join_strings(str_aux,"\"}]}\0",length,5,JOIN_NO_FREE); chHeapFree(str_aux); return json_statement; diff --git a/Project/applications/smartcities/sensors.c b/Project/applications/smartcities/sensors.c index 780eef4..749b44a 100644 --- a/Project/applications/smartcities/sensors.c +++ b/Project/applications/smartcities/sensors.c @@ -1,57 +1,248 @@ #include "sensors.h" -sensor pressure_sensor = {0xC0, "Presure sensor", "pressure", "KPa"}; /* append 0/1 flag for read write */ +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", "kPa",NULL}; -uint16_t get_pressure_data() +uint8_t register_sensor(sensor sens) { - I2C_init(); - printf("I2C Initialized\r\n"); - pressure_start_conversion(); - chThdSleepMilliseconds(10000); - pressure_get_coefficients(); - return pressure_process_data(pressure_read_pressure_data()); + uint8_t result; + if(sens.ID == PRESSURE_ADDR) + { + sens.additional_data = callibration_pressure_data_csv(get_pressure_callibration_data()); + } + char *statement = prepare_json_register_statement(mod,sens); + chHeapFree(sens.additional_data); + sens.additional_data = NULL; + result = send_json(statement,strlen(statement),mod.ID,sens.ID); + chHeapFree(statement); + return result; } -void pressure_start_conversion() +uint32_t get_light_data(void) { - I2C_start(I2C1,pressure_sensor.ID << 1, I2C_Direction_Transmitter); - I2C_write(I2C1, 0x12); - I2C_stop(I2C1); - printf("I2C Pressure measurment request sent\r\n"); + uint32_t data = 0; + + data = get_light_ch1(); + data = data << 16; + data = data | get_light_ch0(); + + return data; } +uint16_t get_light_ch0(void) +{ + uint16_t data = 0; + + I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter); + I2C_write(I2C1, 0x0D); + I2C_stop(I2C1); + + I2C_start(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver); + data = I2C_read_ack(I2C1); + data = data << 8; + + I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter); + I2C_write(I2C1, 0x0C); + I2C_stop(I2C1); + + I2C_start(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver); + data = data | I2C_read_ack(I2C1); + I2C_stop(I2C1); -void pressure_get_coefficients() + return data; +} +uint16_t get_light_ch1(void) { + uint16_t data = 0; + + I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter); + I2C_write(I2C1, 0x0F); + I2C_stop(I2C1); + + I2C_start(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver); + data = I2C_read_ack(I2C1); + data = data << 8; + + I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter); + I2C_write(I2C1, 0x0E); + I2C_stop(I2C1); + I2C_start(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver); + data = data | I2C_read_ack(I2C1); + I2C_stop(I2C1); + + return data; } -uint16_t pressure_read_pressure_data() +uint16_t get_distance_data(void) { - uint16_t received_data; - I2C_start(I2C1,pressure_sensor.ID << 1, I2C_Direction_Transmitter); + init_ultrasound(); + uint16_t data = get_distance_high(); + data = data << 8; + data = data | get_distance_low(); + return data; +} +void init_ultrasound(void) +{ + I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter); I2C_write(I2C1, 0x00); I2C_stop(I2C1); - printf("I2C Read MSB pressure data sent\r\n"); - I2C_start(I2C1, pressure_sensor.ID << 1, I2C_Direction_Receiver); - printf("I2C Read request sent\r\n"); - received_data = I2C_read_ack(I2C1); - received_data = received_data << 8; - printf("I2C data received\r\n"); - - I2C_start(I2C1,pressure_sensor.ID << 1, I2C_Direction_Transmitter); - I2C_write(I2C1, 0x01); + I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter); + I2C_write(I2C1, 0x50); I2C_stop(I2C1); - printf("I2C Read LSB pressure data sent\r\n"); - I2C_start(I2C1, pressure_sensor.ID << 1, I2C_Direction_Receiver); - printf("I2C Read request sent\r\n"); - received_data = received_data | I2C_read_ack(I2C1); - printf("I2C data received\r\n"); - return received_data; + chThdSleepMilliseconds(70); +} +uint8_t get_distance_low(void) +{ + uint8_t data; + I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter); + I2C_write(I2C1, 0x03); + I2C_stop(I2C1); + + I2C_start(I2C1, DISTANCE_ADDR << 1, I2C_Direction_Receiver); + data = I2C_read_ack(I2C1); + I2C_stop(I2C1); + return data; +} +uint8_t get_distance_high(void) +{ + uint8_t data; + I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter); + I2C_write(I2C1, 0x02); + I2C_stop(I2C1); + + I2C_start(I2C1, DISTANCE_ADDR << 1, I2C_Direction_Receiver); + data = I2C_read_ack(I2C1); + I2C_stop(I2C1); + return data; } -uint16_t pressure_process_data(uint16_t data) +uint16_t get_pressure_data(void) { + uint16_t data; + init_pressure(); + + I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter); + I2C_write(I2C1, 0xF6); + I2C_stop(I2C1); + + I2C_start(I2C1, DISTANCE_ADDR << 1, I2C_Direction_Receiver); + data = I2C_read_ack(I2C1); + data = data << 8; + data = data | I2C_read_nack(I2C1); return data; -} \ No newline at end of file +} + +bmp085_callibration get_pressure_callibration_data(void) +{ + bmp085_callibration calib_data; + + I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); + I2C_write(I2C1, 0xAA); + I2C_stop(I2C1); + I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); + calib_data.AC1 = I2C_read_ack(I2C1); + I2C_stop(I2C1); + + I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); + I2C_write(I2C1, 0xAC); + I2C_stop(I2C1); + I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); + calib_data.AC2 = I2C_read_ack(I2C1); + I2C_stop(I2C1); + + I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); + I2C_write(I2C1, 0xAE); + I2C_stop(I2C1); + I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); + calib_data.AC3 = I2C_read_ack(I2C1); + I2C_stop(I2C1); + + I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); + I2C_write(I2C1, 0xB0); + I2C_stop(I2C1); + I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); + calib_data.AC4 = I2C_read_ack(I2C1); + I2C_stop(I2C1); + + I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); + I2C_write(I2C1, 0xB2); + I2C_stop(I2C1); + I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); + calib_data.AC5 = I2C_read_ack(I2C1); + I2C_stop(I2C1); + + I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); + I2C_write(I2C1, 0xB4); + I2C_stop(I2C1); + I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); + calib_data.AC6 = I2C_read_ack(I2C1); + I2C_stop(I2C1); + + I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); + I2C_write(I2C1, 0xB6); + I2C_stop(I2C1); + I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); + calib_data.B1 = I2C_read_ack(I2C1); + I2C_stop(I2C1); + + I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); + I2C_write(I2C1, 0xB8); + I2C_stop(I2C1); + I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); + calib_data.B2 = I2C_read_ack(I2C1); + I2C_stop(I2C1); + + I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); + I2C_write(I2C1, 0xBA); + I2C_stop(I2C1); + I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); + calib_data.MB = I2C_read_ack(I2C1); + I2C_stop(I2C1); + + I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); + I2C_write(I2C1, 0xBC); + I2C_stop(I2C1); + I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); + calib_data.MC = I2C_read_ack(I2C1); + I2C_stop(I2C1); + + I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); + I2C_write(I2C1, 0xBE); + I2C_stop(I2C1); + I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); + calib_data.MD = I2C_read_ack(I2C1); + I2C_stop(I2C1); + + return calib_data; +} +char* callibration_pressure_data_csv(bmp085_callibration parameters) +{ + uint8_t i; + char *str = chHeapAlloc(NULL,sizeof(char)*(4*11-1)); + memset(str,0x00,sizeof(char)*(4*11-1)); + sprintf(str + strlen(str),"%d,",parameters.AC1); + sprintf(str + strlen(str),"%d,",parameters.AC2); + sprintf(str + strlen(str),"%d,",parameters.AC3); + sprintf(str + strlen(str),"%d,",parameters.AC4); + sprintf(str + strlen(str),"%d,",parameters.AC5); + sprintf(str + strlen(str),"%d,",parameters.AC6); + sprintf(str + strlen(str),"%d,",parameters.B1); + sprintf(str + strlen(str),"%d,",parameters.B2); + sprintf(str + strlen(str),"%d,",parameters.MB); + sprintf(str + strlen(str),"%d,",parameters.MC); + sprintf(str + strlen(str),"%d",parameters.MD); + return str; +} + +void init_pressure(void) +{ + I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); + I2C_write(I2C1, 0xF4); + I2C_write(I2C1, 0x34); + I2C_stop(I2C1); + + chThdSleepMilliseconds(5); +} -- libgit2 0.22.2