MultiTech Socket Modem and MTSAS ST MEMS Demo X-NUCLEO-IKS01A1 Aeris Aercloud
Dependencies: MQTT Nucleo_Sensor_Shield mbed
main.cpp@1:ce1ed2844521, 2015-01-28 (annotated)
- Committer:
- BlueShadow
- Date:
- Wed Jan 28 20:57:32 2015 +0000
- Revision:
- 1:ce1ed2844521
- Parent:
- 0:37336794ce7e
ST Conference Demo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
BlueShadow | 0:37336794ce7e | 1 | /* This is an example program which reads in the 3-axis acceleration, pressure, and temperature data from |
BlueShadow | 1:ce1ed2844521 | 2 | * a sensor board. It then uses an MTSAS SocketModem Shield board to send the read data over |
BlueShadow | 0:37336794ce7e | 3 | * a cellular connection to Aeris AerCloud using an MQTT client protocol. |
BlueShadow | 0:37336794ce7e | 4 | */ |
BlueShadow | 0:37336794ce7e | 5 | |
BlueShadow | 0:37336794ce7e | 6 | #include "mbed.h" |
BlueShadow | 0:37336794ce7e | 7 | #include "mtsas.h" |
BlueShadow | 0:37336794ce7e | 8 | #include "PubSubClient.h" |
BlueShadow | 0:37336794ce7e | 9 | #include "x_cube_mems.h" |
BlueShadow | 0:37336794ce7e | 10 | |
BlueShadow | 0:37336794ce7e | 11 | // |
BlueShadow | 0:37336794ce7e | 12 | // PLEASE READ THIS! |
BlueShadow | 0:37336794ce7e | 13 | // |
BlueShadow | 0:37336794ce7e | 14 | // Example was created for the following hardware: |
BlueShadow | 0:37336794ce7e | 15 | // ST Nucleo F401RE http://developer.mbed.org/platforms/ST-Nucleo-F401RE/ |
BlueShadow | 0:37336794ce7e | 16 | // ST Sensor Board http://developer.mbed.org/teams/ST-Americas-mbed-Team/wiki/Getting-Started-with-Nucleo-Sensors |
BlueShadow | 0:37336794ce7e | 17 | // MultiTech Socket Modem Shield: http://developer.mbed.org/components/Multi-Tech-SocketModem-Arduino-Shield-MT/ |
BlueShadow | 0:37336794ce7e | 18 | // MultiTech MTSMC-H5 GSM Socket Modem http://www.multitech.com/models/92503252LF#gsc.tab=0 |
BlueShadow | 0:37336794ce7e | 19 | // |
BlueShadow | 0:37336794ce7e | 20 | // To get the sample running, you'll need to fill in the following parameters below |
BlueShadow | 0:37336794ce7e | 21 | // Your cellular provider's APN: _APN |
BlueShadow | 0:37336794ce7e | 22 | // AerCloud API Key: _AERCLOUD_API_APIKEY |
BlueShadow | 0:37336794ce7e | 23 | // AerCloud Account ID: _AERCLOUD_ACCOUNT_ID |
BlueShadow | 0:37336794ce7e | 24 | // AerCloud Container: _ACERCLOUD_CONTAINER |
BlueShadow | 0:37336794ce7e | 25 | // |
BlueShadow | 0:37336794ce7e | 26 | // The AerCloud container needa a Data Model with the following schema: |
BlueShadow | 0:37336794ce7e | 27 | // accel_x FLOAT |
BlueShadow | 0:37336794ce7e | 28 | // accel_y FLOAT |
BlueShadow | 0:37336794ce7e | 29 | // accel-Z FLOAT |
BlueShadow | 0:37336794ce7e | 30 | // pressure FLOAT |
BlueShadow | 0:37336794ce7e | 31 | // temperature FLOAT |
BlueShadow | 0:37336794ce7e | 32 | // |
BlueShadow | 0:37336794ce7e | 33 | // To check if data has made it in the container, create a long polling subscription in AerPort and they use the following url in your browswer |
BlueShadow | 0:37336794ce7e | 34 | // |
BlueShadow | 0:37336794ce7e | 35 | // http://longpoll.aercloud.aeris.com/v1/10677/containers/NUCLEO_STmems/subscriptions/NUCLEO_STmems_SI/notificationChannels/longPoll |
BlueShadow | 0:37336794ce7e | 36 | // |
BlueShadow | 0:37336794ce7e | 37 | // |
BlueShadow | 0:37336794ce7e | 38 | // You should see the something that looks like this in the browser: |
BlueShadow | 0:37336794ce7e | 39 | // {"sclContentInstances":[{"sclId":"nucleo-0001","containerId":"Nucleo_Test","contentInstance":{"id":"a40c8e60-8248-11e4-8b38-0677f0dfdf5e","contentSize":90,"creationTime":1418420922950,"content":{"contentType":"application/json","contentTypeBinary":"{\"x\":0.005615,\"y\":-0.041260,\"z\":1.015137,\"pressure\":101098.500000,\"temperature\":25.125000}"}}}, |
BlueShadow | 0:37336794ce7e | 40 | // |
BlueShadow | 0:37336794ce7e | 41 | |
BlueShadow | 0:37336794ce7e | 42 | |
BlueShadow | 0:37336794ce7e | 43 | char _APN[] = "Fast.t-mobile.com"; |
BlueShadow | 0:37336794ce7e | 44 | |
BlueShadow | 0:37336794ce7e | 45 | char _AERCLOUD_API_KEY[] ="5c6fdcba-7be8-11e2-bef9-3383855fced8"; |
BlueShadow | 0:37336794ce7e | 46 | char _AERCLOUD_ACCOUNT_ID[] = "10677"; // account ID is correct. |
BlueShadow | 1:ce1ed2844521 | 47 | char _AERCLOUD_CONTAINER[] = "NUCLEO_STmems"; // new container name |
BlueShadow | 1:ce1ed2844521 | 48 | char _AERCLOUD_DEVICE_ID[] = "NUCLEO-STmems"; // last 4 digits of IMEI |
BlueShadow | 0:37336794ce7e | 49 | char _AERCLOUD_SUBSCRIPTION_ID[] = "NUCLEO_STmems_Sid"; // created at Continer Name then Subscription ID for LongPoll |
BlueShadow | 0:37336794ce7e | 50 | |
BlueShadow | 0:37336794ce7e | 51 | |
BlueShadow | 0:37336794ce7e | 52 | /* //referenece info for training material// |
BlueShadow | 0:37336794ce7e | 53 | char _APN[] = "Set_carrier_APN"; |
BlueShadow | 0:37336794ce7e | 54 | |
BlueShadow | 0:37336794ce7e | 55 | char _AERCLOUD_API_KEY[] ="_Click_On_KEY_AerCloud_Tab"; |
BlueShadow | 0:37336794ce7e | 56 | char _AERCLOUD_ACCOUNT_ID[] = "_Company_Number_Top_of_Page"; // account ID is correct. |
BlueShadow | 0:37336794ce7e | 57 | char _AERCLOUD_CONTAINER[] = "_From_Container_Page"; // new container name |
BlueShadow | 0:37336794ce7e | 58 | char _AERCLOUD_DEVICE_ID[] = "_Random_Title"; // last 4 digits of IMEI |
BlueShadow | 0:37336794ce7e | 59 | char _AERCLOUD_SUBSCRIPTION_ID = "_Random_Subscription_ID"; |
BlueShadow | 0:37336794ce7e | 60 | */ |
BlueShadow | 0:37336794ce7e | 61 | |
BlueShadow | 0:37336794ce7e | 62 | char _host[] = "mqtt.aercloud.aeris.com"; |
BlueShadow | 0:37336794ce7e | 63 | int _port = 1883; |
BlueShadow | 0:37336794ce7e | 64 | |
BlueShadow | 0:37336794ce7e | 65 | #define DATA_INTERVAL 15 |
BlueShadow | 0:37336794ce7e | 66 | |
BlueShadow | 0:37336794ce7e | 67 | void callback(char* topic, char* payload, unsigned int len) { |
BlueShadow | 0:37336794ce7e | 68 | logInfo("topic: [%s]\r\npayload: [%s]", topic, payload); |
BlueShadow | 0:37336794ce7e | 69 | } |
BlueShadow | 0:37336794ce7e | 70 | |
BlueShadow | 0:37336794ce7e | 71 | DigitalOut myled(LED1); |
BlueShadow | 0:37336794ce7e | 72 | Serial pc(SERIAL_TX, SERIAL_RX); |
BlueShadow | 0:37336794ce7e | 73 | volatile float TEMPERATURE_Value_C; |
BlueShadow | 0:37336794ce7e | 74 | volatile float TEMPERATURE_Value_F; |
BlueShadow | 0:37336794ce7e | 75 | volatile float HUMIDITY_Value; |
BlueShadow | 0:37336794ce7e | 76 | volatile float PRESSURE_Value; |
BlueShadow | 0:37336794ce7e | 77 | volatile AxesRaw_TypeDef MAG_Value; |
BlueShadow | 0:37336794ce7e | 78 | volatile AxesRaw_TypeDef ACC_Value; |
BlueShadow | 0:37336794ce7e | 79 | volatile AxesRaw_TypeDef GYR_Value; |
BlueShadow | 0:37336794ce7e | 80 | |
BlueShadow | 0:37336794ce7e | 81 | int main() { |
BlueShadow | 0:37336794ce7e | 82 | printf("Hello ST!\r\n"); |
BlueShadow | 0:37336794ce7e | 83 | MTSLog::setLogLevel(MTSLog::TRACE_LEVEL); |
BlueShadow | 0:37336794ce7e | 84 | |
BlueShadow | 0:37336794ce7e | 85 | // for Nucleo boards |
BlueShadow | 0:37336794ce7e | 86 | MTSSerialFlowControl io(D8, D2, D3, D13); |
BlueShadow | 0:37336794ce7e | 87 | io.baud(115200); |
BlueShadow | 0:37336794ce7e | 88 | |
BlueShadow | 0:37336794ce7e | 89 | Cellular* radio = CellularFactory::create(&io); |
BlueShadow | 0:37336794ce7e | 90 | if (! radio) { |
BlueShadow | 0:37336794ce7e | 91 | logFatal("failed to create Cellular object - exiting"); |
BlueShadow | 0:37336794ce7e | 92 | return 1; |
BlueShadow | 0:37336794ce7e | 93 | } |
BlueShadow | 0:37336794ce7e | 94 | |
BlueShadow | 0:37336794ce7e | 95 | radio->configureSignals(D11,D7,RESET); |
BlueShadow | 0:37336794ce7e | 96 | Transport::setTransport(radio); |
BlueShadow | 0:37336794ce7e | 97 | |
BlueShadow | 0:37336794ce7e | 98 | while (radio->setApn(_APN) != MTS_SUCCESS) { |
BlueShadow | 0:37336794ce7e | 99 | logError("failed to set APN [%s]", _APN); |
BlueShadow | 0:37336794ce7e | 100 | wait(1); |
BlueShadow | 0:37336794ce7e | 101 | } |
BlueShadow | 0:37336794ce7e | 102 | |
BlueShadow | 0:37336794ce7e | 103 | while (! radio->connect()) { |
BlueShadow | 0:37336794ce7e | 104 | logError("failed to bring up PPP link"); |
BlueShadow | 0:37336794ce7e | 105 | wait(1); |
BlueShadow | 0:37336794ce7e | 106 | } |
BlueShadow | 0:37336794ce7e | 107 | |
BlueShadow | 0:37336794ce7e | 108 | printf("Signal Strength: %d\n\r", radio->getSignalStrength()); //Check the signal strength should be above 8 |
BlueShadow | 0:37336794ce7e | 109 | |
BlueShadow | 0:37336794ce7e | 110 | printf("data is stored at:\n\r\r"); |
BlueShadow | 1:ce1ed2844521 | 111 | printf("http://longpoll.aercloud.aeris.com/v1/%s/containers/subscriptions/%s/notificationChannels/longPoll?apiKey=%s",_AERCLOUD_ACCOUNT_ID,_AERCLOUD_SUBSCRIPTION_ID,_AERCLOUD_API_KEY); |
BlueShadow | 0:37336794ce7e | 112 | printf(" \n\r \n\r"); |
BlueShadow | 0:37336794ce7e | 113 | |
BlueShadow | 0:37336794ce7e | 114 | // If you suspect a connectivity issue, uncomment the code below and if ping works. If you are not getting a |
BlueShadow | 0:37336794ce7e | 115 | // valid ping, there's a connectivity problem. First step is to verify you've got the right APN set |
BlueShadow | 0:37336794ce7e | 116 | // |
BlueShadow | 0:37336794ce7e | 117 | // Try pinging default server "8.8.8.8" (Google's DNS) |
BlueShadow | 0:37336794ce7e | 118 | // int ping_valid = 0; |
BlueShadow | 0:37336794ce7e | 119 | // while (ping_valid == 0) { |
BlueShadow | 0:37336794ce7e | 120 | // ping_valid = radio->ping(); |
BlueShadow | 0:37336794ce7e | 121 | // printf("Ping Valid: %s\n\r", ping_valid ? "true" : "false"); |
BlueShadow | 0:37336794ce7e | 122 | // |
BlueShadow | 0:37336794ce7e | 123 | // if (ping_valid == 0) { |
BlueShadow | 0:37336794ce7e | 124 | // wait(3); |
BlueShadow | 0:37336794ce7e | 125 | // printf("wait 3"); |
BlueShadow | 0:37336794ce7e | 126 | // } |
BlueShadow | 0:37336794ce7e | 127 | //} |
BlueShadow | 0:37336794ce7e | 128 | |
BlueShadow | 0:37336794ce7e | 129 | PubSubClient mqtt(_host, _port, callback); |
BlueShadow | 0:37336794ce7e | 130 | |
BlueShadow | 0:37336794ce7e | 131 | char buf[128]; |
BlueShadow | 0:37336794ce7e | 132 | |
BlueShadow | 0:37336794ce7e | 133 | static X_CUBE_MEMS *mems_expansion_board = X_CUBE_MEMS::Instance(); |
BlueShadow | 0:37336794ce7e | 134 | |
BlueShadow | 0:37336794ce7e | 135 | |
BlueShadow | 0:37336794ce7e | 136 | while (true) { |
BlueShadow | 0:37336794ce7e | 137 | if (! mqtt.connect(_AERCLOUD_DEVICE_ID, _AERCLOUD_ACCOUNT_ID, _AERCLOUD_API_KEY)) { |
BlueShadow | 0:37336794ce7e | 138 | logError("failed to connect to AerCloud Server"); |
BlueShadow | 0:37336794ce7e | 139 | wait(1); |
BlueShadow | 0:37336794ce7e | 140 | continue; |
BlueShadow | 0:37336794ce7e | 141 | } |
BlueShadow | 0:37336794ce7e | 142 | |
BlueShadow | 0:37336794ce7e | 143 | |
BlueShadow | 0:37336794ce7e | 144 | /* Get instantaneous data from all sensors */ |
BlueShadow | 0:37336794ce7e | 145 | mems_expansion_board->hts221.GetTemperature((float *)&TEMPERATURE_Value_C); |
BlueShadow | 0:37336794ce7e | 146 | mems_expansion_board->hts221.GetHumidity((float *)&HUMIDITY_Value); |
BlueShadow | 0:37336794ce7e | 147 | mems_expansion_board->lps25h.GetPressure((float *)&PRESSURE_Value); |
BlueShadow | 0:37336794ce7e | 148 | mems_expansion_board->lis3mdl.GetAxes((AxesRaw_TypeDef *)&MAG_Value); |
BlueShadow | 0:37336794ce7e | 149 | mems_expansion_board->lsm6ds0.Acc_GetAxes((AxesRaw_TypeDef *)&ACC_Value); |
BlueShadow | 0:37336794ce7e | 150 | mems_expansion_board->lsm6ds0.Gyro_GetAxes((AxesRaw_TypeDef *)&GYR_Value); |
BlueShadow | 0:37336794ce7e | 151 | |
BlueShadow | 0:37336794ce7e | 152 | pc.printf("Temperature:\t\t %f C\r\n", TEMPERATURE_Value_C ); |
BlueShadow | 0:37336794ce7e | 153 | pc.printf("Humidity:\t\t %f%%\r\n", HUMIDITY_Value); |
BlueShadow | 0:37336794ce7e | 154 | pc.printf("Pressure:\t\t %f hPa\r\n", PRESSURE_Value); |
BlueShadow | 0:37336794ce7e | 155 | pc.printf("Magnetometer (mGauss):\t mX: %d, mY: %d, mZ: %d\r\n", MAG_Value.AXIS_X, MAG_Value.AXIS_Y, MAG_Value.AXIS_Z); |
BlueShadow | 0:37336794ce7e | 156 | 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); |
BlueShadow | 0:37336794ce7e | 157 | 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); |
BlueShadow | 0:37336794ce7e | 158 | pc.printf("\r\n"); |
BlueShadow | 0:37336794ce7e | 159 | |
BlueShadow | 0:37336794ce7e | 160 | // myled = 1; // LED is ON |
BlueShadow | 0:37336794ce7e | 161 | // wait(0.2); // 200 ms |
BlueShadow | 0:37336794ce7e | 162 | // myled = 0; // LED is OFF |
BlueShadow | 0:37336794ce7e | 163 | // wait(1.0); // 1 sec |
BlueShadow | 0:37336794ce7e | 164 | |
BlueShadow | 0:37336794ce7e | 165 | |
BlueShadow | 0:37336794ce7e | 166 | snprintf(buf, sizeof(buf), "{\"xxx\":%d,\"yyy\":%d,\"zzz\":%d,\"pressure\":%f,\"temperature\":%f,\"humidity\":%f,\"magX\":%d,\"magY\":%d,\"magZ\":%d,\"gyrX\":%d,\"gyrY\":%d,\"gyrZ\":%d}",ACC_Value.AXIS_X, ACC_Value.AXIS_Y, ACC_Value.AXIS_Z, PRESSURE_Value, TEMPERATURE_Value_C, HUMIDITY_Value, MAG_Value.AXIS_X, MAG_Value.AXIS_Y, MAG_Value.AXIS_Z, GYR_Value.AXIS_X, GYR_Value.AXIS_Y, GYR_Value.AXIS_Z); |
BlueShadow | 0:37336794ce7e | 167 | |
BlueShadow | 0:37336794ce7e | 168 | logInfo("publishing: [%s]\n\r", buf); |
BlueShadow | 0:37336794ce7e | 169 | if (! mqtt.publish(_AERCLOUD_CONTAINER, buf)) { |
BlueShadow | 0:37336794ce7e | 170 | logError("failed to publish: [%s]", buf); |
BlueShadow | 0:37336794ce7e | 171 | } |
BlueShadow | 0:37336794ce7e | 172 | wait(1); |
BlueShadow | 0:37336794ce7e | 173 | mqtt.loop(); |
BlueShadow | 0:37336794ce7e | 174 | mqtt.disconnect(); |
BlueShadow | 0:37336794ce7e | 175 | wait(DATA_INTERVAL); |
BlueShadow | 0:37336794ce7e | 176 | |
BlueShadow | 0:37336794ce7e | 177 | } |
BlueShadow | 0:37336794ce7e | 178 | |
BlueShadow | 0:37336794ce7e | 179 | } |
BlueShadow | 0:37336794ce7e | 180 |