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

main.cpp

Committer:
Vanger
Date:
2014-10-28
Revision:
2:643e3707d043
Parent:
1:76498fdfbecf
Child:
3:9736a8776102

File content as of revision 2:643e3707d043:

/* This is an example program which reads in the 3-axis acceleration, pressure, and temperature data from
 * a FRDM-FXS-MULTI sensor board. It then uses an MTSAS SocketModem Shield board to send the read data over
 * a cellular connection to the 2lemetry cloud using an MQTT client protocol.
 */

#include "mbed.h"
#include "mtsas.h"
#include "PubSubClient.h"
#include "FXLS8471Q.h"
#include "MPL3115A2.h"

/* PLEASE READ THIS!
 * 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
 * You must have a hacker (or higher) account at http://app.thingfabric.com
 * After you register and login, follow the steps below to set up your client
 * Click on "Default ThingFabric Project"
 *      Set _2LEMETRY_DOMAIN to the string after "Project" at the top of the page
 * Click on "Credentials"
 * Click on "Default ThingFabric Credential"
 *      Set _2LEMETRY_USER_ID to the value in the "Key (Username)" field
 *      Set _2LEMETRY_TOKEN to the value in the "MD5 Secret" field
 * If you are running this code on multiple devices, you will want to make the _2LEMETRY_DEVICE_ID field unique for each device
 * Set the _APN field to the APN provided with your sim card
 * Build, flash, and run
 *      This code sends a random integer value approximately every 30 seconds
 * Click on "Default ThingFabric Project"
 * Click on "Analytics"
 * You should be able to see your test data (page needs to be refreshed periodically, it doesn't automatically refresh)
 */

char _2LEMETRY_USERID[] = "";
char _2LEMETRY_TOKEN[] = "";
char _2LEMETRY_DOMAIN[] = "";
char _2LEMETRY_STUFF[] = "mbed";
char _2LEMETRY_DEVICE_ID[] = "nucleo-0001";

char _APN[] = "";

char _host[] = "q.mq.tt";
int _port = 1883;

#define MPL3115A2_I2C_ADDRESS (0x60<<1)

#define DATA_INTERVAL 30

void callback(char* topic, char* payload, unsigned int len) {
    logInfo("topic: [%s]\r\npayload: [%s]", topic, payload);
}

int main() {
    MTSLog::setLogLevel(MTSLog::TRACE_LEVEL);
    
    // for Nucleo boards
    MTSSerialFlowControl io(D8, D2, D3, D6);
    io.baud(115200);
    
    Cellular* radio = CellularFactory::create(&io);
    if (! radio) {
        logFatal("failed to create Cellular object - exiting");
        return 1;
    }
    
    radio->configureSignals(D4,D7,RESET);
    Transport::setTransport(radio);
    
    while (radio->setApn(_APN) != MTS_SUCCESS) {
        logError("failed to set APN [%s]", _APN);
        wait(2);
    }
    
    while (! radio->connect()) {
        logError("failed to bring up PPP link");
        wait(2);
    }
    
    PubSubClient mqtt(_host, _port, callback);
    
    char topicStr[128];
    char buf[128];
    snprintf(topicStr, sizeof(topicStr), "%s/%s/%s", _2LEMETRY_DOMAIN, _2LEMETRY_STUFF, _2LEMETRY_DEVICE_ID);
    
    FXLS8471Q acc(D11, D12, D13, D10);
    MPL3115A2 alt(D14, D15, MPL3115A2_I2C_ADDRESS, D4, D3);
    alt.Barometric_Mode(); 
    
    float acc_data[3];
    float temperature, pressure;
    
    while (true) {
        if (! mqtt.connect(_2LEMETRY_DEVICE_ID, _2LEMETRY_USERID, _2LEMETRY_TOKEN)) {
            logError("failed to connect to 2lemetry server");
            continue;
        }
        
        acc.ReadXYZ(acc_data);
        
        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());
        logInfo("publishing: [%s]", buf);
        if (! mqtt.publish(topicStr, buf)) {
            logError("failed to publish: [%s]", buf);
        }
        wait(1);
        mqtt.loop();
        mqtt.disconnect();
        wait(DATA_INTERVAL);
    }
    
}