Sample program for using Pubnub on AT&T IoT Starter Kit (which has the WNC modem)

Dependencies:   Pubnub_mbed2_sync WNCInterface mbed-rtos mbed

Fork of WNCInterface_M2XMQTTdemo by Avnet

Committer:
JMF
Date:
Wed Sep 21 18:21:52 2016 +0000
Revision:
0:b476b3f6cb65
Child:
1:ceb54785e67c
Initial Commit;

Who changed what in which revision?

UserRevisionLine numberNew 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
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 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 0:b476b3f6cb65 45 eth.init();
JMF 0:b476b3f6cb65 46 eth.connect();
JMF 0:b476b3f6cb65 47 printf(CRLF CRLF "M2X MQTT Test..." CRLF);
JMF 0:b476b3f6cb65 48 printf("IP Address: %s" CRLF, eth.getIPAddress());
JMF 0:b476b3f6cb65 49
JMF 0:b476b3f6cb65 50 while (true) {
JMF 0:b476b3f6cb65 51 tval += 0.1;
JMF 0:b476b3f6cb65 52 printf("\r\n\r\nSending readings #%d\r\n",cnt++);
JMF 0:b476b3f6cb65 53 response = m2xClient.updateStreamValue(deviceId, tstreamName, tval);
JMF 0:b476b3f6cb65 54 printf("Sending temperature value: %lf, Response= %d" CRLF, tval, response);
JMF 0:b476b3f6cb65 55
JMF 0:b476b3f6cb65 56 hval -= 1.0;
JMF 0:b476b3f6cb65 57 printf("Sending humidity value: %lf", hval);
JMF 0:b476b3f6cb65 58 response = m2xClient.updateStreamValue(deviceId, hstreamName, hval);
JMF 0:b476b3f6cb65 59 printf(", Response= %d" CRLF, response);
JMF 0:b476b3f6cb65 60
JMF 0:b476b3f6cb65 61 printf("Calling postDeviceUpdates...");
JMF 0:b476b3f6cb65 62 response = m2xClient.postDeviceUpdates(deviceId,
JMF 0:b476b3f6cb65 63 streamNum,
JMF 0:b476b3f6cb65 64 streamNames,
JMF 0:b476b3f6cb65 65 counts,
JMF 0:b476b3f6cb65 66 ats,
JMF 0:b476b3f6cb65 67 values);
JMF 0:b476b3f6cb65 68 printf(" Response = %d" CRLF, response);
JMF 0:b476b3f6cb65 69 printf("Calling updateLocation...");
JMF 0:b476b3f6cb65 70 response = m2xClient.updateLocation(deviceId, name, latitude, longitude, elevation);
JMF 0:b476b3f6cb65 71 printf(" Response = %d" CRLF, response);
JMF 0:b476b3f6cb65 72 elevation++;
JMF 0:b476b3f6cb65 73
JMF 0:b476b3f6cb65 74 delay(6000);
JMF 0:b476b3f6cb65 75 //eth.doDebug(3);
JMF 0:b476b3f6cb65 76 }
JMF 0:b476b3f6cb65 77 }
JMF 0:b476b3f6cb65 78