mbed project
Dependencies: ESP8266NodeMCUInterface Pubnub_mbed2_sync mbed
main.cpp@0:2730fbdae986, 2017-04-11 (annotated)
- Committer:
- ECE4180
- Date:
- Tue Apr 11 20:37:05 2017 +0000
- Revision:
- 0:2730fbdae986
- Child:
- 1:694056dc14d9
pubnub context error
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ECE4180 | 0:2730fbdae986 | 1 | #include "pubnub_sync.h" |
ECE4180 | 0:2730fbdae986 | 2 | #include "mbed.h" |
ECE4180 | 0:2730fbdae986 | 3 | #include "ESP8266Interface.h" |
ECE4180 | 0:2730fbdae986 | 4 | #include "TCPSocket.h" |
ECE4180 | 0:2730fbdae986 | 5 | |
ECE4180 | 0:2730fbdae986 | 6 | Serial pc(USBTX, USBRX); |
ECE4180 | 0:2730fbdae986 | 7 | DigitalOut led1(LED1); |
ECE4180 | 0:2730fbdae986 | 8 | ESP8266Interface esp(p28, p27, p19, 9600, 3000); |
ECE4180 | 0:2730fbdae986 | 9 | |
ECE4180 | 0:2730fbdae986 | 10 | char ssid[] = "New England Clam Router"; |
ECE4180 | 0:2730fbdae986 | 11 | char pwd[] = "gettincutgettinbutt"; |
ECE4180 | 0:2730fbdae986 | 12 | const char pubkey[] = "pub-c-f9091d93-c3a8-41a6-80c2-c6e3f840504f"; |
ECE4180 | 0:2730fbdae986 | 13 | const char subkey[] = "sub-c-f558e36c-130b-11e7-b59a-02ee2ddab7fe"; |
ECE4180 | 0:2730fbdae986 | 14 | const char channel[] = "demo"; |
ECE4180 | 0:2730fbdae986 | 15 | int port = 80; |
ECE4180 | 0:2730fbdae986 | 16 | char host[] = "pubsub.pubnub.com"; |
ECE4180 | 0:2730fbdae986 | 17 | 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"; |
ECE4180 | 0:2730fbdae986 | 18 | char sendbuf[180]; |
ECE4180 | 0:2730fbdae986 | 19 | char recvbuf[180]; |
ECE4180 | 0:2730fbdae986 | 20 | //ubNub pn(pubkey, subkey); |
ECE4180 | 0:2730fbdae986 | 21 | |
ECE4180 | 0:2730fbdae986 | 22 | int main() { |
ECE4180 | 0:2730fbdae986 | 23 | |
ECE4180 | 0:2730fbdae986 | 24 | |
ECE4180 | 0:2730fbdae986 | 25 | char buf[] = "hello world! please work"; |
ECE4180 | 0:2730fbdae986 | 26 | |
ECE4180 | 0:2730fbdae986 | 27 | bool success = esp.init(); |
ECE4180 | 0:2730fbdae986 | 28 | if (success) pc.printf("Successful init\r\n"); |
ECE4180 | 0:2730fbdae986 | 29 | else pc.printf("Initialize Fail\r\n"); |
ECE4180 | 0:2730fbdae986 | 30 | bool connection = esp.connect(ssid, pwd); |
ECE4180 | 0:2730fbdae986 | 31 | if (connection) pc.printf("Successful connection\r\n"); |
ECE4180 | 0:2730fbdae986 | 32 | else pc.printf("Connection Fail\r\n"); |
ECE4180 | 0:2730fbdae986 | 33 | const char *ip = esp.getIPAddress(); |
ECE4180 | 0:2730fbdae986 | 34 | if (ip) pc.printf("IP Address: %s\r\n", ip); |
ECE4180 | 0:2730fbdae986 | 35 | |
ECE4180 | 0:2730fbdae986 | 36 | //pc.printf("publishing\r\n"); |
ECE4180 | 0:2730fbdae986 | 37 | /* |
ECE4180 | 0:2730fbdae986 | 38 | TCPSocket sock; |
ECE4180 | 0:2730fbdae986 | 39 | sock.open(&esp); |
ECE4180 | 0:2730fbdae986 | 40 | sock.connect(host, 80); |
ECE4180 | 0:2730fbdae986 | 41 | if(sock.is_connected()) pc.printf("connection w/ google is good"); |
ECE4180 | 0:2730fbdae986 | 42 | |
ECE4180 | 0:2730fbdae986 | 43 | strncpy(sendbuf, message, 180); |
ECE4180 | 0:2730fbdae986 | 44 | pc.printf(message); |
ECE4180 | 0:2730fbdae986 | 45 | pc.printf("\n"); |
ECE4180 | 0:2730fbdae986 | 46 | sock.send_all(message, sizeof(message)); |
ECE4180 | 0:2730fbdae986 | 47 | sock.receive_all(recvbuf, 180); |
ECE4180 | 0:2730fbdae986 | 48 | |
ECE4180 | 0:2730fbdae986 | 49 | pc.printf(recvbuf); |
ECE4180 | 0:2730fbdae986 | 50 | */ |
ECE4180 | 0:2730fbdae986 | 51 | |
ECE4180 | 0:2730fbdae986 | 52 | // set up context |
ECE4180 | 0:2730fbdae986 | 53 | pubnub_t* ctx = pubnub_alloc(); |
ECE4180 | 0:2730fbdae986 | 54 | if (ctx == NULL) |
ECE4180 | 0:2730fbdae986 | 55 | pc.printf("FUCKING CTX IS NULL"); |
ECE4180 | 0:2730fbdae986 | 56 | // initialize pubnub api |
ECE4180 | 0:2730fbdae986 | 57 | pubnub_init(ctx, pubkey, subkey); |
ECE4180 | 0:2730fbdae986 | 58 | |
ECE4180 | 0:2730fbdae986 | 59 | pubnub_publish(ctx, channel, "hello world from the mbed"); |
ECE4180 | 0:2730fbdae986 | 60 | int pbresult = pubnub_await(ctx); |
ECE4180 | 0:2730fbdae986 | 61 | if (PNR_OK == pbresult) { |
ECE4180 | 0:2730fbdae986 | 62 | pc.printf("published! hell yes!"); |
ECE4180 | 0:2730fbdae986 | 63 | /* Published successfully */ |
ECE4180 | 0:2730fbdae986 | 64 | } else { |
ECE4180 | 0:2730fbdae986 | 65 | pc.printf("pubnub err: %d\r\n", pbresult); |
ECE4180 | 0:2730fbdae986 | 66 | } |
ECE4180 | 0:2730fbdae986 | 67 | |
ECE4180 | 0:2730fbdae986 | 68 | pubnub_free(ctx); |
ECE4180 | 0:2730fbdae986 | 69 | while (1) {}; |
ECE4180 | 0:2730fbdae986 | 70 | return 0; |
ECE4180 | 0:2730fbdae986 | 71 | } |