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

main.cpp

Committer:
sveljko
Date:
2016-11-10
Revision:
9:dcb02cfdc63c
Parent:
8:57a5d7fbba6d
Child:
10:f05fb235b75b

File content as of revision 9:dcb02cfdc63c:

#include "mbed.h"
#include "WNCInterface.h"

#include "pubnub_sync.h"

#define CRLF "\r\n"

WNCInterface eth;
MODSERIAL pc(USBTX,USBRX,256,256);

extern "C" int myprintf(char *, ...) { return 0; }

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);

    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);
    }
}