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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "WNCInterface.h"
00003 
00004 #include "pubnub_sync.h"
00005 #include "srand_from_pubnub_time.h"
00006 
00007 
00008 #define CRLF "\r\n"
00009 
00010 WNCInterface eth;
00011 MODSERIAL pc(USBTX,USBRX,256,256);
00012 
00013 
00014 static void generate_uuid(pubnub_t *pbp)
00015 {
00016     char const *uuid_default = "zeka-peka-iz-jendeka";
00017     struct Pubnub_UUID uuid;
00018     static struct Pubnub_UUID_String str_uuid;
00019 
00020     if (0 != pubnub_generate_uuid_v4_random(&uuid)) {
00021         pubnub_set_uuid(pbp, uuid_default);
00022     }
00023     else {
00024         str_uuid = pubnub_uuid_to_string(&uuid);
00025         pubnub_set_uuid(pbp, str_uuid.uuid);
00026         pc.printf("Generated UUID: %s\n", str_uuid.uuid);
00027     }
00028 }
00029 
00030 
00031 int main()
00032 {
00033     pc.baud(115200);
00034     pc.printf(CRLF CRLF "Pubnub Test starting..." CRLF);
00035     
00036     pc.printf("init() returned 0x%04X" CRLF, eth.init(NULL,&pc));
00037     eth.connect();
00038     pc.printf("IP Address: %s" CRLF, eth.getIPAddress());
00039     eth.doDebug(1);
00040 
00041     
00042     pubnub_t *pbp = pubnub_alloc();
00043     pubnub_init(pbp, "demo", "demo");  
00044 
00045     srand_from_pubnub_time(pbp);
00046     generate_uuid(pbp);
00047       
00048     while (true) {
00049         pubnub_res rslt = pubnub_publish(pbp, "hello_world", "\"Hello world from MBed WNC!\"");
00050         if (rslt != PNR_STARTED) {
00051             pc.printf("Failed to start publishing, rslt=%d"CRLF, rslt);
00052         }
00053         else {
00054             rslt = pubnub_await(pbp);
00055             if (rslt != PNR_OK) {
00056                 pc.printf("Failed to finished publishing, rslt=%d"CRLF, rslt);
00057             }
00058             else {
00059                 pc.printf("Published! Response from Pubnub: %s"CRLF, pubnub_last_publish_result(pbp));
00060             }
00061         }
00062         
00063         rslt = pubnub_subscribe(pbp, "hello_world", 0);
00064         if (rslt != PNR_STARTED) {
00065             pc.printf("Failed to start subscribing, rslt=%d"CRLF, rslt);
00066         }
00067         else {
00068             rslt = pubnub_await(pbp);
00069             if (rslt != PNR_OK) {
00070                 pc.printf("Failed to finished subscribing, rslt=%d"CRLF, rslt);
00071             }
00072             else {
00073                 pc.printf("Subscribed! Received messages follow: %s"CRLF);
00074                 while (char const *msg = pubnub_get(pbp)) {
00075                     pc.printf("subscribe got: %s"CRLF, msg);
00076                 }
00077             }
00078         }
00079         wait_ms(1000);
00080     }
00081 }
00082