[211] | 1 |
|
---|
| 2 | /*
|
---|
| 3 | PubNub over WiFi Example using WiFi Shield 101
|
---|
| 4 | This sample client will publish raw (string) PubNub messages from boards like Arduino Uno and Zero with WiFi Shield 101
|
---|
| 5 |
|
---|
| 6 | See https://www.arduino.cc/en/Reference/WiFi101 for more info
|
---|
| 7 |
|
---|
| 8 | created Dec 17, 2015
|
---|
| 9 | by Tomomi Imura
|
---|
| 10 | */
|
---|
| 11 |
|
---|
| 12 | #include <SPI.h>
|
---|
| 13 | #include <WiFi101.h>
|
---|
| 14 | #include <PubNub.h>
|
---|
| 15 |
|
---|
| 16 | static char ssid[] = "your-wifi-network"; // your network SSID (name)
|
---|
| 17 | static char pass[] = "password"; // your network password
|
---|
| 18 | int status = WL_IDLE_STATUS; // the Wifi radio's status
|
---|
| 19 |
|
---|
| 20 | const static char pubkey[] = "your-pub-key";
|
---|
| 21 | const static char subkey[] = "your-sub-key";
|
---|
| 22 | const static char channel[] = "hello_world";
|
---|
| 23 |
|
---|
| 24 |
|
---|
| 25 | void setup() {
|
---|
| 26 | // put your setup code here, to run once:
|
---|
| 27 | Serial.begin(9600);
|
---|
| 28 | Serial.println("Serial set up");
|
---|
| 29 |
|
---|
| 30 | // attempt to connect using WPA2 encryption:
|
---|
| 31 | Serial.println("Attempting to connect to WPA network...");
|
---|
| 32 | status = WiFi.begin(ssid, pass);
|
---|
| 33 |
|
---|
| 34 | // if you're not connected, stop here:
|
---|
| 35 | if ( status != WL_CONNECTED) {
|
---|
| 36 | Serial.println("Couldn't get a wifi connection");
|
---|
| 37 | while (true);
|
---|
| 38 | } else {
|
---|
| 39 | Serial.print("WiFi connecting to SSID: ");
|
---|
| 40 | Serial.println(ssid);
|
---|
| 41 |
|
---|
| 42 | PubNub.begin(pubkey, subkey);
|
---|
| 43 | Serial.println("PubNub set up");
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | void loop() {
|
---|
| 48 |
|
---|
| 49 | /* Publish */
|
---|
| 50 |
|
---|
| 51 | WiFiClient *client;
|
---|
| 52 |
|
---|
| 53 | char msg[] = "\"Yo!\"";
|
---|
| 54 |
|
---|
| 55 | client = PubNub.publish(channel, msg);
|
---|
| 56 |
|
---|
| 57 | if (!client) {
|
---|
| 58 | Serial.println("publishing error");
|
---|
| 59 | delay(1000);
|
---|
| 60 | return;
|
---|
| 61 | }
|
---|
| 62 | client->stop();
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 |
|
---|