Author Archives: Joe Hanson

About Joe Hanson

Joe Hanson is Content Marketing Manager at PubNub, the world's only global data stream network for web, mobile, and IoT applications. PubNub enables IoT developers to easily, securely, and reliably connect and signal Internet of Things devices. Check PubNub out at PubNub.com

Building real-time monitoring for IoT device state

You may have a couple Arduinos, or billions of IoT devices connected in a single instance. A common need today is the requirement to detect when devices are turned on and turned off, also known as device state. And, monitoring the device state of connected devices and machines in real-time is called presence.

In this blog post, we’ll walk you through how to use presence to monitor IoT devices and hardware connected with PubNub (for both Java and JavaScript).

PresenceIotDevices

Why You Need to Monitor Your IoT Devices in Real-Time

IoT hardware comes in all shapes, sizes, and prices. But despite their differences, monitoring device state is essential, and we need to know exactly when they’re online and offline. Say you have an (Atmel based) Arduino hooked up to your apartment doorbell for whatever reason. Your Arduino goes offline, the pizza man is standing outside, and you’re not eating. Or maybe the situation is more dramatic. You may have hundreds of IoT devices hooked up to manage your farm. Keeping tabs on those devices is vital for the health of your farm, and you need to know when they go offline.

Device Monitoring Using Presence

We’ll first walk you through using Presence for IoT devices with Java, then move onto JavaScript. With both, you’ll first need to sign up for a PubNub account. Once you sign up, you can get your unique PubNub keys in the PubNub Developer Portal. In the developer’s portal, click to enable Presence. Feel free to play around as much as you want in our free Sandbox tier.

Check out our simulated Presence demo to get a better idea of how Presence can be used for real-time monitoring of Internet of Things devices.

Java

Step 1: Presence and here_Now() are two features of PubNub that update device or user state in real-time. Whether you choose to use JavaScript or the PubNub Java Presence SDK, the output for Presence is the same. You will get an output in this format:

{"message":"OK","status":200,"uuids":["uuid1"],"service":"Presence",
"occupancy":1}

where “uuids” contains a list of the uuids online and occupancy gives the number of online users.

I will be using the code feature to see ‘who’s there?’. All you need to provide is the channel name, and then check if there is anyone on that channel. The code sample below is basic usage.

pubnub.hereNow("my_channel", new Callback() {
     public void successCallback(String channel, Object response) {
         System.out.println(response);
     }
     public void errorCallback(String channel, PubnubError error) {
         System.out.println(error);
     }
 });

This will output the devices that are online which is identified by the UUIDs. In order to consume this information, all you need is to modify the callback function a little. The following code shows you how:

Step 2:

Callback callback = new Callback() {
	public void successCallback(String channel, Object response) {
	    String temp = response.toString();
	    int start = temp.indexOf('[');
	    int end = temp.indexOf(']');
	    for(int index = start+1;index<end;index++){
		    if(temp.charAt(index)!=','){	
		    	uuid1 = uuid1 + temp.charAt(index);
		    }
		    else{
		    	System.out.println();
		    }
	    }
    	String replaced = uuid1.replace("\""," ");
    	String[] uuidlist = replaced.split("\\s+");
    	for (String tempstring : uuidlist){
    		System.out.println(tempstring);
    	}	
	}
		
	public void errorCallback(String channel, PubnubError error){
		System.out.println(error.toString());
	}
};
	
	public void herenow(){
		Pubnub pubnub = new Pubnub("demo", "demo");
		pubnub.hereNow("my_channel", callback);
	}

This code, modifies the information received by the hereNow function, and stores and prints it in an array called ‘uuidlist’. In this manner, you can now use this information according to your requirements.

JavaScript

Step 1: The PubNub JavaScript Presence feature is an optional parameter used along with the subscribe call in JavaScript. The code sample below is basic usage:

pubnub.subscribe({
     channel: "my_channel",
     presence: function(m){console.log(m)},
     callback: function(m){console.log(m)}
 });

The presence feature will output the devices that are online as identified by their UUIDs, along with their timestamp, an action that indicates join/leave/timeout and the occupancy of the channel. This information will be displayed in the console.

But what if you want to consume this information by publishing it to a screen or store it somewhere? The following code lets you do just that.

Step 2: Now we’ll bring the presence to life with JavaScript

var deviceList[],
devices =[];

pubnub.subscribe({

    channel: 'my_channel',
    presence: function(message,channel){
        if(message.action == "join"){
        	devices.push(message.uuid);
    		deviceList.append("<li text-align:
    		center>" + message.uuid + "</li>");
      		}
        else{
          devices.splice(devices.indexOf(message.uuid), 1);
          deviceList.find(message.uuid).remove();
	}
 }
});

Here, we define a custom function for presence which basically uses the different actions of a presence event that could occur, such as join, timeout and leave.

  • If a ‘join’ occurs, we append the UUID to the list of devices that are online.
  • If a ‘leave or a timeout’ occurs, we remove that UUID from the list of list of devices that are online.

You now have the online users, both in an array called ‘devices’ and also as list printed on a page.

This way, you can now be updated on the different devices joining and leaving your network in real-time.

You can check out the PubNub JavaScript Presence documentation here.

Additional PubNub Presence Resources

Build real-time dashboards to monitor your IoT connected devices

Never to be overlooked is the importance of monitoring connected Internet of Things devices, at both a personal and global scale. You need to know the status of your device; its health, battery, connection status, etc. One way to do that is through a realtime customizable dashboard, enabling you can track, monitor, and visualize your connected devices.

Enter: freeboard.

freeboard is a web-based tool that allows you to build fully customizable and interactive user-interfaces for your connected IoT devices, from dashboards, to consoles, to control panels.

And freeboard now supports PubNub, meaning that you can now monitor your IoT device’s health and status in realtime. Apps and connected devices running on the PubNub Data Stream Network can now integrate data streams with the open source freeboard and customize how the information is displayed.

Monitoring is a powerful tool for any IoT use case. Say you have a number of 3G-connected, GPS-enabled, air-quality sensors attached to city-sponsored rental bikes that can track the air quality down to each city block. When in use, these specially equipped bicycles provide real-time location-tagged air-quality data that is used to create a crowdsourced pollution index map for others bikers to use when planning their routes. All that information can be displayed on a realtime dashboard that looks like this (see it in action here):

Internet of Things Monitoring

Integrating PubNub and freeboard

Both freeboard and PubNub believe in the same core design principles: ease of use, simplicity, and the idea of “it just works.” In the video below, Jim Heising, maker of freeboard, gives a brief demo of using PubNub with freeboard. Additionally, feel free to check out the freeboard GitHub repository.

As we become an increasingly connected world, the importance of monitoring our devices will increase as well. Don’t be left in the dark when it comes to your IoT devices!

How to connect an Arduino to a real-time network in two steps

Arduino is a microcontroller and development environment that can be used, just like Raspberry Pi, to read information from its sensors and control other devices.

Programming on the Arduino is extremely simple, which has led to its widespread use in interactive environments. As with the Raspberry Pi, it’s a great (and low cost) starting point for experimenting with the world of embedded wearables and the Internet of Things.

This blog walks you through the steps to connect an Arduino to the PubNub Data Stream Network. This enables you to connect with a high volume of other devices at a global scale and start sending and receiving data between a wide variety of devices, including other Arduino and Raspberry Pi devices.

PubNub acts as your communication layer for signaling and data transfer between embedded devices. Some of the advantages of using the PubNub Data Stream Network over a custom, open source solution is reliability and speed. PubNub takes care of the backend infrastructure that you’d have to set up otherwise to create a signaling network between Arduino and other connected devices. You can find the Arduino PubNub extensions here. For additional resources, check out the PubNub Arduino documentation page. Arduino

Basics of Connecting the Arduino to The PubNub Data Stream Network

Really, all it takes is two steps to get your Arduino up and running. Follow these steps to get connected:

STEP 1: Connect the Arduino to a monitor, keyboard, mouse and ethernet cable. You’ll also have to sign up for a PubNub account to get your unique keys. PubNub is entirely free in the sandbox tier, which gives you more than enough bandwidth to test and play with your Arduino.

STEP 2: Open a new window and paste the following code.

#include 
#include 
#include "string.h"
#include "iotconnector.h"
 
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
 
char pubkey[] = "demo";char subkey[] = "demo";char channel[] = "iotchannel";char uuid[] = "Arduino";
 
iotbridge arduino;
 
void initialize(){
    Serial.begin(9600);
    Serial.println("Serial set up");
 
    while (!Ethernet.begin(mac)) {
        Serial.println("Ethernet setup error");
        delay(1000);
    }
    Serial.println("Ethernet set up");
}
 
void do_something(String value){
    Serial.println("in the callback");
    Serial.println(value);
   }
void setup()
{
    initialize();
    arduino.init( pubkey, subkey, uuid);
}
 
void loop()
{
    String returnmessage;
    Ethernet.maintain();

    //Publish
    arduino.send(channel,""Hey There"");
 
    //Subscribe
    returnmessage = arduino.connect(channel);

    //callback function of sorts, to work with the received message
    do_something(returnmessage);
    Serial.println();
}

The above code lets you set the following :

  • publish key using “pubkey”
  • subscribe key using “subkey”
  • UUID using “uuid”
  • channel you wish to subscribe and publish to using “channel”

For all of you who are familiar with Arduino, you will notice that this is in fact only a few additional lines of code in loop(). The remaining code sets up the Ethernet connection to the Arduino. The keys lines of code are just three lines:

  • arduino.init() that lets you initialize variables
  • arduino.send() that lets you send a message over the specified channel
  • arduino.connect() that lets you subscribe to a channel to receive messages

We create an instance of the iotbridge called “arduino” and we can use the SEND and CONNECT methods belonging to that class. SEND: This invokes the publish() API to send any message over a desired channel.

bool iotbridge::send(const char *channel, const char *message){
  EthernetClient *client;
  client = PubNub.publish(channel,message);
  return client;
}

CONNECT: This invokes the subscribe() API to listen for messages from other embedded devices connected to the same channel. NOTE: The call back function receive is invoked when subscribe runs. Connect() will block on subscribe, waiting for a message to be sent to the device. You can write your own simple task scheduler and bounce between tasks according to their timing requirements.

String iotbridge::connect(const char *channel){
  PubSubClient *pclient = PubNub.subscribe(channel);

You can customize the channels you publish/subscribe to, the UUID you assume and also the messages you send by just changing the above fields. And that’s it! You can now talk Arduino to any other device or application subscribing/publishing to the same channel.

5 challenges of IoT connectivity

At last month’s MIT Technology Review Digital Summit, PubNub CEO Todd Greene discussed the importance of connecting Internet of Things embedded devices on a reliable and secure realtime network. CPU, battery, and bandwidth consumption, as well as security are all paramount considerations that need to be taken into account when connecting low-powered embedded devices.

You’ll find that when developing and networking Internet of Things devices in the lab, connectivity is fairly seamless. You may have a few embedded devices connected to a backend server, so latency isn’t an issue.

Internet of Things

However, deploying that IoT app on a global scale, to thousands or even millions of users simultaneously, is a whole other ball game. Unfortunately, the Internet isn’t just one big network, but rather is composed of an infinite amount of heterogeneous networks, including proxy servers, firewalls, cell towers, and WiFi networks, all slower and faster than one another.

As a result, there are 5 major challenges when it comes to Internet of Things connectivity. Keep scrolling down to see them, or watch the video below:

At PubNub, we think a lot about IoT connectivity and how we can make it as reliable, secure, and fast as possible. So to make PubNub the best network for connecting and signaling between Internet of Things devices, we first had to understand the challenges of doing so. Presenting the 5 challenges of IoT connectivity:

1. Signaling

When connecting IoT embedded devices, you need to start with bidirectional signaling to collect and route data between devices. Whether it’s embedded devices talking to a server to collect data, or devices signaling one another, you need to stream IoT signals and data quickly and reliably. You need to be 100% sure that that stream of data or signal is going to arrive at its destination every time.

2. Security

Security is a huge umbrella, but it’s paramount in Internet of Things connectivity and should be forethought, not an afterthought. For example, what good is a smart home if anyone can open your garage door? Here are three considerations for IoT security:

  • Authorization: When publishing or subscribing to stream of data or IoT signal, it’s essential to make sure that the IoT device or server has proper authorization to send or receive that stream of data.
  • Encryption: You need end-to-end encryption between devices and servers.
  • Open ports: An IoT device is dangerously vulnerable when it’s sitting and listening to an open port out to the Internet. You need birectional communication, but you don’t want to have open ports out to the Internet.

3. Presence Detection

Who’s there, (or in terms of IoT, what device is there)? It’s important to immediately know when an IoT device drops off the network and goes offline. And when that device comes back online, you need to know that as well.

Presence detection of IoT devices gives an exact, up to the second state of all devices on a network. This gives you the ability to monitor your IoT devices and fix any problems that may arise with your network.

4. Power consumption

IoT embedded devices are small and expensive, so CPU and power consumption need to be considered. When you have hundreds or even thousands of devices sending data and signaling one another, it takes a toll on power and CPU consumption. You need to maximize efficiency while minimizing power and CPU drain.

5. Bandwidth

In addition to power and CPU, bandwidth consumption is another challenge for IoT connectivity. Bandwidth on a cellular network is expensive, especially with hundreds of thousands of IoT devices on a network sending request/response signals to your server.

That’s a huge server issue and a requires a large scale server farm handling all this data. You need a lightweight network that can seamlessly transfer data between devices and servers.

Connecting IoT Devices with PubNub

Connecting devices in the lab is one thing, but once they’re out in the wild, it’s a whole new ballgame. So where do you start? Having a scalable IoT network to connect embedded devices and servers is especially critical for IoT applications with a large user base.

These are the types of Internet of Things challenges we’ve solved at PubNub. With over two hundred million connected devices connected to our global realtime network in fourteen data centers, we average 50 to 60 thousand transactions per second, peaking at over 3 million. PubNub is used to stream data and signal for hundreds of different IoT uses cases including:

  • Automotive: Connected cars need a realtime communication layer to stream data and signal between their fleet, dispatch, and the consumer on the app. Examples: Sidecar, Lyft, Easy Taxi, Gett, Zoomy
  • Home Automation: A realtime network can be used to signal and trigger actions for smart devices and home automation solutions. Examples: Insteon, Revolv, Vivint
  • Wearables: IoT wearables require a low latent, lightweight network to stream data between the device and a server. Battery, CPU, and bandwidth consumption are all important considerations that must be taken into account. Examples: 3rd Eye

By 2020, it’s estimated that there will be between 20 and 30 billion connected devices on the Earth. As a result, how we connect those devices should take precedence as the IoT field grows exponentially.