diff --git a/Project/applications/smartcities/httpClient.c b/Project/applications/smartcities/httpClient.c index e139ac6..39af085 100644 --- a/Project/applications/smartcities/httpClient.c +++ b/Project/applications/smartcities/httpClient.c @@ -79,8 +79,8 @@ int httpRequest(struct httpHeaders head, char* content, int content_size) } printf("packet: %s\r\n",request); // Set connection - while(connection_ok !=0) // != ERR_OK - { + /*while(connection_ok !=0) // != ERR_OK + {*/ printf("httpRequest: Setting connection\r\n"); neocon = netconn_new(NETCONN_TCP); if(neocon == NULL) @@ -109,8 +109,9 @@ int httpRequest(struct httpHeaders head, char* content, int content_size) netbuf_delete(netBufs); netconn_close(neocon); netconn_delete(neocon); + return 0; } - } + //} // Manage Response printf("httpRequest: Response received. Let's parse the information\r\n"); response = (char*)chHeapAlloc(NULL,4*sizeof(char)); diff --git a/Project/applications/smartcities/i2c.c b/Project/applications/smartcities/i2c.c index 2441e17..934727b 100644 --- a/Project/applications/smartcities/i2c.c +++ b/Project/applications/smartcities/i2c.c @@ -7,35 +7,28 @@ void I2C_init(void) GPIO_InitTypeDef GPIO_InitStruct; I2C_InitTypeDef I2C_InitStruct; + // enable APB1 peripheral clock for I2C1 RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); - /* - * 1. SCL on PB6 or PB8 - * 2. SDA on PB7 or PB9 - */ - - GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; - GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_OD; - GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; - GPIO_Init(GPIOB, &GPIO_InitStruct); // init GPIOB - - /* WARNING - * - * Check consistency with above configuration - * - * --Imanol - */ - + GPIO_StructInit(&GPIO_InitStruct); + GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; // PB6 and PB7 + GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_OD; // set pins to alternate function + GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; // set GPIO speed + GPIO_Init(GPIOB, &GPIO_InitStruct); // init GPIOB + // configure I2C1 - I2C_InitStruct.I2C_ClockSpeed = 100000; // 100kHz - STANDARD MODE - I2C_InitStruct.I2C_Mode = I2C_Mode_I2C; - I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2; - I2C_InitStruct.I2C_OwnAddress1 = 0x00; - I2C_InitStruct.I2C_Ack = I2C_Ack_Enable; - I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; - I2C_Init(I2C1, &I2C_InitStruct); - I2C_Cmd(I2C1, ENABLE); + I2C_StructInit(&I2C_InitStruct); + I2C_InitStruct.I2C_ClockSpeed = 100000; // 100kHz(standard) vs 400 + I2C_InitStruct.I2C_Mode = I2C_Mode_I2C; // I2C mode + I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2; // 50% duty cycle --> standard + I2C_InitStruct.I2C_OwnAddress1 = 0x00; // own address, not relevant in master mode + I2C_InitStruct.I2C_Ack = I2C_Ack_Enable; // disable acknowledge when reading (can be changed later on) + I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; // set address length to 7 bit addresses + I2C_Init(I2C1, &I2C_InitStruct); // init I2C1 + + // enable I2C1 + I2C_Cmd(I2C1, ENABLE); //sets PE bit in CR1, at end` } 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) void I2C_write(I2C_TypeDef* I2Cx, uint8_t data) { printf("Sending I2C byte %02x...\r\n",data); - /* WARNING - * - * OJITO QUE ESTÁ ESPERANDO EL EV8 Y NO EL EV8_2 - * - * --Imanol - */ - while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTING)) + I2C_SendData(I2Cx, data); + while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) { - // wait for I2C1 EV8 --> last byte is still being transmitted (last byte in SR, buffer empty), next byte can already be written + // wait for I2C1 EV8_2 --> last byte is transmitted } - I2C_SendData(I2Cx, data); } uint8_t I2C_read_ack(I2C_TypeDef* I2Cx) @@ -92,7 +79,7 @@ uint8_t I2C_read_ack(I2C_TypeDef* I2Cx) // wait until one byte has been received } uint8_t data = I2C_ReceiveData(I2Cx); - printf("Received byte: %02x",data); + printf("Received byte: %02x\r\n",data); return data; } @@ -106,7 +93,7 @@ uint8_t I2C_read_nack(I2C_TypeDef* I2Cx) // wait until one byte has been received } uint8_t data = I2C_ReceiveData(I2Cx); - printf("Received byte: %02x",data); + printf("Received byte: %02x\r\n",data); return data; } @@ -114,10 +101,10 @@ void I2C_stop(I2C_TypeDef* I2Cx) { printf("Sending I2C Stop...\r\n"); I2C_GenerateSTOP(I2Cx, ENABLE); - while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTING)) + /*while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTING)) { // wait for I2C1 EV8_2 --> byte has been transmitted - } + }*/ } uint8_t I2C_check(I2C_TypeDef* I2Cx, uint8_t address) diff --git a/Project/applications/smartcities/include/ntp.h b/Project/applications/smartcities/include/ntp.h index 4dec285..6f0629e 100644 --- a/Project/applications/smartcities/include/ntp.h +++ b/Project/applications/smartcities/include/ntp.h @@ -1,6 +1,14 @@ #ifndef NTP_H #define NTP_H + +#include +#include +#include +#include +#include +#include +#include #include "libwismart.h" #include "lwip/opt.h" #include "lwip/tcp.h" @@ -63,7 +71,7 @@ void udpNtp_test(void); #define SNTP_MODE_MASK 0x07 /* number of seconds between 1900 and 1970 */ -#define DIFF_SEC_1900_1970 (2208988800) +#define DIFF_SEC_1900_1970 (2208988800LL) /*timezone from GMT*/ #define TIME_ZONE 2 diff --git a/Project/applications/smartcities/json.c b/Project/applications/smartcities/json.c index 23150ff..82623e4 100644 --- a/Project/applications/smartcities/json.c +++ b/Project/applications/smartcities/json.c @@ -18,9 +18,8 @@ uint8_t register_sensor(sensor sens) } printf("%s\r\n",statement); char sensor_ID[3]; - sprintf(sensor_ID,"%d",sens.ID); - printf("REMOVE COMMENTARY IN send_json ON json.c LINE 23!!!!!!!\r\n"); - //result = send_json(statement,strlen(statement),mod.ID,sensor_ID); + sprintf(sensor_ID,"%02x",sens.ID); + result = send_json(statement,strlen(statement),mod.ID,sensor_ID); chHeapFree(statement); return result; } diff --git a/Project/applications/smartcities/main2.c b/Project/applications/smartcities/main2.c index 21f7672..51e8081 100644 --- a/Project/applications/smartcities/main2.c +++ b/Project/applications/smartcities/main2.c @@ -183,6 +183,7 @@ void wifi_connect(void) void send_battery_level(unsigned long timestamp) { + uint8_t result; //char *batt_level = battery_value(get_battery_data()); char *batt_level = battery_value(3300); char *batt_data = timestamp_data(batt_level,getDate(timestamp)); @@ -190,8 +191,19 @@ void send_battery_level(unsigned long timestamp) char *statement = prepare_json_observation_statement(&batt_data,1); chHeapFree(batt_data); char id[3]; - sprintf(id,"%d",battery.ID); - send_json(statement,strlen(statement),id,mod.ID); + sprintf(id,"%02x",battery.ID); + result = send_json(statement,strlen(statement),mod.ID,id); + if(result) + { + if(result != 200) + { + printf("ERROR: SERVER RETURNED %d\r\n",result); + } + } + else + { + printf("ERROR: CONNECTION FAILED\r\n"); + } chHeapFree(statement); } @@ -212,8 +224,6 @@ int main(void) libwismart_WiFiInit(); libwismart_SetScanRunsForConnTimeout(4); //Edit a 4 libwismart_EnableBsdSocketAPI(); - - //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 uint8_t sensors[TOTAL_SENSORS]; memset (sensors, 0, TOTAL_SENSORS); @@ -223,11 +233,10 @@ int main(void) printf("Connecting to wifi...\r\n"); wifi_connect(); - - send_battery_level(getSecsSince1900()); wakeup_sensors(0xFF); printf("Scanning sensors...\r\n"); + //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 //Escanea y registra I2C_scan(sensors); sensors_length=strlen((char*)sensors); @@ -357,7 +366,7 @@ int main(void) * - I2C issues * - Test all sensors * - Test ADC - * - Network fault tolerance + * - Test register, data send and battery send * - Reset server-related defines * - Reset timer-related defines */ \ No newline at end of file diff --git a/Project/applications/smartcities/ntp.c b/Project/applications/smartcities/ntp.c index 887ccf3..9d152a7 100644 --- a/Project/applications/smartcities/ntp.c +++ b/Project/applications/smartcities/ntp.c @@ -1,11 +1,3 @@ -#include -#include -#include -#include -#include -#include -#include - #include "ntp.h" Date getDate(unsigned long secsSince1900){ diff --git a/Project/applications/smartcities/sensors.c b/Project/applications/smartcities/sensors.c index 50f43cc..30ef75f 100644 --- a/Project/applications/smartcities/sensors.c +++ b/Project/applications/smartcities/sensors.c @@ -93,7 +93,7 @@ uint16_t get_light_ch0(void) I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter); I2C_write(I2C1, 0x0C); I2C_stop(I2C1); - printf("LIGHT: Sent fetch command for low byte for CH1\r\n"); + printf("LIGHT: Sent fetch command for CH1\r\n"); I2C_start(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver); data = I2C_read_ack(I2C1); @@ -111,7 +111,7 @@ uint16_t get_light_ch1(void) I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter); I2C_write(I2C1, 0x0E); I2C_stop(I2C1); - printf("LIGHT: Sent fetch command for low byte for CH1\r\n"); + printf("LIGHT: Sent fetch command for CH1\r\n"); I2C_start(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver); data = I2C_read_ack(I2C1); @@ -470,8 +470,4 @@ char* battery_value(uint32_t battery) char *value = chHeapAlloc(NULL,11 + 1); sprintf(value,"%4u",(unsigned int)battery); return value; -} - -//light_value(get_light_data()); -//temp_humidity_value(get_temperature_data(),get_humidity_data()); -//pressure_value(get_pressure_data(), get_pressure_temperature_data()); \ No newline at end of file +} \ No newline at end of file