Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: FXOS8700CQ M2XStreamClient-JMF WNCInterface jsonlite mbed-rtos mbed
Fork of WNCInterface_M2Xdemo by
Revision 5:8099493f2c35, committed 2017-04-05
- Comitter:
- jk431j
- Date:
- Wed Apr 05 16:37:18 2017 +0000
- Parent:
- 4:08979e323c6e
- Child:
- 6:731f412e6571
- Commit message:
- Added sensor reading and M2X command handling.
Changed in this revision
--- a/main.cpp Wed Apr 05 04:53:25 2017 +0000
+++ b/main.cpp Wed Apr 05 16:37:18 2017 +0000
@@ -21,6 +21,8 @@
const char *hstreamName = "humidity";
const char *tstreamName = "temp";
+const char *accelstreamNames[] = { "accelX", "accelY", "accelZ" };
+
WNCInterface eth;
Client client;
@@ -35,34 +37,20 @@
K64F_Sensors_t SENSOR_DATA =
{
- .Temperature = "0",
- .Humidity = "0",
- .AccelX = "0",
- .AccelY = "0",
- .AccelZ = "0",
- .MagnetometerX = "0",
- .MagnetometerY = "0",
- .MagnetometerZ = "0",
- .AmbientLightVis = "0",
- .AmbientLightIr = "0",
- .UVindex = "0",
- .Proximity = "0",
- .Temperature_Si7020 = "0",
- .Humidity_Si7020 = "0",
- .Virtual_Sensor1 = "0",
- .Virtual_Sensor2 = "0",
- .Virtual_Sensor3 = "0",
- .Virtual_Sensor4 = "0",
- .Virtual_Sensor5 = "0",
- .Virtual_Sensor6 = "0",
- .Virtual_Sensor7 = "0",
- .Virtual_Sensor8 = "0",
- .GPS_Satellites = "0",
- .GPS_Latitude = "0",
- .GPS_Longitude = "0",
- .GPS_Altitude = "0",
- .GPS_Speed = "0",
- .GPS_Course = "0"
+ .Temperature = 0,
+ .Humidity = 0,
+ .AccelX = 0,
+ .AccelY = 0,
+ .AccelZ = 0,
+ .MagnetometerX = 0,
+ .MagnetometerY = 0,
+ .MagnetometerZ = 0,
+ .AmbientLightVis = 0,
+ .AmbientLightIr = 0,
+ .UVindex = 0,
+ .Proximity = 0,
+ .Temperature_Si7020 = 0,
+ .Humidity_Si7020 = 0,
};
//********************************************************************************************************************************************
@@ -189,13 +177,17 @@
read_sensors();
// post the humidity value
- pc.printf("Post updateStreamValue (humidity = %f)..." CRLF, SENSOR_DATA.fHumidity);
- response = m2xClient.updateStreamValue(deviceId, hstreamName, SENSOR_DATA.fHumidity);
+ pc.printf("Post updateStreamValue (humidity = %f)..." CRLF, SENSOR_DATA.Humidity);
+ response = m2xClient.updateStreamValue(deviceId, hstreamName, SENSOR_DATA.Humidity);
pc.printf("Post response code: %d" CRLF, response);
// post the temp value
- pc.printf("Post updateStreamValue (temp = %f)..." CRLF, SENSOR_DATA.fTemperature);
- response = m2xClient.updateStreamValue(deviceId, tstreamName, SENSOR_DATA.fTemperature);
+ pc.printf("Post updateStreamValue (temp = %f)..." CRLF, SENSOR_DATA.Temperature);
+ response = m2xClient.updateStreamValue(deviceId, tstreamName, SENSOR_DATA.Temperature);
+ pc.printf("Post response code: %d" CRLF, response);
+
+ pc.printf("Post postDeviceUpdate (accelerometer)..." CRLF, SENSOR_DATA.Temperature);
+ response = m2xClient.postDeviceUpdate(deviceId, 3, accelstreamNames, (float []){SENSOR_DATA.AccelX, SENSOR_DATA.AccelY, SENSOR_DATA.AccelZ});
pc.printf("Post response code: %d" CRLF, response);
timeService.getTimestamp(timestamp, &length);
--- a/sensors.cpp Wed Apr 05 04:53:25 2017 +0000
+++ b/sensors.cpp Wed Apr 05 16:37:18 2017 +0000
@@ -152,9 +152,7 @@
//PRINTF("Read Si7020 Humidity = 0x%02X%02X\n", Humidity[0], Humidity[1]);
int rh_code = (Humidity[0] << 8) + Humidity[1];
float fRh = (125.0*rh_code/65536.0) - 6.0; //from datasheet
- //PRINTF("Si7020 Humidity = %*.*f %%\n", 4, 2, fRh); //double % sign for escape //PRINTF("%*.*f\n", myFieldWidth, myPrecision, myFloatValue);
- SENSOR_DATA.fHumidity = fRh;
- sprintf(SENSOR_DATA.Humidity_Si7020, "%0.2f", fRh);
+ SENSOR_DATA.Humidity_Si7020 = fRh;
//Command to read temperature when humidity is already done:
I2C_WriteSingleByte(Si7020_PMOD_I2C_ADDR, 0xE0, false);
@@ -162,9 +160,7 @@
//PRINTF("Read Si7020 Temperature = 0x%02X%02X\n", Temperature[0], Temperature[1]);
int temp_code = (Temperature[0] << 8) + Temperature[1];
float fTemp = (175.72*temp_code/65536.0) - 46.85; //from datasheet in Celcius
- SENSOR_DATA.fTemperature = fTemp;
- //PRINTF("Si7020 Temperature = %*.*f deg C\n", 4, 2, fTemp);
- sprintf(SENSOR_DATA.Temperature_Si7020, "%0.2f", fTemp);
+ SENSOR_DATA.Temperature_Si7020 = fTemp;
} //bool bSi7020_present = true
} //Read_Si7020()
@@ -304,7 +300,7 @@
sprintf(SENSOR_DATA.Proximity, "Object Very Near\0");
}
#else
- sprintf(SENSOR_DATA.Proximity, "%d\0", PS1);
+ SENSOR_DATA.Proximity = PS1;
#endif
//Force ALS read:
@@ -318,13 +314,13 @@
//PRINTF("UV_INDEX_Data = %d\n", UV_INDEX);
//PRINTF("Ambient Light Visible Sensor = %d\n", ALS_VIS);
- sprintf(SENSOR_DATA.AmbientLightVis, "%d", ALS_VIS);
+ SENSOR_DATA.AmbientLightVis = ALS_VIS;
//PRINTF("Ambient Light Infrared Sensor = %d\n", ALS_IR);
- sprintf(SENSOR_DATA.AmbientLightIr, "%d", ALS_IR);
+ SENSOR_DATA.AmbientLightIr = ALS_IR;
//float fUV_value = (UV_INDEX -50.0)/10000.0;
float fUV_value = (UV_INDEX)/100.0; //this is the aux reading
//PRINTF("UV_Data = %0.2f\n", fUV_value);
- sprintf(SENSOR_DATA.UVindex, "%0.2f", fUV_value);
+ SENSOR_DATA.UVindex = fUV_value;
} //bSi1145_present = true
} //Read_Si1145()
@@ -365,9 +361,9 @@
fxos.enable();
fxos.get_data(&accel_data, &magn_data);
//PRINTF("Roll=%5d, Pitch=%5d, Yaw=%5d;\r\n", magn_data.x, magn_data.y, magn_data.z);
- sprintf(SENSOR_DATA.MagnetometerX, "%5d", magn_data.x);
- sprintf(SENSOR_DATA.MagnetometerY, "%5d", magn_data.y);
- sprintf(SENSOR_DATA.MagnetometerZ, "%5d", magn_data.z);
+ SENSOR_DATA.MagnetometerX = magn_data.x;
+ SENSOR_DATA.MagnetometerY = magn_data.y;
+ SENSOR_DATA.MagnetometerZ = magn_data.z;
//Try to normalize (/2048) the values so they will match the eCompass output:
float fAccelScaled_x, fAccelScaled_y, fAccelScaled_z;
@@ -375,9 +371,9 @@
fAccelScaled_y = (accel_data.y/2048.0);
fAccelScaled_z = (accel_data.z/2048.0);
//PRINTF("Acc: X=%2.3f Y=%2.3f Z=%2.3f;\r\n", fAccelScaled_x, fAccelScaled_y, fAccelScaled_z);
- sprintf(SENSOR_DATA.AccelX, "%2.3f", fAccelScaled_x);
- sprintf(SENSOR_DATA.AccelY, "%2.3f", fAccelScaled_y);
- sprintf(SENSOR_DATA.AccelZ, "%2.3f", fAccelScaled_z);
+ SENSOR_DATA.AccelX = fAccelScaled_x;
+ SENSOR_DATA.AccelY = fAccelScaled_y;
+ SENSOR_DATA.AccelZ = fAccelScaled_z;
} //bMotionSensor_present
} //Read_motion_sensor()
@@ -417,150 +413,15 @@
{
double fTemp = hts221.readTemperature();
double fRh = hts221.readHumidity();
- sprintf(SENSOR_DATA.Temperature, "%0.2f", fTemp);
- sprintf(SENSOR_DATA.Humidity, "%02d", fRh);
- SENSOR_DATA.fTemperature = fTemp;
- SENSOR_DATA.fHumidity = fRh;
+ SENSOR_DATA.Temperature = fTemp;
+ SENSOR_DATA.Humidity = fRh;
} //bHTS221_present
} //Read_HTS221()
-
-#ifdef USE_VIRTUAL_SENSORS
-bool bUsbConnected = false;
-volatile uint8_t usb_uart_rx_buff[256];
-//volatile uint8_t usb_uart_tx_buff[256];
-volatile unsigned char usb_uart_rx_buff_putptr = 0;
-volatile unsigned char usb_uart_rx_buff_getptr = 0;
-//volatile unsigned char usb_uart_tx_buff_putptr = 0;
-//volatile unsigned char usb_uart_tx_buff_getptr = 0;
-char usbhost_rx_string[256];
-unsigned char usbhost_rxstring_index;
-char usbhost_tx_string[256];
-
-
-float f_sensor1_value = 12.3;
-float f_sensor2_value = 45.6;
-float f_sensor3_value = 78.9;
-float f_sensor4_value = 78.9;
-float f_sensor5_value = 78.9;
-float f_sensor6_value = 78.9;
-float f_sensor7_value = 78.9;
-float f_sensor8_value = 78.9;
-char usb_sensor_string[110];
-
-
-//********************************************************************************************************************************************
-//* Parse the input sensor data from the USB host
-//********************************************************************************************************************************************
-int parse_usbhost_message()
-{
- //PRINTF("String = %s\n", usbhost_rx_string); //test
- uint8_t length;
- uint8_t x ;
- //It seems that sscanf needs 11 characters to store a 7-character number. There must be some formatting and termination values...
- char Record[8][11]; //There are 8 sensors with up to 7 characters each
- char StringRecord[110]; //There are is a sensor "string" with up to 100 characters in it
-
- // Data format is: "S1:1234,S2:5678,S3:1234,S4:5678,S5:1234,S6:5678,S7:5678,S8:5678,S9:abcde...\n"
- int args_assigned = sscanf(usbhost_rx_string, "%[^','],%[^','],%[^','],%[^','],%[^','],%[^','],%[^','],%[^','],%[^\n]", Record[0], Record[1], Record[2], Record[3], Record[4], Record[5], Record[6], Record[7], StringRecord);
-
- //StringRecord[109] = '\0';
- //PRINTF("Last = %s\n", StringRecord); //test
-
- if (args_assigned == 9)
- { //sscanf was able to assign all 9 values
- for (x=0; x < 8; x++) // loop through the 8 sensors
- {
- // Strip the "Sx:" label characters from the field value
- length = strlen(Record[x]); // max of 7 characters but could be less
- strncpy(Record[x], Record[x] + 3, length);
- Record[x][length] = '\0'; // null termination character manually added
- }
- length = strlen(StringRecord);
- strncpy(StringRecord, StringRecord + 3, length);
- StringRecord[length] = '\0'; // null termination character manually added
-
- if ((usbhost_rx_string[0] == 'S') && (usbhost_rx_string[1] == '1')) //The message starts with "S1"
- {
- f_sensor1_value = atof(Record[0]);
- f_sensor2_value = atof(Record[1]);
- f_sensor3_value = atof(Record[2]);
- f_sensor4_value = atof(Record[3]);
- f_sensor5_value = atof(Record[4]);
- f_sensor6_value = atof(Record[5]);
- f_sensor7_value = atof(Record[6]);
- f_sensor8_value = atof(Record[7]);
- sprintf(usb_sensor_string,StringRecord);
- //PRINTF("Received = %s, %s, %s, %s, %s, %s, %s, %s, %s\n", Record[0], Record[1], Record[2], Record[3], Record[4], Record[5], Record[6], Record[7], usb_sensor_string); //test
- sprintf(SENSOR_DATA.Virtual_Sensor1, "%s", Record[0]);
- sprintf(SENSOR_DATA.Virtual_Sensor2, "%s", Record[1]);
- sprintf(SENSOR_DATA.Virtual_Sensor3, "%s", Record[2]);
- sprintf(SENSOR_DATA.Virtual_Sensor4, "%s", Record[3]);
- sprintf(SENSOR_DATA.Virtual_Sensor5, "%s", Record[4]);
- sprintf(SENSOR_DATA.Virtual_Sensor6, "%s", Record[5]);
- sprintf(SENSOR_DATA.Virtual_Sensor7, "%s", Record[6]);
- sprintf(SENSOR_DATA.Virtual_Sensor8, "%s", Record[7]);
- }
- } //sscanf was able to assign all values
- return args_assigned;
-} //parse_usbhost_message()
-
-//********************************************************************************************************************************************
-//* Process any received message from the USB host
-//********************************************************************************************************************************************
-void process_usb_rx(unsigned char ucNewRxByte)
-{
- if (ucNewRxByte == '?')
- { //just pinging
- usbhost_rxstring_index = 0;
- return;
- } //just pinging
- usbhost_rx_string[usbhost_rxstring_index++] = ucNewRxByte;
- if (ucNewRxByte == '\n')
- { //end of message
- usbhost_rx_string[usbhost_rxstring_index] = 0; //null terminate string
- usbhost_rxstring_index = 0;
- parse_usbhost_message();
- } //end of message
-} //process_usb_rx()
-
-void ProcessUsbInterface(void)
-{
- //Process the USB host UART receive commands:
- if (usb_uart_rx_buff_getptr != usb_uart_rx_buff_putptr)
- {
- bUsbConnected = true;
- while (usb_uart_rx_buff_getptr != usb_uart_rx_buff_putptr)
- {
- unsigned char ucByteFromHost = usb_uart_rx_buff[usb_uart_rx_buff_getptr++]; //Copy latest received byte
- process_usb_rx(ucByteFromHost);
- } //while (usb_uart_rx_buff_getptr != usb_uart_rx_buff_putptr)
- } // if there are USB UART bytes to receive
- //USB host UART transmit:
- //while (usb_uart_tx_buff_getptr != usb_uart_tx_buff_putptr)
- //{
- //pc.putc(usb_uart_tx_buff[usb_uart_tx_buff_getptr++]);
- //}
-} //ProcessUsbInterface()
-
-// This function is called when a character goes into the RX buffer.
-void UsbUartRxCallback(MODSERIAL_IRQ_INFO *info)
-{
- // Get the pointer to our MODSERIAL object that invoked this callback.
- MODSERIAL *pc = info->serial;
- while (pc->readable())
- {
- usb_uart_rx_buff[usb_uart_rx_buff_putptr++] = pc->getcNb();
- }
-}
-#endif
void sensors_init(void)
{
-#ifdef USE_VIRTUAL_SENSORS
- pc.attach(&UsbUartRxCallback, MODSERIAL::RxIrq);
-#endif
Init_HTS221();
Init_Si7020();
Init_Si1145();
--- a/sensors.h Wed Apr 05 04:53:25 2017 +0000
+++ b/sensors.h Wed Apr 05 16:37:18 2017 +0000
@@ -29,36 +29,20 @@
#define SENSOR_FIELD_LEN_LIMIT 32
typedef struct
{
- char Temperature[SENSOR_FIELD_LEN_LIMIT];
- char Humidity[SENSOR_FIELD_LEN_LIMIT];
- char AccelX[SENSOR_FIELD_LEN_LIMIT];
- char AccelY[SENSOR_FIELD_LEN_LIMIT];
- char AccelZ[SENSOR_FIELD_LEN_LIMIT];
- char MagnetometerX[SENSOR_FIELD_LEN_LIMIT];
- char MagnetometerY[SENSOR_FIELD_LEN_LIMIT];
- char MagnetometerZ[SENSOR_FIELD_LEN_LIMIT];
- char AmbientLightVis[SENSOR_FIELD_LEN_LIMIT];
- char AmbientLightIr[SENSOR_FIELD_LEN_LIMIT];
- char UVindex[SENSOR_FIELD_LEN_LIMIT];
- char Proximity[SENSOR_FIELD_LEN_LIMIT];
- char Temperature_Si7020[SENSOR_FIELD_LEN_LIMIT];
- char Humidity_Si7020[SENSOR_FIELD_LEN_LIMIT];
- char Virtual_Sensor1[SENSOR_FIELD_LEN_LIMIT];
- char Virtual_Sensor2[SENSOR_FIELD_LEN_LIMIT];
- char Virtual_Sensor3[SENSOR_FIELD_LEN_LIMIT];
- char Virtual_Sensor4[SENSOR_FIELD_LEN_LIMIT];
- char Virtual_Sensor5[SENSOR_FIELD_LEN_LIMIT];
- char Virtual_Sensor6[SENSOR_FIELD_LEN_LIMIT];
- char Virtual_Sensor7[SENSOR_FIELD_LEN_LIMIT];
- char Virtual_Sensor8[SENSOR_FIELD_LEN_LIMIT];
- char GPS_Satellites[SENSOR_FIELD_LEN_LIMIT];
- char GPS_Latitude[SENSOR_FIELD_LEN_LIMIT];
- char GPS_Longitude[SENSOR_FIELD_LEN_LIMIT];
- char GPS_Altitude[SENSOR_FIELD_LEN_LIMIT];
- char GPS_Speed[SENSOR_FIELD_LEN_LIMIT];
- char GPS_Course[SENSOR_FIELD_LEN_LIMIT];
- float fTemperature;
- float fHumidity;
+ float Temperature;
+ float Humidity;
+ float AccelX;
+ float AccelY;
+ float AccelZ;
+ int MagnetometerX;
+ int MagnetometerY;
+ int MagnetometerZ;
+ int AmbientLightVis;
+ int AmbientLightIr;
+ float UVindex;
+ int Proximity;
+ float Temperature_Si7020;
+ float Humidity_Si7020;
} K64F_Sensors_t ;
extern K64F_Sensors_t SENSOR_DATA;
