Marcus Fisher / Mbed 2 deprecated pubnub_sync

Dependencies:   ESP8266NodeMCUInterface Pubnub_mbed2_sync mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "pubnub_sync.h"
00002 #include "mbed.h"
00003 #include "ESP8266Interface.h"
00004 //#include "TCPSocket.h"
00005 
00006 Serial pc(USBTX, USBRX);
00007 DigitalOut led1(LED1);
00008 ESP8266Interface esp(p28, p27, p19, 9600, 3000);
00009 
00010 char ssid[] = "New England Clam Router";
00011 char pwd[] = "gettincutgettinbutt";
00012 const char pubkey[] = "pub-c-f9091d93-c3a8-41a6-80c2-c6e3f840504f";
00013 const char subkey[] = "sub-c-f558e36c-130b-11e7-b59a-02ee2ddab7fe";
00014 const char channel[] = "demo";
00015 int port = 80;
00016 char host[] = "pubsub.pubnub.com";
00017 char message[] = "/publish/pub-c-f9091d93-c3a8-41a6-80c2-c6e3f840504f/sub-c-f558e36c-130b-11e7-b59a-02ee2ddab7fe/0/demo/0/%%7B%%22text%%22%%3A%%22heymbed%%22%%7D";
00018 char sendbuf[180];
00019 char recvbuf[180];
00020 //ubNub pn(pubkey, subkey);
00021 
00022 int main() {
00023 
00024 
00025     char buf[] = "hello world! please work";
00026     
00027     bool success = esp.init();
00028     if (success) pc.printf("Successful init\r\n");
00029     else pc.printf("Initialize Fail\r\n");
00030     bool connection = esp.connect(ssid, pwd);
00031     if (connection) pc.printf("Successful connection\r\n");
00032     else pc.printf("Connection Fail\r\n");
00033     const char *ip = esp.getIPAddress();
00034     if (ip) pc.printf("IP Address: %s\r\n", ip);
00035     
00036     //pc.printf("publishing\r\n");
00037     /*
00038     TCPSocket sock;
00039     sock.open(&esp);
00040     sock.connect(host, 80);
00041     if(sock.is_connected()) pc.printf("connection w/ google is good");
00042     
00043     strncpy(sendbuf, message, 180);
00044     pc.printf(message);
00045     pc.printf("\n");
00046     sock.send_all(message, sizeof(message));
00047     sock.receive_all(recvbuf, 180);
00048     
00049     pc.printf(recvbuf);
00050     */
00051     
00052     // set up context
00053     pubnub_t* ctx = pubnub_alloc();
00054     if (ctx == NULL)
00055         pc.printf("FUCKING CTX IS NULL");
00056     // initialize pubnub api
00057     pubnub_init(ctx, pubkey, subkey);
00058 
00059     pubnub_publish(ctx, channel, "hello world from the mbed");
00060     int pbresult = pubnub_await(ctx);
00061     if (PNR_OK == pbresult) {
00062         pc.printf("published! hell yes!");
00063         /* Published successfully */
00064     } else {
00065         pc.printf("pubnub err: %d\r\n", pbresult);
00066     } 
00067 
00068     pubnub_free(ctx);
00069     while (1) {};
00070     return 0;
00071 }