The ARM mbed MQTT client library is used to send/receive data to/from AT&T's M2X service (https://m2x.att.com/) using the AT&T Cellular IoT Starter Kit which is sold by Avnet (http://cloudconnectkits.org/product/att-cellular-iot-starter-kit).
Dependencies: M2XMQTTClient WNCInterface mbed-rtos mbed minimal-json minimal-mqtt-JMF
main.cpp@1:ceb54785e67c, 2016-09-27 (annotated)
- Committer:
- JMF
- Date:
- Tue Sep 27 19:54:39 2016 +0000
- Revision:
- 1:ceb54785e67c
- Parent:
- 0:b476b3f6cb65
- Child:
- 2:e355d362a2d8
changed main.cpp to conform with anew way to enable WncController class debug output.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JMF | 0:b476b3f6cb65 | 1 | #include "mbed.h" |
JMF | 0:b476b3f6cb65 | 2 | #include "WNCInterface.h" |
JMF | 0:b476b3f6cb65 | 3 | |
JMF | 0:b476b3f6cb65 | 4 | #define DEBUG |
JMF | 0:b476b3f6cb65 | 5 | #define MBED_PLATFORM |
JMF | 0:b476b3f6cb65 | 6 | |
JMF | 0:b476b3f6cb65 | 7 | #include "minimal-mqtt.h" |
JMF | 0:b476b3f6cb65 | 8 | #include "minimal-json.h" |
JMF | 0:b476b3f6cb65 | 9 | #include "M2XMQTTClient.h" |
JMF | 0:b476b3f6cb65 | 10 | |
JMF | 0:b476b3f6cb65 | 11 | #define CRLF "\n\r" |
JMF | 0:b476b3f6cb65 | 12 | |
JMF | 0:b476b3f6cb65 | 13 | //Device ID: 8bcfff8a8514678f0fc6b56cb0f55c87 |
JMF | 0:b476b3f6cb65 | 14 | //Master Key: 3d1f3c0f42a8c541205f706f62c65330 |
JMF | 0:b476b3f6cb65 | 15 | |
JMF | 0:b476b3f6cb65 | 16 | char deviceId[] = "8bcfff8a8514678f0fc6b56cb0f55c87"; // Device Primary API Key |
JMF | 0:b476b3f6cb65 | 17 | char m2xKey[] = "3d1f3c0f42a8c541205f706f62c65330"; // Your M2X Master API Key |
JMF | 0:b476b3f6cb65 | 18 | char name[] = "Wake Forest"; // Name of current location of datasource |
JMF | 0:b476b3f6cb65 | 19 | |
JMF | 0:b476b3f6cb65 | 20 | const char *hstreamName = "humidity"; |
JMF | 0:b476b3f6cb65 | 21 | const char *tstreamName = "temperature"; |
JMF | 0:b476b3f6cb65 | 22 | |
JMF | 0:b476b3f6cb65 | 23 | double latitude = -37.97884; |
JMF | 0:b476b3f6cb65 | 24 | double longitude = -57.54787; // You can also read those values from a GPS |
JMF | 0:b476b3f6cb65 | 25 | double elevation = 15; |
JMF | 0:b476b3f6cb65 | 26 | |
JMF | 0:b476b3f6cb65 | 27 | const char *streamNames[] = { tstreamName, hstreamName }; |
JMF | 0:b476b3f6cb65 | 28 | int streamNum = sizeof(streamNames)/sizeof(const char *); |
JMF | 0:b476b3f6cb65 | 29 | const int counts[] = { 2, 1 }; |
JMF | 0:b476b3f6cb65 | 30 | const char *ats[] = { "2016-09-12T12:12:12.234Z", |
JMF | 0:b476b3f6cb65 | 31 | "2016-09-12T12:12:12.567Z", |
JMF | 0:b476b3f6cb65 | 32 | "2016-09-12T12:12:12.000Z" }; |
JMF | 0:b476b3f6cb65 | 33 | double values[] = { 27.9, 81.2, 16.1 }; |
JMF | 0:b476b3f6cb65 | 34 | |
JMF | 0:b476b3f6cb65 | 35 | Client client; |
JMF | 0:b476b3f6cb65 | 36 | M2XMQTTClient m2xClient(&client, m2xKey); |
JMF | 0:b476b3f6cb65 | 37 | WNCInterface eth; |
JMF | 1:ceb54785e67c | 38 | MODSERIAL pc(USBTX,USBRX,256,256); |
JMF | 0:b476b3f6cb65 | 39 | |
JMF | 0:b476b3f6cb65 | 40 | int main() { |
JMF | 0:b476b3f6cb65 | 41 | int response, cnt=1; |
JMF | 0:b476b3f6cb65 | 42 | double tval = 0.9; |
JMF | 0:b476b3f6cb65 | 43 | double hval = 101.0; |
JMF | 0:b476b3f6cb65 | 44 | |
JMF | 1:ceb54785e67c | 45 | pc.baud(115200); |
JMF | 1:ceb54785e67c | 46 | printf(CRLF CRLF "M2X MQTT Test starting..." CRLF); |
JMF | 1:ceb54785e67c | 47 | |
JMF | 1:ceb54785e67c | 48 | eth.init(NULL,&pc); |
JMF | 0:b476b3f6cb65 | 49 | eth.connect(); |
JMF | 1:ceb54785e67c | 50 | printf("IP Address: %s" CRLF, eth.getIPAddress()); |
JMF | 1:ceb54785e67c | 51 | eth.doDebug(0); |
JMF | 1:ceb54785e67c | 52 | |
JMF | 0:b476b3f6cb65 | 53 | while (true) { |
JMF | 0:b476b3f6cb65 | 54 | tval += 0.1; |
JMF | 0:b476b3f6cb65 | 55 | printf("\r\n\r\nSending readings #%d\r\n",cnt++); |
JMF | 0:b476b3f6cb65 | 56 | response = m2xClient.updateStreamValue(deviceId, tstreamName, tval); |
JMF | 0:b476b3f6cb65 | 57 | printf("Sending temperature value: %lf, Response= %d" CRLF, tval, response); |
JMF | 0:b476b3f6cb65 | 58 | |
JMF | 0:b476b3f6cb65 | 59 | hval -= 1.0; |
JMF | 0:b476b3f6cb65 | 60 | printf("Sending humidity value: %lf", hval); |
JMF | 0:b476b3f6cb65 | 61 | response = m2xClient.updateStreamValue(deviceId, hstreamName, hval); |
JMF | 0:b476b3f6cb65 | 62 | printf(", Response= %d" CRLF, response); |
JMF | 0:b476b3f6cb65 | 63 | |
JMF | 0:b476b3f6cb65 | 64 | printf("Calling postDeviceUpdates..."); |
JMF | 0:b476b3f6cb65 | 65 | response = m2xClient.postDeviceUpdates(deviceId, |
JMF | 0:b476b3f6cb65 | 66 | streamNum, |
JMF | 0:b476b3f6cb65 | 67 | streamNames, |
JMF | 0:b476b3f6cb65 | 68 | counts, |
JMF | 0:b476b3f6cb65 | 69 | ats, |
JMF | 0:b476b3f6cb65 | 70 | values); |
JMF | 0:b476b3f6cb65 | 71 | printf(" Response = %d" CRLF, response); |
JMF | 0:b476b3f6cb65 | 72 | printf("Calling updateLocation..."); |
JMF | 0:b476b3f6cb65 | 73 | response = m2xClient.updateLocation(deviceId, name, latitude, longitude, elevation); |
JMF | 0:b476b3f6cb65 | 74 | printf(" Response = %d" CRLF, response); |
JMF | 0:b476b3f6cb65 | 75 | elevation++; |
JMF | 0:b476b3f6cb65 | 76 | |
JMF | 0:b476b3f6cb65 | 77 | delay(6000); |
JMF | 0:b476b3f6cb65 | 78 | } |
JMF | 0:b476b3f6cb65 | 79 | } |
JMF | 0:b476b3f6cb65 | 80 |