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@5:f9dc2e9e53a8, 2016-10-07 (annotated)
- Committer:
- JMF
- Date:
- Fri Oct 07 13:50:48 2016 +0000
- Revision:
- 5:f9dc2e9e53a8
- Parent:
- 4:c9290723dce9
- Child:
- 6:853f6aac9a9d
Added code to wrap Temp & Humidity and a #define for the loop delay.
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 | 5:f9dc2e9e53a8 | 6 | #define LOOP_DELAY_MS 3000 |
JMF | 0:b476b3f6cb65 | 7 | |
JMF | 0:b476b3f6cb65 | 8 | #include "minimal-mqtt.h" |
JMF | 0:b476b3f6cb65 | 9 | #include "minimal-json.h" |
JMF | 0:b476b3f6cb65 | 10 | #include "M2XMQTTClient.h" |
JMF | 0:b476b3f6cb65 | 11 | |
JMF | 0:b476b3f6cb65 | 12 | #define CRLF "\n\r" |
JMF | 0:b476b3f6cb65 | 13 | |
JMF | 0:b476b3f6cb65 | 14 | //Device ID: 8bcfff8a8514678f0fc6b56cb0f55c87 |
JMF | 0:b476b3f6cb65 | 15 | //Master Key: 3d1f3c0f42a8c541205f706f62c65330 |
JMF | 0:b476b3f6cb65 | 16 | |
JMF | 0:b476b3f6cb65 | 17 | char deviceId[] = "8bcfff8a8514678f0fc6b56cb0f55c87"; // Device Primary API Key |
JMF | 0:b476b3f6cb65 | 18 | char m2xKey[] = "3d1f3c0f42a8c541205f706f62c65330"; // Your M2X Master API Key |
JMF | 0:b476b3f6cb65 | 19 | char name[] = "Wake Forest"; // Name of current location of datasource |
JMF | 0:b476b3f6cb65 | 20 | |
JMF | 0:b476b3f6cb65 | 21 | const char *hstreamName = "humidity"; |
JMF | 0:b476b3f6cb65 | 22 | const char *tstreamName = "temperature"; |
JMF | 0:b476b3f6cb65 | 23 | |
JMF | 0:b476b3f6cb65 | 24 | double latitude = -37.97884; |
JMF | 0:b476b3f6cb65 | 25 | double longitude = -57.54787; // You can also read those values from a GPS |
JMF | 0:b476b3f6cb65 | 26 | double elevation = 15; |
JMF | 0:b476b3f6cb65 | 27 | |
JMF | 0:b476b3f6cb65 | 28 | const char *streamNames[] = { tstreamName, hstreamName }; |
JMF | 0:b476b3f6cb65 | 29 | int streamNum = sizeof(streamNames)/sizeof(const char *); |
JMF | 0:b476b3f6cb65 | 30 | const int counts[] = { 2, 1 }; |
JMF | 0:b476b3f6cb65 | 31 | const char *ats[] = { "2016-09-12T12:12:12.234Z", |
JMF | 0:b476b3f6cb65 | 32 | "2016-09-12T12:12:12.567Z", |
JMF | 0:b476b3f6cb65 | 33 | "2016-09-12T12:12:12.000Z" }; |
JMF | 0:b476b3f6cb65 | 34 | double values[] = { 27.9, 81.2, 16.1 }; |
JMF | 0:b476b3f6cb65 | 35 | |
JMF | 0:b476b3f6cb65 | 36 | Client client; |
JMF | 0:b476b3f6cb65 | 37 | M2XMQTTClient m2xClient(&client, m2xKey); |
JMF | 0:b476b3f6cb65 | 38 | WNCInterface eth; |
JMF | 1:ceb54785e67c | 39 | MODSERIAL pc(USBTX,USBRX,256,256); |
JMF | 0:b476b3f6cb65 | 40 | |
JMF | 0:b476b3f6cb65 | 41 | int main() { |
JMF | 0:b476b3f6cb65 | 42 | int response, cnt=1; |
JMF | 0:b476b3f6cb65 | 43 | double tval = 0.9; |
JMF | 0:b476b3f6cb65 | 44 | double hval = 101.0; |
JMF | 0:b476b3f6cb65 | 45 | |
JMF | 1:ceb54785e67c | 46 | pc.baud(115200); |
JMF | 2:e355d362a2d8 | 47 | pc.printf(CRLF CRLF "M2X MQTT Test starting..." CRLF); |
JMF | 1:ceb54785e67c | 48 | |
JMF | 4:c9290723dce9 | 49 | pc.printf("init() returned 0x%04X" CRLF, eth.init(NULL,&pc)); |
JMF | 0:b476b3f6cb65 | 50 | eth.connect(); |
JMF | 4:c9290723dce9 | 51 | pc.printf("IP Address: %s" CRLF, eth.getIPAddress()); |
JMF | 5:f9dc2e9e53a8 | 52 | eth.doDebug(0); |
JMF | 1:ceb54785e67c | 53 | |
JMF | 0:b476b3f6cb65 | 54 | while (true) { |
JMF | 0:b476b3f6cb65 | 55 | tval += 0.1; |
JMF | 5:f9dc2e9e53a8 | 56 | if( tval > 100.0 ) tval = 0.9; |
JMF | 2:e355d362a2d8 | 57 | pc.printf("\r\n\r\nSending readings #%d\r\n",cnt++); |
JMF | 0:b476b3f6cb65 | 58 | response = m2xClient.updateStreamValue(deviceId, tstreamName, tval); |
JMF | 2:e355d362a2d8 | 59 | pc.printf("Sending temperature value: %lf, Response= %d" CRLF, tval, response); |
JMF | 0:b476b3f6cb65 | 60 | |
JMF | 5:f9dc2e9e53a8 | 61 | if( hval < 2.0 ) hval = 101.0; |
JMF | 0:b476b3f6cb65 | 62 | hval -= 1.0; |
JMF | 2:e355d362a2d8 | 63 | pc.printf("Sending humidity value: %lf", hval); |
JMF | 0:b476b3f6cb65 | 64 | response = m2xClient.updateStreamValue(deviceId, hstreamName, hval); |
JMF | 2:e355d362a2d8 | 65 | pc.printf(", Response= %d" CRLF, response); |
JMF | 0:b476b3f6cb65 | 66 | |
JMF | 2:e355d362a2d8 | 67 | pc.printf("Calling postDeviceUpdates..."); |
JMF | 0:b476b3f6cb65 | 68 | response = m2xClient.postDeviceUpdates(deviceId, |
JMF | 0:b476b3f6cb65 | 69 | streamNum, |
JMF | 0:b476b3f6cb65 | 70 | streamNames, |
JMF | 0:b476b3f6cb65 | 71 | counts, |
JMF | 0:b476b3f6cb65 | 72 | ats, |
JMF | 0:b476b3f6cb65 | 73 | values); |
JMF | 2:e355d362a2d8 | 74 | pc.printf(" Response = %d" CRLF, response); |
JMF | 2:e355d362a2d8 | 75 | pc.printf("Calling updateLocation..."); |
JMF | 0:b476b3f6cb65 | 76 | response = m2xClient.updateLocation(deviceId, name, latitude, longitude, elevation); |
JMF | 2:e355d362a2d8 | 77 | pc.printf(" Response = %d" CRLF, response); |
JMF | 0:b476b3f6cb65 | 78 | elevation++; |
JMF | 0:b476b3f6cb65 | 79 | |
JMF | 5:f9dc2e9e53a8 | 80 | delay(LOOP_DELAY_MS); |
JMF | 0:b476b3f6cb65 | 81 | } |
JMF | 0:b476b3f6cb65 | 82 | } |
JMF | 0:b476b3f6cb65 | 83 |