Commit e08f57630c57a2e97ca1cfd635655200e34a0b17

Authored by Imanol-Mikel Barba Sabariego
1 parent 2b4419ff

--no commit message

Project/applications/smartcities/httpClient.c
... ... @@ -79,8 +79,8 @@ int httpRequest(struct httpHeaders head, char* content, int content_size)
79 79 }
80 80 printf("packet: %s\r\n",request);
81 81 // Set connection
82   - while(connection_ok !=0) // != ERR_OK
83   - {
  82 + /*while(connection_ok !=0) // != ERR_OK
  83 + {*/
84 84 printf("httpRequest: Setting connection\r\n");
85 85 neocon = netconn_new(NETCONN_TCP);
86 86 if(neocon == NULL)
... ... @@ -109,8 +109,9 @@ int httpRequest(struct httpHeaders head, char* content, int content_size)
109 109 netbuf_delete(netBufs);
110 110 netconn_close(neocon);
111 111 netconn_delete(neocon);
  112 + return 0;
112 113 }
113   - }
  114 + //}
114 115 // Manage Response
115 116 printf("httpRequest: Response received. Let's parse the information\r\n");
116 117 response = (char*)chHeapAlloc(NULL,4*sizeof(char));
... ...
Project/applications/smartcities/i2c.c
... ... @@ -7,35 +7,28 @@ void I2C_init(void)
7 7 GPIO_InitTypeDef GPIO_InitStruct;
8 8 I2C_InitTypeDef I2C_InitStruct;
9 9  
  10 + // enable APB1 peripheral clock for I2C1
10 11 RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
11 12 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
12 13  
13   - /*
14   - * 1. SCL on PB6 or PB8
15   - * 2. SDA on PB7 or PB9
16   - */
17   -
18   - GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
19   - GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_OD;
20   - GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
21   - GPIO_Init(GPIOB, &GPIO_InitStruct); // init GPIOB
22   -
23   - /* WARNING
24   - *
25   - * Check consistency with above configuration
26   - *
27   - * --Imanol
28   - */
29   -
  14 + 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
  19 +
30 20 // configure I2C1
31   - I2C_InitStruct.I2C_ClockSpeed = 100000; // 100kHz - STANDARD MODE
32   - I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;
33   - I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2;
34   - I2C_InitStruct.I2C_OwnAddress1 = 0x00;
35   - I2C_InitStruct.I2C_Ack = I2C_Ack_Enable;
36   - I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
37   - I2C_Init(I2C1, &I2C_InitStruct);
38   - I2C_Cmd(I2C1, ENABLE);
  21 + 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 I2C1
  31 + I2C_Cmd(I2C1, ENABLE); //sets PE bit in CR1, at end`
39 32 }
40 33  
41 34 void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction)
... ... @@ -70,17 +63,11 @@ void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction)
70 63 void I2C_write(I2C_TypeDef* I2Cx, uint8_t data)
71 64 {
72 65 printf("Sending I2C byte %02x...\r\n",data);
73   - /* WARNING
74   - *
75   - * OJITO QUE ESTÁ ESPERANDO EL EV8 Y NO EL EV8_2
76   - *
77   - * --Imanol
78   - */
79   - while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTING))
  66 + I2C_SendData(I2Cx, data);
  67 + while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
80 68 {
81   - // wait for I2C1 EV8 --> last byte is still being transmitted (last byte in SR, buffer empty), next byte can already be written
  69 + // wait for I2C1 EV8_2 --> last byte is transmitted
82 70 }
83   - I2C_SendData(I2Cx, data);
84 71 }
85 72  
86 73 uint8_t I2C_read_ack(I2C_TypeDef* I2Cx)
... ... @@ -92,7 +79,7 @@ uint8_t I2C_read_ack(I2C_TypeDef* I2Cx)
92 79 // wait until one byte has been received
93 80 }
94 81 uint8_t data = I2C_ReceiveData(I2Cx);
95   - printf("Received byte: %02x",data);
  82 + printf("Received byte: %02x\r\n",data);
96 83 return data;
97 84 }
98 85  
... ... @@ -106,7 +93,7 @@ uint8_t I2C_read_nack(I2C_TypeDef* I2Cx)
106 93 // wait until one byte has been received
107 94 }
108 95 uint8_t data = I2C_ReceiveData(I2Cx);
109   - printf("Received byte: %02x",data);
  96 + printf("Received byte: %02x\r\n",data);
110 97 return data;
111 98 }
112 99  
... ... @@ -114,10 +101,10 @@ void I2C_stop(I2C_TypeDef* I2Cx)
114 101 {
115 102 printf("Sending I2C Stop...\r\n");
116 103 I2C_GenerateSTOP(I2Cx, ENABLE);
117   - while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTING))
  104 + /*while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTING))
118 105 {
119 106 // wait for I2C1 EV8_2 --> byte has been transmitted
120   - }
  107 + }*/
121 108 }
122 109  
123 110 uint8_t I2C_check(I2C_TypeDef* I2Cx, uint8_t address)
... ...
Project/applications/smartcities/include/ntp.h
1 1 #ifndef NTP_H
2 2 #define NTP_H
3 3  
  4 +
  5 +#include <stdio.h>
  6 +#include <time.h>
  7 +#include <string.h>
  8 +#include <stdlib.h>
  9 +#include <unistd.h>
  10 +#include <math.h>
  11 +#include <sys/time.h>
4 12 #include "libwismart.h"
5 13 #include "lwip/opt.h"
6 14 #include "lwip/tcp.h"
... ... @@ -63,7 +71,7 @@ void udpNtp_test(void);
63 71 #define SNTP_MODE_MASK 0x07
64 72  
65 73 /* number of seconds between 1900 and 1970 */
66   -#define DIFF_SEC_1900_1970 (2208988800)
  74 +#define DIFF_SEC_1900_1970 (2208988800LL)
67 75  
68 76 /*timezone from GMT*/
69 77 #define TIME_ZONE 2
... ...
Project/applications/smartcities/json.c
... ... @@ -18,9 +18,8 @@ uint8_t register_sensor(sensor sens)
18 18 }
19 19 printf("%s\r\n",statement);
20 20 char sensor_ID[3];
21   - sprintf(sensor_ID,"%d",sens.ID);
22   - printf("REMOVE COMMENTARY IN send_json ON json.c LINE 23!!!!!!!\r\n");
23   - //result = send_json(statement,strlen(statement),mod.ID,sensor_ID);
  21 + sprintf(sensor_ID,"%02x",sens.ID);
  22 + result = send_json(statement,strlen(statement),mod.ID,sensor_ID);
24 23 chHeapFree(statement);
25 24 return result;
26 25 }
... ...
Project/applications/smartcities/main2.c
... ... @@ -183,6 +183,7 @@ void wifi_connect(void)
183 183  
184 184 void send_battery_level(unsigned long timestamp)
185 185 {
  186 + uint8_t result;
186 187 //char *batt_level = battery_value(get_battery_data());
187 188 char *batt_level = battery_value(3300);
188 189 char *batt_data = timestamp_data(batt_level,getDate(timestamp));
... ... @@ -190,8 +191,19 @@ void send_battery_level(unsigned long timestamp)
190 191 char *statement = prepare_json_observation_statement(&batt_data,1);
191 192 chHeapFree(batt_data);
192 193 char id[3];
193   - sprintf(id,"%d",battery.ID);
194   - send_json(statement,strlen(statement),id,mod.ID);
  194 + sprintf(id,"%02x",battery.ID);
  195 + result = send_json(statement,strlen(statement),mod.ID,id);
  196 + if(result)
  197 + {
  198 + if(result != 200)
  199 + {
  200 + printf("ERROR: SERVER RETURNED %d\r\n",result);
  201 + }
  202 + }
  203 + else
  204 + {
  205 + printf("ERROR: CONNECTION FAILED\r\n");
  206 + }
195 207 chHeapFree(statement);
196 208 }
197 209  
... ... @@ -212,8 +224,6 @@ int main(void)
212 224 libwismart_WiFiInit();
213 225 libwismart_SetScanRunsForConnTimeout(4); //Edit a 4
214 226 libwismart_EnableBsdSocketAPI();
215   -
216   - //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
217 227  
218 228 uint8_t sensors[TOTAL_SENSORS];
219 229 memset (sensors, 0, TOTAL_SENSORS);
... ... @@ -223,11 +233,10 @@ int main(void)
223 233  
224 234 printf("Connecting to wifi...\r\n");
225 235 wifi_connect();
226   -
227   - send_battery_level(getSecsSince1900());
228 236  
229 237 wakeup_sensors(0xFF);
230 238 printf("Scanning sensors...\r\n");
  239 + //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
231 240 //Escanea y registra
232 241 I2C_scan(sensors);
233 242 sensors_length=strlen((char*)sensors);
... ... @@ -357,7 +366,7 @@ int main(void)
357 366 * - I2C issues
358 367 * - Test all sensors
359 368 * - Test ADC
360   - * - Network fault tolerance
  369 + * - Test register, data send and battery send
361 370 * - Reset server-related defines
362 371 * - Reset timer-related defines
363 372 */
364 373 \ No newline at end of file
... ...
Project/applications/smartcities/ntp.c
1   -#include <stdio.h>
2   -#include <time.h>
3   -#include <string.h>
4   -#include <stdlib.h>
5   -#include <unistd.h>
6   -#include <math.h>
7   -#include <sys/time.h>
8   -
9 1 #include "ntp.h"
10 2  
11 3 Date getDate(unsigned long secsSince1900){
... ...
Project/applications/smartcities/sensors.c
... ... @@ -93,7 +93,7 @@ uint16_t get_light_ch0(void)
93 93 I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter);
94 94 I2C_write(I2C1, 0x0C);
95 95 I2C_stop(I2C1);
96   - printf("LIGHT: Sent fetch command for low byte for CH1\r\n");
  96 + printf("LIGHT: Sent fetch command for CH1\r\n");
97 97  
98 98 I2C_start(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver);
99 99 data = I2C_read_ack(I2C1);
... ... @@ -111,7 +111,7 @@ uint16_t get_light_ch1(void)
111 111 I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter);
112 112 I2C_write(I2C1, 0x0E);
113 113 I2C_stop(I2C1);
114   - printf("LIGHT: Sent fetch command for low byte for CH1\r\n");
  114 + printf("LIGHT: Sent fetch command for CH1\r\n");
115 115  
116 116 I2C_start(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver);
117 117 data = I2C_read_ack(I2C1);
... ... @@ -470,8 +470,4 @@ char* battery_value(uint32_t battery)
470 470 char *value = chHeapAlloc(NULL,11 + 1);
471 471 sprintf(value,"%4u",(unsigned int)battery);
472 472 return value;
473   -}
474   -
475   -//light_value(get_light_data());
476   -//temp_humidity_value(get_temperature_data(),get_humidity_data());
477   -//pressure_value(get_pressure_data(), get_pressure_temperature_data());
478 473 \ No newline at end of file
  474 +}
479 475 \ No newline at end of file
... ...