Example program that reads in sensor data and sends it to the 2lemetry cloud.

Dependencies:   FXLS8471Q MPL3115A2 MQTT mbed

Fork of 2lemetry by Mike Fiore

Committer:
Vanger
Date:
Wed Mar 25 18:17:35 2015 +0000
Revision:
7:fbc201a3fd48
Parent:
6:569f6ef58f0f
mtsas library updated

Who changed what in which revision?

UserRevisionLine numberNew 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
Vanger 1:76498fdfbecf 3 * a cellular connection to the 2lemetry cloud 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
mfiore 0:b8d93d878bcb 12 /* PLEASE READ THIS!
mfiore 0:b8d93d878bcb 13 * The following fields must be populated in order to properly send data to the "Default ThingFabric Project" in your 2lemetry account using the MQTT client
mfiore 0:b8d93d878bcb 14 * You must have a hacker (or higher) account at http://app.thingfabric.com
mfiore 0:b8d93d878bcb 15 * After you register and login, follow the steps below to set up your client
mfiore 0:b8d93d878bcb 16 * Click on "Default ThingFabric Project"
mfiore 0:b8d93d878bcb 17 * Set _2LEMETRY_DOMAIN to the string after "Project" at the top of the page
mfiore 0:b8d93d878bcb 18 * Click on "Credentials"
mfiore 0:b8d93d878bcb 19 * Click on "Default ThingFabric Credential"
mfiore 0:b8d93d878bcb 20 * Set _2LEMETRY_USER_ID to the value in the "Key (Username)" field
mfiore 0:b8d93d878bcb 21 * Set _2LEMETRY_TOKEN to the value in the "MD5 Secret" field
mfiore 0:b8d93d878bcb 22 * If you are running this code on multiple devices, you will want to make the _2LEMETRY_DEVICE_ID field unique for each device
mfiore 0:b8d93d878bcb 23 * Set the _APN field to the APN provided with your sim card
mfiore 0:b8d93d878bcb 24 * Build, flash, and run
mfiore 0:b8d93d878bcb 25 * This code sends a random integer value approximately every 30 seconds
mfiore 0:b8d93d878bcb 26 * Click on "Default ThingFabric Project"
mfiore 0:b8d93d878bcb 27 * Click on "Analytics"
mfiore 0:b8d93d878bcb 28 * You should be able to see your test data (page needs to be refreshed periodically, it doesn't automatically refresh)
mfiore 0:b8d93d878bcb 29 */
mfiore 0:b8d93d878bcb 30
Vanger 2:643e3707d043 31 char _2LEMETRY_USERID[] = "";
Vanger 2:643e3707d043 32 char _2LEMETRY_TOKEN[] = "";
Vanger 2:643e3707d043 33 char _2LEMETRY_DOMAIN[] = "";
Vanger 1:76498fdfbecf 34 char _2LEMETRY_STUFF[] = "mbed";
mfiore 0:b8d93d878bcb 35 char _2LEMETRY_DEVICE_ID[] = "nucleo-0001";
mfiore 0:b8d93d878bcb 36
Vanger 2:643e3707d043 37 char _APN[] = "";
mfiore 0:b8d93d878bcb 38
mfiore 0:b8d93d878bcb 39 char _host[] = "q.mq.tt";
mfiore 0:b8d93d878bcb 40 int _port = 1883;
mfiore 0:b8d93d878bcb 41
Vanger 1:76498fdfbecf 42 #define MPL3115A2_I2C_ADDRESS (0x60<<1)
Vanger 1:76498fdfbecf 43
Vanger 1:76498fdfbecf 44 #define DATA_INTERVAL 30
Vanger 1:76498fdfbecf 45
mfiore 0:b8d93d878bcb 46 void callback(char* topic, char* payload, unsigned int len) {
mfiore 0:b8d93d878bcb 47 logInfo("topic: [%s]\r\npayload: [%s]", topic, payload);
mfiore 0:b8d93d878bcb 48 }
mfiore 0:b8d93d878bcb 49
mfiore 0:b8d93d878bcb 50 int main() {
mfiore 0:b8d93d878bcb 51 MTSLog::setLogLevel(MTSLog::TRACE_LEVEL);
mfiore 0:b8d93d878bcb 52
Vanger 6:569f6ef58f0f 53 /** STMicro Nucelo F401RE
Vanger 6:569f6ef58f0f 54 * The supported jumper configurations of the MTSAS do not line up with
Vanger 6:569f6ef58f0f 55 * the pin mapping of the Nucleo F401RE. Therefore, the MTSAS serial TX
Vanger 6:569f6ef58f0f 56 * pin (JP8 Pin 2) must be manually jumped to Serial1 RX (Shield pin D2)
Vanger 6:569f6ef58f0f 57 * and the MTSAS serial RX pin (JP9 Pin 2) pin must be manually jumped to
Vanger 6:569f6ef58f0f 58 * Serial1 TX (Shield pin D8).
Vanger 6:569f6ef58f0f 59 * Uncomment the following line to use the STMicro Nuceleo F401RE
Vanger 6:569f6ef58f0f 60 */
mfiore 0:b8d93d878bcb 61 MTSSerialFlowControl io(D8, D2, D3, D6);
Vanger 6:569f6ef58f0f 62
Vanger 6:569f6ef58f0f 63 /** Freescale KL46Z
Vanger 6:569f6ef58f0f 64 * To configure the serial pins for the Freescale KL46Z board, use MTSAS jumper
Vanger 6:569f6ef58f0f 65 * configuration B. Uncomment the following line to use the Freescale KL46Z board
Vanger 6:569f6ef58f0f 66 */
Vanger 6:569f6ef58f0f 67 //MTSSerialFlowControl io(D2, D9, D3, D6);
Vanger 6:569f6ef58f0f 68
Vanger 6:569f6ef58f0f 69 /** Freescale K64F
Vanger 6:569f6ef58f0f 70 * To configure the serial pins for the Freescale K64F board, use MTSAS jumper
Vanger 6:569f6ef58f0f 71 * configuration A. Uncomment the following line to use the Freescale K64F board
Vanger 6:569f6ef58f0f 72 */
Vanger 6:569f6ef58f0f 73 //MTSSerialFlowControl io(D1, D0, D3, D6);
Vanger 6:569f6ef58f0f 74
Vanger 6:569f6ef58f0f 75 //Set baudrate between the board and the radio to 115200
mfiore 0:b8d93d878bcb 76 io.baud(115200);
mfiore 0:b8d93d878bcb 77
mfiore 0:b8d93d878bcb 78 Cellular* radio = CellularFactory::create(&io);
mfiore 0:b8d93d878bcb 79 if (! radio) {
mfiore 0:b8d93d878bcb 80 logFatal("failed to create Cellular object - exiting");
mfiore 0:b8d93d878bcb 81 return 1;
mfiore 0:b8d93d878bcb 82 }
mfiore 0:b8d93d878bcb 83
mfiore 0:b8d93d878bcb 84 Transport::setTransport(radio);
mfiore 0:b8d93d878bcb 85
mfiore 0:b8d93d878bcb 86 while (radio->setApn(_APN) != MTS_SUCCESS) {
mfiore 0:b8d93d878bcb 87 logError("failed to set APN [%s]", _APN);
mfiore 0:b8d93d878bcb 88 wait(2);
mfiore 0:b8d93d878bcb 89 }
mfiore 0:b8d93d878bcb 90
mfiore 0:b8d93d878bcb 91 while (! radio->connect()) {
mfiore 0:b8d93d878bcb 92 logError("failed to bring up PPP link");
mfiore 0:b8d93d878bcb 93 wait(2);
mfiore 0:b8d93d878bcb 94 }
mfiore 0:b8d93d878bcb 95
mfiore 0:b8d93d878bcb 96 PubSubClient mqtt(_host, _port, callback);
mfiore 0:b8d93d878bcb 97
mfiore 0:b8d93d878bcb 98 char topicStr[128];
Vanger 1:76498fdfbecf 99 char buf[128];
mfiore 0:b8d93d878bcb 100 snprintf(topicStr, sizeof(topicStr), "%s/%s/%s", _2LEMETRY_DOMAIN, _2LEMETRY_STUFF, _2LEMETRY_DEVICE_ID);
mfiore 0:b8d93d878bcb 101
Vanger 1:76498fdfbecf 102 FXLS8471Q acc(D11, D12, D13, D10);
Vanger 1:76498fdfbecf 103 MPL3115A2 alt(D14, D15, MPL3115A2_I2C_ADDRESS, D4, D3);
Vanger 1:76498fdfbecf 104 alt.Barometric_Mode();
Vanger 1:76498fdfbecf 105
Vanger 1:76498fdfbecf 106 float acc_data[3];
Vanger 1:76498fdfbecf 107
mfiore 0:b8d93d878bcb 108 while (true) {
mfiore 0:b8d93d878bcb 109 if (! mqtt.connect(_2LEMETRY_DEVICE_ID, _2LEMETRY_USERID, _2LEMETRY_TOKEN)) {
mfiore 0:b8d93d878bcb 110 logError("failed to connect to 2lemetry server");
mfiore 0:b8d93d878bcb 111 continue;
mfiore 0:b8d93d878bcb 112 }
Vanger 1:76498fdfbecf 113
Vanger 1:76498fdfbecf 114 acc.ReadXYZ(acc_data);
Vanger 1:76498fdfbecf 115
Vanger 1:76498fdfbecf 116 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 117 logInfo("publishing: [%s]", buf);
mfiore 0:b8d93d878bcb 118 if (! mqtt.publish(topicStr, buf)) {
mfiore 0:b8d93d878bcb 119 logError("failed to publish: [%s]", buf);
mfiore 0:b8d93d878bcb 120 }
mfiore 0:b8d93d878bcb 121 wait(1);
mfiore 0:b8d93d878bcb 122 mqtt.loop();
mfiore 0:b8d93d878bcb 123 mqtt.disconnect();
Vanger 1:76498fdfbecf 124 wait(DATA_INTERVAL);
mfiore 0:b8d93d878bcb 125 }
mfiore 0:b8d93d878bcb 126
mfiore 0:b8d93d878bcb 127 }