Includes support for IKS01A1 per HelloWorld_IKS01A1
Dependencies: NetworkSocketAPI X_NUCLEO_IDW01M1v2 X_NUCLEO_IKS01A1 mbed
Fork of Nucleo_read_a0_thingspace by
Diff: main.cpp
- Revision:
- 1:c0a277c96fd6
- Parent:
- 0:45e9e04829d0
- Child:
- 2:553393c6c088
--- a/main.cpp Wed Nov 02 13:25:17 2016 +0000 +++ b/main.cpp Thu Nov 03 20:37:46 2016 +0000 @@ -6,6 +6,7 @@ #include "SpwfInterface.h" #include "TCPSocket.h" #include <string> +#include "x_nucleo_iks01a1.h" /************************************* //FRDM-K64: D9->UART1_TX, D7->UART1_RX @@ -41,11 +42,62 @@ DigitalOut myLed(LED1); SpwfSAInterface spwf(D8, D2, false); +/* Instantiate the expansion board */ +static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(D14, D15); + +/* Retrieve the composing elements of the expansion board */ +static GyroSensor *gyroscope = mems_expansion_board->GetGyroscope(); +static MotionSensor *accelerometer = mems_expansion_board->GetAccelerometer(); +static MagneticSensor *magnetometer = mems_expansion_board->magnetometer; +static HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor; +static PressureSensor *pressure_sensor = mems_expansion_board->pt_sensor; +static TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor; +static TempSensor *temp_sensor2 = mems_expansion_board->pt_sensor; + int errConnect; int errSend; +/* Helper function for printing floats & doubles */ +static char *printDouble(char* str, double v, int decimalDigits=2) +{ + int i = 1; + int intPart, fractPart; + int len; + char *ptr; + + /* prepare decimal digits multiplicator */ + for (;decimalDigits!=0; i*=10, decimalDigits--); + + /* calculate integer & fractinal parts */ + intPart = (int)v; + fractPart = (int)((v-(double)(int)v)*i); + + /* fill in integer part */ + sprintf(str, "%i.", intPart); + + /* prepare fill in of fractional part */ + len = strlen(str); + ptr = &str[len]; + + /* fill in leading fractional zeros */ + for (i/=10;i>1; i/=10, ptr++) { + if(fractPart >= i) break; + *ptr = '0'; + } + + /* fill in (rest of) fractional part */ + sprintf(ptr, "%i", fractPart); + + return str; +} + int main() { + uint8_t id; + float value1, value2; + char buffer1[32], buffer2[32]; + int32_t axes[3]; + float panel_voltage; DigitalOut led(LED1); serial_port.baud(9600); @@ -99,10 +151,51 @@ macString.erase(2,1); macString.erase(4,1); const char *macBytes = macString.c_str(); + + humidity_sensor->ReadID(&id); + pressure_sensor->ReadID(&id); + magnetometer->ReadID(&id); + gyroscope->ReadID(&id); + wait(3); while(1) { + + std::string readings = ""; + temp_sensor1->GetTemperature(&value1); + humidity_sensor->GetHumidity(&value2); + + readings = readings + "&temperature1="; + readings = readings + printDouble(buffer1,value1); + readings = readings + "&humidity="; + readings = readings + printDouble(buffer2,value2); + + temp_sensor2->GetFahrenheit(&value1); + pressure_sensor->GetPressure(&value2); + + readings = readings + "&temperature2="; + readings = readings + printDouble(buffer1,value1); + readings = readings + "&pressure="; + readings = readings + printDouble(buffer2,value2); + + char magBuff[64] = ""; + magnetometer->Get_M_Axes(axes); + sprintf(magBuff, "&magnometer0=%ld&magnometer1=%ld&magnometer2=%ld", axes[0], axes[1], axes[2]); + readings = readings + magBuff; + + char accBuff[64] = ""; + accelerometer->Get_X_Axes(axes); + sprintf(accBuff, "&accelerometer0=%ld&accelerometer1=%ld&accelerometer2=%ld", axes[0], axes[1], axes[2]); + readings = readings + accBuff; + + char gyroBuff[64] = ""; + gyroscope->Get_G_Axes(axes); + sprintf(gyroBuff, "&gyroscope0=%ld&gyroscope1=%ld&gyroscope2=%ld", axes[0], axes[1], axes[2]); + readings = readings + gyroBuff; + + printf(readings.c_str()); + panel_voltage = analog_input_A0.read(); - printf("voltage: %f\r\n", panel_voltage); + // printf("voltage: %f\r\n", panel_voltage); // A0 is voltage tolerant to 3.3V, and analog read returns a percentage of the maximum // need to convert the percentage back to a representative number panel_voltage = panel_voltage * 3300; // change the value to be in the 0 to 3300 range @@ -121,14 +214,17 @@ // get length of jsonContent as string without streams // adapted from http://codereview.stackexchange.com/questions/51270/socket-http-post-request - char dweetBuffer[72] = ""; + char dweetBuffer[512] = ""; // create GET HTTP header for dweeting strcpy(dweetBuffer, "GET /dweet/for/nucleo-"); - strcat(dweetBuffer, macBytes); - char valueRead[20]; - sprintf(valueRead, "?voltage=%f", panel_voltage); + // uncomment to use the last 6 digits of WiFi MAC + // strcat(dweetBuffer, macBytes); + strcat(dweetBuffer, "toma"); + char valueRead[32]; + sprintf(valueRead, "?voltage=%f&units=millivolts", panel_voltage); strcat(dweetBuffer, valueRead); + strcat(dweetBuffer, readings.c_str()); strcat(dweetBuffer, " HTTP/1.1\r\n\r\n"); serial_port.printf("\r\n%s", dweetBuffer); @@ -138,9 +234,9 @@ serial_port.printf("sending and receiving data ...\r\n"); errSend = socket.send(dweetBuffer, strlen(dweetBuffer)); countRx = socket.recv(bufferRx, sizeof bufferRx); - printf("sent %d bytes\r\nand received %d bytes\r\n\r\n", errSend, countRx); + printf("sent %d bytes and received %d bytes\r\n\r\n", errSend, countRx); printf(bufferRx); - printf("\r\n\r\n*** 5-second pause ...\r\n"); + printf("\r\n\r\n*** 5-second pause ...\r\n\r\n"); } wait(5.0); // 5000 ms delay before looping to next read }