ACKme WiFi module + Nucleo MEMS module example. Connect and publish sensor data to M2X.
Dependencies: M2XStreamClient WiConnect Nucleo_Sensor_Shield jsonlite mbed
Fork of m2x-MEMS_ACKme_Wifi_demo by
Diff: main.cpp
- Revision:
- 0:db24f1dae4a4
- Child:
- 1:276e4607719f
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Dec 11 20:56:33 2014 +0000 @@ -0,0 +1,206 @@ +#include "mbed.h" +// include target specific defines +#include "target_config.h" +// include X-CUBE-MEMS1 Library +#include "x_cube_mems.h" +// include the Wiconnect Host Library API header +#include "Wiconnect.h" +// include M2X Library +#include "M2XStreamClient.h" + +/** + * Connect the ACKme WiFi module directly to the Nucleo board. + * Connect the X-NUCLEO-IKS01A1 module on top of the ACKme WiFi module. + */ + +/** + * Hyperterminal configuration + * 115200 bauds, 8-bit data, no parity + */ + +/** + * This is the name of your WiFi network. + * Look for this name in your WiFi settings. + * (e.g. your phone's list of WiFi networks in the WiFi settings menu.) + * tip: add double-quotes around SSID to add spaces to name. + */ +#define NETWORK_SSID "\"<YOUR NETWORK NAME HERE>\"" + +/** + * This is the password of your WiFi network. + * Leave as empty string (e.g "") to connect to OPEN network. + */ +#define NETWORK_PASSWORD "\"<YOUR NETWORK PASSWORD HERE>\"" + +const char key[] = "0a49487bc297bc061ca49ac01eb3755f"; // Replace with your M2X API key +const char feed[] = "db817fd389c05eca79c331b4bb6daf8d"; // Replace with your blueprint Feed ID +const char tempStream[] = "temperature"; // Replace with your stream name +const char humStream[] = "humidity"; // Replace with your stream name +const char accStream[] = "acceleration"; // Replace with your stream name + +char name[] = "redmond_st_office"; // Name of current location of datasource +double latitude = 47.633889; // You can also read those values from a GPS +double longitude = -122.138611; +double elevation = 97.46; + +/* Instantiate the serial console. */ +Serial pc(SERIAL_TX, SERIAL_RX); + +int main() +{ + /* Set the console terminal to 115200 bps. */ + pc.baud(CONSOLE_BAUD); + + /* Instantiate the X-CUBE-MEMS Library. */ + static X_CUBE_MEMS *mems_expansion_board = X_CUBE_MEMS::Instance(); + + /* Read and output the humidity sensor id to confirm communication. */ + uint8_t hts221_id = mems_expansion_board->hts221.ReadID(); + + pc.printf("HTS221_ID = 0x%x\n\t\r", hts221_id); + + /** + * WIFI Setup + */ + + /* Setup wiconnect serial interface configuration. */ + + /** + * Here we only specify the rx buffer size and not rx buffer pointer, this means + * the serial RX buffer will be dynamically allocated. + */ + SerialConfig serialConfig(WICONNECT_RX_PIN, WICONNECT_TX_PIN, 256, NULL); + + /* Instantiate WiConnect Library. */ + + /** + * Here we only specify the buffer size and not buffer pointer, this means + * the internal buffer will be dynamically allocated. + */ + Wiconnect wiconnect(serialConfig, 256, NULL, WICONNECT_RESET_PIN); + + /* Initiate Communication with WiFi Module. */ + pc.printf("Initializing WiConnect Library...\r\n"); + + WiconnectResult result; + + if(WICONNECT_FAILED(result, wiconnect.init(true))) + { + if(result == WICONNECT_FIRMWARE_OUTDATED) + { + pc.printf("** The WiFi firmware is not supported. Run the ota example to update the firmware:\r\n"); + pc.printf("https://developer.mbed.org/teams/ACKme/code/wiconnect-ota_example\r\n\r\n"); + } + else + { + pc.printf("Failed to initialize communication with WiFi module!\r\n" + "Make sure the wires are connected correctly\r\n"); + } + + return -1; + } + + /* Manually connected to the specified network (to ensure SDK backward compatibility. */ + pc.printf("Setting network SSID: %s\r\n", NETWORK_SSID); + + if(WICONNECT_FAILED(result, wiconnect.setSetting("wlan.ssid", NETWORK_SSID))) + { + pc.printf("Failed to set wlan.ssid setting\r\n"); + return -1; + } + + pc.printf("Setting network password\r\n"); + + if(WICONNECT_FAILED(result, wiconnect.setSetting("wlan.passkey", NETWORK_PASSWORD))) + { + pc.printf("Failed to set wlan.passkey setting\r\n"); + return -1; + } + + pc.printf("Saving settings to Non-volatile Memory\r\n"); + + if(WICONNECT_FAILED(result, wiconnect.saveSettings())) + { + pc.printf("Failed save settings\r\n"); + return -1; + } + + pc.printf("IP Address: %s\r\n", wiconnect.getIpAddress()); + pc.printf("Network joined!\r\n"); + + /** + * M2X Setup + */ + + /* Instantiate the M2X Stream Client. */ + Client client; + M2XStreamClient m2xClient(&client, key); + + /* Update device location. */ + int m2x_response = m2xClient.updateLocation(feed, name, latitude, longitude, elevation); + + pc.printf("updateLocation response code: %d\r\n", m2x_response); + + /* Main loop */ + while(1) + { + volatile float TEMPERATURE_Value; + volatile float HUMIDITY_Value; + volatile float PRESSURE_Value; + volatile AxesRaw_TypeDef MAG_Value; + volatile AxesRaw_TypeDef ACC_Value; + volatile AxesRaw_TypeDef GYR_Value; + + /* Update sensors. */ + mems_expansion_board->hts221.GetTemperature((float *)&TEMPERATURE_Value); + mems_expansion_board->hts221.GetHumidity((float *)&HUMIDITY_Value); + mems_expansion_board->lps25h.GetPressure((float *)&PRESSURE_Value); + mems_expansion_board->lis3mdl.GetAxes((AxesRaw_TypeDef *)&MAG_Value); + mems_expansion_board->lsm6ds0.Acc_GetAxes((AxesRaw_TypeDef *)&ACC_Value); + mems_expansion_board->lsm6ds0.Gyro_GetAxes((AxesRaw_TypeDef *)&GYR_Value); + + /* Output sensor data. */ + pc.printf("TEMP: %f HUMIDITY: %f PRESSURE: %f\t\r\n", TEMPERATURE_Value, HUMIDITY_Value, PRESSURE_Value); + pc.printf("X_MAG: %d, Y_MAG: %d, Z_MAG: %d\t\r\n", MAG_Value.AXIS_X, MAG_Value.AXIS_Y, MAG_Value.AXIS_Z); + pc.printf("X_ACC: %d, Y_ACC: %d, Z_ACC: %d\t\r\n", ACC_Value.AXIS_X, ACC_Value.AXIS_Y, ACC_Value.AXIS_Z); + pc.printf("X_GYR: %d, Y_GYR: %d, Z_GYR: %d\t\r\n\n", GYR_Value.AXIS_X, GYR_Value.AXIS_Y, GYR_Value.AXIS_Z); + + /* Convert temperature to degrees Farhenheit. */ + float temperature_f = (1.8f * TEMPERATURE_Value) + 32.0f; + + /* Post temperature to the m2x stream. */ + m2x_response = m2xClient.updateStreamValue(feed, tempStream, temperature_f); + + pc.printf("Temperature updateStreamValue response code: %d\r\n", m2x_response); + + if (m2x_response == -1) + { + pc.printf("Temperature data transmit post error\n"); + } + + /* Post humidity to the m2x stream. */ + m2x_response = m2xClient.updateStreamValue(feed, humStream, HUMIDITY_Value); + + pc.printf("Humidity updateStreamValue response code: %d\r\n", m2x_response); + + if (m2x_response == -1) + { + pc.printf("Humidity data transmit post error\n"); + } + + /* Post acceleration (x-axis) to the m2x stream. */ + m2x_response = m2xClient.updateStreamValue(feed, accStream, ACC_Value.AXIS_X); + + pc.printf("Acceleration updateStreamValue response code: %d\r\n", m2x_response); + + if (m2x_response == -1) + { + pc.printf("Acceleration data transmit post error\n"); + } + + pc.printf("\n"); + + wait(30); // 30 s + } +} + \ No newline at end of file