Mike Fiore
/
2lemetry
send data to 2lemetry platform (http://app.thingfabric.com)
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 #include "mtsas.h" 00003 #include "PubSubClient.h" 00004 00005 /* PLEASE READ THIS! 00006 * 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 00007 * You must have a hacker (or higher) account at http://app.thingfabric.com 00008 * After you register and login, follow the steps below to set up your client 00009 * Click on "Default ThingFabric Project" 00010 * Set _2LEMETRY_DOMAIN to the string after "Project" at the top of the page 00011 * Click on "Credentials" 00012 * Click on "Default ThingFabric Credential" 00013 * Set _2LEMETRY_USER_ID to the value in the "Key (Username)" field 00014 * Set _2LEMETRY_TOKEN to the value in the "MD5 Secret" field 00015 * If you are running this code on multiple devices, you will want to make the _2LEMETRY_DEVICE_ID field unique for each device 00016 * Set the _APN field to the APN provided with your sim card 00017 * Build, flash, and run 00018 * This code sends a random integer value approximately every 30 seconds 00019 * Click on "Default ThingFabric Project" 00020 * Click on "Analytics" 00021 * You should be able to see your test data (page needs to be refreshed periodically, it doesn't automatically refresh) 00022 */ 00023 00024 char _2LEMETRY_USERID[] = ""; 00025 char _2LEMETRY_TOKEN[] = ""; 00026 char _2LEMETRY_DOMAIN[] = ""; 00027 char _2LEMETRY_STUFF[] = "things"; 00028 char _2LEMETRY_DEVICE_ID[] = "nucleo-0001"; 00029 00030 char _APN[] = ""; 00031 00032 char _host[] = "q.mq.tt"; 00033 int _port = 1883; 00034 00035 void callback(char* topic, char* payload, unsigned int len) { 00036 logInfo("topic: [%s]\r\npayload: [%s]", topic, payload); 00037 } 00038 00039 int main() { 00040 MTSLog::setLogLevel(MTSLog::TRACE_LEVEL); 00041 00042 // for Nucleo boards 00043 MTSSerialFlowControl io(D8, D2, D3, D6); 00044 io.baud(115200); 00045 00046 Cellular* radio = CellularFactory::create(&io); 00047 if (! radio) { 00048 logFatal("failed to create Cellular object - exiting"); 00049 return 1; 00050 } 00051 00052 radio->configureSignals(D4,D7,RESET); 00053 Transport::setTransport(radio); 00054 00055 while (radio->setApn(_APN) != MTS_SUCCESS) { 00056 logError("failed to set APN [%s]", _APN); 00057 wait(2); 00058 } 00059 00060 while (! radio->connect()) { 00061 logError("failed to bring up PPP link"); 00062 wait(2); 00063 } 00064 00065 PubSubClient mqtt(_host, _port, callback); 00066 00067 char topicStr[128]; 00068 char buf[64]; 00069 snprintf(topicStr, sizeof(topicStr), "%s/%s/%s", _2LEMETRY_DOMAIN, _2LEMETRY_STUFF, _2LEMETRY_DEVICE_ID); 00070 00071 while (true) { 00072 if (! mqtt.connect(_2LEMETRY_DEVICE_ID, _2LEMETRY_USERID, _2LEMETRY_TOKEN)) { 00073 logError("failed to connect to 2lemetry server"); 00074 continue; 00075 } 00076 snprintf(buf, sizeof(buf), "{\"test\":%d}", rand()); 00077 logInfo("publishing: [%s]", buf); 00078 if (! mqtt.publish(topicStr, buf)) { 00079 logError("failed to publish: [%s]", buf); 00080 } 00081 wait(1); 00082 mqtt.loop(); 00083 mqtt.disconnect(); 00084 wait(30); 00085 } 00086 00087 return 0; 00088 }
Generated on Wed Jul 13 2022 12:29:05 by 1.7.2