diff --git a/Project/applications/smartcities/buglist.txt b/Project/applications/smartcities/buglist.txt index 51f770d..6dd8ff9 100644 --- a/Project/applications/smartcities/buglist.txt +++ b/Project/applications/smartcities/buglist.txt @@ -1,4 +1,4 @@ -- NTP sudden reset - Investigate leaks +- Pressure sensor memory - Test sound sensor -- Test multiple sensors \ No newline at end of file +- Test multiple sensors with DB9 \ No newline at end of file diff --git a/Project/applications/smartcities/callbacks.c b/Project/applications/smartcities/callbacks.c index 2d47ec9..ef6d990 100644 --- a/Project/applications/smartcities/callbacks.c +++ b/Project/applications/smartcities/callbacks.c @@ -7,7 +7,7 @@ void dhcp_connect_result_cb(int result) { libwismart_GetCurrentIP(&ip,NULL,NULL); DBG_CALLBACKS("IP: %d.%d.%d.%d \r\n",ip.addr[3],ip.addr[2],ip.addr[1],ip.addr[0]); - connected = 1; + connected |= CONNECTED_NOW; retries = 0; } else if(result==LIBWISMART_DHCP_TIMEOUT) diff --git a/Project/applications/smartcities/flash.sh b/Project/applications/smartcities/flash.sh index ae2a651..02c3880 100755 --- a/Project/applications/smartcities/flash.sh +++ b/Project/applications/smartcities/flash.sh @@ -2,4 +2,4 @@ make clean make all -st-flash write build/smartcities.bin 0x8000000 +st-flash --reset write build/smartcities.bin 0x8000000 diff --git a/Project/applications/smartcities/i2c.c b/Project/applications/smartcities/i2c.c index 208aea9..4580224 100644 --- a/Project/applications/smartcities/i2c.c +++ b/Project/applications/smartcities/i2c.c @@ -51,7 +51,6 @@ void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction) { // wait for I2Cx EV6, check if Slave has acknowledged Master transmitter mode } - DBG_I2C("Slave -> Master ACK\r\n"); } else if(direction == I2C_Direction_Receiver) { @@ -59,8 +58,8 @@ void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction) { // wait for I2Cx EV6, check if Slave has acknowledged Master receiver mode } - DBG_I2C("Slave -> Master ACK\r\n"); } + DBG_I2C("Slave -> Master ACK\r\n"); } void I2C_restart(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction) @@ -81,7 +80,6 @@ void I2C_restart(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction) { // wait for I2Cx EV6, check if Slave has acknowledged Master transmitter mode } - DBG_I2C("Slave -> Master ACK\r\n"); } else if(direction == I2C_Direction_Receiver) { @@ -89,8 +87,8 @@ void I2C_restart(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction) { // wait for I2Cx EV6, check if Slave has acknowledged Master receiver mode } - DBG_I2C("Slave -> Master ACK\r\n"); } + DBG_I2C("Slave -> Master ACK\r\n"); } void I2C_write(I2C_TypeDef* I2Cx, uint8_t data) @@ -121,7 +119,6 @@ uint8_t I2C_read_nack(I2C_TypeDef* I2Cx) { DBG_I2C("Listening for byte in I2C (NACK)\r\n"); I2C_AcknowledgeConfig(I2Cx, DISABLE); - //I2C_GenerateSTOP(I2Cx, ENABLE); while( !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED)) { // wait until one byte has been received @@ -135,10 +132,6 @@ void I2C_stop(I2C_TypeDef* I2Cx) { DBG_I2C("Sending I2C Stop...\r\n"); I2C_GenerateSTOP(I2Cx, ENABLE); - /*while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTING)) - { - // wait for I2C1 EV8_2 --> byte has been transmitted - }*/ DBG_I2C("Stop sent\r\n"); } diff --git a/Project/applications/smartcities/include/callbacks.h b/Project/applications/smartcities/include/callbacks.h index 42d05d5..faf3f66 100644 --- a/Project/applications/smartcities/include/callbacks.h +++ b/Project/applications/smartcities/include/callbacks.h @@ -9,6 +9,9 @@ #define WIFI_MODE_SOFTAP 2 #define MAX_RETRIES 5 +#define CONNECTED_PAST 0x02 +#define CONNECTED_NOW 0x01 + void dhcp_connect_result_cb(int result); void wifi_connect_result_cb(int result); void wifi_connect_ap_result_cb(int result); diff --git a/Project/applications/smartcities/include/i2c.h b/Project/applications/smartcities/include/i2c.h index bd082f1..62f8948 100644 --- a/Project/applications/smartcities/include/i2c.h +++ b/Project/applications/smartcities/include/i2c.h @@ -11,6 +11,7 @@ #define DBG_I2C(fmt,...) printf("%c[1;35mi2c.c:%c[1;00m "fmt,0x1B,0x1B, ##__VA_ARGS__) + void I2C_init(void); void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction); void I2C_stop(I2C_TypeDef* I2Cx); diff --git a/Project/applications/smartcities/include/sensors.h b/Project/applications/smartcities/include/sensors.h index c5c489e..f6346f5 100644 --- a/Project/applications/smartcities/include/sensors.h +++ b/Project/applications/smartcities/include/sensors.h @@ -43,6 +43,7 @@ extern sensor* sensors[TOTAL_SENSORS+1]; //SENSOR FUNCTIONS //LIGHT SENSOR +void init_light(void); uint32_t get_light_data(void); uint16_t get_light_ch0(void); uint16_t get_light_ch1(void); diff --git a/Project/applications/smartcities/json.c b/Project/applications/smartcities/json.c index a0f830e..c2f8473 100644 --- a/Project/applications/smartcities/json.c +++ b/Project/applications/smartcities/json.c @@ -178,7 +178,7 @@ uint8_t send_json(char* statement, uint32_t length, char* provider_ID, char* sen chHeapFree(URL); if(response_code == 200) { - DBG_JSON("Success: JSON_POST_OK\r\n"); + DBG_JSON("Success: %c[1;32mJSON_POST_OK%c[1;00m\r\n",0x1B,0x1B); return JSON_POST_OK; } else if((response_code >= 400) && (response_code < 500)) diff --git a/Project/applications/smartcities/main.c b/Project/applications/smartcities/main.c index 5a46fdf..627f1ee 100644 --- a/Project/applications/smartcities/main.c +++ b/Project/applications/smartcities/main.c @@ -31,13 +31,21 @@ void initLibwismart(void) void update_time(unsigned long *time) { - DBG("Requesting new NTP time...\r\n"); - unsigned long new_time = getSecsSince1900(); - if(new_time) + do { - *time = getSecsSince1900(); - } - DBG("Time updated\r\n"); + DBG("Requesting new NTP time...\r\n"); + unsigned long new_time = getSecsSince1900(); + if(new_time) + { + DBG("Updating new time to %ul\r\n",new_time); + *time = new_time; + DBG("Time updated\r\n"); + } + else + { + DBG("%c[1;31m[ERROR] NTP query FAILED%c[1;00m\r\n",0x1B,0x1B); + } + }while(!(*time)); } void init_registry(void) @@ -96,7 +104,7 @@ void init_registry(void) libwismart_WiFiConnect(config.ssid,config.wepkey,wifi_connect_result_cb); libwismart_WiFiSetWep(config.wepkey,1); } - while(connected == 0 && timeout != 1 ) + while((connected & CONNECTED_NOW) == 0 && timeout != 1 ) {chThdSleepMilliseconds(500);} } @@ -150,7 +158,7 @@ void wifi_connect(void) { DBG("Connecting wifi...\r\n"); init_registry(); - if(timeout==1) + if(timeout==1 && (connected & CONNECTED_PAST) == 0) { DBG("Creating AP\r\n"); configServer_start(1); @@ -160,13 +168,13 @@ void wifi_connect(void) chThdSleepMilliseconds(1000); } } + timeout = 0; } void send_battery_level(unsigned long timestamp) { DBG("Polling battery level...\r\n"); char *batt_level = battery_value(get_battery_data()); - //char *batt_level = battery_value(3300); char *batt_data = timestamp_data(batt_level,getDate(timestamp)); chHeapFree(batt_level); char *statement = prepare_json_observation_statement(&batt_data,1); @@ -183,7 +191,12 @@ void send_battery_level(unsigned long timestamp) void wifi_disconnect(void) { DBG("Disconnecting wifi...\r\n"); - connected = 0; + if(connected & CONNECTED_NOW) + { + connected |= CONNECTED_PAST; + } + connected = connected & CONNECTED_PAST ; + DBG("Connnected: %d\r\n",connected); timeout = 0; uint8_t res=libwismart_WiFiDisconnect(); if(res) @@ -206,7 +219,7 @@ int main(void) int j; uint8_t sensors[TOTAL_SENSORS]; char* valueSensors[TOTAL_SENSORS]; - unsigned long time; + unsigned long time = 0; unsigned long timestamp; unsigned long delay; uint32_t ind[TOTAL_SENSORS]={0}; @@ -223,7 +236,6 @@ int main(void) } DBG("%c[1;35m--------------WI-SENSE STARTED-------------%c[1;00m\r\n",0x1B,0x1B); - wifi_connect(); check_memory(); diff --git a/Project/applications/smartcities/ntp.c b/Project/applications/smartcities/ntp.c index e56e641..6379693 100644 --- a/Project/applications/smartcities/ntp.c +++ b/Project/applications/smartcities/ntp.c @@ -109,11 +109,11 @@ unsigned long getSecsSince1900 (void) // If this is a SNTP response... if (((sntp_response[0] & SNTP_MODE_MASK) == SNTP_MODE_SERVER) || ((sntp_response[0] & SNTP_MODE_MASK) == SNTP_MODE_BROADCAST)) { + DBG_NTP("Successfully got new time\r\n"); /* extract GMT time from response */ memcpy(×tamp, (sntp_response + SNTP_RCV_TIME_OFS), sizeof(timestamp)); timestamp=(ntohl(timestamp)); - - DBG_NTP("Received timestamp %u\r\n", timestamp); + DBG_NTP("Received timestamp %ul\r\n", timestamp); } else { @@ -165,10 +165,12 @@ unsigned long getSecsSince1900 (void) char* timestamp_data(char* value,Date time) { + DBG_NTP("Writing timestamp...\r\n"); uint8_t length = strlen(value) + strlen(",00/00/0000T00:00:00") + 1; char str_day[3],str_month[3],str_year[5],str_hour[3],str_minute[3],str_second[3]; + DBG_NTP("Asking for %d bytes in timestamp_data\r\n", length); char* data = chHeapAlloc(NULL,length*sizeof(char)); - + DBG_NTP("Data allocated in timestamp_data\r\n", length); sprintf(str_day,"%d",time.day); sprintf(str_month,"%d",time.month); sprintf(str_year,"%d",time.year); diff --git a/Project/applications/smartcities/report b/Project/applications/smartcities/report new file mode 100644 index 0000000..cc124f7 --- /dev/null +++ b/Project/applications/smartcities/report @@ -0,0 +1,418 @@ +main.c: --------------WI-SENSE STARTED------------- +main.c: Connecting wifi... +main.c: SSID = SERVER_WLAN +main.c: Wep key = TGW2YHNKT9TW +main.c: Passphrase = TGW2YHNKT9TW +main.c: User = +main.c: Password = +main.c: Encryption type = 4 +main.c: Geo Localization = 41.557255 2.096183 +main.c: WPA network detected +callbacks.c: WiFi Connect indication: Connected +Connected to: 00:26:5B:D9:0D:18 +callbacks.c: IP: 192.168.0.47 +buffer.c: Checking available memory... +buffer.c: Available memory: 33320 B +buffer.c: Used memory: 1384 B +sensors.c: Waking up sensors... +sensors.c: Woken Pin 3 +sensors.c: Woken Pin 4 +sensors.c: Woken Pin 5 +sensors.c: Woken Pin 6 +sensors.c: Woken Pin 7 +i2c.c: Scanning sensors... +i2c.c: Initializing I2C... +i2c.c: Probing address 72 ... +i2c.c: Resetting I2C... +i2c.c: Initializing I2C... +i2c.c: Probing address 2 ... +i2c.c: Resetting I2C... +i2c.c: Initializing I2C... +i2c.c: Probing address EE ... +i2c.c: I2C address detected: EE +i2c.c: Sending I2C byte 00... +i2c.c: Data sent +i2c.c: Sending I2C Stop... +i2c.c: Stop sent +json.c: Registering sensor: Pressure sensor +json.c: Putting additional info for pressure sensor... +sensors.c: [PRESSURE] Fetching callibration data +i2c.c: Sending I2C Start... +i2c.c: I2C ready +i2c.c: Start sent +i2c.c: Start ACK +i2c.c: Master -> Slave Address and R/W sent +i2c.c: Slave -> Master ACK +i2c.c: Sending I2C byte AA... +i2c.c: Data sent +i2c.c: Sending repeated I2C Start... +i2c.c: Start sent +i2c.c: Start ACK +i2c.c: Master -> Slave Address and R/W sent +i2c.c: Slave -> Master ACK +i2c.c: Listening for byte in I2C (ACK) +i2c.c: Received byte: 1D +i2c.c: Listening for byte in I2C (NACK) +i2c.c: Received byte: 1D +i2c.c: Sending I2C Stop... +i2c.c: Stop sent +i2c.c: Sending I2C Start... +i2c.c: I2C ready +i2c.c: Start sent +i2c.c: Start ACK +i2c.c: Master -> Slave Address and R/W sent +i2c.c: Slave -> Master ACK +i2c.c: Sending I2C byte AC... +i2c.c: Data sent +i2c.c: Sending repeated I2C Start... +i2c.c: Start sent +i2c.c: Start ACK +i2c.c: Master -> Slave Address and R/W sent +i2c.c: Slave -> Master ACK +i2c.c: Listening for byte in I2C (ACK) +i2c.c: Received byte: FC +i2c.c: Listening for byte in I2C (NACK) +i2c.c: Received byte: FF +i2c.c: Sending I2C Stop... +i2c.c: Stop sent +i2c.c: Sending I2C Start... +i2c.c: I2C ready +i2c.c: Start sent +i2c.c: Start ACK +i2c.c: Master -> Slave Address and R/W sent +i2c.c: Slave -> Master ACK +i2c.c: Sending I2C byte AE... +i2c.c: Data sent +i2c.c: Sending repeated I2C Start... +i2c.c: Start sent +i2c.c: Start ACK +i2c.c: Master -> Slave Address and R/W sent +i2c.c: Slave -> Master ACK +i2c.c: Listening for byte in I2C (ACK) +i2c.c: Received byte: C7 +i2c.c: Listening for byte in I2C (NACK) +i2c.c: Received byte: FF +i2c.c: Sending I2C Stop... +i2c.c: Stop sent +i2c.c: Sending I2C Start... +i2c.c: I2C ready +i2c.c: Start sent +i2c.c: Start ACK +i2c.c: Master -> Slave Address and R/W sent +i2c.c: Slave -> Master ACK +i2c.c: Sending I2C byte B0... +i2c.c: Data sent +i2c.c: Sending repeated I2C Start... +i2c.c: Start sent +i2c.c: Start ACK +i2c.c: Master -> Slave Address and R/W sent +i2c.c: Slave -> Master ACK +i2c.c: Listening for byte in I2C (ACK) +i2c.c: Received byte: 7D +i2c.c: Listening for byte in I2C (NACK) +i2c.c: Received byte: FF +i2c.c: Sending I2C Stop... +i2c.c: Stop sent +i2c.c: Sending I2C Start... +i2c.c: I2C ready +i2c.c: Start sent +i2c.c: Start ACK +i2c.c: Master -> Slave Address and R/W sent +i2c.c: Slave -> Master ACK +i2c.c: Sending I2C byte B2... +i2c.c: Data sent +i2c.c: Sending repeated I2C Start... +i2c.c: Start sent +i2c.c: Start ACK +i2c.c: Master -> Slave Address and R/W sent +i2c.c: Slave -> Master ACK +i2c.c: Listening for byte in I2C (ACK) +i2c.c: Received byte: 60 +i2c.c: Listening for byte in I2C (NACK) +i2c.c: Received byte: FF +i2c.c: Sending I2C Stop... +i2c.c: Stop sent +i2c.c: Sending I2C Start... +i2c.c: I2C ready +i2c.c: Start sent +i2c.c: Start ACK +i2c.c: Master -> Slave Address and R/W sent +i2c.c: Slave -> Master ACK +i2c.c: Sending I2C byte B4... +i2c.c: Data sent +i2c.c: Sending repeated I2C Start... +i2c.c: Start sent +i2c.c: Start ACK +i2c.c: Master -> Slave Address and R/W sent +i2c.c: Slave -> Master ACK +i2c.c: Listening for byte in I2C (ACK) +i2c.c: Received byte: 4F +i2c.c: Listening for byte in I2C (NACK) +i2c.c: Received byte: FF +i2c.c: Sending I2C Stop... +i2c.c: Stop sent +i2c.c: Sending I2C Start... +i2c.c: I2C ready +i2c.c: Start sent +i2c.c: Start ACK +i2c.c: Master -> Slave Address and R/W sent +i2c.c: Slave -> Master ACK +i2c.c: Sending I2C byte B6... +i2c.c: Data sent +i2c.c: Sending repeated I2C Start... +i2c.c: Start sent +i2c.c: Start ACK +i2c.c: Master -> Slave Address and R/W sent +i2c.c: Slave -> Master ACK +i2c.c: Listening for byte in I2C (ACK) +i2c.c: Received byte: 15 +i2c.c: Listening for byte in I2C (NACK) +i2c.c: Received byte: FF +i2c.c: Sending I2C Stop... +i2c.c: Stop sent +i2c.c: Sending I2C Start... +i2c.c: I2C ready +i2c.c: Start sent +i2c.c: Start ACK +i2c.c: Master -> Slave Address and R/W sent +i2c.c: Slave -> Master ACK +i2c.c: Sending I2C byte B8... +i2c.c: Data sent +i2c.c: Sending repeated I2C Start... +i2c.c: Start sent +i2c.c: Start ACK +i2c.c: Master -> Slave Address and R/W sent +i2c.c: Slave -> Master ACK +i2c.c: Listening for byte in I2C (ACK) +i2c.c: Received byte: 00 +i2c.c: Listening for byte in I2C (NACK) +i2c.c: Received byte: FF +i2c.c: Sending I2C Stop... +i2c.c: Stop sent +i2c.c: Sending I2C Start... +i2c.c: I2C ready +i2c.c: Start sent +i2c.c: Start ACK +i2c.c: Master -> Slave Address and R/W sent +i2c.c: Slave -> Master ACK +i2c.c: Sending I2C byte BA... +i2c.c: Data sent +i2c.c: Sending repeated I2C Start... +i2c.c: Start sent +i2c.c: Start ACK +i2c.c: Master -> Slave Address and R/W sent +i2c.c: Slave -> Master ACK +i2c.c: Listening for byte in I2C (ACK) +i2c.c: Received byte: 80 +i2c.c: Listening for byte in I2C (NACK) +i2c.c: Received byte: FF +i2c.c: Sending I2C Stop... +i2c.c: Stop sent +i2c.c: Sending I2C Start... +i2c.c: I2C ready +i2c.c: Start sent +i2c.c: Start ACK +i2c.c: Master -> Slave Address and R/W sent +i2c.c: Slave -> Master ACK +i2c.c: Sending I2C byte BC... +i2c.c: Data sent +i2c.c: Sending repeated I2C Start... +i2c.c: Start sent +i2c.c: Start ACK +i2c.c: Master -> Slave Address and R/W sent +i2c.c: Slave -> Master ACK +i2c.c: Listening for byte in I2C (ACK) +i2c.c: Received byte: D4 +i2c.c: Listening for byte in I2C (NACK) +i2c.c: Received byte: FF +i2c.c: Sending I2C Stop... +i2c.c: Stop sent +i2c.c: Sending I2C Start... +i2c.c: I2C ready +i2c.c: Start sent +i2c.c: Start ACK +i2c.c: Master -> Slave Address and R/W sent +i2c.c: Slave -> Master ACK +i2c.c: Sending I2C byte BE... +i2c.c: Data sent +i2c.c: Sending repeated I2C Start... +i2c.c: Start sent +i2c.c: Start ACK +i2c.c: Master -> Slave Address and R/W sent +i2c.c: Slave -> Master ACK +i2c.c: Listening for byte in I2C (ACK) +i2c.c: Received byte: 09 +i2c.c: Listening for byte in I2C (NACK) +i2c.c: Received byte: FF +i2c.c: Sending I2C Stop... +i2c.c: Stop sent +sensors.c: [PRESSURE] Got callibration data +json.c: callibration_data is: 7453,64767,51199,32255,24831,20479,5631,255,33023,54527,2559 +json.c: In prepare_json_register_statement: Detected additional_info +json.c: Server is lewis.upc.es +httpClient.c: Checking params +httpClient.c: Calculating header size +httpClient.c: Building request head +httpClient.c: Adding content to request string +httpClient.c: Packet: + +PUT /modularsense/wordpress/opendata/catalog/register HTTP/1.1 +Host: 147.83.39.240 +Content-Type: application/json; charset=UTF-8 +Content-Length: 210 + +{"sensors":[{"sensor":"00000177","description":"Pressure sensor","type":"pressure","unit":"hPa","location":"41.557255 2.096183","additionalInfo":"7453,64767,51199,32255,24831,20479,5631,255,33023,54527,2559"}]} + +httpClient.c: Setting connection +httpClient.c: Establishing connection +httpClient.c: Sending request +httpClient.c: Write returned: 0 +httpClient.c: Waiting for response +httpClient.c: Receive returned: 0 +httpClient.c: Response received. Let's parse the information +httpClient.c: Response code: 200€+0 +json.c: Success: JSON_POST_OK +i2c.c: Probing address 4E ... +i2c.c: Resetting I2C... +i2c.c: Initializing I2C... +i2c.c: Probing address E4 ... +i2c.c: Resetting I2C... +i2c.c: Initializing I2C... +json.c: Registering sensor: Battery Level +json.c: Server is lewis.upc.es +httpClient.c: Checking params +httpClient.c: Calculating header size +httpClient.c: Building request head +httpClient.c: Adding content to request string +httpClient.c: Packet: + +PUT /modularsense/wordpress/opendata/catalog/register HTTP/1.1 +Host: 147.83.39.240 +Content-Type: application/json; charset=UTF-8 +Content-Length: 124 + +{"sensors":[{"sensor":"00000100","description":"Battery Level","type":"power","unit":"mV","location":"41.557255 2.096183"}]} + +httpClient.c: Setting connection +httpClient.c: Establishing connection +httpClient.c: Sending request +httpClient.c: Write returned: 0 +httpClient.c: Waiting for response +httpClient.c: Receive returned: 0 +httpClient.c: Response received. Let's parse the information +httpClient.c: Response code: 200 +json.c: Success: JSON_POST_OK +main.c: 1 sensor detected... +main.c: Requesting new NTP time... +ntp.c: Successfully got new time +ntp.c: Received timestamp 3610877978 +main.c: Updating new time to 3610877978 +main.c: Time updated +main.c: Disconnecting wifi... +main.c: Disconnecting Successful: WIFI_DISCONNECT_SUCCESS +timer-loop.c: Time to sleep! for 22 seconds +callbacks.c: WiFi Connect indication: Failed +main.c: Time (absolute): 3610878000 +Time LONG_PERIOD: 0 +Time SHORT_PERIOD: 0 +sensors.c: Collecting data... +sensors.c: Not freeing memory on index 0 +sensors.c: Fetching data from pressure sensor +i2c.c: Sending I2C Start... +i2c.c: I2C ready +i2c.c: Start sent +i2c.c: Start ACK +i2c.c: Master -> Slave Address and R/W sent +i2c.c: Slave -> Master ACK +i2c.c: Sending I2C byte F4... +i2c.c: Data sent +i2c.c: Sending I2C byte 34... +i2c.c: Data sent +i2c.c: Sending I2C Stop... +i2c.c: Stop sent +sensors.c: [PRESSURE] Initialized +i2c.c: Sending I2C Start... +i2c.c: I2C ready +i2c.c: Start sent +i2c.c: Start ACK +i2c.c: Master -> Slave Address and R/W sent +i2c.c: Slave -> Master ACK +i2c.c: Sending I2C byte F6... +i2c.c: Data sent +i2c.c: Sending repeated I2C Start... +i2c.c: Start sent +i2c.c: Start ACK +i2c.c: Master -> Slave Address and R/W sent +i2c.c: Slave -> Master ACK +i2c.c: Listening for byte in I2C (ACK) +i2c.c: Received byte: 98 +sensors.c: [PRESSURE] Got high byte +i2c.c: Listening for byte in I2C (NACK) +i2c.c: Received byte: C6 +sensors.c: [PRESSURE] Got low byte +i2c.c: Sending I2C Stop... +i2c.c: Stop sent +i2c.c: Sending I2C Start... +i2c.c: I2C ready +i2c.c: Start sent +i2c.c: Start ACK +i2c.c: Master -> Slave Address and R/W sent +i2c.c: Slave -> Master ACK +i2c.c: Sending I2C byte F4... +i2c.c: Data sent +i2c.c: Sending I2C byte 2E... +i2c.c: Data sent +i2c.c: Sending I2C Stop... +i2c.c: Stop sent +sensors.c: [PRESSURE] Initialized temperature +i2c.c: Sending I2C Start... +i2c.c: I2C ready +i2c.c: Start sent +i2c.c: Start ACK +i2c.c: Master -> Slave Address and R/W sent +i2c.c: Slave -> Master ACK +i2c.c: Sending I2C byte F6... +i2c.c: Data sent +i2c.c: Sending repeated I2C Start... +i2c.c: Start sent +i2c.c: Start ACK +i2c.c: Master -> Slave Address and R/W sent +i2c.c: Slave -> Master ACK +i2c.c: Listening for byte in I2C (ACK) +i2c.c: Received byte: 71 +sensors.c: [PRESSURE] Got temperature high byte +i2c.c: Listening for byte in I2C (NACK) +i2c.c: Received byte: FF +sensors.c: [PRESSURE] Got temperature low byte +i2c.c: Sending I2C Stop... +i2c.c: Stop sent +sensors.c: Data collected +main.c: Time (absolute): 3610878000 +Time LONG_PERIOD: 0 +Time SHORT_PERIOD: 0 +main.c: Timestamp (absolute): 3610878000 +Timestamp LONG_PERIOD: 0 +Timestamp SHORT_PERIOD: 0 +main.c: Timestamping data... +main.c: Data is: 39110,29183,4/6/2014T15:40:0 +main.c: Returning timestamped data... +main.c: Putting data in buffers... +main.c: Putting data: 39110,29183,4/6/2014T15:40:0 + buffer.c: Joining buffer... +buffer.c: Start buffer join... +buffer.c: All data transferred +buffer.c: Old buffer cleared +buffer.c: Joined buffer... +buffer.c: Memory allocated... +buffer.c: Data copied... +main.c: Message put: 39110,29183,4/6/2014T15:40:0 +main.c: Memory freed + main.c: [ASSERTION] Message put: 39110,29183,4/6/2014T15:40:0 with length 28 +main.c: Data is now in buffer + buffer.c: Checking available memory... +buffer.c: Available memory: 1859003 B +buffer.c: Used memory: -1824299 B +main.c: Time (absolute): 3610878000 +Time LONG_PERIOD: 0 +Time SHORT_PERIOD: 0 +timer-loop.c: Time to sleep! for 60 seconds diff --git a/Project/applications/smartcities/sensors.c b/Project/applications/smartcities/sensors.c index 3f467ea..43ae60b 100644 --- a/Project/applications/smartcities/sensors.c +++ b/Project/applications/smartcities/sensors.c @@ -74,15 +74,39 @@ void wakeup_sensors(uint8_t logic_address) } } +void init_light(void) +{ + I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter); + I2C_write(I2C1, 0x80); + I2C_write(I2C1, 0x03); + I2C_stop(I2C1); + DBG_SENSORS("[LIGHT] Sent power on command\r\n"); + + I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter); + I2C_write(I2C1, 0x81); + I2C_write(I2C1, 0x0A); + I2C_stop(I2C1); + DBG_SENSORS("[LIGHT] Sent start conversion command\r\n"); + chThdSleepMilliseconds(500); + + I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter); + I2C_write(I2C1, 0x81); + I2C_write(I2C1, 0x02); + I2C_stop(I2C1); + DBG_SENSORS("[LIGHT] Sent stop conversion command\r\n"); +} + uint32_t get_light_data(void) { + init_light(); uint32_t data = 0; - data = get_light_ch1(); DBG_SENSORS("[LIGHT] Got ch1\r\n"); data = data << 16; data = data | get_light_ch0(); DBG_SENSORS("[LIGHT] Got ch0\r\n"); + DBG_SENSORS("[LIGHT] CH1 = %02x\r\n",(data & 0xFFFF0000) >> 16); + DBG_SENSORS("[LIGHT] CH0 = %02x\r\n",data & 0x0000FFFF); return data; } uint16_t get_light_ch0(void) @@ -90,15 +114,21 @@ uint16_t get_light_ch0(void) uint16_t data = 0; I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter); - I2C_write(I2C1, 0x0C); - DBG_SENSORS("[LIGHT] Sent fetch command for CH1\r\n"); + I2C_write(I2C1, 0x8C); + DBG_SENSORS("[LIGHT] Sent fetch command for CH0\r\n"); + + I2C_restart(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver); + data = I2C_read_nack(I2C1); + I2C_stop(I2C1); + DBG_SENSORS("[LIGHT] Got low byte for CH0\r\n"); + + I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter); + I2C_write(I2C1, 0x8D); I2C_restart(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver); - data = I2C_read_ack(I2C1); - DBG_SENSORS("[LIGHT] Got low byte for CH1\r\n"); data = data | (I2C_read_nack(I2C1) << 8); - DBG_SENSORS("[LIGHT] Got high byte for CH1\r\n"); I2C_stop(I2C1); + DBG_SENSORS("[LIGHT] Got high byte for CH0\r\n"); return data; } @@ -107,23 +137,29 @@ uint16_t get_light_ch1(void) uint16_t data = 0; I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter); - I2C_write(I2C1, 0x0E); + I2C_write(I2C1, 0x8E); DBG_SENSORS("[LIGHT] Sent fetch command for CH1\r\n"); I2C_restart(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver); - data = I2C_read_ack(I2C1); + data = I2C_read_nack(I2C1); + I2C_stop(I2C1); DBG_SENSORS("[LIGHT] Got low byte for CH1\r\n"); + + I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter); + I2C_write(I2C1, 0x8F); + + I2C_restart(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver); data = data | (I2C_read_nack(I2C1) << 8); + I2C_stop(I2C1); DBG_SENSORS("[LIGHT] Got high byte for CH1\r\n"); - I2C_stop(I2C1); - + return data; } char* light_value(uint32_t light) { char *value = chHeapAlloc(NULL,10 + 1); - sprintf(value,"%d",(unsigned int)light); + sprintf(value,"%u",(unsigned int)light); return value; } @@ -159,7 +195,7 @@ uint16_t get_distance_data(void) char* distance_value(uint16_t distance) { char *value = chHeapAlloc(NULL,5 + 1); - sprintf(value,"%d",distance); + sprintf(value,"%u",distance); return value; } @@ -273,17 +309,17 @@ char* callibration_pressure_data_csv(bmp085_callibration parameters) { char *str = chHeapAlloc(NULL,sizeof(char)*(11*11-1)); memset(str,0x00,sizeof(char)*(11*11)); - sprintf(str + strlen(str),"%d,",parameters.AC1); - sprintf(str + strlen(str),"%d,",parameters.AC2); - sprintf(str + strlen(str),"%d,",parameters.AC3); - sprintf(str + strlen(str),"%d,",parameters.AC4); - sprintf(str + strlen(str),"%d,",parameters.AC5); - sprintf(str + strlen(str),"%d,",parameters.AC6); - sprintf(str + strlen(str),"%d,",parameters.B1); - sprintf(str + strlen(str),"%d,",parameters.B2); - sprintf(str + strlen(str),"%d,",parameters.MB); - sprintf(str + strlen(str),"%d,",parameters.MC); - sprintf(str + strlen(str),"%d",parameters.MD); + sprintf(str + strlen(str),"%u,",parameters.AC1); + sprintf(str + strlen(str),"%u,",parameters.AC2); + sprintf(str + strlen(str),"%u,",parameters.AC3); + sprintf(str + strlen(str),"%u,",parameters.AC4); + sprintf(str + strlen(str),"%u,",parameters.AC5); + sprintf(str + strlen(str),"%u,",parameters.AC6); + sprintf(str + strlen(str),"%u,",parameters.B1); + sprintf(str + strlen(str),"%u,",parameters.B2); + sprintf(str + strlen(str),"%u,",parameters.MB); + sprintf(str + strlen(str),"%u,",parameters.MC); + sprintf(str + strlen(str),"%u",parameters.MD); return str; } @@ -326,7 +362,7 @@ uint16_t get_pressure_temperature_data(void) char* pressure_value(uint16_t pressure, uint16_t temperature) { char *value = chHeapAlloc(NULL,11 + 1); - sprintf(value,"%d,%d",pressure,temperature); + sprintf(value,"%u,%u",pressure,temperature); return value; } @@ -373,7 +409,7 @@ uint16_t get_temperature_data(void) char* temp_humidity_value(uint16_t temp, uint16_t humidity) { char *value = chHeapAlloc(NULL,11 + 1); - sprintf(value,"%d,%d",temp,humidity); + sprintf(value,"%u,%u",temp,humidity); return value; }