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
Diff: main.cpp
- Revision:
- 8:57a5d7fbba6d
- Parent:
- 6:853f6aac9a9d
- Child:
- 10:f05fb235b75b
--- a/main.cpp Sat Oct 08 00:59:51 2016 +0000 +++ b/main.cpp Thu Nov 10 22:44:09 2016 +0000 @@ -1,83 +1,60 @@ #include "mbed.h" #include "WNCInterface.h" -//#define DEBUG -#define MBED_PLATFORM -#define LOOP_DELAY_MS 8000 - -#include "minimal-mqtt.h" -#include "minimal-json.h" -#include "M2XMQTTClient.h" - -#define CRLF "\n\r" - -//Device ID: 8bcfff8a8514678f0fc6b56cb0f55c87 -//Master Key: 3d1f3c0f42a8c541205f706f62c65330 - -char deviceId[] = "8bcfff8a8514678f0fc6b56cb0f55c87"; // Device Primary API Key -char m2xKey[] = "3d1f3c0f42a8c541205f706f62c65330"; // Your M2X Master API Key -char name[] = "Wake Forest"; // Name of current location of datasource +#include "pubnub_sync.h" -const char *hstreamName = "humidity"; -const char *tstreamName = "temperature"; - -double latitude = -37.97884; -double longitude = -57.54787; // You can also read those values from a GPS -double elevation = 15; +#define CRLF "\r\n" -const char *streamNames[] = { tstreamName, hstreamName }; -int streamNum = sizeof(streamNames)/sizeof(const char *); -const int counts[] = { 2, 1 }; -const char *ats[] = { "2016-09-12T12:12:12.234Z", - "2016-09-12T12:12:12.567Z", - "2016-09-12T12:12:12.000Z" }; -double values[] = { 27.9, 81.2, 16.1 }; - -Client client; -M2XMQTTClient m2xClient(&client, m2xKey); WNCInterface eth; MODSERIAL pc(USBTX,USBRX,256,256); -int main() { - int response, cnt=1; - double tval = 0.9; - double hval = 101.0; - - pc.baud(115200); - pc.printf(CRLF CRLF "M2X MQTT Test starting..." CRLF); +extern "C" int myprintf(char *, ...) { return 0; } - pc.printf("init() returned 0x%04X" CRLF, eth.init(NULL,&pc)); - eth.connect(); - pc.printf("IP Address: %s" CRLF, eth.getIPAddress()); - eth.doDebug(0); - - while (true) { - tval += 0.1; - if( tval > 100.0 ) tval = 0.9; - pc.printf("\r\n\r\nSending readings #%d\r\n",cnt++); - response = m2xClient.updateStreamValue(deviceId, tstreamName, tval); - pc.printf("Sending temperature value: %lf, Response= %d" CRLF, tval, response); +int main() +{ + pc.baud(115200); + pc.printf(CRLF CRLF "Pubnub Test starting..." CRLF); + + pc.printf("init() returned 0x%04X" CRLF, eth.init(NULL,&pc)); + eth.connect(); + pc.printf("IP Address: %s" CRLF, eth.getIPAddress()); + eth.doDebug(1); - if( hval < 2.0 ) hval = 101.0; - hval -= 1.0; - pc.printf("Sending humidity value: %lf", hval); - response = m2xClient.updateStreamValue(deviceId, hstreamName, hval); - pc.printf(", Response= %d" CRLF, response); - - pc.printf("Calling postDeviceUpdates..."); - response = m2xClient.postDeviceUpdates(deviceId, - streamNum, - streamNames, - counts, - ats, - values); - pc.printf(" Response = %d" CRLF, response); - pc.printf("Calling updateLocation..."); - response = m2xClient.updateLocation(deviceId, name, latitude, longitude, elevation); - pc.printf(" Response = %d" CRLF, response); - elevation++; - - delay(LOOP_DELAY_MS); - } + pubnub_t *pbp = pubnub_alloc(); + pubnub_init(pbp, "demo", "demo"); + + while (true) { + pubnub_res rslt = pubnub_publish(pbp, "hello_world", "\"Hello world from MBed WNC!\""); + if (rslt != PNR_STARTED) { + pc.printf("Failed to start publishing, rslt=%d"CRLF, rslt); + } + else { + rslt = pubnub_await(pbp); + if (rslt != PNR_OK) { + pc.printf("Failed to finished publishing, rslt=%d"CRLF, rslt); + } + else { + pc.printf("Published! Response from Pubnub: %s"CRLF, pubnub_last_publish_result(pbp)); + } + } + + rslt = pubnub_subscribe(pbp, "hello_world", 0); + if (rslt != PNR_STARTED) { + pc.printf("Failed to start subscribing, rslt=%d"CRLF, rslt); + } + else { + rslt = pubnub_await(pbp); + if (rslt != PNR_OK) { + pc.printf("Failed to finished subscribing, rslt=%d"CRLF, rslt); + } + else { + pc.printf("Subscribed! Received messages follow: %s"CRLF); + while (char const *msg = pubnub_get(pbp)) { + pc.printf("subscribe got: %s"CRLF, msg); + } + } + } + wait_ms(1000); + } }