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"}

pubnub.h

Committer:
sveljko
Date:
2016-09-02
Revision:
81:a5df87708b9a

File content as of revision 81:a5df87708b9a:

#if !defined INC_PUBNUB
#define      INC_PUBNUB

#include <string>
#include <vector>


/** A pubnub context.
*/
class pubnub_ctx {
public:
    /** Give publish key as @p pub_key and subscribe key as @p key_sub
        to initialize the context.
    */
    pubnub_ctx(char const* pub_key, char const* key_sub);
    
    ~pubnub_ctx();
    
    /** Result of a Pubnub transaction */
    enum result {
        format_error,
        response_too_short,
        missing_open_bracket,
        missing_close_bracket,
        missing_time_token,
        bad_time_token,
        publish_failed,
        ok
    };
    
    /** Publish a JSON @p message to a pubnub @p channel (or channels,
        separated by commas).
        This is a synchronous call, so it will take a while to return
        (depending on your Internet connection, could be 100s of ms).
        */
    result publish(char const* channel, char const* message);
    
    /** Subscribe on a @p channel (or channels, separated by commas).
        Messages received (if any) are pushed back to the @p messages
        vector.
        
        This is a synchronous call, but it will not wait the full Pubnub ~5 min
        for a message to appear on the channel. It will be much less, ~1 s.
        
        @return vector of messages - empty if there are no messages (like on
        the very first subscribe)
    */
    result subscribe(char const* channel, std::vector<std::string>& messages);
    
    std::string uuid() const { return d_uuid; }
    void set_uuid(char const *s) { d_uuid = s; }

    std::string auth() const { return d_auth; }
    void set_auth(char const *s) { d_auth = s; }
    
private:
    /// The publish key to use
    std::string d_pub_key;
    
    /// The subscribe key to use
    std::string d_key_sub;
    
    /// the last timetoken
    std::string d_token;
    
    /// The UUID to use (empty - do not use)
    std::string d_uuid;
    
    /// The auth key to use (empty - do not use)
    std::string d_auth;
};

#endif // !defined INC_PUBNUB