David Kwak / Mbed 2 deprecated m2x-MEMS_ACKme_Wifi_demo

Dependencies:   M2XStreamClient WiConnect MEMS-Sensors jsonlite mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 // include target specific defines
00003 #include "target_config.h"
00004 // include X-CUBE-MEMS1 Library
00005 #include "x_cube_mems.h"
00006 // include the Wiconnect Host Library API header
00007 #include "Wiconnect.h"
00008 // include M2X Library
00009 #include "M2XStreamClient.h"
00010 
00011 /**
00012   * Connect the ACKme WiFi module directly to the Nucleo board.
00013   * Connect the X-NUCLEO-IKS01A1 module on top of the ACKme WiFi module.
00014   */
00015 
00016 /**
00017   * Hyperterminal configuration
00018   * 9600 bauds, 8-bit data, no parity
00019   */
00020 
00021 /**
00022   * This is the name of your WiFi network.
00023   * Look for this name in your WiFi settings.
00024   * (e.g. your phone's list of WiFi networks in the WiFi settings menu.)
00025   * tip: add double-quotes around SSID to add spaces to name.
00026   */
00027 #define NETWORK_SSID "\"<YOUR NETWORK NAME HERE>\""
00028 
00029 /**
00030   * This is the password of your WiFi network.
00031   * Leave as empty string (e.g "") to connect to OPEN network.
00032   */
00033 #define NETWORK_PASSWORD "\"<YOUR NETWORK PASSWORD HERE>\""
00034 
00035 const char key[] = "123ad8ee16ef56dfafd0c42a3a3ef109";      // Replace with your M2X API key
00036 const char feed[] = "d3ffd3ab9f659943e2302ba232acf198";     // Replace with your blueprint Feed ID
00037 const char tempStream[] = "temperature";                    // Replace with your stream name  
00038 const char humStream[] = "humidity";                        // Replace with your stream name  
00039 const char accStream[] = "acceleration";                    // Replace with your stream name  
00040 
00041 char name[] = "redmond_st_office";  // Name of current location of datasource
00042 double latitude = 47.633889;        // You can also read those values from a GPS
00043 double longitude = -122.138611;
00044 double elevation = 97.46;
00045 
00046 /* Instantiate the serial console. */
00047 Serial pc(SERIAL_TX, SERIAL_RX);
00048  
00049 int main()
00050 {
00051     /* Set the console terminal to 9600 bps. */
00052     pc.baud(CONSOLE_BAUD);
00053     
00054     /* Instantiate the X-CUBE-MEMS Library. */
00055     static X_CUBE_MEMS *mems_expansion_board = X_CUBE_MEMS::Instance();
00056     
00057     /* Read and output the humidity sensor id to confirm communication. */
00058     uint8_t hts221_id = mems_expansion_board->hts221.ReadID();
00059     
00060     pc.printf("HTS221_ID = 0x%x\n\t\r", hts221_id);
00061     
00062     /**
00063       * WIFI Setup
00064       */
00065     
00066     /* Setup wiconnect serial interface configuration. */
00067     
00068     /**
00069       * Here we only specify the rx buffer size and not rx buffer pointer, this means
00070       * the serial RX buffer will be dynamically allocated.
00071       */
00072     SerialConfig serialConfig(WICONNECT_RX_PIN, WICONNECT_TX_PIN, 256, NULL);
00073     
00074     /* Instantiate WiConnect Library. */
00075     
00076     /**
00077       * Here we only specify the buffer size and not buffer pointer, this means
00078       * the internal buffer will be dynamically allocated.
00079       */
00080     Wiconnect wiconnect(serialConfig, 256, NULL, WICONNECT_RESET_PIN);
00081     
00082     /* Initiate Communication with WiFi Module. */
00083     pc.printf("Initializing WiConnect Library...\r\n");
00084     
00085     WiconnectResult result;
00086     
00087     if(WICONNECT_FAILED(result, wiconnect.init(true)))
00088     {
00089         if(result == WICONNECT_FIRMWARE_OUTDATED)
00090         {
00091             pc.printf("** The WiFi firmware is not supported. Run the ota example to update the firmware:\r\n");
00092             pc.printf("https://developer.mbed.org/teams/ACKme/code/wiconnect-ota_example\r\n\r\n");
00093         }
00094         else
00095         {
00096             pc.printf("Failed to initialize communication with WiFi module!\r\n"
00097                       "Make sure the wires are connected correctly\r\n");
00098         }
00099         
00100         return -1;
00101     }
00102     
00103     /* Manually connected to the specified network (to ensure SDK backward compatibility. */
00104     pc.printf("Setting network SSID: %s\r\n", NETWORK_SSID);
00105     
00106     if(WICONNECT_FAILED(result, wiconnect.setSetting("wlan.ssid", NETWORK_SSID)))
00107     {
00108         pc.printf("Failed to set wlan.ssid setting\r\n");
00109         return -1;
00110     }
00111     
00112     pc.printf("Setting network password\r\n");
00113     
00114     if(WICONNECT_FAILED(result, wiconnect.setSetting("wlan.passkey", NETWORK_PASSWORD)))
00115     {
00116         pc.printf("Failed to set wlan.passkey setting\r\n");
00117         return -1;
00118     }
00119 
00120     pc.printf("Saving settings to Non-volatile Memory\r\n");
00121     
00122     if(WICONNECT_FAILED(result, wiconnect.saveSettings()))
00123     {
00124         pc.printf("Failed save settings\r\n");
00125         return -1;
00126     }
00127     
00128     pc.printf("IP Address: %s\r\n", wiconnect.getIpAddress());
00129     pc.printf("Network joined!\r\n");
00130     
00131     /**
00132       * M2X Setup
00133       */
00134     
00135     /* Instantiate the M2X Stream Client. */
00136     Client client;    
00137     M2XStreamClient m2xClient(&client, key);
00138     
00139     /* Update device location. */
00140     int m2x_response = m2xClient.updateLocation(feed, name, latitude, longitude, elevation);
00141     
00142     pc.printf("updateLocation response code: %d\r\n", m2x_response);
00143     
00144     /* Main loop */
00145     while(1)
00146     {
00147         volatile float TEMPERATURE_Value;
00148         volatile float HUMIDITY_Value;
00149         volatile float PRESSURE_Value;
00150         volatile AxesRaw_TypeDef MAG_Value;
00151         volatile AxesRaw_TypeDef ACC_Value;
00152         volatile AxesRaw_TypeDef GYR_Value;
00153         
00154         /* Update sensors.  */
00155         mems_expansion_board->hts221.GetTemperature((float *)&TEMPERATURE_Value);
00156         mems_expansion_board->hts221.GetHumidity((float *)&HUMIDITY_Value);
00157         mems_expansion_board->lps25h.GetPressure((float *)&PRESSURE_Value);
00158         mems_expansion_board->lis3mdl.GetAxes((AxesRaw_TypeDef *)&MAG_Value);
00159         mems_expansion_board->lsm6ds0.Acc_GetAxes((AxesRaw_TypeDef *)&ACC_Value);
00160         mems_expansion_board->lsm6ds0.Gyro_GetAxes((AxesRaw_TypeDef *)&GYR_Value);     
00161         
00162         /* Output sensor data. */
00163         pc.printf("TEMP: %f HUMIDITY: %f PRESSURE: %f\t\r\n", TEMPERATURE_Value, HUMIDITY_Value, PRESSURE_Value);
00164         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);
00165         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);
00166         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);
00167         
00168         /* Convert temperature to degrees Farhenheit. */
00169         float temperature_f = (1.8f * TEMPERATURE_Value) + 32.0f;
00170         
00171         /* Post temperature to the m2x stream. */
00172         m2x_response = m2xClient.updateStreamValue(feed, tempStream, temperature_f);
00173         
00174         pc.printf("Temperature updateStreamValue response code: %d\r\n", m2x_response);
00175         
00176         if (m2x_response == -1) 
00177         {
00178             pc.printf("Temperature data transmit post error\n");
00179         }
00180         
00181         /* Post humidity to the m2x stream. */
00182         m2x_response = m2xClient.updateStreamValue(feed, humStream, HUMIDITY_Value);
00183         
00184         pc.printf("Humidity updateStreamValue response code: %d\r\n", m2x_response);
00185         
00186         if (m2x_response == -1) 
00187         {
00188             pc.printf("Humidity data transmit post error\n");
00189         }
00190         
00191         /* Post acceleration (x-axis) to the m2x stream. */
00192         m2x_response = m2xClient.updateStreamValue(feed, accStream, ACC_Value.AXIS_X);
00193         
00194         pc.printf("Acceleration updateStreamValue response code: %d\r\n", m2x_response);
00195         
00196         if (m2x_response == -1) 
00197         {
00198             pc.printf("Acceleration data transmit post error\n");
00199         }
00200         
00201         pc.printf("\n");
00202         
00203         wait(30); // 30 s
00204     }
00205 }
00206