source: rtos_arduino/trunk/arduino_lib/libraries/PubNub/README.md@ 211

Last change on this file since 211 was 211, checked in by ertl-honda, 8 years ago

公開されているコードを追加

File size: 8.0 KB
Line 
1#PubNub Arduino Library
2
3This library allows your sketches to communicate with the PubNub cloud
4message passing system using an Ethernet shield. Your application can
5receive and send messages.
6
7##Copy-and-Paste-Ready Code!
8See how easy it is to [Publish](examples/PubNubPublisher) and [Subscribe](examples/PubNubSubscriber)!
9
10###Synopsis
11
12
13 void setup() {
14 Serial.begin(9600);
15 Ethernet.begin(mac);
16 PubNub.begin(pubkey, subkey);
17 }
18
19 void loop() {
20 /* Maintain DHCP lease. */
21 Ethernet.maintain();
22
23 /* Publish message. */
24 EthernetClient *pclient = PubNub.publish(pubchannel, "\"message\"");
25 if (pclient)
26 pclient->stop();
27
28 /* Wait for news. */
29 PubSubClient *sclient = PubNub.subscribe(subchannel);
30 if (!sclient) return; // error
31 char buffer[64]; size_t buflen = 0;
32 while (sclient->wait_for_data()) {
33 buffer[buflen++] = sclient->read();
34 }
35 buffer[buflen] = 0;
36 sclient->stop();
37
38 /* Print received message. You will want to look at it from
39 * your code instead. */
40 Serial.println(buffer);
41 delay(10000);
42 }
43
44##Library Reference
45
46``bool PubNub.begin(char *publish_key, char *subscribe_key, char *origin)``
47
48To start using PubNub, use PubNub.begin(). This should be called after
49Ethernet.begin().
50
51Note that the string parameters are not copied; do not overwrite or free the
52memory where you stored the keys! (If you are passing string literals, don't
53worry about it.) Note that you should run only one of publish, subscribe and
54history requests each at once.
55
56The origin parameter is optional, defaulting to "pubsub.pubnub.com".
57
58``EthernetClient *publish(char *channel, char *message, int timeout)``
59
60Send a message (assumed to be well-formed JSON) to a given channel.
61
62Returns NULL in case of error, instead an instance of EthernetClient
63that you can use to read the reply to the publish command. If you
64don't care about it, call ``client->stop()`` right away.
65
66The timeout parameter is optional, defaulting to 305. See also
67a note about timeouts below.
68
69``PubSubClient *subscribe(char *channel)``
70
71Listen for a message on a given channel. The function will block
72and return when a message arrives. NULL is returned in case of error.
73The return type is PubSubClient, but from user perspective, you can
74work with it exactly like with EthernetClient; it also provides
75an extra convenience method ``wait_for_data()`` that allows you
76to wait for more data with sensible timeout.
77
78Typically, you will run this function from loop() function to keep
79listening for messages indefinitely.
80
81As a reply, you will get a JSON array with messages, e.g.:
82
83```
84 ["msg1",{msg2:"x"}]
85```
86
87and so on. Empty reply [] is also normal and your code must be
88able to handle that. Note that the reply specifically does not
89include the time token present in the raw reply from PubNub;
90no need to worry about that.
91
92The timeout parameter is optional, defaulting to 305. See also
93a note about timeouts below.
94
95``EthernetClient *history(char *channel, int limit, int timeout)``
96
97Receive list of the last messages published on the given channel.
98The limit argument is optional and defaults to 10.
99
100The timeout parameter is optional, defaulting to 305. See also
101a note about timeouts below.
102
103##Installation
104
105Move the contents of the ``pubnub/arduino/`` directory to
106``~/sketchbook/libraries/PubNub/`` and restart your Arduino IDE.
107Try out the examples!
108
109##Supported Hardware
110
111The Arduino ecosystem features a multitude of platforms that
112have significant differences regarding their hardware capabilities;
113here, we list the status of PubNub support for various platforms:
114
115 * ATMega168-based generic boards (Duemilanove, Diecimila...):
116Not supported due to too small RAM and flash memory size.
117 * ATMega328-based generic boards (Duemilanove, Uno), Arduino
118Ethernet: Supported. The space for user code may be somewhat limited.
119When using the aJson library, some memory saving tricks are required,
120see the top of the PubNubJson example.
121
122 * Arduino Mega (ATMega2560): Supported.
123 * Arduino Mega (ATMega1280): Untested, but should work just fine.
124
125Except Arduino Ethernet, network connectivity is provided by
126an external board ("shield"):
127
128 * Arduino Ethernet Shield: Supported. We tested only a shield that
129is based on the WizNet W5100 chip - but this should be the case for
130all but some ancient non-official ethernet shields.
131 * Arduino WiFi Shield: Supported, but new enough firmware and software
132is required. The support must be enabled in the PubNub library.
133Please see the "WiFi Shield Support" section for details.
134
135##WiFi Shield Support
136
137The PubNub library supports the WiFi shield as well. In order
138to use the library with a WiFi-enabled Arduino, you will need
139to edit the file PubNub.h, commenting out the line
140
141 //#define PubNub_Ethernet
142
143and uncommenting
144
145 #define PubNub_WiFi
146
147It is essential to use a new enough Arduino version so that
148the WiFi library actually works properly. Most notably, version 1.0.5
149has been confirmed to work while Arduino 1.0.4 is broken!
150
151The WiFi shield carries its own microcontroller dedicated to the network
152communication and this microcontroller has upgradeable firmware.
153If some of the PubNub calls fail with your WiFi shield (e.g. you
154see "subscribe error" and similar messages in serial console),
155the firmware loaded on your WiFi shield may be buggy - e.g. a WiFi
156shield bought commercially in May 2013 came preloaded with firmware
157that had to be upgraded. This is not so difficult to do, simply follow:
158
159 http://arduino.cc/en/Hacking/WiFiShieldFirmwareUpgrading
160
161
162## WiFi Shield Support for "WiFi Shield 101"
163
164If you are using [Arduino WiFi Shield 101](https://www.arduino.cc/en/Main/ArduinoWiFiShield101), use WiFi101 library, instead of the WiFi library.
165
166Download a zip from Download a [zipped WiFi101 library](https://github.com/arduino-libraries/WiFi101/releases)
167and import the lib.
168
169To enable it in PubNub Library, edit `PubNub.h`:
170
171Comment out line 12:
172
173 //#define PubNub_Ethernet
174
175and uncomment line 13:
176
177 #define PubNub_WiFi
178
179then at line 21, where it says `#include <WiFi.h>`, replace it with:
180
181 #include <WiFi101.h>
182
183Now the `#define PubNub_WiFi` block should look like:
184
185```c
186#define PubNub_WiFi
187
188#if defined(PubNub_Ethernet)
189#include <Ethernet.h>
190#define PubNub_BASE_CLIENT EthernetClient
191
192#elif defined(PubNub_WiFi)
193#include <WiFi101.h>
194#define PubNub_BASE_CLIENT WiFiClient
195
196#else
197#error PubNub_BASE_CLIENT set to an invalid value!
198#endif
199```
200
201##Notes
202
203* There is no SSL support on Arduino, it is unfeasible with
204Arduino Uno or even Arduino Mega's computing power and memory limits.
205All the traffic goes on the wire unencrypted and unsigned.
206
207* We re-resolve the origin server IP address before each request.
208This means some slow-down for intensive communication, but we rather
209expect light traffic and very long-running sketches (days, months),
210where refreshing the IP address is quite desirable.
211
212* We let the users read replies at their leisure instead of
213returning an already preloaded string so that (a) they can do that
214in loop() code while taking care of other things as well (b) we don't
215waste precious RAM by pre-allocating buffers that are never needed.
216
217* If you are having problems connecting, maybe you have hit
218a bug in Debian's version of Arduino pertaining the DNS code. Try using
219an IP address as origin and/or upgrading your Arduino package.
220
221* The optional timeout parameter allows you to specify a timeout
222period after which the subscribe call shall be retried. Note
223that this timeout is applied only for reading response, not for
224connecting or sending data; use retransmission parameters of
225the Ethernet library to tune this. As a rule of thumb, timeout
226smaller than 30 seconds may still block longer with flaky
227network. Default server-side timeout of PubNub API is 300s.
228
229* The vendor firmware for the WiFi shield has dubious TCP implementation;
230for example, TCP ports of outgoing connections are always chosen from the
231same sequence, so if you reset your Arduino, some of the new connections
232may interfere with an outstanding TCP connection that has not been closed
233before the reset; i.e. you will typically see a single failed request
234somewhere down the road after a reset.
Note: See TracBrowser for help on using the repository browser.