Pubnub demo for AT&T IoT Starter Kit. Functionally similar to the Flow demo.

Dependencies:   FXOS8700CQ MODSERIAL mbed

http://pubnub.github.io/slides/workshop/pictures/broadcast.png

Pubnub demo for AT&T IoT Starter Kit

This demo is functionally similar to the Flow demo, so you can find general information here: https://developer.mbed.org/users/JMF/code/Avnet_ATT_Cellular_IOT/.

The only difference is that we use Pubnub to publish the measurements and subscribe to receiving the instructions to set the LED.

Settings

Pubnub related settings are:

Pubnub settings in `config_me.h`

PUBNUB_SUBSCRIBE_KEY
PUBNUB_PUBLISH_KEY
PUBNUB_CHANNEL

All are documented in their respective comments.

Pubnub context class

Similar to Pubnub SDKs, we provide a Pubnub context class. It is defined in pubnub.h header file and implemented in pubnub.cpp.

It provides only the fundamental "publish" and "subscribe" methods. They are documented in the header file.

This class is reusable in other code (it is not specific to this demo), it has a very narrow interface to the AT&T IoT cellular modem code. For example of use, you can look at the main() (in main.c).

Sample of published data

Published message w/measurement data

{"serial":"vstarterkit001","temp":89.61,"humidity":35,"accelX":0.97,"accelY":0.013,"accelZ":-0.038}

Don't worry, nobody got burnt, the temperature is in degrees Fahrenheit. :)

Publish a message (from, say, the Pubnub console http://pubnub.com/console) of the form {"LED":<name-of-the-color>} on the channel that this demo listens to (default is hello_world) to turn the LED to that color on the Starter Kit:

Turn LED to red

{"LED":"Red"}

Turn LED to green

{"LED":"Green"}

Turn LED to blue

{"LED":"Blue"}
Committer:
sveljko
Date:
Fri Sep 02 17:44:55 2016 +0000
Revision:
81:a5df87708b9a
First version that works, forked from official AT&T IoT starter kit repository.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sveljko 81:a5df87708b9a 1 #if !defined INC_PUBNUB
sveljko 81:a5df87708b9a 2 #define INC_PUBNUB
sveljko 81:a5df87708b9a 3
sveljko 81:a5df87708b9a 4 #include <string>
sveljko 81:a5df87708b9a 5 #include <vector>
sveljko 81:a5df87708b9a 6
sveljko 81:a5df87708b9a 7
sveljko 81:a5df87708b9a 8 /** A pubnub context.
sveljko 81:a5df87708b9a 9 */
sveljko 81:a5df87708b9a 10 class pubnub_ctx {
sveljko 81:a5df87708b9a 11 public:
sveljko 81:a5df87708b9a 12 /** Give publish key as @p pub_key and subscribe key as @p key_sub
sveljko 81:a5df87708b9a 13 to initialize the context.
sveljko 81:a5df87708b9a 14 */
sveljko 81:a5df87708b9a 15 pubnub_ctx(char const* pub_key, char const* key_sub);
sveljko 81:a5df87708b9a 16
sveljko 81:a5df87708b9a 17 ~pubnub_ctx();
sveljko 81:a5df87708b9a 18
sveljko 81:a5df87708b9a 19 /** Result of a Pubnub transaction */
sveljko 81:a5df87708b9a 20 enum result {
sveljko 81:a5df87708b9a 21 format_error,
sveljko 81:a5df87708b9a 22 response_too_short,
sveljko 81:a5df87708b9a 23 missing_open_bracket,
sveljko 81:a5df87708b9a 24 missing_close_bracket,
sveljko 81:a5df87708b9a 25 missing_time_token,
sveljko 81:a5df87708b9a 26 bad_time_token,
sveljko 81:a5df87708b9a 27 publish_failed,
sveljko 81:a5df87708b9a 28 ok
sveljko 81:a5df87708b9a 29 };
sveljko 81:a5df87708b9a 30
sveljko 81:a5df87708b9a 31 /** Publish a JSON @p message to a pubnub @p channel (or channels,
sveljko 81:a5df87708b9a 32 separated by commas).
sveljko 81:a5df87708b9a 33 This is a synchronous call, so it will take a while to return
sveljko 81:a5df87708b9a 34 (depending on your Internet connection, could be 100s of ms).
sveljko 81:a5df87708b9a 35 */
sveljko 81:a5df87708b9a 36 result publish(char const* channel, char const* message);
sveljko 81:a5df87708b9a 37
sveljko 81:a5df87708b9a 38 /** Subscribe on a @p channel (or channels, separated by commas).
sveljko 81:a5df87708b9a 39 Messages received (if any) are pushed back to the @p messages
sveljko 81:a5df87708b9a 40 vector.
sveljko 81:a5df87708b9a 41
sveljko 81:a5df87708b9a 42 This is a synchronous call, but it will not wait the full Pubnub ~5 min
sveljko 81:a5df87708b9a 43 for a message to appear on the channel. It will be much less, ~1 s.
sveljko 81:a5df87708b9a 44
sveljko 81:a5df87708b9a 45 @return vector of messages - empty if there are no messages (like on
sveljko 81:a5df87708b9a 46 the very first subscribe)
sveljko 81:a5df87708b9a 47 */
sveljko 81:a5df87708b9a 48 result subscribe(char const* channel, std::vector<std::string>& messages);
sveljko 81:a5df87708b9a 49
sveljko 81:a5df87708b9a 50 std::string uuid() const { return d_uuid; }
sveljko 81:a5df87708b9a 51 void set_uuid(char const *s) { d_uuid = s; }
sveljko 81:a5df87708b9a 52
sveljko 81:a5df87708b9a 53 std::string auth() const { return d_auth; }
sveljko 81:a5df87708b9a 54 void set_auth(char const *s) { d_auth = s; }
sveljko 81:a5df87708b9a 55
sveljko 81:a5df87708b9a 56 private:
sveljko 81:a5df87708b9a 57 /// The publish key to use
sveljko 81:a5df87708b9a 58 std::string d_pub_key;
sveljko 81:a5df87708b9a 59
sveljko 81:a5df87708b9a 60 /// The subscribe key to use
sveljko 81:a5df87708b9a 61 std::string d_key_sub;
sveljko 81:a5df87708b9a 62
sveljko 81:a5df87708b9a 63 /// the last timetoken
sveljko 81:a5df87708b9a 64 std::string d_token;
sveljko 81:a5df87708b9a 65
sveljko 81:a5df87708b9a 66 /// The UUID to use (empty - do not use)
sveljko 81:a5df87708b9a 67 std::string d_uuid;
sveljko 81:a5df87708b9a 68
sveljko 81:a5df87708b9a 69 /// The auth key to use (empty - do not use)
sveljko 81:a5df87708b9a 70 std::string d_auth;
sveljko 81:a5df87708b9a 71 };
sveljko 81:a5df87708b9a 72
sveljko 81:a5df87708b9a 73 #endif // !defined INC_PUBNUB