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:
mfiore
Date:
Tue Oct 28 17:14:45 2014 +0000
Revision:
0:b8d93d878bcb
Child:
1:76498fdfbecf
initial commit - working code once APN and _2LEMETRY_* fields are set up

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mfiore 0:b8d93d878bcb 1 #include "mbed.h"
mfiore 0:b8d93d878bcb 2 #include "mtsas.h"
mfiore 0:b8d93d878bcb 3 #include "PubSubClient.h"
mfiore 0:b8d93d878bcb 4
mfiore 0:b8d93d878bcb 5 /* PLEASE READ THIS!
mfiore 0:b8d93d878bcb 6 * 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 7 * You must have a hacker (or higher) account at http://app.thingfabric.com
mfiore 0:b8d93d878bcb 8 * After you register and login, follow the steps below to set up your client
mfiore 0:b8d93d878bcb 9 * Click on "Default ThingFabric Project"
mfiore 0:b8d93d878bcb 10 * Set _2LEMETRY_DOMAIN to the string after "Project" at the top of the page
mfiore 0:b8d93d878bcb 11 * Click on "Credentials"
mfiore 0:b8d93d878bcb 12 * Click on "Default ThingFabric Credential"
mfiore 0:b8d93d878bcb 13 * Set _2LEMETRY_USER_ID to the value in the "Key (Username)" field
mfiore 0:b8d93d878bcb 14 * Set _2LEMETRY_TOKEN to the value in the "MD5 Secret" field
mfiore 0:b8d93d878bcb 15 * 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 16 * Set the _APN field to the APN provided with your sim card
mfiore 0:b8d93d878bcb 17 * Build, flash, and run
mfiore 0:b8d93d878bcb 18 * This code sends a random integer value approximately every 30 seconds
mfiore 0:b8d93d878bcb 19 * Click on "Default ThingFabric Project"
mfiore 0:b8d93d878bcb 20 * Click on "Analytics"
mfiore 0:b8d93d878bcb 21 * You should be able to see your test data (page needs to be refreshed periodically, it doesn't automatically refresh)
mfiore 0:b8d93d878bcb 22 */
mfiore 0:b8d93d878bcb 23
mfiore 0:b8d93d878bcb 24 char _2LEMETRY_USERID[] = "";
mfiore 0:b8d93d878bcb 25 char _2LEMETRY_TOKEN[] = "";
mfiore 0:b8d93d878bcb 26 char _2LEMETRY_DOMAIN[] = "";
mfiore 0:b8d93d878bcb 27 char _2LEMETRY_STUFF[] = "things";
mfiore 0:b8d93d878bcb 28 char _2LEMETRY_DEVICE_ID[] = "nucleo-0001";
mfiore 0:b8d93d878bcb 29
mfiore 0:b8d93d878bcb 30 char _APN[] = "";
mfiore 0:b8d93d878bcb 31
mfiore 0:b8d93d878bcb 32 char _host[] = "q.mq.tt";
mfiore 0:b8d93d878bcb 33 int _port = 1883;
mfiore 0:b8d93d878bcb 34
mfiore 0:b8d93d878bcb 35 void callback(char* topic, char* payload, unsigned int len) {
mfiore 0:b8d93d878bcb 36 logInfo("topic: [%s]\r\npayload: [%s]", topic, payload);
mfiore 0:b8d93d878bcb 37 }
mfiore 0:b8d93d878bcb 38
mfiore 0:b8d93d878bcb 39 int main() {
mfiore 0:b8d93d878bcb 40 MTSLog::setLogLevel(MTSLog::TRACE_LEVEL);
mfiore 0:b8d93d878bcb 41
mfiore 0:b8d93d878bcb 42 // for Nucleo boards
mfiore 0:b8d93d878bcb 43 MTSSerialFlowControl io(D8, D2, D3, D6);
mfiore 0:b8d93d878bcb 44 io.baud(115200);
mfiore 0:b8d93d878bcb 45
mfiore 0:b8d93d878bcb 46 Cellular* radio = CellularFactory::create(&io);
mfiore 0:b8d93d878bcb 47 if (! radio) {
mfiore 0:b8d93d878bcb 48 logFatal("failed to create Cellular object - exiting");
mfiore 0:b8d93d878bcb 49 return 1;
mfiore 0:b8d93d878bcb 50 }
mfiore 0:b8d93d878bcb 51
mfiore 0:b8d93d878bcb 52 radio->configureSignals(D4,D7,RESET);
mfiore 0:b8d93d878bcb 53 Transport::setTransport(radio);
mfiore 0:b8d93d878bcb 54
mfiore 0:b8d93d878bcb 55 while (radio->setApn(_APN) != MTS_SUCCESS) {
mfiore 0:b8d93d878bcb 56 logError("failed to set APN [%s]", _APN);
mfiore 0:b8d93d878bcb 57 wait(2);
mfiore 0:b8d93d878bcb 58 }
mfiore 0:b8d93d878bcb 59
mfiore 0:b8d93d878bcb 60 while (! radio->connect()) {
mfiore 0:b8d93d878bcb 61 logError("failed to bring up PPP link");
mfiore 0:b8d93d878bcb 62 wait(2);
mfiore 0:b8d93d878bcb 63 }
mfiore 0:b8d93d878bcb 64
mfiore 0:b8d93d878bcb 65 PubSubClient mqtt(_host, _port, callback);
mfiore 0:b8d93d878bcb 66
mfiore 0:b8d93d878bcb 67 char topicStr[128];
mfiore 0:b8d93d878bcb 68 char buf[64];
mfiore 0:b8d93d878bcb 69 snprintf(topicStr, sizeof(topicStr), "%s/%s/%s", _2LEMETRY_DOMAIN, _2LEMETRY_STUFF, _2LEMETRY_DEVICE_ID);
mfiore 0:b8d93d878bcb 70
mfiore 0:b8d93d878bcb 71 while (true) {
mfiore 0:b8d93d878bcb 72 if (! mqtt.connect(_2LEMETRY_DEVICE_ID, _2LEMETRY_USERID, _2LEMETRY_TOKEN)) {
mfiore 0:b8d93d878bcb 73 logError("failed to connect to 2lemetry server");
mfiore 0:b8d93d878bcb 74 continue;
mfiore 0:b8d93d878bcb 75 }
mfiore 0:b8d93d878bcb 76 snprintf(buf, sizeof(buf), "{\"test\":%d}", rand());
mfiore 0:b8d93d878bcb 77 logInfo("publishing: [%s]", buf);
mfiore 0:b8d93d878bcb 78 if (! mqtt.publish(topicStr, buf)) {
mfiore 0:b8d93d878bcb 79 logError("failed to publish: [%s]", buf);
mfiore 0:b8d93d878bcb 80 }
mfiore 0:b8d93d878bcb 81 wait(1);
mfiore 0:b8d93d878bcb 82 mqtt.loop();
mfiore 0:b8d93d878bcb 83 mqtt.disconnect();
mfiore 0:b8d93d878bcb 84 wait(30);
mfiore 0:b8d93d878bcb 85 }
mfiore 0:b8d93d878bcb 86
mfiore 0:b8d93d878bcb 87 return 0;
mfiore 0:b8d93d878bcb 88 }