Paul Jaeger / YYY_Dragonfly_ST_MEMS_2015-10

Dependencies:   MQTT Nucleo_Sensor_Shield mbed-src mtsas

Fork of YYY_Dragonfly_ST_MEMS by Paul Jaeger

Committer:
BlueShadow
Date:
Thu Jun 04 23:36:46 2015 +0000
Revision:
0:d87e01c5969f
Child:
1:0535928f65ca
first attempt to get st mems working on a dragonfly processor board.  Next is to get Aeris SIM card working.  Finally the data transfer MQTT or HTTPS

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