![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Sample code for using Aeris AerCloud with MultiTech Socket Modem
Dependencies: FXLS8471Q MPL3115A2 MQTT mbed
Dependents: MultiTech_AerCloud_ST-sensor_Modem
Fork of 2lemetry_Sensor_Example by
main.cpp@4:81c6b9d73cb1, 2014-12-12 (annotated)
- Committer:
- mchowla
- Date:
- Fri Dec 12 22:26:35 2014 +0000
- Revision:
- 4:81c6b9d73cb1
- Parent:
- 3:9736a8776102
Initial Version of MultiTech Socket Modem Example
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Vanger | 1:76498fdfbecf | 1 | /* This is an example program which reads in the 3-axis acceleration, pressure, and temperature data from |
Vanger | 1:76498fdfbecf | 2 | * a FRDM-FXS-MULTI sensor board. It then uses an MTSAS SocketModem Shield board to send the read data over |
mchowla | 4:81c6b9d73cb1 | 3 | * a cellular connection to Aeris AerCloud using an MQTT client protocol. |
Vanger | 1:76498fdfbecf | 4 | */ |
Vanger | 1:76498fdfbecf | 5 | |
mfiore | 0:b8d93d878bcb | 6 | #include "mbed.h" |
mfiore | 0:b8d93d878bcb | 7 | #include "mtsas.h" |
mfiore | 0:b8d93d878bcb | 8 | #include "PubSubClient.h" |
Vanger | 1:76498fdfbecf | 9 | #include "FXLS8471Q.h" |
Vanger | 1:76498fdfbecf | 10 | #include "MPL3115A2.h" |
mfiore | 0:b8d93d878bcb | 11 | |
mchowla | 4:81c6b9d73cb1 | 12 | // |
mchowla | 4:81c6b9d73cb1 | 13 | // PLEASE READ THIS! |
mchowla | 4:81c6b9d73cb1 | 14 | // |
mchowla | 4:81c6b9d73cb1 | 15 | // Example was created for the following hardware: |
mchowla | 4:81c6b9d73cb1 | 16 | // ST Nucleo F401RE http://developer.mbed.org/platforms/ST-Nucleo-F401RE/ |
mchowla | 4:81c6b9d73cb1 | 17 | // Freescale Multi-Sensor Shield https://developer.mbed.org/components/Freescale-Multi-Sensor-Shield/ |
mchowla | 4:81c6b9d73cb1 | 18 | // MultiTech Socket Modem Shield: http://developer.mbed.org/components/Multi-Tech-SocketModem-Arduino-Shield-MT/ |
mchowla | 4:81c6b9d73cb1 | 19 | // MultiTech MTSMC-H5 GSM Socket Modem http://www.multitech.com/models/92503252LF#gsc.tab=0 |
mchowla | 4:81c6b9d73cb1 | 20 | // |
mchowla | 4:81c6b9d73cb1 | 21 | // To get the sample running, you'll need to fill in the following parameters below |
mchowla | 4:81c6b9d73cb1 | 22 | // Your cellular provider's APN: _APN |
mchowla | 4:81c6b9d73cb1 | 23 | // AerCloud API Key: _AERCLOUD_API_APIKEY |
mchowla | 4:81c6b9d73cb1 | 24 | // AerCloud Account ID: _AERCLOUD_ACCOUNT_ID |
mchowla | 4:81c6b9d73cb1 | 25 | // AerCloud Container: _ACERCLOUD_CONTAINER |
mchowla | 4:81c6b9d73cb1 | 26 | // |
mchowla | 4:81c6b9d73cb1 | 27 | // The AerCloud container needa a Data Model with the following schema: |
mchowla | 4:81c6b9d73cb1 | 28 | // accel_x FLOAT |
mchowla | 4:81c6b9d73cb1 | 29 | // accel_y FLOAT |
mchowla | 4:81c6b9d73cb1 | 30 | // accel-Z FLOAT |
mchowla | 4:81c6b9d73cb1 | 31 | // pressure FLOAT |
mchowla | 4:81c6b9d73cb1 | 32 | // temperature FLOAT |
mchowla | 4:81c6b9d73cb1 | 33 | // |
mchowla | 4:81c6b9d73cb1 | 34 | // 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 |
mchowla | 4:81c6b9d73cb1 | 35 | // |
mchowla | 4:81c6b9d73cb1 | 36 | // http://longpoll.aercloud.aeris.com/v1/1/containers/subscriptions/<subscrption_name>/notificationChannels/longPoll?apiKey=<api_key> |
mchowla | 4:81c6b9d73cb1 | 37 | // |
mchowla | 4:81c6b9d73cb1 | 38 | // You should see the something that looks like this in the browser: |
mchowla | 4:81c6b9d73cb1 | 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}"}}}, |
mchowla | 4:81c6b9d73cb1 | 40 | // |
mchowla | 4:81c6b9d73cb1 | 41 | // |
mfiore | 0:b8d93d878bcb | 42 | |
mchowla | 4:81c6b9d73cb1 | 43 | char _AERCLOUD_API_KEY[] = "SET_YOUR_API_KEY"; |
mchowla | 4:81c6b9d73cb1 | 44 | char _AERCLOUD_ACCOUNT_ID[] = "SET_YOUR_ACCOUNT_NUMBER"; |
mchowla | 4:81c6b9d73cb1 | 45 | char _AERCLOUD_CONTAINER[] = "Nucleo_Test"; |
mchowla | 4:81c6b9d73cb1 | 46 | char _AERCLOUD_DEVICE_ID[] = "nucleo-0001"; |
mfiore | 0:b8d93d878bcb | 47 | |
mchowla | 4:81c6b9d73cb1 | 48 | char _APN[] = "SET_YOUR_CELLULAR_PROVIDERS_APN" "; |
mfiore | 0:b8d93d878bcb | 49 | |
mchowla | 4:81c6b9d73cb1 | 50 | char _host[] = "mqtt.aercloud.aeris.com"; |
mfiore | 0:b8d93d878bcb | 51 | int _port = 1883; |
mfiore | 0:b8d93d878bcb | 52 | |
Vanger | 1:76498fdfbecf | 53 | #define MPL3115A2_I2C_ADDRESS (0x60<<1) |
Vanger | 1:76498fdfbecf | 54 | |
Vanger | 1:76498fdfbecf | 55 | #define DATA_INTERVAL 30 |
Vanger | 1:76498fdfbecf | 56 | |
mfiore | 0:b8d93d878bcb | 57 | void callback(char* topic, char* payload, unsigned int len) { |
mfiore | 0:b8d93d878bcb | 58 | logInfo("topic: [%s]\r\npayload: [%s]", topic, payload); |
mfiore | 0:b8d93d878bcb | 59 | } |
mfiore | 0:b8d93d878bcb | 60 | |
mfiore | 0:b8d93d878bcb | 61 | int main() { |
mchowla | 4:81c6b9d73cb1 | 62 | printf("Hello World\r\n"); |
mfiore | 0:b8d93d878bcb | 63 | MTSLog::setLogLevel(MTSLog::TRACE_LEVEL); |
mfiore | 0:b8d93d878bcb | 64 | |
mfiore | 0:b8d93d878bcb | 65 | // for Nucleo boards |
mfiore | 0:b8d93d878bcb | 66 | MTSSerialFlowControl io(D8, D2, D3, D6); |
mfiore | 0:b8d93d878bcb | 67 | io.baud(115200); |
mfiore | 0:b8d93d878bcb | 68 | |
mfiore | 0:b8d93d878bcb | 69 | Cellular* radio = CellularFactory::create(&io); |
mfiore | 0:b8d93d878bcb | 70 | if (! radio) { |
mfiore | 0:b8d93d878bcb | 71 | logFatal("failed to create Cellular object - exiting"); |
mfiore | 0:b8d93d878bcb | 72 | return 1; |
mfiore | 0:b8d93d878bcb | 73 | } |
mfiore | 0:b8d93d878bcb | 74 | |
mfiore | 0:b8d93d878bcb | 75 | radio->configureSignals(D4,D7,RESET); |
mfiore | 0:b8d93d878bcb | 76 | Transport::setTransport(radio); |
mfiore | 0:b8d93d878bcb | 77 | |
mfiore | 0:b8d93d878bcb | 78 | while (radio->setApn(_APN) != MTS_SUCCESS) { |
mfiore | 0:b8d93d878bcb | 79 | logError("failed to set APN [%s]", _APN); |
mfiore | 0:b8d93d878bcb | 80 | wait(2); |
mfiore | 0:b8d93d878bcb | 81 | } |
mfiore | 0:b8d93d878bcb | 82 | |
mfiore | 0:b8d93d878bcb | 83 | while (! radio->connect()) { |
mfiore | 0:b8d93d878bcb | 84 | logError("failed to bring up PPP link"); |
mfiore | 0:b8d93d878bcb | 85 | wait(2); |
mfiore | 0:b8d93d878bcb | 86 | } |
mfiore | 0:b8d93d878bcb | 87 | |
mchowla | 4:81c6b9d73cb1 | 88 | printf("Signal Strength: %d\n\r", radio->getSignalStrength()); //Check the signal strength should be above 8 |
mchowla | 4:81c6b9d73cb1 | 89 | |
mchowla | 4:81c6b9d73cb1 | 90 | // If you suspect a connectivity issue, uncomment the code below and if ping works. If you are not getting a |
mchowla | 4:81c6b9d73cb1 | 91 | // valid ping, there's a connectivity problem. First step is to verify you've got the right APN set |
mchowla | 4:81c6b9d73cb1 | 92 | // |
mchowla | 4:81c6b9d73cb1 | 93 | // Try pinging default server "8.8.8.8" (Google's DNS) |
mchowla | 4:81c6b9d73cb1 | 94 | //int ping_valid = 0; |
mchowla | 4:81c6b9d73cb1 | 95 | //while (ping_valid == 0) { |
mchowla | 4:81c6b9d73cb1 | 96 | // ping_valid = radio->ping(); |
mchowla | 4:81c6b9d73cb1 | 97 | // printf("Ping Valid: %s\n\r", ping_valid ? "true" : "false"); |
mchowla | 4:81c6b9d73cb1 | 98 | // |
mchowla | 4:81c6b9d73cb1 | 99 | // if (ping_valid == 0) { |
mchowla | 4:81c6b9d73cb1 | 100 | // wait(3); |
mchowla | 4:81c6b9d73cb1 | 101 | // } |
mchowla | 4:81c6b9d73cb1 | 102 | //} |
mchowla | 4:81c6b9d73cb1 | 103 | |
mfiore | 0:b8d93d878bcb | 104 | PubSubClient mqtt(_host, _port, callback); |
mfiore | 0:b8d93d878bcb | 105 | |
Vanger | 1:76498fdfbecf | 106 | char buf[128]; |
mfiore | 0:b8d93d878bcb | 107 | |
Vanger | 1:76498fdfbecf | 108 | FXLS8471Q acc(D11, D12, D13, D10); |
Vanger | 1:76498fdfbecf | 109 | MPL3115A2 alt(D14, D15, MPL3115A2_I2C_ADDRESS, D4, D3); |
Vanger | 1:76498fdfbecf | 110 | alt.Barometric_Mode(); |
Vanger | 1:76498fdfbecf | 111 | |
Vanger | 1:76498fdfbecf | 112 | float acc_data[3]; |
Vanger | 1:76498fdfbecf | 113 | |
mfiore | 0:b8d93d878bcb | 114 | while (true) { |
mchowla | 4:81c6b9d73cb1 | 115 | if (! mqtt.connect(_AERCLOUD_DEVICE_ID, _AERCLOUD_ACCOUNT_ID, _AERCLOUD_API_KEY)) { |
mchowla | 4:81c6b9d73cb1 | 116 | logError("failed to connect to AerCloud Server"); |
mchowla | 4:81c6b9d73cb1 | 117 | wait(5); |
mfiore | 0:b8d93d878bcb | 118 | continue; |
mfiore | 0:b8d93d878bcb | 119 | } |
Vanger | 1:76498fdfbecf | 120 | |
Vanger | 1:76498fdfbecf | 121 | acc.ReadXYZ(acc_data); |
Vanger | 1:76498fdfbecf | 122 | |
Vanger | 1:76498fdfbecf | 123 | snprintf(buf, sizeof(buf), "{\"x\":%f,\"y\":%f,\"z\":%f,\"pressure\":%f,\"temperature\":%f}", acc_data[0],acc_data[1],acc_data[2], alt.getPressure(), alt.getTemperature()); |
mfiore | 0:b8d93d878bcb | 124 | logInfo("publishing: [%s]", buf); |
mchowla | 4:81c6b9d73cb1 | 125 | if (! mqtt.publish(_AERCLOUD_CONTAINER, buf)) { |
mfiore | 0:b8d93d878bcb | 126 | logError("failed to publish: [%s]", buf); |
mfiore | 0:b8d93d878bcb | 127 | } |
mfiore | 0:b8d93d878bcb | 128 | wait(1); |
mfiore | 0:b8d93d878bcb | 129 | mqtt.loop(); |
mfiore | 0:b8d93d878bcb | 130 | mqtt.disconnect(); |
Vanger | 1:76498fdfbecf | 131 | wait(DATA_INTERVAL); |
mfiore | 0:b8d93d878bcb | 132 | } |
mfiore | 0:b8d93d878bcb | 133 | |
mfiore | 0:b8d93d878bcb | 134 | } |