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
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 Nucleo sensor 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 wait(5); 00104 00105 /* Manually connected to the specified network (to ensure SDK backward compatibility). */ 00106 pc.printf("Setting network SSID: %s\r\n", NETWORK_SSID); 00107 00108 if (WICONNECT_FAILED(result, wiconnect.setSetting("wlan.ssid", NETWORK_SSID))) 00109 { 00110 pc.printf("Failed to set wlan.ssid setting\r\n"); 00111 return -1; 00112 } 00113 00114 pc.printf("Setting network password\r\n"); 00115 00116 if (WICONNECT_FAILED(result, wiconnect.setSetting("wlan.passkey", NETWORK_PASSWORD))) 00117 { 00118 pc.printf("Failed to set wlan.passkey setting\r\n"); 00119 return -1; 00120 } 00121 00122 pc.printf("Saving settings to Non-volatile Memory\r\n"); 00123 00124 if (WICONNECT_FAILED(result, wiconnect.saveSettings())) 00125 { 00126 pc.printf("Failed save settings\r\n"); 00127 return -1; 00128 } 00129 00130 NetworkStatus status; 00131 00132 result = wiconnect.getNetworkStatus(&status); 00133 00134 switch (status) 00135 { 00136 case NETWORK_STATUS_DOWN: 00137 { 00138 pc.printf("NETWORK_STATUS_DOWN\r\n"); 00139 00140 return -1; 00141 00142 break; 00143 } 00144 case NETWORK_STATUS_WIFI_ONLY: 00145 { 00146 pc.printf("NETWORK_STATUS_WIFI_ONLY\r\n"); 00147 break; 00148 } 00149 case NETWORK_STATUS_UP: 00150 { 00151 pc.printf("NETWORK_STATUS_UP\r\n"); 00152 break; 00153 } 00154 case NETWORK_STATUS_JOINING: 00155 { 00156 pc.printf("NETWORK_STATUS_JOINING\r\n"); 00157 break; 00158 } 00159 default: 00160 { 00161 pc.printf("UNKNOWN\r\n"); 00162 break; 00163 } 00164 } 00165 00166 pc.printf("IP Address: %s\r\n", wiconnect.getIpAddress()); 00167 pc.printf("Network joined!\r\n"); 00168 00169 /** 00170 * M2X Setup 00171 */ 00172 00173 /* Instantiate the M2X Stream Client. */ 00174 Client client; 00175 M2XStreamClient m2xClient(&client, key); 00176 00177 /* Update device location. */ 00178 int m2x_response = m2xClient.updateLocation(feed, name, latitude, longitude, elevation); 00179 00180 pc.printf("updateLocation response code: %d\r\n", m2x_response); 00181 00182 if (m2x_response == -1) 00183 { 00184 printf("Location data update error\n"); 00185 } 00186 00187 /* Main loop */ 00188 while(1) 00189 { 00190 volatile float TEMPERATURE_Value_C; 00191 volatile float TEMPERATURE_Value_F; 00192 volatile float HUMIDITY_Value; 00193 volatile float PRESSURE_Value; 00194 volatile AxesRaw_TypeDef MAG_Value; 00195 volatile AxesRaw_TypeDef ACC_Value; 00196 volatile AxesRaw_TypeDef GYR_Value; 00197 00198 /* Update sensors. */ 00199 mems_expansion_board->hts221.GetTemperature((float *)&TEMPERATURE_Value_C); 00200 mems_expansion_board->hts221.GetHumidity((float *)&HUMIDITY_Value); 00201 mems_expansion_board->lps25h.GetPressure((float *)&PRESSURE_Value); 00202 mems_expansion_board->lis3mdl.GetAxes((AxesRaw_TypeDef *)&MAG_Value); 00203 mems_expansion_board->lsm6ds0.Acc_GetAxes((AxesRaw_TypeDef *)&ACC_Value); 00204 mems_expansion_board->lsm6ds0.Gyro_GetAxes((AxesRaw_TypeDef *)&GYR_Value); 00205 00206 /* Convert temperature to degrees Farhenheit. */ 00207 TEMPERATURE_Value_F = (1.8f * TEMPERATURE_Value_C) + 32.0f; 00208 00209 /* Output sensor data. */ 00210 pc.printf("Temperature:\t\t %f C / %f F\r\n", TEMPERATURE_Value_C, TEMPERATURE_Value_F); 00211 pc.printf("Humidity:\t\t %f%%\r\n", HUMIDITY_Value); 00212 pc.printf("Pressure:\t\t %f hPa\r\n", PRESSURE_Value); 00213 pc.printf("Magnetometer (mGauss):\t X: %d, Y: %d, Z: %d\r\n", MAG_Value.AXIS_X, MAG_Value.AXIS_Y, MAG_Value.AXIS_Z); 00214 pc.printf("Accelerometer (mg):\t X: %d, Y: %d, Z: %d\r\n", ACC_Value.AXIS_X, ACC_Value.AXIS_Y, ACC_Value.AXIS_Z); 00215 pc.printf("Gyroscope (mdps):\t X: %d, Y: %d, Z: %d\r\n", GYR_Value.AXIS_X, GYR_Value.AXIS_Y, GYR_Value.AXIS_Z); 00216 pc.printf("\r\n"); 00217 00218 /* Post temperature to the m2x stream. */ 00219 m2x_response = m2xClient.updateStreamValue(feed, tempStream, TEMPERATURE_Value_F); 00220 00221 pc.printf("Temperature updateStreamValue response code: %d\r\n", m2x_response); 00222 00223 if (m2x_response == -1) 00224 { 00225 pc.printf("Temperature data update error\n"); 00226 } 00227 00228 /* Post humidity to the m2x stream. */ 00229 m2x_response = m2xClient.updateStreamValue(feed, humStream, HUMIDITY_Value); 00230 00231 pc.printf("Humidity updateStreamValue response code: %d\r\n", m2x_response); 00232 00233 if (m2x_response == -1) 00234 { 00235 pc.printf("Humidity data update error\n"); 00236 } 00237 00238 /* Post acceleration (x-axis) to the m2x stream. */ 00239 m2x_response = m2xClient.updateStreamValue(feed, accStream, ACC_Value.AXIS_X); 00240 00241 pc.printf("Acceleration updateStreamValue response code: %d\r\n", m2x_response); 00242 00243 if (m2x_response == -1) 00244 { 00245 pc.printf("Acceleration data update error\n"); 00246 } 00247 00248 pc.printf("\n"); 00249 00250 wait(30); // 30 s 00251 } 00252 } 00253
Generated on Sat Jul 16 2022 22:21:36 by 1.7.2