Initial attempt to get ST MEMS working on the device.

Dependencies:   MQTT Nucleo_Sensor_Shield mbed-src mtsas

Fork of YYY_Dragonfly_ST_MEMS by Paul Jaeger

Committer:
BlueShadow
Date:
Tue Oct 20 00:37:18 2015 +0000
Revision:
2:26b2e25d323a
Parent:
1:0535928f65ca
Latest mbed source and updated location for mtsas library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
BlueShadow 0:d87e01c5969f 1 /* All modem code Aeris code and have been removed to get the ST Mems sensor board to function initially.
BlueShadow 0:d87e01c5969f 2 * This was done to see if anything was working at all. Currently the sensors are read every 33 seconds.
BlueShadow 0:d87e01c5969f 3 * This can be changed to a higher speed to capture more interesting events.
BlueShadow 0:d87e01c5969f 4 */
BlueShadow 0:d87e01c5969f 5
BlueShadow 0:d87e01c5969f 6 /* This is an example program which reads in the 3-axis acceleration, pressure, and temperature data from
BlueShadow 1:0535928f65ca 7 * a sensor board. It then uses the Dragonfly board to send the read data over a cellular connection to Aeris AerCloud using an MQTT client protocol.
BlueShadow 0:d87e01c5969f 8 */
BlueShadow 0:d87e01c5969f 9
BlueShadow 0:d87e01c5969f 10 #include "mbed.h"
BlueShadow 0:d87e01c5969f 11 #include "mtsas.h"
BlueShadow 0:d87e01c5969f 12 #include "PubSubClient.h"
BlueShadow 0:d87e01c5969f 13 #include "x_cube_mems.h"
BlueShadow 0:d87e01c5969f 14
BlueShadow 0:d87e01c5969f 15 //
BlueShadow 0:d87e01c5969f 16 // PLEASE READ THIS!
BlueShadow 0:d87e01c5969f 17 //
BlueShadow 0:d87e01c5969f 18 // Example was created for the following hardware:
BlueShadow 0:d87e01c5969f 19 // ST Sensor Board http://developer.mbed.org/teams/ST-Americas-mbed-Team/wiki/Getting-Started-with-Nucleo-Sensors
BlueShadow 0:d87e01c5969f 20 // MultiTech Dragonfly:
BlueShadow 0:d87e01c5969f 21 //
BlueShadow 0:d87e01c5969f 22 // To get the sample running, you'll need to fill in the following parameters below
BlueShadow 0:d87e01c5969f 23 // Your cellular provider's APN: _APN
BlueShadow 0:d87e01c5969f 24 // AerCloud API Key: _AERCLOUD_API_APIKEY
BlueShadow 0:d87e01c5969f 25 // AerCloud Account ID: _AERCLOUD_ACCOUNT_ID
BlueShadow 0:d87e01c5969f 26 // AerCloud Container: _AERCLOUD_CONTAINER
BlueShadow 0:d87e01c5969f 27 // AerCloud Subscription: _AERCLOUD_SUBSCRIPTION_ID[]
BlueShadow 0:d87e01c5969f 28 //
BlueShadow 0:d87e01c5969f 29 // The AerCloud container needa a Data Model with the following schema:
BlueShadow 0:d87e01c5969f 30 // accel_X "xxx" String
BlueShadow 0:d87e01c5969f 31 // accel_y "yyy" String
BlueShadow 0:d87e01c5969f 32 // accel-Z "zzz" String
BlueShadow 0:d87e01c5969f 33 // "pressure" FLOAT
BlueShadow 0:d87e01c5969f 34 // "temperature" FLOAT
BlueShadow 0:d87e01c5969f 35 // "humidity" FLOAT
BlueShadow 0:d87e01c5969f 36 // Magnitometer_X "magX" String
BlueShadow 0:d87e01c5969f 37 // Magnitometer_Y "magY" String
BlueShadow 0:d87e01c5969f 38 // Magnitometer_Z "magZ" String
BlueShadow 0:d87e01c5969f 39 // Gyro_X "gyrX" String
BlueShadow 0:d87e01c5969f 40 // Gyro_Y "gyrY" String
BlueShadow 0:d87e01c5969f 41 // Gyro_Z "gyrZ" String
BlueShadow 0:d87e01c5969f 42 //
BlueShadow 0:d87e01c5969f 43 // To check if data has made it in the container, create a long polling subscription in AerPort
BlueShadow 0:d87e01c5969f 44 //
BlueShadow 0:d87e01c5969f 45 // http://longpoll.aercloud.aeris.com/v1/1/containers/subscriptions/<subscrption_name>/notificationChannels/longPoll?apiKey=<api_key>
BlueShadow 0:d87e01c5969f 46 //
BlueShadow 0:d87e01c5969f 47 // You should see the something that looks like this in the browser:
BlueShadow 0:d87e01c5969f 48 // {"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:d87e01c5969f 49 //
BlueShadow 0:d87e01c5969f 50
BlueShadow 0:d87e01c5969f 51
BlueShadow 0:d87e01c5969f 52
BlueShadow 0:d87e01c5969f 53 // const char _APN[] = "aer.aerisapn.net"; // Cellular provider APN address for data services
BlueShadow 0:d87e01c5969f 54 // const char _APN[] = "aer05.aerisapn.net";
BlueShadow 0:d87e01c5969f 55
BlueShadow 1:0535928f65ca 56 //char _AERCLOUD_API_KEY[] ="_Click_On_KEY_AerCloud_Tab";
BlueShadow 1:0535928f65ca 57 //char _AERCLOUD_ACCOUNT_ID[] = "_Company_Number_Top_of_Page"; // account ID.
BlueShadow 1:0535928f65ca 58 //char _AERCLOUD_CONTAINER[] = "_From_Container_Page"; // new container name
BlueShadow 1:0535928f65ca 59 //char _AERCLOUD_DEVICE_ID[] = "_Random_Title"; // title with unique identifier, i.e. last 4 digits of IMEI
BlueShadow 1:0535928f65ca 60 //char _AERCLOUD_SUBSCRIPTION_ID[] = "_Random_Subscription_ID"; // created at Continer Name then Subscription ID for LongPoll
BlueShadow 1:0535928f65ca 61 //
BlueShadow 1:0535928f65ca 62 //char _host[] = "mqtt.aercloud.aeris.com";
BlueShadow 1:0535928f65ca 63 //int _port = 1883;
BlueShadow 0:d87e01c5969f 64
BlueShadow 0:d87e01c5969f 65
BlueShadow 2:26b2e25d323a 66 #define DATA_INTERVAL 3 // delay between samples, could be reduced for quicker response and more data
BlueShadow 0:d87e01c5969f 67
BlueShadow 1:0535928f65ca 68 //void callback(char* topic, char* payload, unsigned int len)
BlueShadow 1:0535928f65ca 69 //{
BlueShadow 1:0535928f65ca 70 // logInfo("topic: [%s]\r\npayload: [%s]", topic, payload);
BlueShadow 1:0535928f65ca 71 //}
BlueShadow 1:0535928f65ca 72 //
BlueShadow 0:d87e01c5969f 73 DigitalOut myled(PA_0); // D3
BlueShadow 0:d87e01c5969f 74 //Serial pc(PA_2, PA_3); // D1-PA_2-SERIAL_TX, D0-PA_3-SERIAL_RX
BlueShadow 0:d87e01c5969f 75 volatile float TEMPERATURE_Value_C;
BlueShadow 0:d87e01c5969f 76 volatile float TEMPERATURE_Value_F;
BlueShadow 0:d87e01c5969f 77 volatile float PRESSURE_Value;
BlueShadow 0:d87e01c5969f 78 volatile AxesRaw_TypeDef ACC_Value;
BlueShadow 0:d87e01c5969f 79 //volatile float HUMIDITY_Value;
BlueShadow 2:26b2e25d323a 80 volatile AxesRaw_TypeDef MAG_Value;
BlueShadow 0:d87e01c5969f 81 //volatile AxesRaw_TypeDef GYR_Value;
BlueShadow 0:d87e01c5969f 82
BlueShadow 0:d87e01c5969f 83 int main()
BlueShadow 0:d87e01c5969f 84 {
BlueShadow 1:0535928f65ca 85 printf("\r\n\r\nHello!\r\n\r\n");
BlueShadow 0:d87e01c5969f 86 MTSLog::setLogLevel(MTSLog::TRACE_LEVEL);
BlueShadow 0:d87e01c5969f 87
BlueShadow 0:d87e01c5969f 88 // for Nucleo boards
BlueShadow 0:d87e01c5969f 89 // MTSSerialFlowControl io(RADIO_TX, RADIO_RX, RADIO_RTS, RADIO_CTS); // Tx, Rx, RTS, CTS
BlueShadow 0:d87e01c5969f 90 // io.baud(115200);
BlueShadow 0:d87e01c5969f 91
BlueShadow 0:d87e01c5969f 92 // Cellular* radio = CellularFactory::create(&io);
BlueShadow 0:d87e01c5969f 93 // if (! radio) {
BlueShadow 0:d87e01c5969f 94 // logFatal("failed to create Cellular object - exiting");
BlueShadow 0:d87e01c5969f 95 // return 1;
BlueShadow 0:d87e01c5969f 96 // }
BlueShadow 0:d87e01c5969f 97
BlueShadow 0:d87e01c5969f 98 // radio->configureSignals(RADIO_DCD,RADIO_DTR,RESET); // DCD:JP10-4 to D11:X4-4, DTR:JP10-10 to D7:JP10-9 (Modified Flow Control for ST Sensor Board)
BlueShadow 0:d87e01c5969f 99 // Transport::setTransport(radio); // Required to control Cell Radios vs WiFi solutions
BlueShadow 0:d87e01c5969f 100
BlueShadow 0:d87e01c5969f 101 // while (radio->setApn(_APN) != MTS_SUCCESS) {
BlueShadow 0:d87e01c5969f 102 // logError("failed to set APN [%s]", _APN);
BlueShadow 0:d87e01c5969f 103 // wait(2);
BlueShadow 0:d87e01c5969f 104 // }
BlueShadow 0:d87e01c5969f 105
BlueShadow 0:d87e01c5969f 106 // while (! radio->connect()) {
BlueShadow 0:d87e01c5969f 107 // logError("failed to bring up PPP link");
BlueShadow 0:d87e01c5969f 108 // wait(2);
BlueShadow 0:d87e01c5969f 109 // }
BlueShadow 0:d87e01c5969f 110
BlueShadow 0:d87e01c5969f 111 // printf("Signal Strength: %d\n\r", radio->getSignalStrength()); //Check the signal strength should be above 8
BlueShadow 0:d87e01c5969f 112
BlueShadow 2:26b2e25d323a 113 // printf("data is stored at:\n\r\r");
BlueShadow 2:26b2e25d323a 114 // 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 2:26b2e25d323a 115 // printf(" \n\r \n\r");
BlueShadow 0:d87e01c5969f 116
BlueShadow 0:d87e01c5969f 117 // If you suspect a connectivity issue, uncomment the code below and if ping works. If you are not getting a
BlueShadow 0:d87e01c5969f 118 // valid ping, there's a connectivity problem. First step is to verify you've got the right APN set
BlueShadow 0:d87e01c5969f 119 //
BlueShadow 0:d87e01c5969f 120 // Try pinging default server "8.8.8.8" (Google's DNS)
BlueShadow 0:d87e01c5969f 121 // int ping_valid = 0;
BlueShadow 0:d87e01c5969f 122 // while (ping_valid == 0) {
BlueShadow 0:d87e01c5969f 123 // ping_valid = radio->ping();
BlueShadow 0:d87e01c5969f 124 // printf("Ping Valid: %s\n\r", ping_valid ? "true" : "false");
BlueShadow 0:d87e01c5969f 125 // //
BlueShadow 0:d87e01c5969f 126 // if (ping_valid == 0) {
BlueShadow 0:d87e01c5969f 127 // wait(3);
BlueShadow 0:d87e01c5969f 128 // printf("wait 33");
BlueShadow 0:d87e01c5969f 129 // wait(33);
BlueShadow 0:d87e01c5969f 130 //
BlueShadow 0:d87e01c5969f 131 // }
BlueShadow 0:d87e01c5969f 132 // }
BlueShadow 0:d87e01c5969f 133
BlueShadow 0:d87e01c5969f 134 // PubSubClient mqtt(_host, _port, callback);
BlueShadow 0:d87e01c5969f 135
BlueShadow 0:d87e01c5969f 136 // char buf[128];
BlueShadow 0:d87e01c5969f 137
BlueShadow 0:d87e01c5969f 138 static X_CUBE_MEMS *mems_expansion_board = X_CUBE_MEMS::Instance();
BlueShadow 0:d87e01c5969f 139
BlueShadow 0:d87e01c5969f 140
BlueShadow 0:d87e01c5969f 141 /* while (true) {
BlueShadow 0:d87e01c5969f 142 if (! mqtt.connect(_AERCLOUD_DEVICE_ID, _AERCLOUD_ACCOUNT_ID, _AERCLOUD_API_KEY)) {
BlueShadow 0:d87e01c5969f 143 logError("failed to connect to AerCloud Server");
BlueShadow 0:d87e01c5969f 144 wait(1);
BlueShadow 0:d87e01c5969f 145 continue;
BlueShadow 0:d87e01c5969f 146 }
BlueShadow 0:d87e01c5969f 147 */
BlueShadow 0:d87e01c5969f 148
BlueShadow 0:d87e01c5969f 149 for (int i = 0; i < 101 ; i++) {
BlueShadow 0:d87e01c5969f 150 /* Get instantaneous data from half of the sensors */
BlueShadow 0:d87e01c5969f 151 mems_expansion_board->hts221.GetTemperature((float *)&TEMPERATURE_Value_C);
BlueShadow 0:d87e01c5969f 152 mems_expansion_board->lps25h.GetPressure((float *)&PRESSURE_Value);
BlueShadow 0:d87e01c5969f 153 mems_expansion_board->lsm6ds0.Acc_GetAxes((AxesRaw_TypeDef *)&ACC_Value);
BlueShadow 0:d87e01c5969f 154 // mems_expansion_board->hts221.GetHumidity((float *)&HUMIDITY_Value);
BlueShadow 2:26b2e25d323a 155 mems_expansion_board->lis3mdl.GetAxes((AxesRaw_TypeDef *)&MAG_Value);
BlueShadow 0:d87e01c5969f 156 // mems_expansion_board->lsm6ds0.Gyro_GetAxes((AxesRaw_TypeDef *)&GYR_Value);
BlueShadow 0:d87e01c5969f 157
BlueShadow 0:d87e01c5969f 158
BlueShadow 0:d87e01c5969f 159
BlueShadow 0:d87e01c5969f 160 printf("Temperature:\t\t %f C\r\n", TEMPERATURE_Value_C );
BlueShadow 0:d87e01c5969f 161 printf("Pressure:\t\t %f hPa\r\n", PRESSURE_Value);
BlueShadow 0:d87e01c5969f 162 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 1:0535928f65ca 163 // printf("Humidity:\t\t %f%%\r\n", HUMIDITY_Value);
BlueShadow 2:26b2e25d323a 164 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 1:0535928f65ca 165 // 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:d87e01c5969f 166 printf("\r\n");
BlueShadow 0:d87e01c5969f 167
BlueShadow 0:d87e01c5969f 168 myled = 1; // LED is ON
BlueShadow 0:d87e01c5969f 169 wait(0.2); // 200 ms
BlueShadow 0:d87e01c5969f 170 myled = 0; // LED is OFF
BlueShadow 0:d87e01c5969f 171 wait(1.0); // 1 sec
BlueShadow 0:d87e01c5969f 172
BlueShadow 0:d87e01c5969f 173
BlueShadow 0:d87e01c5969f 174 // 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:d87e01c5969f 175 /* snprintf(buf, sizeof(buf), "{\"xxx\":%d,\"yyy\":%d,\"zzz\":%d,\"pressure\":%f,\"temperature\":%f}",ACC_Value.AXIS_X, ACC_Value.AXIS_Y, ACC_Value.AXIS_Z, PRESSURE_Value, TEMPERATURE_Value_C);
BlueShadow 0:d87e01c5969f 176 * logInfo("publishing: [%s]\n\r", buf);
BlueShadow 0:d87e01c5969f 177 * if (! mqtt.publish(_AERCLOUD_CONTAINER, buf)) {
BlueShadow 0:d87e01c5969f 178 * logError("failed to publish: [%s]", buf);
BlueShadow 0:d87e01c5969f 179 * }
BlueShadow 0:d87e01c5969f 180
BlueShadow 0:d87e01c5969f 181 * mqtt.loop();
BlueShadow 0:d87e01c5969f 182 * mqtt.disconnect();
BlueShadow 0:d87e01c5969f 183 */
BlueShadow 0:d87e01c5969f 184 wait(DATA_INTERVAL);
BlueShadow 0:d87e01c5969f 185
BlueShadow 0:d87e01c5969f 186 }
BlueShadow 0:d87e01c5969f 187 }