diff --git a/Project/applications/smartcities/Makefile b/Project/applications/smartcities/Makefile index 6b2518b..3be00e8 100644 --- a/Project/applications/smartcities/Makefile +++ b/Project/applications/smartcities/Makefile @@ -10,7 +10,7 @@ SDK_ROOT = ../../ PROJECT_OUT = smartcities -USER_SRC = main.c configServer.c httpClient.c callbacks.c module.c sensors.c json.c i2c.c ntp.c buffer.c +USER_SRC = main.c configServer.c httpClient.c callbacks.c module.c sensors.c json.c i2c.c ntp.c buffer.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/adc.c b/Project/applications/smartcities/adc.c index fb42fbc..8f03fc8 100644 --- a/Project/applications/smartcities/adc.c +++ b/Project/applications/smartcities/adc.c @@ -1,56 +1,28 @@ #include "adc.h" -#define DBG(fmt,...) if(1){printf("[ADC] "fmt"\r\n", ##__VA_ARGS__);}else{({});} - - -#define ADC_MIN_VALUE (0) -#define ADC_MAX_VALUE (0xfff) /* 12-bit adc */ -#define ADC_VREF_PLUS (3300) /* Vref+ is connected with Vdda (3.3v) */ -#define ADC_VREF_MINUS (0) /* Vref- is connected with Vssa (0v) */ - -#define ADCbatt ADC1 -#define ADCbatt_RCC RCC_APB2Periph_ADC1 /* */ -#define ADCbatt_DIVIDER RCC_PCLK2_Div4 /* Can be 2/4/6/8. PCLK2 runs at 32Mhz, and max ADC clock is 14Mhz */ -#define ADCbatt_CHANNEL ADC_Channel_12 /* 0/17 */ -#define ADCbatt_SAMPLETIME ADC_SampleTime_239Cycles5 -#define ADCbatt_GPIO_PIN GPIO_Pin_2 -#define ADCbatt_GPIO_PORT GPIOC -#define ADCbatt_GPIO_RCC RCC_APB2Periph_GPIOC -#define ADCbatt_GPIO_STR "PC2" - -#define ADCsound ADC1 -#define ADCsound_RCC RCC_APB2Periph_ADC1 /* */ -#define ADCsound_DIVIDER RCC_PCLK2_Div4 /* Can be 2/4/6/8. PCLK2 runs at 32Mhz, and max ADC clock is 14Mhz */ -#define ADCsound_CHANNEL ADC_Channel_12 /* 0/17 */ -#define ADCsound_SAMPLETIME ADC_SampleTime_239Cycles5 -#define ADCsound_GPIO_PIN GPIO_Pin_2 -#define ADCsound_GPIO_PORT GPIOC -#define ADCsound_GPIO_RCC RCC_APB2Periph_GPIOC -#define ADCsound_GPIO_STR "PC2" - -void adc_init() +void adc_batt_init() { - adc_peripheralInit(); + adc_batt_peripheralInit(); } -uint32_t adc_process() +uint32_t adc_batt_process() { uint16_t adcRegisterValue; uint32_t analogValue; - adcRegisterValue = adc_read(); + adcRegisterValue = adc_batt_read(); /* * Calculate the register value to volts * 1. adcRegisterValue Range is [ADC_MIN_VALUE, ADC_MAX_VALUE] * 2. analogValue Range is [ADC_VREF_MINUS, ADC_VREF_PLUS] */ - analogValue = ADC_VREF_MINUS + (((adcRegisterValue - ADC_MIN_VALUE)*(ADC_VREF_PLUS - ADC_VREF_MINUS))/(ADC_MAX_VALUE - ADC_MIN_VALUE)); + analogValue = ADCbatt_VREF_MINUS + (((adcRegisterValue - ADCbatt_MIN_VALUE)*(ADCbatt_VREF_PLUS - ADCbatt_VREF_MINUS))/(ADCbatt_MAX_VALUE - ADCbatt_MIN_VALUE)); DBG("Register value is %4u [%4u milliVolts are currently given to gpio %s]",adcRegisterValue, analogValue, ADCbatt_GPIO_STR); return analogValue; } -void adc_peripheralInit() +void adc_batt_peripheralInit() { GPIO_InitTypeDef GPIO_InitStructure; ADC_InitTypeDef ADC_InitStructure; @@ -97,7 +69,7 @@ void adc_peripheralInit() while(ADC_GetCalibrationStatus(ADCbatt)); } -uint16_t adc_read() +uint16_t adc_batt_read() { ADC_RegularChannelConfig(ADCbatt, ADCbatt_CHANNEL, 1, ADCbatt_SAMPLETIME); /* Start the conversion */ @@ -108,5 +80,83 @@ uint16_t adc_read() return ADC_GetConversionValue(ADCbatt); } +void adc_sound_init() +{ + adc_sound_peripheralInit(); +} +uint32_t adc_sound_process() +{ + uint16_t adcRegisterValue; + uint32_t analogValue; + + adcRegisterValue = adc_sound_read(); + + /* + * Calculate the register value to volts + * 1. adcRegisterValue Range is [ADC_MIN_VALUE, ADC_MAX_VALUE] + * 2. analogValue Range is [ADC_VREF_MINUS, ADC_VREF_PLUS] + */ + analogValue = ADCsound_VREF_MINUS + (((adcRegisterValue - ADCsound_MIN_VALUE)*(ADCsound_VREF_PLUS - ADCsound_VREF_MINUS))/(ADCsound_MAX_VALUE - ADCsound_MIN_VALUE)); + DBG("Register value is %4u [%4u milliVolts are currently given to gpio %s]",adcRegisterValue, analogValue, ADCbatt_GPIO_STR); + return analogValue; +} + +void adc_sound_peripheralInit() +{ + GPIO_InitTypeDef GPIO_InitStructure; + ADC_InitTypeDef ADC_InitStructure; + RCC_APB2PeriphClockCmd(ADCsound_GPIO_RCC, ENABLE); + GPIO_InitStructure.GPIO_Pin = ADCsound_GPIO_PIN; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_Init(ADCsound_GPIO_PORT, &GPIO_InitStructure); + + + /* Max ADC clk is 14mhz and because PCLK2 runs @32Mhz */ + RCC_ADCCLKConfig(ADCsound_DIVIDER); + /* Enable ADCsound clock so that we can talk to it */ + RCC_APB2PeriphClockCmd(ADCsound_RCC, ENABLE); + /* Put everything back to power-on defaults */ + ADC_DeInit(ADCsound); + + + /* ADC1 and ADC2 operate independently */ + ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; + /* Disable the scan conversion so we do one at a time */ + ADC_InitStructure.ADC_ScanConvMode = DISABLE; + /* Don't do contimuous conversions - do them on demand */ + ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; + /* Start conversin by software, not an external trigger */ + ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; + /* Conversions are 12 bit - put them in the lower 12 bits of the result */ + ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; + /* Say how many channels would be used by the sequencer */ + ADC_InitStructure.ADC_NbrOfChannel = 1; + + /* Now do the setup */ + ADC_Init(ADCsound, &ADC_InitStructure); + /* Enable ADCsound */ + ADC_Cmd(ADCsound, ENABLE); + + /* Enable ADCsound reset calibaration register */ + ADC_ResetCalibration(ADCsound); + /* Check the end of ADCsound reset calibration register */ + while(ADC_GetResetCalibrationStatus(ADCsound)); + /* Start ADCsound calibaration */ + ADC_StartCalibration(ADCsound); + /* Check the end of ADC1 calibration */ + while(ADC_GetCalibrationStatus(ADCsound)); +} + +uint16_t adc_sound_read() +{ + ADC_RegularChannelConfig(ADCsound, ADCsound_CHANNEL, 1, ADCsound_SAMPLETIME); + /* Start the conversion */ + ADC_SoftwareStartConvCmd(ADCsound, ENABLE); + /* Wait until conversion completion */ + while(ADC_GetFlagStatus(ADCsound, ADC_FLAG_EOC) == RESET); + /* Get the conversion value */ + return ADC_GetConversionValue(ADCsound); +} diff --git a/Project/applications/smartcities/adc.h b/Project/applications/smartcities/adc.h index aa31d46..85a8a47 100644 --- a/Project/applications/smartcities/adc.h +++ b/Project/applications/smartcities/adc.h @@ -7,9 +7,46 @@ #include "stm32f10x_gpio.h" #include "stm32f10x_adc.h" -void adc_init(void); -uint32_t adc_process(void); -uint16_t adc_read(void); -void adc_peripheralInit(void); +#define DBG(fmt,...) if(1){printf("[ADC] "fmt"\r\n", ##__VA_ARGS__);}else{({});} + +#define ADCbatt_MIN_VALUE (0) +#define ADCbatt_MAX_VALUE (0xfff) /* 12-bit adc */ +#define ADCbatt_VREF_PLUS (3300) /* Vref+ is connected with Vdda (3.3v) */ +#define ADCbatt_VREF_MINUS (0) /* Vref- is connected with Vssa (0v) */ + +#define ADCsound_MIN_VALUE (0) +#define ADCsound_MAX_VALUE (0xfff) /* 12-bit adc */ +#define ADCsound_VREF_PLUS (2600) /* Vref+ is connected with Vdda (3.3v) */ +#define ADCsound_VREF_MINUS (0) /* Vref- is connected with Vssa (0v) */ + +#define ADCbatt ADC1 +#define ADCbatt_RCC RCC_APB2Periph_ADC1 /* */ +#define ADCbatt_DIVIDER RCC_PCLK2_Div4 /* Can be 2/4/6/8. PCLK2 runs at 32Mhz, and max ADC clock is 14Mhz */ +#define ADCbatt_CHANNEL ADC_Channel_12 /* 0/17 */ +#define ADCbatt_SAMPLETIME ADC_SampleTime_239Cycles5 +#define ADCbatt_GPIO_PIN GPIO_Pin_2 +#define ADCbatt_GPIO_PORT GPIOC +#define ADCbatt_GPIO_RCC RCC_APB2Periph_GPIOC +#define ADCbatt_GPIO_STR "PC2" + +#define ADCsound ADC2 +#define ADCsound_RCC RCC_APB2Periph_ADC2 /* */ +#define ADCsound_DIVIDER RCC_PCLK2_Div4 /* Can be 2/4/6/8. PCLK2 runs at 32Mhz, and max ADC clock is 14Mhz */ +#define ADCsound_CHANNEL ADC_Channel_12 /* 0/17 */ +#define ADCsound_SAMPLETIME ADC_SampleTime_239Cycles5 +#define ADCsound_GPIO_PIN GPIO_Pin_2 +#define ADCsound_GPIO_PORT GPIOA +#define ADCsound_GPIO_RCC RCC_APB2Periph_GPIOA +#define ADCsound_GPIO_STR "PA2" + +void adc_batt_init(void); +uint32_t adc_batt_process(void); +uint16_t adc_batt_read(void); +void adc_batt_peripheralInit(void); + +void adc_sound_init(void); +uint32_t adc_sound_process(void); +uint16_t adc_sound_read(void); +void adc_sound_peripheralInit(void); #endif \ No newline at end of file diff --git a/Project/applications/smartcities/include/sensors.h b/Project/applications/smartcities/include/sensors.h index 6adb26f..c27050f 100644 --- a/Project/applications/smartcities/include/sensors.h +++ b/Project/applications/smartcities/include/sensors.h @@ -6,12 +6,13 @@ #include "i2c.h" #include "libwismart.h" #include "module.h" -//#include "json.h" +#include "adc.h" #define LIGHT_ADDR 0x39 #define DISTANCE_ADDR 0x01 #define PRESSURE_ADDR 0x77 //Cambiar a MPL115A #define HUMIDITY_TEMP_ADDR 0x27 +#define SOUND_ADDR 0x27 //REMEMBER TO UPDATE I2C_scan ROUTINE WITH EACH NEW SENSOR!!!! #define TOTAL_SENSORS 4 @@ -74,4 +75,10 @@ uint16_t get_humidity_data(void); uint16_t get_temperature_data(void); char* temp_humidity_value(uint16_t temp, uint16_t humidity); +//SOUND SENSOR + +void init_sound(void); +uint32_t get_sound_data(void); +char* sound_value(uint32_t sound); + #endif diff --git a/Project/applications/smartcities/json.c b/Project/applications/smartcities/json.c index 68859b8..e3f17c0 100644 --- a/Project/applications/smartcities/json.c +++ b/Project/applications/smartcities/json.c @@ -1,5 +1,4 @@ #include "json.h" -#include "buffer.h" uint8_t register_sensor(sensor sens) { @@ -31,7 +30,6 @@ char* prepare_json_observation_statement(char** data, uint32_t nObservations) str_aux = join_strings(str_aux,str_aux2,length,observation_length,JOIN_FREE_MEM); length += observation_length; printf("data=%s %d \r\n",data[i],i); - check_memory(); } length--; //REMOVE LAST ',' json_statement = join_strings(str_aux,"]}\0",length,3,JOIN_NO_FREE); diff --git a/Project/applications/smartcities/main.c b/Project/applications/smartcities/main.c index 0205b85..85687a6 100644 --- a/Project/applications/smartcities/main.c +++ b/Project/applications/smartcities/main.c @@ -94,16 +94,11 @@ int main(void) libwismart_SetScanRunsForConnTimeout(4); //Edit a 4 libwismart_EnableBsdSocketAPI(); - struct httpHeaders head200 = { get, "/", 1, "http://www.w3.org/", 19 }; - struct httpHeaders head301 = { get, "/", 1, "w3.org", 6 }; - struct httpHeaders head404 = { get, "/errorerrorerror", 15, "www.w3.org" }; - uint32_t ind[4]={0}; char** buffers[4]; uint32_t sizes[4]={0}; - char* sensor_id[4]; + char* sensor_id[TOTAL_SENSORS]; int i; - int num_sensors; init_registry(); @@ -193,3 +188,5 @@ int main(void) //chThdSleepMilliseconds(100); } } + +//SE DEBE LIBERAR LA MEMORIA DE LAS FUNCIONES value DE LOS SENSORES!!! \ No newline at end of file diff --git a/Project/applications/smartcities/sensors.c b/Project/applications/smartcities/sensors.c index 944609d..2993562 100644 --- a/Project/applications/smartcities/sensors.c +++ b/Project/applications/smartcities/sensors.c @@ -264,7 +264,7 @@ uint16_t get_temperature_data(void) char* light_value(uint32_t light) { char *value = chHeapAlloc(NULL,10 + 1); - sprintf(value,"%d",light); + sprintf(value,"%d",(unsigned int)light); return value; } char* distance_value(uint16_t distance) @@ -285,8 +285,20 @@ char* temp_humidity_value(uint16_t temp, uint16_t humidity) sprintf(value,"%d,%d",temp,humidity); return value; } -void collecData(char* valueSensors, uint8_t* sensors){ - + +void init_sound(void) +{ + adc_sound_init(); +} +uint32_t get_sound_data(void) +{ + return adc_sound_process(); +} +char* sound_value(uint32_t sound) +{ + char *value = chHeapAlloc(NULL,21 + 1); + sprintf(value,"%d",(unsigned int)sound); + return value; } //light_value(get_light_data());