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 Multi-Hackers

Committer:
Vanger
Date:
Tue Oct 28 19:09:28 2014 +0000
Revision:
3:9736a8776102
Parent:
2:643e3707d043
Child:
4:81c6b9d73cb1
Removed unneeded variable line

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
mfiore 0:b8d93d878bcb 53 // for Nucleo boards
mfiore 0:b8d93d878bcb 54 MTSSerialFlowControl io(D8, D2, D3, D6);
mfiore 0:b8d93d878bcb 55 io.baud(115200);
mfiore 0:b8d93d878bcb 56
mfiore 0:b8d93d878bcb 57 Cellular* radio = CellularFactory::create(&io);
mfiore 0:b8d93d878bcb 58 if (! radio) {
mfiore 0:b8d93d878bcb 59 logFatal("failed to create Cellular object - exiting");
mfiore 0:b8d93d878bcb 60 return 1;
mfiore 0:b8d93d878bcb 61 }
mfiore 0:b8d93d878bcb 62
mfiore 0:b8d93d878bcb 63 radio->configureSignals(D4,D7,RESET);
mfiore 0:b8d93d878bcb 64 Transport::setTransport(radio);
mfiore 0:b8d93d878bcb 65
mfiore 0:b8d93d878bcb 66 while (radio->setApn(_APN) != MTS_SUCCESS) {
mfiore 0:b8d93d878bcb 67 logError("failed to set APN [%s]", _APN);
mfiore 0:b8d93d878bcb 68 wait(2);
mfiore 0:b8d93d878bcb 69 }
mfiore 0:b8d93d878bcb 70
mfiore 0:b8d93d878bcb 71 while (! radio->connect()) {
mfiore 0:b8d93d878bcb 72 logError("failed to bring up PPP link");
mfiore 0:b8d93d878bcb 73 wait(2);
mfiore 0:b8d93d878bcb 74 }
mfiore 0:b8d93d878bcb 75
mfiore 0:b8d93d878bcb 76 PubSubClient mqtt(_host, _port, callback);
mfiore 0:b8d93d878bcb 77
mfiore 0:b8d93d878bcb 78 char topicStr[128];
Vanger 1:76498fdfbecf 79 char buf[128];
mfiore 0:b8d93d878bcb 80 snprintf(topicStr, sizeof(topicStr), "%s/%s/%s", _2LEMETRY_DOMAIN, _2LEMETRY_STUFF, _2LEMETRY_DEVICE_ID);
mfiore 0:b8d93d878bcb 81
Vanger 1:76498fdfbecf 82 FXLS8471Q acc(D11, D12, D13, D10);
Vanger 1:76498fdfbecf 83 MPL3115A2 alt(D14, D15, MPL3115A2_I2C_ADDRESS, D4, D3);
Vanger 1:76498fdfbecf 84 alt.Barometric_Mode();
Vanger 1:76498fdfbecf 85
Vanger 1:76498fdfbecf 86 float acc_data[3];
Vanger 1:76498fdfbecf 87
mfiore 0:b8d93d878bcb 88 while (true) {
mfiore 0:b8d93d878bcb 89 if (! mqtt.connect(_2LEMETRY_DEVICE_ID, _2LEMETRY_USERID, _2LEMETRY_TOKEN)) {
mfiore 0:b8d93d878bcb 90 logError("failed to connect to 2lemetry server");
mfiore 0:b8d93d878bcb 91 continue;
mfiore 0:b8d93d878bcb 92 }
Vanger 1:76498fdfbecf 93
Vanger 1:76498fdfbecf 94 acc.ReadXYZ(acc_data);
Vanger 1:76498fdfbecf 95
Vanger 1:76498fdfbecf 96 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 97 logInfo("publishing: [%s]", buf);
mfiore 0:b8d93d878bcb 98 if (! mqtt.publish(topicStr, buf)) {
mfiore 0:b8d93d878bcb 99 logError("failed to publish: [%s]", buf);
mfiore 0:b8d93d878bcb 100 }
mfiore 0:b8d93d878bcb 101 wait(1);
mfiore 0:b8d93d878bcb 102 mqtt.loop();
mfiore 0:b8d93d878bcb 103 mqtt.disconnect();
Vanger 1:76498fdfbecf 104 wait(DATA_INTERVAL);
mfiore 0:b8d93d878bcb 105 }
mfiore 0:b8d93d878bcb 106
mfiore 0:b8d93d878bcb 107 }