Commit a04d7379ea87082203f00388be53eecf0f6eff26

Authored by Imanol-Mikel Barba Sabariego
1 parent 7a957821

--no commit message

Project/applications/smartcities/Makefile
... ... @@ -10,7 +10,7 @@
10 10 SDK_ROOT = ../../
11 11 PROJECT_OUT = smartcities
12 12  
13   -USER_SRC = main.c configServer.c httpClient.c callbacks.c module.c sensors.c json.c i2c.c ntp.c buffer.c
  13 +USER_SRC = main.c configServer.c httpClient.c callbacks.c module.c sensors.c json.c i2c.c ntp.c buffer.c adc.c
14 14 USER_INCDIR = include/ makefsdata/
15 15  
16 16 # if you need to add build Defines options add to USER_DEFS define
... ...
Project/applications/smartcities/adc.c
1 1 #include "adc.h"
2 2  
3   -#define DBG(fmt,...) if(1){printf("[ADC] "fmt"\r\n", ##__VA_ARGS__);}else{({});}
4   -
5   -
6   -#define ADC_MIN_VALUE (0)
7   -#define ADC_MAX_VALUE (0xfff) /* 12-bit adc */
8   -#define ADC_VREF_PLUS (3300) /* Vref+ is connected with Vdda (3.3v) */
9   -#define ADC_VREF_MINUS (0) /* Vref- is connected with Vssa (0v) */
10   -
11   -#define ADCbatt ADC1
12   -#define ADCbatt_RCC RCC_APB2Periph_ADC1 /* */
13   -#define ADCbatt_DIVIDER RCC_PCLK2_Div4 /* Can be 2/4/6/8. PCLK2 runs at 32Mhz, and max ADC clock is 14Mhz */
14   -#define ADCbatt_CHANNEL ADC_Channel_12 /* 0/17 */
15   -#define ADCbatt_SAMPLETIME ADC_SampleTime_239Cycles5
16   -#define ADCbatt_GPIO_PIN GPIO_Pin_2
17   -#define ADCbatt_GPIO_PORT GPIOC
18   -#define ADCbatt_GPIO_RCC RCC_APB2Periph_GPIOC
19   -#define ADCbatt_GPIO_STR "PC2"
20   -
21   -#define ADCsound ADC1
22   -#define ADCsound_RCC RCC_APB2Periph_ADC1 /* */
23   -#define ADCsound_DIVIDER RCC_PCLK2_Div4 /* Can be 2/4/6/8. PCLK2 runs at 32Mhz, and max ADC clock is 14Mhz */
24   -#define ADCsound_CHANNEL ADC_Channel_12 /* 0/17 */
25   -#define ADCsound_SAMPLETIME ADC_SampleTime_239Cycles5
26   -#define ADCsound_GPIO_PIN GPIO_Pin_2
27   -#define ADCsound_GPIO_PORT GPIOC
28   -#define ADCsound_GPIO_RCC RCC_APB2Periph_GPIOC
29   -#define ADCsound_GPIO_STR "PC2"
30   -
31   -void adc_init()
  3 +void adc_batt_init()
32 4 {
33   - adc_peripheralInit();
  5 + adc_batt_peripheralInit();
34 6 }
35 7  
36   -uint32_t adc_process()
  8 +uint32_t adc_batt_process()
37 9 {
38 10 uint16_t adcRegisterValue;
39 11 uint32_t analogValue;
40 12  
41   - adcRegisterValue = adc_read();
  13 + adcRegisterValue = adc_batt_read();
42 14  
43 15 /*
44 16 * Calculate the register value to volts
45 17 * 1. adcRegisterValue Range is [ADC_MIN_VALUE, ADC_MAX_VALUE]
46 18 * 2. analogValue Range is [ADC_VREF_MINUS, ADC_VREF_PLUS]
47 19 */
48   - analogValue = ADC_VREF_MINUS + (((adcRegisterValue - ADC_MIN_VALUE)*(ADC_VREF_PLUS - ADC_VREF_MINUS))/(ADC_MAX_VALUE - ADC_MIN_VALUE));
  20 + analogValue = ADCbatt_VREF_MINUS + (((adcRegisterValue - ADCbatt_MIN_VALUE)*(ADCbatt_VREF_PLUS - ADCbatt_VREF_MINUS))/(ADCbatt_MAX_VALUE - ADCbatt_MIN_VALUE));
49 21 DBG("Register value is %4u [%4u milliVolts are currently given to gpio %s]",adcRegisterValue, analogValue, ADCbatt_GPIO_STR);
50 22 return analogValue;
51 23 }
52 24  
53   -void adc_peripheralInit()
  25 +void adc_batt_peripheralInit()
54 26 {
55 27 GPIO_InitTypeDef GPIO_InitStructure;
56 28 ADC_InitTypeDef ADC_InitStructure;
... ... @@ -97,7 +69,7 @@ void adc_peripheralInit()
97 69 while(ADC_GetCalibrationStatus(ADCbatt));
98 70 }
99 71  
100   -uint16_t adc_read()
  72 +uint16_t adc_batt_read()
101 73 {
102 74 ADC_RegularChannelConfig(ADCbatt, ADCbatt_CHANNEL, 1, ADCbatt_SAMPLETIME);
103 75 /* Start the conversion */
... ... @@ -108,5 +80,83 @@ uint16_t adc_read()
108 80 return ADC_GetConversionValue(ADCbatt);
109 81 }
110 82  
  83 +void adc_sound_init()
  84 +{
  85 + adc_sound_peripheralInit();
  86 +}
111 87  
  88 +uint32_t adc_sound_process()
  89 +{
  90 + uint16_t adcRegisterValue;
  91 + uint32_t analogValue;
  92 +
  93 + adcRegisterValue = adc_sound_read();
  94 +
  95 + /*
  96 + * Calculate the register value to volts
  97 + * 1. adcRegisterValue Range is [ADC_MIN_VALUE, ADC_MAX_VALUE]
  98 + * 2. analogValue Range is [ADC_VREF_MINUS, ADC_VREF_PLUS]
  99 + */
  100 + analogValue = ADCsound_VREF_MINUS + (((adcRegisterValue - ADCsound_MIN_VALUE)*(ADCsound_VREF_PLUS - ADCsound_VREF_MINUS))/(ADCsound_MAX_VALUE - ADCsound_MIN_VALUE));
  101 + DBG("Register value is %4u [%4u milliVolts are currently given to gpio %s]",adcRegisterValue, analogValue, ADCbatt_GPIO_STR);
  102 + return analogValue;
  103 +}
  104 +
  105 +void adc_sound_peripheralInit()
  106 +{
  107 + GPIO_InitTypeDef GPIO_InitStructure;
  108 + ADC_InitTypeDef ADC_InitStructure;
  109 + RCC_APB2PeriphClockCmd(ADCsound_GPIO_RCC, ENABLE);
  110 + GPIO_InitStructure.GPIO_Pin = ADCsound_GPIO_PIN;
  111 + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  112 + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  113 + GPIO_Init(ADCsound_GPIO_PORT, &GPIO_InitStructure);
  114 +
  115 +
  116 + /* Max ADC clk is 14mhz and because PCLK2 runs @32Mhz */
  117 + RCC_ADCCLKConfig(ADCsound_DIVIDER);
  118 + /* Enable ADCsound clock so that we can talk to it */
  119 + RCC_APB2PeriphClockCmd(ADCsound_RCC, ENABLE);
  120 + /* Put everything back to power-on defaults */
  121 + ADC_DeInit(ADCsound);
  122 +
  123 +
  124 + /* ADC1 and ADC2 operate independently */
  125 + ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  126 + /* Disable the scan conversion so we do one at a time */
  127 + ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  128 + /* Don't do contimuous conversions - do them on demand */
  129 + ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
  130 + /* Start conversin by software, not an external trigger */
  131 + ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  132 + /* Conversions are 12 bit - put them in the lower 12 bits of the result */
  133 + ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  134 + /* Say how many channels would be used by the sequencer */
  135 + ADC_InitStructure.ADC_NbrOfChannel = 1;
  136 +
  137 + /* Now do the setup */
  138 + ADC_Init(ADCsound, &ADC_InitStructure);
  139 + /* Enable ADCsound */
  140 + ADC_Cmd(ADCsound, ENABLE);
  141 +
  142 + /* Enable ADCsound reset calibaration register */
  143 + ADC_ResetCalibration(ADCsound);
  144 + /* Check the end of ADCsound reset calibration register */
  145 + while(ADC_GetResetCalibrationStatus(ADCsound));
  146 + /* Start ADCsound calibaration */
  147 + ADC_StartCalibration(ADCsound);
  148 + /* Check the end of ADC1 calibration */
  149 + while(ADC_GetCalibrationStatus(ADCsound));
  150 +}
  151 +
  152 +uint16_t adc_sound_read()
  153 +{
  154 + ADC_RegularChannelConfig(ADCsound, ADCsound_CHANNEL, 1, ADCsound_SAMPLETIME);
  155 + /* Start the conversion */
  156 + ADC_SoftwareStartConvCmd(ADCsound, ENABLE);
  157 + /* Wait until conversion completion */
  158 + while(ADC_GetFlagStatus(ADCsound, ADC_FLAG_EOC) == RESET);
  159 + /* Get the conversion value */
  160 + return ADC_GetConversionValue(ADCsound);
  161 +}
112 162  
... ...
Project/applications/smartcities/adc.h
... ... @@ -7,9 +7,46 @@
7 7 #include "stm32f10x_gpio.h"
8 8 #include "stm32f10x_adc.h"
9 9  
10   -void adc_init(void);
11   -uint32_t adc_process(void);
12   -uint16_t adc_read(void);
13   -void adc_peripheralInit(void);
  10 +#define DBG(fmt,...) if(1){printf("[ADC] "fmt"\r\n", ##__VA_ARGS__);}else{({});}
  11 +
  12 +#define ADCbatt_MIN_VALUE (0)
  13 +#define ADCbatt_MAX_VALUE (0xfff) /* 12-bit adc */
  14 +#define ADCbatt_VREF_PLUS (3300) /* Vref+ is connected with Vdda (3.3v) */
  15 +#define ADCbatt_VREF_MINUS (0) /* Vref- is connected with Vssa (0v) */
  16 +
  17 +#define ADCsound_MIN_VALUE (0)
  18 +#define ADCsound_MAX_VALUE (0xfff) /* 12-bit adc */
  19 +#define ADCsound_VREF_PLUS (2600) /* Vref+ is connected with Vdda (3.3v) */
  20 +#define ADCsound_VREF_MINUS (0) /* Vref- is connected with Vssa (0v) */
  21 +
  22 +#define ADCbatt ADC1
  23 +#define ADCbatt_RCC RCC_APB2Periph_ADC1 /* */
  24 +#define ADCbatt_DIVIDER RCC_PCLK2_Div4 /* Can be 2/4/6/8. PCLK2 runs at 32Mhz, and max ADC clock is 14Mhz */
  25 +#define ADCbatt_CHANNEL ADC_Channel_12 /* 0/17 */
  26 +#define ADCbatt_SAMPLETIME ADC_SampleTime_239Cycles5
  27 +#define ADCbatt_GPIO_PIN GPIO_Pin_2
  28 +#define ADCbatt_GPIO_PORT GPIOC
  29 +#define ADCbatt_GPIO_RCC RCC_APB2Periph_GPIOC
  30 +#define ADCbatt_GPIO_STR "PC2"
  31 +
  32 +#define ADCsound ADC2
  33 +#define ADCsound_RCC RCC_APB2Periph_ADC2 /* */
  34 +#define ADCsound_DIVIDER RCC_PCLK2_Div4 /* Can be 2/4/6/8. PCLK2 runs at 32Mhz, and max ADC clock is 14Mhz */
  35 +#define ADCsound_CHANNEL ADC_Channel_12 /* 0/17 */
  36 +#define ADCsound_SAMPLETIME ADC_SampleTime_239Cycles5
  37 +#define ADCsound_GPIO_PIN GPIO_Pin_2
  38 +#define ADCsound_GPIO_PORT GPIOA
  39 +#define ADCsound_GPIO_RCC RCC_APB2Periph_GPIOA
  40 +#define ADCsound_GPIO_STR "PA2"
  41 +
  42 +void adc_batt_init(void);
  43 +uint32_t adc_batt_process(void);
  44 +uint16_t adc_batt_read(void);
  45 +void adc_batt_peripheralInit(void);
  46 +
  47 +void adc_sound_init(void);
  48 +uint32_t adc_sound_process(void);
  49 +uint16_t adc_sound_read(void);
  50 +void adc_sound_peripheralInit(void);
14 51  
15 52 #endif
16 53 \ No newline at end of file
... ...
Project/applications/smartcities/include/sensors.h
... ... @@ -6,12 +6,13 @@
6 6 #include "i2c.h"
7 7 #include "libwismart.h"
8 8 #include "module.h"
9   -//#include "json.h"
  9 +#include "adc.h"
10 10  
11 11 #define LIGHT_ADDR 0x39
12 12 #define DISTANCE_ADDR 0x01
13 13 #define PRESSURE_ADDR 0x77 //Cambiar a MPL115A
14 14 #define HUMIDITY_TEMP_ADDR 0x27
  15 +#define SOUND_ADDR 0x27
15 16 //REMEMBER TO UPDATE I2C_scan ROUTINE WITH EACH NEW SENSOR!!!!
16 17 #define TOTAL_SENSORS 4
17 18  
... ... @@ -74,4 +75,10 @@ uint16_t get_humidity_data(void);
74 75 uint16_t get_temperature_data(void);
75 76 char* temp_humidity_value(uint16_t temp, uint16_t humidity);
76 77  
  78 +//SOUND SENSOR
  79 +
  80 +void init_sound(void);
  81 +uint32_t get_sound_data(void);
  82 +char* sound_value(uint32_t sound);
  83 +
77 84 #endif
... ...
Project/applications/smartcities/json.c
1 1 #include "json.h"
2   -#include "buffer.h"
3 2  
4 3 uint8_t register_sensor(sensor sens)
5 4 {
... ... @@ -31,7 +30,6 @@ char* prepare_json_observation_statement(char** data, uint32_t nObservations)
31 30 str_aux = join_strings(str_aux,str_aux2,length,observation_length,JOIN_FREE_MEM);
32 31 length += observation_length;
33 32 printf("data=%s %d \r\n",data[i],i);
34   - check_memory();
35 33 }
36 34 length--; //REMOVE LAST ','
37 35 json_statement = join_strings(str_aux,"]}\0",length,3,JOIN_NO_FREE);
... ...
Project/applications/smartcities/main.c
... ... @@ -94,16 +94,11 @@ int main(void)
94 94 libwismart_SetScanRunsForConnTimeout(4); //Edit a 4
95 95 libwismart_EnableBsdSocketAPI();
96 96  
97   - struct httpHeaders head200 = { get, "/", 1, "http://www.w3.org/", 19 };
98   - struct httpHeaders head301 = { get, "/", 1, "w3.org", 6 };
99   - struct httpHeaders head404 = { get, "/errorerrorerror", 15, "www.w3.org" };
100   -
101 97 uint32_t ind[4]={0};
102 98 char** buffers[4];
103 99 uint32_t sizes[4]={0};
104   - char* sensor_id[4];
  100 + char* sensor_id[TOTAL_SENSORS];
105 101 int i;
106   - int num_sensors;
107 102  
108 103 init_registry();
109 104  
... ... @@ -193,3 +188,5 @@ int main(void)
193 188 //chThdSleepMilliseconds(100);
194 189 }
195 190 }
  191 +
  192 +//SE DEBE LIBERAR LA MEMORIA DE LAS FUNCIONES value DE LOS SENSORES!!!
196 193 \ No newline at end of file
... ...
Project/applications/smartcities/sensors.c
... ... @@ -264,7 +264,7 @@ uint16_t get_temperature_data(void)
264 264 char* light_value(uint32_t light)
265 265 {
266 266 char *value = chHeapAlloc(NULL,10 + 1);
267   - sprintf(value,"%d",light);
  267 + sprintf(value,"%d",(unsigned int)light);
268 268 return value;
269 269 }
270 270 char* distance_value(uint16_t distance)
... ... @@ -285,8 +285,20 @@ char* temp_humidity_value(uint16_t temp, uint16_t humidity)
285 285 sprintf(value,"%d,%d",temp,humidity);
286 286 return value;
287 287 }
288   -void collecData(char* valueSensors, uint8_t* sensors){
289   -
  288 +
  289 +void init_sound(void)
  290 +{
  291 + adc_sound_init();
  292 +}
  293 +uint32_t get_sound_data(void)
  294 +{
  295 + return adc_sound_process();
  296 +}
  297 +char* sound_value(uint32_t sound)
  298 +{
  299 + char *value = chHeapAlloc(NULL,21 + 1);
  300 + sprintf(value,"%d",(unsigned int)sound);
  301 + return value;
290 302 }
291 303  
292 304 //light_value(get_light_data());
... ...