[211] | 1 | /*
|
---|
| 2 | PubNub sample subscribe client
|
---|
| 3 |
|
---|
| 4 | This sample client will subscribe to and handle raw PubNub messages
|
---|
| 5 | (not doing any JSON decoding). It does so with a randomly generated
|
---|
| 6 | UUID.
|
---|
| 7 |
|
---|
| 8 | Circuit:
|
---|
| 9 | * Ethernet shield attached to pins 10, 11, 12, 13
|
---|
| 10 | * (Optional.) LED on pin 8 for reception indication.
|
---|
| 11 | * Pin A4 unconnected (noise source for random number generator)
|
---|
| 12 |
|
---|
| 13 | created 23 October 2012
|
---|
| 14 | by Petr Baudis
|
---|
| 15 |
|
---|
| 16 | https://github.com/pubnub/pubnub-api/tree/master/arduino
|
---|
| 17 | This code is in the public domain.
|
---|
| 18 | */
|
---|
| 19 |
|
---|
| 20 | #include <SPI.h>
|
---|
| 21 | #include <Ethernet.h>
|
---|
| 22 | #include <PubNub.h>
|
---|
| 23 |
|
---|
| 24 | // Some Ethernet shields have a MAC address printed on a sticker on the shield;
|
---|
| 25 | // fill in that address here, or choose your own at random:
|
---|
| 26 | byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
|
---|
| 27 |
|
---|
| 28 | const int subLedPin = 8;
|
---|
| 29 |
|
---|
| 30 | char pubkey[] = "demo";
|
---|
| 31 | char subkey[] = "demo";
|
---|
| 32 | char channel[] = "hello_world";
|
---|
| 33 | char uuid[] = "xxxxxxxx-xxxx-4444-9999-xxxxxxxxxxxx";
|
---|
| 34 |
|
---|
| 35 | void random_uuid() {
|
---|
| 36 | randomSeed(analogRead(4) + millis() * 1024);
|
---|
| 37 | snprintf(uuid, sizeof(uuid), "%04lx%04lx-%04lx-4444-9999-%04lx%04lx%04lx",
|
---|
| 38 | random(0x10000), random(0x10000), random(0x10000),
|
---|
| 39 | random(0x10000), random(0x10000), random(0x10000));
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | void setup()
|
---|
| 43 | {
|
---|
| 44 | pinMode(subLedPin, OUTPUT);
|
---|
| 45 | digitalWrite(subLedPin, LOW);
|
---|
| 46 |
|
---|
| 47 | Serial.begin(9600);
|
---|
| 48 | Serial.println("Serial set up");
|
---|
| 49 |
|
---|
| 50 | while (!Ethernet.begin(mac)) {
|
---|
| 51 | Serial.println("Ethernet setup error");
|
---|
| 52 | delay(1000);
|
---|
| 53 | }
|
---|
| 54 | Serial.println("Ethernet set up");
|
---|
| 55 |
|
---|
| 56 | PubNub.begin(pubkey, subkey);
|
---|
| 57 | random_uuid();
|
---|
| 58 | PubNub.set_uuid(uuid);
|
---|
| 59 | Serial.println("PubNub set up");
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | void flash(int ledPin)
|
---|
| 63 | {
|
---|
| 64 | /* Flash LED three times. */
|
---|
| 65 | for (int i = 0; i < 3; i++) {
|
---|
| 66 | digitalWrite(ledPin, HIGH);
|
---|
| 67 | delay(100);
|
---|
| 68 | digitalWrite(ledPin, LOW);
|
---|
| 69 | delay(100);
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | void loop()
|
---|
| 74 | {
|
---|
| 75 | Ethernet.maintain();
|
---|
| 76 |
|
---|
| 77 | PubSubClient *client;
|
---|
| 78 |
|
---|
| 79 | Serial.println("waiting for a message (subscribe)");
|
---|
| 80 | client = PubNub.subscribe(channel);
|
---|
| 81 | if (!client) {
|
---|
| 82 | Serial.println("subscription error");
|
---|
| 83 | delay(1000);
|
---|
| 84 | return;
|
---|
| 85 | }
|
---|
| 86 | Serial.print("Received: ");
|
---|
| 87 | while (client->wait_for_data()) {
|
---|
| 88 | char c = client->read();
|
---|
| 89 | Serial.print(c);
|
---|
| 90 | }
|
---|
| 91 | client->stop();
|
---|
| 92 | Serial.println();
|
---|
| 93 | flash(subLedPin);
|
---|
| 94 |
|
---|
| 95 | delay(200);
|
---|
| 96 | }
|
---|