Commit f352470ce435a7f355ddaaf155da7ccfa2c419f3

Authored by Imanol-Mikel Barba Sabariego
1 parent 554a12ad

--no commit message

Project/applications/smartcities/adc.c
@@ -22,49 +22,36 @@ void adc_batt_peripheralInit() @@ -22,49 +22,36 @@ void adc_batt_peripheralInit()
22 ADC_InitTypeDef ADC_InitStructure; 22 ADC_InitTypeDef ADC_InitStructure;
23 RCC_APB2PeriphClockCmd(ADCbatt_GPIO_RCC, ENABLE); 23 RCC_APB2PeriphClockCmd(ADCbatt_GPIO_RCC, ENABLE);
24 GPIO_InitStructure.GPIO_Pin = ADCbatt_GPIO_PIN; 24 GPIO_InitStructure.GPIO_Pin = ADCbatt_GPIO_PIN;
25 - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;  
26 - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
27 - GPIO_Init(ADCbatt_GPIO_PORT, &GPIO_InitStructure); 25 + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  26 + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  27 + GPIO_Init(ADCbatt_GPIO_PORT, &GPIO_InitStructure);
28 28
29 RCC_ADCCLKConfig(ADCbatt_DIVIDER); 29 RCC_ADCCLKConfig(ADCbatt_DIVIDER);
30 RCC_APB2PeriphClockCmd(ADCbatt_RCC, ENABLE); 30 RCC_APB2PeriphClockCmd(ADCbatt_RCC, ENABLE);
31 ADC_DeInit(ADCbatt); 31 ADC_DeInit(ADCbatt);
32 32
33 - /* ADC1 and ADC2 operate independently */  
34 ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; 33 ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
35 - /* Disable the scan conversion so we do one at a time */  
36 ADC_InitStructure.ADC_ScanConvMode = DISABLE; 34 ADC_InitStructure.ADC_ScanConvMode = DISABLE;
37 - /* Don't do contimuous conversions - do them on demand */  
38 ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; 35 ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
39 - /* Start conversin by software, not an external trigger */  
40 ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; 36 ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
41 - /* Conversions are 12 bit - put them in the lower 12 bits of the result */  
42 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; 37 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
43 - /* Say how many channels would be used by the sequencer */  
44 ADC_InitStructure.ADC_NbrOfChannel = 1; 38 ADC_InitStructure.ADC_NbrOfChannel = 1;
45 39
46 ADC_RegularChannelConfig(ADCbatt, ADCbatt_CHANNEL, 1, ADCbatt_SAMPLETIME); 40 ADC_RegularChannelConfig(ADCbatt, ADCbatt_CHANNEL, 1, ADCbatt_SAMPLETIME);
47 ADC_Init(ADCbatt, &ADC_InitStructure); 41 ADC_Init(ADCbatt, &ADC_InitStructure);
48 ADC_Cmd(ADCbatt, ENABLE); 42 ADC_Cmd(ADCbatt, ENABLE);
49 43
50 - /* Enable ADCbatt reset calibaration register */  
51 ADC_ResetCalibration(ADCbatt); 44 ADC_ResetCalibration(ADCbatt);
52 - /* Check the end of ADCbatt reset calibration register */  
53 while(ADC_GetResetCalibrationStatus(ADCbatt)); 45 while(ADC_GetResetCalibrationStatus(ADCbatt));
54 - /* Start ADCbatt calibaration */  
55 ADC_StartCalibration(ADCbatt); 46 ADC_StartCalibration(ADCbatt);
56 - /* Check the end of ADC1 calibration */  
57 while(ADC_GetCalibrationStatus(ADCbatt)); 47 while(ADC_GetCalibrationStatus(ADCbatt));
58 } 48 }
59 49
60 uint16_t adc_batt_read() 50 uint16_t adc_batt_read()
61 { 51 {
62 - /* Start the conversion */  
63 - ADC_SoftwareStartConvCmd(ADCbatt, ENABLE);  
64 - /* Wait until conversion completion */  
65 - while(ADC_GetFlagStatus(ADCbatt, ADC_FLAG_EOC) == RESET);  
66 - /* Get the conversion value */  
67 - return ADC_GetConversionValue(ADCbatt); 52 + ADC_SoftwareStartConvCmd(ADCbatt, ENABLE);
  53 + while(ADC_GetFlagStatus(ADCbatt, ADC_FLAG_EOC) == RESET);
  54 + return ADC_GetConversionValue(ADCbatt);
68 } 55 }
69 56
70 void adc_sound_init() 57 void adc_sound_init()
@@ -89,49 +76,35 @@ void adc_sound_peripheralInit() @@ -89,49 +76,35 @@ void adc_sound_peripheralInit()
89 ADC_InitTypeDef ADC_InitStructure; 76 ADC_InitTypeDef ADC_InitStructure;
90 RCC_APB2PeriphClockCmd(ADCsound_GPIO_RCC, ENABLE); 77 RCC_APB2PeriphClockCmd(ADCsound_GPIO_RCC, ENABLE);
91 GPIO_InitStructure.GPIO_Pin = ADCsound_GPIO_PIN; 78 GPIO_InitStructure.GPIO_Pin = ADCsound_GPIO_PIN;
92 - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;  
93 - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
94 - GPIO_Init(ADCsound_GPIO_PORT, &GPIO_InitStructure); 79 + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  80 + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  81 + GPIO_Init(ADCsound_GPIO_PORT, &GPIO_InitStructure);
95 82
96 RCC_ADCCLKConfig(ADCsound_DIVIDER); 83 RCC_ADCCLKConfig(ADCsound_DIVIDER);
97 RCC_APB2PeriphClockCmd(ADCsound_RCC, ENABLE); 84 RCC_APB2PeriphClockCmd(ADCsound_RCC, ENABLE);
98 ADC_DeInit(ADCsound); 85 ADC_DeInit(ADCsound);
99 86
100 -  
101 - /* ADC1 and ADC2 operate independently */  
102 ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; 87 ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
103 - /* Disable the scan conversion so we do one at a time */  
104 ADC_InitStructure.ADC_ScanConvMode = DISABLE; 88 ADC_InitStructure.ADC_ScanConvMode = DISABLE;
105 - /* Don't do contimuous conversions - do them on demand */  
106 ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; 89 ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
107 - /* Start conversin by software, not an external trigger */  
108 ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; 90 ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
109 - /* Conversions are 12 bit - put them in the lower 12 bits of the result */  
110 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; 91 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
111 - /* Say how many channels would be used by the sequencer */  
112 ADC_InitStructure.ADC_NbrOfChannel = 1; 92 ADC_InitStructure.ADC_NbrOfChannel = 1;
113 -  
114 - ADC_RegularChannelConfig(ADCsound, ADCsound_CHANNEL, 1, ADCsound_SAMPLETIME); 93 +
  94 + ADC_RegularChannelConfig(ADCsound, ADCsound_CHANNEL, 1, ADCsound_SAMPLETIME);
115 ADC_Init(ADCsound, &ADC_InitStructure); 95 ADC_Init(ADCsound, &ADC_InitStructure);
116 ADC_Cmd(ADCsound, ENABLE); 96 ADC_Cmd(ADCsound, ENABLE);
117 97
118 - /* Enable ADCsound reset calibaration register */  
119 ADC_ResetCalibration(ADCsound); 98 ADC_ResetCalibration(ADCsound);
120 - /* Check the end of ADCsound reset calibration register */  
121 while(ADC_GetResetCalibrationStatus(ADCsound)); 99 while(ADC_GetResetCalibrationStatus(ADCsound));
122 - /* Start ADCsound calibaration */  
123 ADC_StartCalibration(ADCsound); 100 ADC_StartCalibration(ADCsound);
124 - /* Check the end of ADC1 calibration */  
125 while(ADC_GetCalibrationStatus(ADCsound)); 101 while(ADC_GetCalibrationStatus(ADCsound));
126 } 102 }
127 103
128 uint16_t adc_sound_read() 104 uint16_t adc_sound_read()
129 { 105 {
130 - /* Start the conversion */  
131 - ADC_SoftwareStartConvCmd(ADCsound, ENABLE);  
132 - /* Wait until conversion completion */  
133 - while(ADC_GetFlagStatus(ADCsound, ADC_FLAG_EOC) == RESET);  
134 - /* Get the conversion value */  
135 - return ADC_GetConversionValue(ADCsound); 106 + ADC_SoftwareStartConvCmd(ADCsound, ENABLE);
  107 + while(ADC_GetFlagStatus(ADCsound, ADC_FLAG_EOC) == RESET);
  108 + return ADC_GetConversionValue(ADCsound);
136 } 109 }
137 110
Project/applications/smartcities/buffer.c
1 #include "buffer.h" 1 #include "buffer.h"
2 #include "globals.h" 2 #include "globals.h"
3 3
4 -char** put_message(char* info, char** buf,uint32_t *index, uint32_t *buf_len){  
5 -  
6 -uint32_t i,len;  
7 -len=*buf_len;  
8 -if(len==*index)  
9 -{  
10 - printf("Joining buffer...\r\n");  
11 - char** buffer=join_buf(buf, buf_len);  
12 - printf("Joined buffer...\r\n");  
13 - buffer[*index]=chHeapAlloc(NULL,strlen(info)+1);  
14 - printf("Memory allocated...\r\n");  
15 - strcpy(buffer[*index],info);  
16 - printf("Data copied...\r\n");  
17 - i=*index+1;  
18 - *index = i;  
19 - return buffer;  
20 -}  
21 -else 4 +char** put_message(char* info, char** buf,uint32_t *index, uint32_t *buf_len)
22 { 5 {
23 - printf("WTF in put_message\r\n");  
24 - buf[*index] = chHeapAlloc(NULL,strlen(info)+1);  
25 - buf[*index]=info;  
26 - i=*index+1;  
27 - *index = i;  
28 - return buf;  
29 -}  
30 - 6 + uint32_t i,len;
  7 + len=*buf_len;
  8 + if(len==*index)
  9 + {
  10 + printf("Joining buffer...\r\n");
  11 + char** buffer = join_buf(buf, buf_len);
  12 + printf("Joined buffer...\r\n");
  13 + buffer[*index] = chHeapAlloc(NULL,strlen(info)+1);
  14 + printf("Memory allocated...\r\n");
  15 + strcpy(buffer[*index],info);
  16 + printf("Data copied...\r\n");
  17 + i = *index+1;
  18 + *index = i;
  19 + return buffer;
  20 + }
  21 + else
  22 + {
  23 + printf("WTF in put_message\r\n");
  24 + buf[*index] = chHeapAlloc(NULL,strlen(info)+1);
  25 + buf[*index] = info;
  26 + i = *index+1;
  27 + *index = i;
  28 + return buf;
  29 + }
31 } 30 }
32 31
33 -int check_memory(){  
34 - 32 +int check_memory()
  33 +{
35 printf("Memรฒria disponible en bytes segons:\r\n"); 34 printf("Memรฒria disponible en bytes segons:\r\n");
36 -  
37 - int mem_free=libwismart_GetMemFree_Ram().free;  
38 - //printf("- free_core: %d %d\r\n", mem_free, MAX_RAM-mem_free);  
39 - printf("- free: %d %d\r\n", libwismart_GetMemFree_Ram().free, MAX_RAM- libwismart_GetMemFree_Ram().free);  
40 -  
41 -  
42 - //int mem_free=20000;  
43 - int mem=MAX_RAM-mem_free; 35 + int mem_free = libwismart_GetMemFree_Ram().free;
  36 + printf("- free: %d %d\r\n", libwismart_GetMemFree_Ram().free, MAX_RAM - libwismart_GetMemFree_Ram().free);
  37 + int mem = MAX_RAM - mem_free;
44 printf("mem ocupada=%d\r\n",mem); 38 printf("mem ocupada=%d\r\n",mem);
45 - if(mem>=HARD_LIMIT) 39 + if(mem >= HARD_LIMIT)
46 { 40 {
47 return HARD_REACHED; 41 return HARD_REACHED;
48 } 42 }
49 - else if(mem >= SOFT_LIMIT){ 43 + else if(mem >= SOFT_LIMIT)
  44 + {
50 return SOFT_REACHED; 45 return SOFT_REACHED;
51 } 46 }
52 return MEMORY_OK; 47 return MEMORY_OK;
53 } 48 }
54 49
55 -int send(char** buf, uint32_t *index, uint32_t *size_buf, char *provider_ID, char *sensor_ID){ 50 +int send(char** buf, uint32_t *index, uint32_t *size_buf, char *provider_ID, char *sensor_ID)
  51 +{
56 printf("abans del JSON statement\r\n"); 52 printf("abans del JSON statement\r\n");
57 printf("index= %d \r\n",*index); 53 printf("index= %d \r\n",*index);
58 char* statement=prepare_json_observation_statement(buf, *index); 54 char* statement=prepare_json_observation_statement(buf, *index);
59 uint32_t size = strlen(statement); 55 uint32_t size = strlen(statement);
60 if(buf==NULL) 56 if(buf==NULL)
61 - { return 2; } 57 + {
  58 + return 2;
  59 + }
62 printf("abans del JSON\r\n"); 60 printf("abans del JSON\r\n");
63 - uint8_t res=send_json(statement, size, provider_ID, sensor_ID); 61 + uint8_t res = send_json(statement, size, provider_ID, sensor_ID);
64 chHeapFree(statement); 62 chHeapFree(statement);
65 int i; 63 int i;
66 printf("JSON_POST_OK ? %d\r\n",res == JSON_POST_OK); 64 printf("JSON_POST_OK ? %d\r\n",res == JSON_POST_OK);
67 - if(res==JSON_POST_OK){ 65 + if(res==JSON_POST_OK)
  66 + {
68 for(i=0;i<*index;i++) 67 for(i=0;i<*index;i++)
69 { 68 {
70 chHeapFree(buf[i]); 69 chHeapFree(buf[i]);
71 } 70 }
72 - printf("djfkhvldkfjhdรฑkfgv\r\n");  
73 if(buf!=NULL) 71 if(buf!=NULL)
74 chHeapFree(buf); 72 chHeapFree(buf);
75 73
@@ -81,15 +79,17 @@ int send(char** buf, uint32_t *index, uint32_t *size_buf, char *provider_ID, cha @@ -81,15 +79,17 @@ int send(char** buf, uint32_t *index, uint32_t *size_buf, char *provider_ID, cha
81 } 79 }
82 80
83 //realloc 81 //realloc
84 -char** join_buf(char** buf, uint32_t *buf_len){ 82 +char** join_buf(char** buf, uint32_t *buf_len)
  83 +{
85 char** n_buf; 84 char** n_buf;
86 - int len= *buf_len;  
87 - n_buf=chHeapAlloc (NULL,sizeof (char *) * (len+1)); 85 + int len = *buf_len;
  86 + n_buf = chHeapAlloc (NULL,sizeof (char *) * (len+1));
88 int i; 87 int i;
89 printf("Start buffer join...\r\n"); 88 printf("Start buffer join...\r\n");
90 - for(i=0; i<len;i++){ 89 + for(i=0; i<len;i++)
  90 + {
91 printf("in join_buf for, Asking for %d bytes...\r\n",strlen(buf[i]+1)); 91 printf("in join_buf for, Asking for %d bytes...\r\n",strlen(buf[i]+1));
92 - n_buf[i]=chHeapAlloc(NULL,strlen(buf[i])+1); 92 + n_buf[i] = chHeapAlloc(NULL,strlen(buf[i])+1);
93 printf("in join_buf for, Memory allocated...\r\n"); 93 printf("in join_buf for, Memory allocated...\r\n");
94 strcpy(n_buf[i],buf[i]); 94 strcpy(n_buf[i],buf[i]);
95 printf("in join_buf for, Message copied...\r\n"); 95 printf("in join_buf for, Message copied...\r\n");
@@ -97,10 +97,12 @@ char** join_buf(char** buf, uint32_t *buf_len){ @@ -97,10 +97,12 @@ char** join_buf(char** buf, uint32_t *buf_len){
97 printf("in join_buf for, Memory cleared...\r\n"); 97 printf("in join_buf for, Memory cleared...\r\n");
98 } 98 }
99 printf("All data transferred\r\n"); 99 printf("All data transferred\r\n");
100 - if(len!=0)  
101 - { chHeapFree(buf); } 100 + if(len != 0)
  101 + {
  102 + chHeapFree(buf);
  103 + }
102 printf("Old buffer cleared\r\n"); 104 printf("Old buffer cleared\r\n");
103 len++; 105 len++;
104 *buf_len=len; 106 *buf_len=len;
105 return n_buf; 107 return n_buf;
106 -} 108 +}
107 \ No newline at end of file 109 \ No newline at end of file
Project/applications/smartcities/i2c.c
@@ -7,31 +7,25 @@ void I2C_init(void) @@ -7,31 +7,25 @@ void I2C_init(void)
7 GPIO_InitTypeDef GPIO_InitStruct; 7 GPIO_InitTypeDef GPIO_InitStruct;
8 I2C_InitTypeDef I2C_InitStruct; 8 I2C_InitTypeDef I2C_InitStruct;
9 9
10 - // enable APB1 peripheral clock for I2C1  
11 RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); 10 RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
12 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); 11 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
13 12
14 GPIO_StructInit(&GPIO_InitStruct); 13 GPIO_StructInit(&GPIO_InitStruct);
15 - GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; // PB6 and PB7  
16 - GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_OD; // set pins to alternate function  
17 - GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; // set GPIO speed  
18 - GPIO_Init(GPIOB, &GPIO_InitStruct); // init GPIOB 14 + GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
  15 + GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_OD;
  16 + GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  17 + GPIO_Init(GPIOB, &GPIO_InitStruct);
19 18
20 - // configure I2C1  
21 I2C_StructInit(&I2C_InitStruct); 19 I2C_StructInit(&I2C_InitStruct);
22 - I2C_InitStruct.I2C_ClockSpeed = 100000; // 100kHz(standard) vs 400  
23 - I2C_InitStruct.I2C_Mode = I2C_Mode_I2C; // I2C mode  
24 - I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2; // 50% duty cycle --> standard  
25 - I2C_InitStruct.I2C_OwnAddress1 = 0x00; // own address, not relevant in master mode  
26 - I2C_InitStruct.I2C_Ack = I2C_Ack_Enable; // disable acknowledge when reading (can be changed later on)  
27 - I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; // set address length to 7 bit addresses  
28 - I2C_Init(I2C1, &I2C_InitStruct); // init I2C1  
29 -  
30 - //Enable clock stretching 20 + I2C_InitStruct.I2C_ClockSpeed = 100000;
  21 + I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;
  22 + I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2;
  23 + I2C_InitStruct.I2C_OwnAddress1 = 0x00;
  24 + I2C_InitStruct.I2C_Ack = I2C_Ack_Enable;
  25 + I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
  26 + I2C_Init(I2C1, &I2C_InitStruct);
31 I2C_StretchClockCmd(I2C1, ENABLE); 27 I2C_StretchClockCmd(I2C1, ENABLE);
32 -  
33 - // enable I2C1  
34 - I2C_Cmd(I2C1, ENABLE); //sets PE bit in CR1, at end` 28 + I2C_Cmd(I2C1, ENABLE);
35 } 29 }
36 30
37 void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction) 31 void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction)
Project/applications/smartcities/main.c
@@ -8,10 +8,12 @@ @@ -8,10 +8,12 @@
8 #include "buffer.h" 8 #include "buffer.h"
9 #include "i2c.h" 9 #include "i2c.h"
10 #include "configServer.h" 10 #include "configServer.h"
  11 +#include "timer-loop.h"
  12 +#include "ntp.h"
11 #include "sensors.h" 13 #include "sensors.h"
12 14
13 #define WIFI_MODE WIFI_MODE_CLIENT 15 #define WIFI_MODE WIFI_MODE_CLIENT
14 -#define NETWORK_SSID_AP "modularsens" 16 +#define NETWORK_SSID_AP "modularsense"
15 #define NETWORK_KEY_AP NULL 17 #define NETWORK_KEY_AP NULL
16 #define NETWORK_CHANNEL_AP 1 18 #define NETWORK_CHANNEL_AP 1
17 19
@@ -29,14 +31,33 @@ void initLibwismart(void) @@ -29,14 +31,33 @@ void initLibwismart(void)
29 uint8_t connected=0; 31 uint8_t connected=0;
30 uint8_t timeout=0; 32 uint8_t timeout=0;
31 uint8_t retries=0; 33 uint8_t retries=0;
  34 +uint8_t sensors_length=0;
  35 +uint8_t registry_opened=0;
  36 +
  37 +module mod;
  38 +
  39 +void update_time(unsigned long *time)
  40 +{
  41 + printf("Requesting new NTP time...\r\n");
  42 + unsigned long new_time = getSecsSince1900();
  43 + if(new_time)
  44 + {
  45 + *time = getSecsSince1900();
  46 + }
  47 + printf("Time updated...\r\n");
  48 +}
32 49
33 void init_registry(void) 50 void init_registry(void)
34 { 51 {
35 config_params_t config; 52 config_params_t config;
36 53
37 strcpy(config.localization,"none"); 54 strcpy(config.localization,"none");
38 - libwismart_RegistryCreateKey(&geo, 1, sizeof(config));  
39 - libwismart_RegistryOpen(1); 55 + if(!registry_opened)
  56 + {
  57 + libwismart_RegistryCreateKey(&geo, 1, sizeof(config));
  58 + libwismart_RegistryOpen(1);
  59 + registry_opened = 1;
  60 + }
40 if(!libwismart_RegistryIsValueEmpty(&geo)) 61 if(!libwismart_RegistryIsValueEmpty(&geo))
41 { 62 {
42 libwismart_RegistryGet(&geo,&config); 63 libwismart_RegistryGet(&geo,&config);
@@ -51,6 +72,7 @@ void init_registry(void) @@ -51,6 +72,7 @@ void init_registry(void)
51 printf("Geo Localization = %s\r\n", config.localization); 72 printf("Geo Localization = %s\r\n", config.localization);
52 73
53 strcpy(mod.geoloc,config.localization); 74 strcpy(mod.geoloc,config.localization);
  75 + strcpy(mod.ID,MODULE_ID);
54 if(config.security == PROFILE_SECURITY_OPEN) 76 if(config.security == PROFILE_SECURITY_OPEN)
55 { 77 {
56 printf("open detected\r\n"); 78 printf("open detected\r\n");
@@ -79,29 +101,75 @@ void init_registry(void) @@ -79,29 +101,75 @@ void init_registry(void)
79 printf("wep detected\r\n"); 101 printf("wep detected\r\n");
80 //Is WEP 102 //Is WEP
81 libwismart_WiFiConnect(config.ssid,config.wepkey,wifi_connect_result_cb); 103 libwismart_WiFiConnect(config.ssid,config.wepkey,wifi_connect_result_cb);
  104 + libwismart_WiFiSetWep(config.wepkey,1);
82 } 105 }
83 while(connected == 0 && timeout != 1 ) 106 while(connected == 0 && timeout != 1 )
84 {chThdSleepMilliseconds(500);} 107 {chThdSleepMilliseconds(500);}
85 108
86 } 109 }
87 -int main(void) 110 +
  111 +void send_data(char** buffers[], uint32_t ind[], uint32_t sizes[], uint8_t sensors[])
88 { 112 {
89 - initLibwismart();  
90 - libwismart_PowerSave_Enable();  
91 - libwismart_PowerSave_HigherProfile(TRUE);  
92 - libwismart_RegisterDhcpCB(dhcp_connect_result_cb);  
93 - libwismart_WiFiInit();  
94 - libwismart_SetScanRunsForConnTimeout(4); //Edit a 4  
95 - libwismart_EnableBsdSocketAPI(); 113 + int j;
  114 + for(j=0;j<sensors_length;j++)
  115 + {
  116 + printf(" enviant buffer %d\r\n",j);
  117 +
  118 + // fem servir 085 de prova, perรฒ haurem de fer servir sensor_id[j]
  119 + char id[3];
  120 + sprintf(id,"%x",sensors[j]);
  121 + int ok=send(buffers[j],&ind[j],&sizes[j], mod.ID, id);
96 122
97 - uint32_t ind[4]={0};  
98 - char** buffers[4];  
99 - uint32_t sizes[4]={0};  
100 - char* sensor_id[TOTAL_SENSORS]; 123 + if(ok==JSON_COMM_ERROR)
  124 + {
  125 + printf("wismart is not connected\r\n");
  126 + }
  127 + else if( ok==JSON_OTHER_ERROR){
  128 + printf("some error ocurred\r\n");
  129 + }
  130 + else if(ok ==JSON_POST_OK){
  131 + printf(" send OK \r\n");
  132 + }
  133 + }
  134 +}
  135 +
  136 +void put_buffers(char** buffers[],uint32_t ind[],uint32_t sizes[],char** cooked, uint8_t* sensors)
  137 +{
101 int i; 138 int i;
102 -  
103 - init_registry();  
104 - 139 + for(i=0;i<sensors_length;i++){
  140 + printf("For %d and %d\n\r", ind[i],sizes[i]);
  141 + printf("Putting data: %s\n\r",cooked[i]);
  142 + buffers[i] = put_message(cooked[i], buffers[i] ,&ind[i],&sizes[i]);
  143 + printf("Message put: %s\r\n",buffers[i][ind[i]-1]);
  144 + chHeapFree(cooked[i]);
  145 + printf("Memory freed\n\r");
  146 + printf("Message put (assertion): %s with length %d\r\n",buffers[i][ind[i]-1] ,strlen(buffers[i][ind[i]-1]));
  147 + }
  148 +
  149 +}
  150 +char** timestamp_datas(char* value[],unsigned long timestamp, uint8_t* sensors)
  151 +{
  152 + char** cooked_data;
  153 + cooked_data = (char**) chHeapAlloc(NULL,sensors_length * sizeof(char*));
  154 + printf("Data allocated...\r\n");
  155 + Date t=getDate(timestamp);
  156 + int i;
  157 + for(i=0;i<sensors_length;i++)
  158 + {
  159 + printf("Calling timestamp_data...\r\n");
  160 + cooked_data[i]=timestamp_data(value[i], t);
  161 + printf("Finished timestamp_data...\r\n");
  162 + printf("Data is: %s\r\n",cooked_data[i]);
  163 + chHeapFree(value[i]);
  164 + value[i] = NULL;
  165 + }
  166 + printf("Returning timestamped data...\r\n");
  167 + return cooked_data;
  168 +}
  169 +
  170 +void wifi_connect(void)
  171 +{
  172 + init_registry();
105 if(timeout==1) 173 if(timeout==1)
106 { 174 {
107 printf("Creating AP\r\n"); 175 printf("Creating AP\r\n");
@@ -113,80 +181,197 @@ int main(void) @@ -113,80 +181,197 @@ int main(void)
113 chThdSleepMilliseconds(1000); 181 chThdSleepMilliseconds(1000);
114 } 182 }
115 } 183 }
116 - for(;;) 184 +}
  185 +
  186 +void send_battery_level(unsigned long timestamp)
  187 +{
  188 + uint8_t result;
  189 + char *batt_level = battery_value(get_battery_data());
  190 + //char *batt_level = battery_value(3300);
  191 + char *batt_data = timestamp_data(batt_level,getDate(timestamp));
  192 + chHeapFree(batt_level);
  193 + char *statement = prepare_json_observation_statement(&batt_data,1);
  194 + chHeapFree(batt_data);
  195 + char id[3];
  196 + sprintf(id,"%02x",battery.ID);
  197 + result = send_json(statement,strlen(statement),mod.ID,id);
  198 + if(result)
117 { 199 {
118 - // i2c gets the data and combines it with the time stamp  
119 - char* data="message,0";  
120 - char* data2="message,1";  
121 - char* data3="message,2";  
122 - for(i=0;i<TOTAL_SENSORS;i++){  
123 -  
124 - printf("------------------BUFFER %d ----------------------\r\n",i);  
125 - printf("index=%d\r\n",ind[i]);  
126 -  
127 - // 3 missatges de prova a cada buffer  
128 - buffers[i]=put_message(data, buffers[i] ,&ind[i],&sizes[i]);  
129 - buffers[i]=put_message(data2, buffers[i] ,&ind[i],&sizes[i]);  
130 - buffers[i]=put_message(data3, buffers[i] ,&ind[i],&sizes[i]);  
131 - printf("mirant memoria\r\n");  
132 - int res=check_memory();  
133 - if(res==SOFT_REACHED){  
134 - printf("--------------soft limit-------------\r\n");  
135 - int j;  
136 - for(j=0;j<TOTAL_SENSORS;j++)  
137 - {  
138 - printf(" enviant buffer %d\r\n",j);  
139 -  
140 - // fem servir 085 de prova, perรฒ haurem de fer servir sensor_id[j]  
141 - int ok=send(buffers[j],&ind[j],&sizes[j], mod.ID, sensor_id[j]);  
142 -  
143 - if(ok==JSON_COMM_ERROR)  
144 - {  
145 - printf("wismart is not connected\r\n");  
146 - }  
147 - else if( ok==JSON_OTHER_ERROR){  
148 - printf("some error ocurred\r\n");  
149 - }  
150 - else if(ok ==JSON_POST_OK){  
151 - printf(" send OK \r\n");  
152 - }  
153 - } 200 + if(result != 200)
  201 + {
  202 + printf("ERROR: SERVER RETURNED %d\r\n",result);
  203 + }
  204 + }
  205 + else
  206 + {
  207 + printf("ERROR: CONNECTION FAILED\r\n");
  208 + }
  209 + chHeapFree(statement);
  210 +}
  211 +
  212 +void wifi_disconnect(void){
  213 + connected = 0;
  214 + timeout = 0;
  215 + uint8_t res=libwismart_WiFiDisconnect();
  216 + if(res)
  217 + printf("WIFI_DISCONNECT_SUCCESS\r\n");
  218 + else
  219 + printf("WIFI_DISCONNECT_FAILURE\r\n");
  220 +}
  221 +
  222 +int main(void)
  223 +{
  224 + initLibwismart();
  225 + libwismart_PowerSave_Enable();
  226 + libwismart_PowerSave_HigherProfile(TRUE);
  227 + libwismart_RegisterDhcpCB(dhcp_connect_result_cb);
  228 + libwismart_WiFiInit();
  229 + libwismart_SetScanRunsForConnTimeout(4); //Edit a 4
  230 + libwismart_EnableBsdSocketAPI();
  231 +
  232 + int i;
  233 +
  234 + uint8_t sensors[TOTAL_SENSORS];
  235 + memset (sensors, 0, TOTAL_SENSORS);
  236 +
  237 + char* valueSensors[TOTAL_SENSORS];
  238 + //memset (valueSensors, 0, TOTAL_SENSORS);
  239 +
  240 + for(i = 0; i < TOTAL_SENSORS; i++)
  241 + {
  242 + valueSensors[i] = NULL;
  243 + }
  244 +
  245 + printf("Connecting to wifi...\r\n");
  246 + wifi_connect();
  247 +
  248 + wakeup_sensors(0xFF);
  249 + printf("Scanning sensors...\r\n");
  250 + //Escanea los sensores -> retorna un vector con las direcciones en cada posiciรณn del vector, si la posiciรณn del vector retorna un cero -> no existe el sensor
  251 + //Escanea y registra
  252 + I2C_scan(I2C1,sensors);
  253 + sensors_length=strlen((char*)sensors);
  254 + printf("%d sensor detected...\r\n",sensors_length);
  255 +
  256 + unsigned long time;
  257 + update_time(&time);
  258 + //desconectarwifi
  259 + printf("Disconecting wifi...\r\n");
  260 + wifi_disconnect();
  261 +
  262 + unsigned long timestamp = 0;
  263 + unsigned long delay = getSystemTime();
  264 + sleep_thread(SHORT_PERIOD - time%SHORT_PERIOD);
  265 +
  266 + uint32_t ind[TOTAL_SENSORS]={0};
  267 + char** buffers[TOTAL_SENSORS];
  268 + uint32_t sizes[TOTAL_SENSORS]={0};
  269 +
  270 + i = 1;
  271 +
  272 + while(1)
  273 + {
  274 + time += getElapsedTime(delay);
  275 + printf("time (absolute):\t%ul\r\ntime mod LONG_PERIOD:\t%ul\r\ntime mod SHORT_PERIOD:\t%ul\r\n",time,time%LONG_PERIOD,time%SHORT_PERIOD);
  276 +
  277 + delay = getSystemTime();
  278 + /* Collect data from sensors */
  279 + printf("Collecting data...\r\n");
  280 + collectData(valueSensors, sensors);
  281 + printf("Data collected...\r\n");
  282 + time += getElapsedTime(delay);
  283 + printf("time (absolute):\t%ul\r\ntime mod LONG_PERIOD:\t%ul\r\ntime mod SHORT_PERIOD:\t%ul\r\n",time,time%LONG_PERIOD,time%SHORT_PERIOD);
  284 + timestamp = time;
  285 + printf("timestamp (absolute):\t%ul\r\ntimestamp mod LONG_PERIOD:\t%ul\r\ntimestamp mod SHORT_PERIOD:\t%ul\r\n",timestamp,timestamp%LONG_PERIOD,timestamp%SHORT_PERIOD);
  286 + delay = getSystemTime();
  287 + printf("Timestamping data...\r\n");
  288 + char** values=timestamp_datas(valueSensors,timestamp,sensors);
  289 + printf("Putting data in buffer...\r\n");
  290 + put_buffers(buffers,ind,sizes,values,sensors);
  291 + printf("Data is now in buffer...\n\r");
  292 + if (i == LONG_PERIOD/SHORT_PERIOD ){
  293 + printf("Programmed Send cycle...\r\n");
  294 + /* Wi-Fi connect */
  295 + printf("Connecting to wifi...\r\n");
  296 + wifi_connect();
  297 + /* Send data to server, empty the buffer */
  298 + send_data(buffers, ind, sizes, sensors);
  299 + printf("Data sent!\r\n");
  300 + //Now sending battery level
  301 + send_battery_level(timestamp);
  302 + //time = getNTPTime();
  303 + update_time(&time);
  304 + delay = getSystemTime();
  305 + //desconectar wifi
  306 + wifi_disconnect();
  307 + printf("new ntp time (absolute):\t%ul\r\ntime mod LONG_PERIOD:\t%ul\r\ntime mod SHORT_PERIOD:\t%ul\r\n",time,time%LONG_PERIOD,time%SHORT_PERIOD);
  308 + i = 0;
  309 + }
  310 +
  311 + printf("mirant memoria\r\n");
  312 + int res=check_memory();
  313 + if(res==SOFT_REACHED){
  314 + printf("--------------soft limit-------------\r\n");
  315 + wifi_connect();
  316 + send_data(buffers, ind, sizes, sensors);
  317 + //Now sending battery level
  318 + send_battery_level(timestamp);
  319 + //disconect_wifi()
  320 + wifi_disconnect();
  321 + }
  322 + else if(res==HARD_REACHED){
  323 + printf("--------------hard limit-------------\r\n");
  324 + wifi_connect();
  325 + char id_0[3];
  326 + sprintf(id_0,"%x",sensors[0]);
  327 + while( send(buffers[0],&ind[0],&sizes[0], mod.ID, id_0) != JSON_POST_OK )
  328 + {
  329 + // El servidor no ens sap dir si tenim permisos o no sense registrar una mostra.
  330 + // Intentem enviar un buffer sencer, a veure si ja podem buidar.
  331 + // No podem enviar nomรฉs una mostra perquรจ json รฉs rรญgid en aquest sentit.
  332 +
  333 + printf("hard_reached and unable to sentd.\r\nLa biblia en vers: i Jesรบs diguรฉ: Aixeca't, Llร tzer!!\r\n");
  334 + chThdSleepMilliseconds(HARD_LIMIT_WAIT_TIME);
  335 + }
  336 + int j;
  337 + for(j=1;j<sensors_length;j++)
  338 + {
  339 +
  340 + printf(" enviant buffer %d\r\n",j);
  341 + char id[3];
  342 + sprintf(id,"%x",sensors[j]);
  343 + int ok=send(buffers[j],&ind[j],&sizes[j], mod.ID, id);
  344 + if(ok==JSON_COMM_ERROR)
  345 + {
  346 + printf("wismart is not connected\r\n");
154 } 347 }
155 - else if(res==HARD_REACHED){  
156 - /*printf("--------------hard limit-------------\r\n");  
157 -  
158 - // fem servir 085 de prova, perรฒ haurem de fer servir sensor_id[0]  
159 - while( send(buffers[0],&ind[0],&sizes[0], mod.ID, sensor_id[j]) != JSON_POST_OK )  
160 - {  
161 - // El servidor no ens sap dir si tenim permisos o no sense registrar una mostra.  
162 - // Intentem enviar un buffer sencer, a veure si ja podem buidar.  
163 - // No podem enviar nomรฉs una mostra perquรจ json รฉs rรญgid en aquest sentit.  
164 -  
165 - printf("hard_reached and unable to sentd.\r\nLa biblia en vers: i Jesรบs diguรฉ: Aixeca't, Llร tzer!!\r\n");  
166 - chThdSleepMilliseconds(HARD_LIMIT_WAIT_TIME);  
167 - }  
168 - int j;  
169 - for(j=1;j<TOTAL_SENSORS;j++)  
170 - {  
171 - printf(" enviant buffer %d\r\n",j);  
172 -  
173 - // fem servir 085 de prova, perรฒ haurem de fer servir sensor_id[j]  
174 - int ok=send(buffers[j],&ind[j],&sizes[j], mod.ID, sensor_id[j]);  
175 - if(ok==JSON_COMM_ERROR)  
176 - {  
177 - printf("wismart is not connected\r\n");  
178 - }  
179 - else if( ok==JSON_OTHER_ERROR){  
180 - printf("some error ocurred\r\n");  
181 - }  
182 - else if(ok ==JSON_POST_OK){  
183 - printf(" send OK \r\n");  
184 - }  
185 - }*/ 348 + else if( ok==JSON_OTHER_ERROR){
  349 + printf("some error ocurred\r\n");
186 } 350 }
  351 + else if(ok ==JSON_POST_OK){
  352 + printf(" send OK \r\n");
  353 + }
  354 +
187 } 355 }
188 - //chThdSleepMilliseconds(100);  
189 - } 356 + //Now sending battery level
  357 + send_battery_level(timestamp);
  358 + wifi_disconnect();
  359 + }
  360 + time += getElapsedTime(delay);
  361 + printf("time (absolute):\t%ul\r\ntime mod LONG_PERIOD:\t%ul\r\ntime mod SHORT_PERIOD:\t%ul\r\n",time,time%LONG_PERIOD,time%SHORT_PERIOD);
  362 +
  363 + delay = getSystemTime();
  364 + printf("Time to sleep! for %d seconds\r\n", SHORT_PERIOD - time%SHORT_PERIOD);
  365 + sleep_thread(SHORT_PERIOD - time%SHORT_PERIOD);
  366 + i++;
  367 + }
190 } 368 }
191 369
192 -//SE DEBE LIBERAR LA MEMORIA DE LAS FUNCIONES value DE LOS SENSORES!!!  
193 \ No newline at end of file 370 \ No newline at end of file
  371 +/*
  372 + * TO-DO
  373 + *
  374 + * - Test distance and humidity sensors
  375 + * - Test ADC
  376 + * - Test register, data send and battery send
  377 + * - Reset timer-related defines
  378 + */
194 \ No newline at end of file 379 \ No newline at end of file
Project/applications/smartcities/main2.c deleted
1 -#include <string.h>  
2 -#include "libwismart.h"  
3 -#include "libwismart_irqs.h" /* implement irq handlers */  
4 -#include "lwip/inet.h"  
5 -#include "globals.h"  
6 -#include "httpClient.h"  
7 -#include "callbacks.h"  
8 -#include "buffer.h"  
9 -#include "i2c.h"  
10 -#include "configServer.h"  
11 -#include "timer-loop.h"  
12 -#include "ntp.h"  
13 -#include "sensors.h"  
14 -  
15 -#define WIFI_MODE WIFI_MODE_CLIENT  
16 -#define NETWORK_SSID_AP "modularsense"  
17 -#define NETWORK_KEY_AP NULL  
18 -#define NETWORK_CHANNEL_AP 1  
19 -  
20 -#define HARD_LIMIT_WAIT_TIME 10*1000 //5*60*1000  
21 -  
22 -wismart_registryKey_t geo;  
23 -  
24 -  
25 -void initLibwismart(void)  
26 -{  
27 - wismart_hwif_t hwif = libwismart_GetDefaultHWIF();  
28 - libwismart_Init(hwif);  
29 -}  
30 -  
31 -uint8_t connected=0;  
32 -uint8_t timeout=0;  
33 -uint8_t retries=0;  
34 -uint8_t sensors_length=0;  
35 -uint8_t registry_opened=0;  
36 -  
37 -module mod;  
38 -  
39 -void update_time(unsigned long *time)  
40 -{  
41 - printf("Requesting new NTP time...\r\n");  
42 - unsigned long new_time = getSecsSince1900();  
43 - if(new_time)  
44 - {  
45 - *time = getSecsSince1900();  
46 - }  
47 - printf("Time updated...\r\n");  
48 -}  
49 -  
50 -void init_registry(void)  
51 -{  
52 - config_params_t config;  
53 -  
54 - strcpy(config.localization,"none");  
55 - if(!registry_opened)  
56 - {  
57 - libwismart_RegistryCreateKey(&geo, 1, sizeof(config));  
58 - libwismart_RegistryOpen(1);  
59 - registry_opened = 1;  
60 - }  
61 - if(!libwismart_RegistryIsValueEmpty(&geo))  
62 - {  
63 - libwismart_RegistryGet(&geo,&config);  
64 - }  
65 -  
66 - printf("SSID = %s\r\n",config.ssid);  
67 - printf("Wep key = %s\r\n",config.wepkey);  
68 - printf("Passphrase = %s\r\n", config.passphrase);  
69 - printf("User = %s\r\n",config.user);  
70 - printf("Password = %s\r\n",config.password);  
71 - printf("Encryption type = %d\r\n",config.security);  
72 - printf("Geo Localization = %s\r\n", config.localization);  
73 -  
74 - strcpy(mod.geoloc,config.localization);  
75 - strcpy(mod.ID,MODULE_ID);  
76 - if(config.security == PROFILE_SECURITY_OPEN)  
77 - {  
78 - printf("open detected\r\n");  
79 - libwismart_WiFiConnect(config.ssid,NULL,wifi_connect_result_cb);  
80 - }  
81 - else if(config.security == PROFILE_SECURITY_WPA_WPA2)  
82 - {  
83 - if(!strcmp(config.user,""))  
84 - {  
85 - printf("wpa detected\r\n");  
86 - libwismart_WiFiConnect(config.ssid,config.passphrase,wifi_connect_result_cb);  
87 - }  
88 - else  
89 - {  
90 - printf("wpa Enterprise detected\r\n");  
91 - struct wpa_param wpa;  
92 - wpa.eap_method = WISMART_EAP_METHOD_TTLS;  
93 - wpa.u.ttls.identity = config.user;  
94 - wpa.u.ttls.password = config.password;  
95 - wpa.u.ttls.ca_cert=NULL;  
96 - libwismart_WiFiConnectEnterprise(config.ssid, &wpa, wifi_connect_result_cb);  
97 - }  
98 - }  
99 - else  
100 - {  
101 - printf("wep detected\r\n");  
102 - //Is WEP  
103 - libwismart_WiFiConnect(config.ssid,config.wepkey,wifi_connect_result_cb);  
104 - libwismart_WiFiSetWep(config.wepkey,1);  
105 - }  
106 - while(connected == 0 && timeout != 1 )  
107 - {chThdSleepMilliseconds(500);}  
108 -  
109 -}  
110 -  
111 -void send_data(char** buffers[], uint32_t ind[], uint32_t sizes[], uint8_t sensors[])  
112 -{  
113 - int j;  
114 - for(j=0;j<sensors_length;j++)  
115 - {  
116 - printf(" enviant buffer %d\r\n",j);  
117 -  
118 - // fem servir 085 de prova, perรฒ haurem de fer servir sensor_id[j]  
119 - char id[3];  
120 - sprintf(id,"%x",sensors[j]);  
121 - int ok=send(buffers[j],&ind[j],&sizes[j], mod.ID, id);  
122 -  
123 - if(ok==JSON_COMM_ERROR)  
124 - {  
125 - printf("wismart is not connected\r\n");  
126 - }  
127 - else if( ok==JSON_OTHER_ERROR){  
128 - printf("some error ocurred\r\n");  
129 - }  
130 - else if(ok ==JSON_POST_OK){  
131 - printf(" send OK \r\n");  
132 - }  
133 - }  
134 -}  
135 -  
136 -void put_buffers(char** buffers[],uint32_t ind[],uint32_t sizes[],char** cooked, uint8_t* sensors)  
137 -{  
138 - int i;  
139 - for(i=0;i<sensors_length;i++){  
140 - printf("For %d and %d\n\r", ind[i],sizes[i]);  
141 - printf("Putting data: %s\n\r",cooked[i]);  
142 - buffers[i] = put_message(cooked[i], buffers[i] ,&ind[i],&sizes[i]);  
143 - printf("Message put: %s\r\n",buffers[i][ind[i]-1]);  
144 - chHeapFree(cooked[i]);  
145 - printf("Memory freed\n\r");  
146 - printf("Message put (assertion): %s with length %d\r\n",buffers[i][ind[i]-1] ,strlen(buffers[i][ind[i]-1]));  
147 - }  
148 -  
149 -}  
150 -char** timestamp_datas(char* value[],unsigned long timestamp, uint8_t* sensors)  
151 -{  
152 - char** cooked_data;  
153 - cooked_data = (char**) chHeapAlloc(NULL,sensors_length * sizeof(char*));  
154 - printf("Data allocated...\r\n");  
155 - Date t=getDate(timestamp);  
156 - int i;  
157 - for(i=0;i<sensors_length;i++)  
158 - {  
159 - printf("Calling timestamp_data...\r\n");  
160 - cooked_data[i]=timestamp_data(value[i], t);  
161 - printf("Finished timestamp_data...\r\n");  
162 - printf("Data is: %s\r\n",cooked_data[i]);  
163 - chHeapFree(value[i]);  
164 - value[i] = NULL;  
165 - }  
166 - printf("Returning timestamped data...\r\n");  
167 - return cooked_data;  
168 -}  
169 -  
170 -void wifi_connect(void)  
171 -{  
172 - init_registry();  
173 - if(timeout==1)  
174 - {  
175 - printf("Creating AP\r\n");  
176 - //corroborar los parametros del AP  
177 - configServer_start(1);  
178 - libwismart_WiFi_SoftAP_Start(NETWORK_SSID_AP,NETWORK_CHANNEL_AP,NULL,softapMode_apStartedCb, softapMode_clientIndicationCb);  
179 - for(;;)  
180 - {  
181 - chThdSleepMilliseconds(1000);  
182 - }  
183 - }  
184 -}  
185 -  
186 -void send_battery_level(unsigned long timestamp)  
187 -{  
188 - uint8_t result;  
189 - char *batt_level = battery_value(get_battery_data());  
190 - //char *batt_level = battery_value(3300);  
191 - char *batt_data = timestamp_data(batt_level,getDate(timestamp));  
192 - chHeapFree(batt_level);  
193 - char *statement = prepare_json_observation_statement(&batt_data,1);  
194 - chHeapFree(batt_data);  
195 - char id[3];  
196 - sprintf(id,"%02x",battery.ID);  
197 - result = send_json(statement,strlen(statement),mod.ID,id);  
198 - if(result)  
199 - {  
200 - if(result != 200)  
201 - {  
202 - printf("ERROR: SERVER RETURNED %d\r\n",result);  
203 - }  
204 - }  
205 - else  
206 - {  
207 - printf("ERROR: CONNECTION FAILED\r\n");  
208 - }  
209 - chHeapFree(statement);  
210 -}  
211 -  
212 -void wifi_disconnect(void){  
213 - connected = 0;  
214 - timeout = 0;  
215 - uint8_t res=libwismart_WiFiDisconnect();  
216 - if(res)  
217 - printf("WIFI_DISCONNECT_SUCCESS\r\n");  
218 - else  
219 - printf("WIFI_DISCONNECT_FAILURE\r\n");  
220 -}  
221 -  
222 -int main(void)  
223 -{  
224 - initLibwismart();  
225 - libwismart_PowerSave_Enable();  
226 - libwismart_PowerSave_HigherProfile(TRUE);  
227 - libwismart_RegisterDhcpCB(dhcp_connect_result_cb);  
228 - libwismart_WiFiInit();  
229 - libwismart_SetScanRunsForConnTimeout(4); //Edit a 4  
230 - libwismart_EnableBsdSocketAPI();  
231 -  
232 - int i;  
233 -  
234 - uint8_t sensors[TOTAL_SENSORS];  
235 - memset (sensors, 0, TOTAL_SENSORS);  
236 -  
237 - char* valueSensors[TOTAL_SENSORS];  
238 - //memset (valueSensors, 0, TOTAL_SENSORS);  
239 -  
240 - for(i = 0; i < TOTAL_SENSORS; i++)  
241 - {  
242 - valueSensors[i] = NULL;  
243 - }  
244 -  
245 - printf("Connecting to wifi...\r\n");  
246 - wifi_connect();  
247 -  
248 - wakeup_sensors(0xFF);  
249 - printf("Scanning sensors...\r\n");  
250 - //Escanea los sensores -> retorna un vector con las direcciones en cada posiciรณn del vector, si la posiciรณn del vector retorna un cero -> no existe el sensor  
251 - //Escanea y registra  
252 - I2C_scan(I2C1,sensors);  
253 - sensors_length=strlen((char*)sensors);  
254 - printf("%d sensor detected...\r\n",sensors_length);  
255 -  
256 - unsigned long time;  
257 - update_time(&time);  
258 - //desconectarwifi  
259 - printf("Disconecting wifi...\r\n");  
260 - wifi_disconnect();  
261 -  
262 - unsigned long timestamp = 0;  
263 - unsigned long delay = getSystemTime();  
264 - sleep_thread(SHORT_PERIOD - time%SHORT_PERIOD);  
265 -  
266 - uint32_t ind[TOTAL_SENSORS]={0};  
267 - char** buffers[TOTAL_SENSORS];  
268 - uint32_t sizes[TOTAL_SENSORS]={0};  
269 -  
270 - i = 1;  
271 -  
272 - while(1)  
273 - {  
274 - time += getElapsedTime(delay);  
275 - printf("time (absolute):\t%ul\r\ntime mod LONG_PERIOD:\t%ul\r\ntime mod SHORT_PERIOD:\t%ul\r\n",time,time%LONG_PERIOD,time%SHORT_PERIOD);  
276 -  
277 - delay = getSystemTime();  
278 - /* Collect data from sensors */  
279 - printf("Collecting data...\r\n");  
280 - collectData(valueSensors, sensors);  
281 - printf("Data collected...\r\n");  
282 - time += getElapsedTime(delay);  
283 - printf("time (absolute):\t%ul\r\ntime mod LONG_PERIOD:\t%ul\r\ntime mod SHORT_PERIOD:\t%ul\r\n",time,time%LONG_PERIOD,time%SHORT_PERIOD);  
284 - timestamp = time;  
285 - printf("timestamp (absolute):\t%ul\r\ntimestamp mod LONG_PERIOD:\t%ul\r\ntimestamp mod SHORT_PERIOD:\t%ul\r\n",timestamp,timestamp%LONG_PERIOD,timestamp%SHORT_PERIOD);  
286 - delay = getSystemTime();  
287 - printf("Timestamping data...\r\n");  
288 - char** values=timestamp_datas(valueSensors,timestamp,sensors);  
289 - printf("Putting data in buffer...\r\n");  
290 - put_buffers(buffers,ind,sizes,values,sensors);  
291 - printf("Data is now in buffer...\n\r");  
292 - if (i == LONG_PERIOD/SHORT_PERIOD ){  
293 - printf("Programmed Send cycle...\r\n");  
294 - /* Wi-Fi connect */  
295 - printf("Connecting to wifi...\r\n");  
296 - wifi_connect();  
297 - /* Send data to server, empty the buffer */  
298 - send_data(buffers, ind, sizes, sensors);  
299 - printf("Data sent!\r\n");  
300 - //Now sending battery level  
301 - send_battery_level(timestamp);  
302 - //time = getNTPTime();  
303 - update_time(&time);  
304 - delay = getSystemTime();  
305 - //desconectar wifi  
306 - wifi_disconnect();  
307 - printf("new ntp time (absolute):\t%ul\r\ntime mod LONG_PERIOD:\t%ul\r\ntime mod SHORT_PERIOD:\t%ul\r\n",time,time%LONG_PERIOD,time%SHORT_PERIOD);  
308 - i = 0;  
309 - }  
310 -  
311 - printf("mirant memoria\r\n");  
312 - int res=check_memory();  
313 - if(res==SOFT_REACHED){  
314 - printf("--------------soft limit-------------\r\n");  
315 - wifi_connect();  
316 - send_data(buffers, ind, sizes, sensors);  
317 - //Now sending battery level  
318 - send_battery_level(timestamp);  
319 - //disconect_wifi()  
320 - wifi_disconnect();  
321 - }  
322 - else if(res==HARD_REACHED){  
323 - printf("--------------hard limit-------------\r\n");  
324 - wifi_connect();  
325 - char id_0[3];  
326 - sprintf(id_0,"%x",sensors[0]);  
327 - while( send(buffers[0],&ind[0],&sizes[0], mod.ID, id_0) != JSON_POST_OK )  
328 - {  
329 - // El servidor no ens sap dir si tenim permisos o no sense registrar una mostra.  
330 - // Intentem enviar un buffer sencer, a veure si ja podem buidar.  
331 - // No podem enviar nomรฉs una mostra perquรจ json รฉs rรญgid en aquest sentit.  
332 -  
333 - printf("hard_reached and unable to sentd.\r\nLa biblia en vers: i Jesรบs diguรฉ: Aixeca't, Llร tzer!!\r\n");  
334 - chThdSleepMilliseconds(HARD_LIMIT_WAIT_TIME);  
335 - }  
336 - int j;  
337 - for(j=1;j<sensors_length;j++)  
338 - {  
339 -  
340 - printf(" enviant buffer %d\r\n",j);  
341 - char id[3];  
342 - sprintf(id,"%x",sensors[j]);  
343 - int ok=send(buffers[j],&ind[j],&sizes[j], mod.ID, id);  
344 - if(ok==JSON_COMM_ERROR)  
345 - {  
346 - printf("wismart is not connected\r\n");  
347 - }  
348 - else if( ok==JSON_OTHER_ERROR){  
349 - printf("some error ocurred\r\n");  
350 - }  
351 - else if(ok ==JSON_POST_OK){  
352 - printf(" send OK \r\n");  
353 - }  
354 -  
355 - }  
356 - //Now sending battery level  
357 - send_battery_level(timestamp);  
358 - wifi_disconnect();  
359 - }  
360 - time += getElapsedTime(delay);  
361 - printf("time (absolute):\t%ul\r\ntime mod LONG_PERIOD:\t%ul\r\ntime mod SHORT_PERIOD:\t%ul\r\n",time,time%LONG_PERIOD,time%SHORT_PERIOD);  
362 -  
363 - delay = getSystemTime();  
364 - printf("Time to sleep! for %d seconds\r\n", SHORT_PERIOD - time%SHORT_PERIOD);  
365 - sleep_thread(SHORT_PERIOD - time%SHORT_PERIOD);  
366 - i++;  
367 - }  
368 -}  
369 -  
370 -/*  
371 - * TO-DO  
372 - *  
373 - * - Test distance and humidity sensors  
374 - * - Test ADC  
375 - * - Test register, data send and battery send  
376 - * - Reset timer-related defines  
377 - */  
378 \ No newline at end of file 0 \ No newline at end of file
Project/applications/smartcities/main_demo.c deleted
1 -#include "libwismart.h"  
2 -#include "libwismart_irqs.h" /* implement irq handlers */  
3 -#include "lwip/inet.h"  
4 -#include "globals.h"  
5 -#include "httpClient.h"  
6 -#include "callbacks.h"  
7 -//#include "buffer.h"  
8 -#include "json.h"  
9 -#include "ntp.h"  
10 -#include "i2c.h"  
11 -  
12 -#define WIFI_MODE WIFI_MODE_CLIENT  
13 -#define NETWORK_SSID "linksys"  
14 -#define NETWORK_KEY ""  
15 -#define WPA_USER "smartcities"  
16 -#define WPA_PASS "superpass"  
17 -  
18 -uint8_t connected=0;  
19 -uint8_t timeout=0;  
20 -uint8_t retries=0;  
21 -  
22 -void initLibwismart(void)  
23 -{  
24 - wismart_hwif_t hwif = libwismart_GetDefaultHWIF();  
25 - libwismart_Init(hwif);  
26 -}  
27 -  
28 -int main(void)  
29 -{  
30 - struct wpa_param wpa;  
31 - wpa.eap_method = WISMART_EAP_METHOD_TTLS;  
32 - wpa.u.ttls.identity=WPA_USER;  
33 - wpa.u.ttls.password=WPA_PASS;  
34 - wpa.u.ttls.ca_cert=NULL;  
35 - initLibwismart();  
36 - libwismart_PowerSave_Enable();  
37 - libwismart_PowerSave_HigherProfile(TRUE);  
38 - libwismart_RegisterDhcpCB(dhcp_connect_result_cb);  
39 - libwismart_WiFiInit();  
40 - libwismart_WiFiConnectEnterprise(NETWORK_SSID, &wpa, wifi_connect_result_cb);  
41 -  
42 - printf("WiSmart initialized\r\n");  
43 -  
44 - while(!connected)  
45 - {chThdSleepMilliseconds(500);}  
46 -  
47 - uint16_t received_data;  
48 - char answer[10];  
49 -  
50 - I2C_init();  
51 - printf("I2C Initialized\r\n");  
52 -  
53 - I2C_start(I2C1,0x39 << 1, I2C_Direction_Transmitter);  
54 - I2C_write(I2C1, 0x0C);  
55 - I2C_stop(I2C1);  
56 - printf("I2C Temperature measurment request sent\r\n");  
57 -  
58 - I2C_start(I2C1, 0x39<<1, I2C_Direction_Receiver);  
59 - printf("I2C Read request sent\r\n");  
60 - received_data = I2C_read_ack(I2C1);  
61 - received_data = received_data << 8;  
62 - received_data = received_data | I2C_read_ack(I2C1);  
63 - printf("I2C data received\r\n");  
64 -  
65 - sprintf(answer,"%d",received_data);  
66 - printf("Data: %s\r\n",answer);  
67 -  
68 - char **buffer;  
69 - buffer = chHeapAlloc(NULL,sizeof (char *) * 1);  
70 -  
71 - Date time=getDate(getSecsSince1900());  
72 -  
73 - libwismart_EnableBsdSocketAPI();  
74 -  
75 - buffer[0] = timestamp_data(answer,time);  
76 -  
77 - printf("Init JSON statement\r\n");  
78 - char* statement = prepare_json_observation_statement(buffer,1);  
79 - chHeapFree(buffer[0]);  
80 - printf("Statement: ");  
81 - printf("%s\r\n",statement);  
82 - uint8_t result = send_json(statement, strlen(statement), "bmp", "085");  
83 - if(result == JSON_POST_OK)  
84 - {  
85 - printf("JSON_POST_OK\r\n");  
86 - }  
87 - else if(result == JSON_COMM_ERROR)  
88 - {  
89 - printf("JSON_COMM_ERROR\r\n");  
90 - }  
91 - else if(result == JSON_OTHER_ERROR)  
92 - {  
93 - printf("JSON_OTHER_ERROR\r\n");  
94 - }  
95 - else  
96 - {  
97 - printf("WTF I don't even\r\n");  
98 - }  
99 -  
100 - for(;;)  
101 - {  
102 - chThdSleepMilliseconds(500);  
103 - }  
104 -}  
105 -