Tag Archives: RealTime Monitoring

Finger on the IoT Pulse: ‘Presence’ Functionality

We talk a lot about connecting, networking, and securing the Internet of Things, and the billions of devices spread across the globe. Another essential piece of the IoT puzzle is monitoring those devices, specifically with what we call presence. 

Presence functionality gives IoT developers a way to monitor individual or groups of IoT devices in realtime. Whenever the state of the device changes, the change is reflected in realtime to a dashboard, with an alert, or any other way you want to display your tracking.

Internet-Of-Things-Presence

What Can Presence Monitor?

As soon as you start streaming large volumes of data, or signaling and trigger actions to devices, you need to know what devices are connected. So what kinds of device states can you monitor with presence functionality? Pretty much anything you want! With Presence functionality, you can build out custom device states including:

  • Online/offline status
  • Device health
  • Capacity for fleet management
  • Total device count in field
  • Battery/location status
  • Machine status (eg. currently working on X task, driver driving/offline)
  • Temperature and weather data from IoT sensors

With presence data, you can also log a history of device connectivity for audits and analytics. It’s not just about having realtime insight into your devices, but also tracking and logging performance, health, and other key metrics.

Why Is It Important?

Devices may get expensive: IoT devices can be expensive, so keeping tabs on your investment is essential. Device health presence monitoring gives you up to the millisecond health reports for device temperature, connectivity, battery life, etc, ensuring you that your device is 100% operational, all the time. And if any issues arise, you’ll know immediately that maintenance is required.

Devices may be imperative to operations/business: If IoT devices are at the core of business and operations, monitoring their health and status is paramount. Whether it’s agriculture readings, security sensors, or delivery fleet management, up to the millisecond device status can make or break a business.

Device Analytics: Accurate and up to date statistics and analytics is important to any IoT application or business. Presence functionality can store, retrieve, and playback collected analytics, for example, to give a history of device connectivity or health for audits.

Machine-to-Machine and IoT Use Cases for Presence

As we know, connected devices come in all shapes and sizes. And as IoT devices get smarter, more connected, more secure, and faster, they’re use in the field is skyrocketing across the globe. And as we add more devices into the field, realtime presence functionality is just as important as our device networks and IoT security.

Agriculture: As with other connected technologies, the Internet of Things has found heavy adoption in the agricultural industry. Sensors and monitoring devices for temperature, irrigation, weather patterns, and harvest management give farmers a realtime, accurate data stream, giving them full control over their agriculture system. As a result, keeping tabs on their vast system of IoT devices with presence functionality is key.

Figure-1_Rosphere-537x300

Connected Car/Shipping & Freight: Smart cars are shifting IoT boundaries and constitutes a disruptive and transformative environment. Connected car represents a large number of IoT use cases for automobiles including taxi, fleet management, shipping and freight, and delivery service. Connected cars require a secure and reliable connection to counter the various roadblocks that arise in the wild, such as constantly changing cell and network towers and dropped connections.

For taxi, shipping, freight, and delivery management, custom presence functionality is a vital component of the business, providing realtime custom vehicle and device states, such as vehicle and cargo capacity, location data, and device health.

2a818e001e8179cd0a0888b8dba99809

Home Automation: We’re well aware that our homes are getting smart. It seems today, every appliance has an IP address. It’s safe to say that the smart home market is prepared to take the world by storm. Especially for applications that enable users to control their homes remotely, presence functionality is essential. In the smart home, presence gives users a realtime view of their devices status (lights on, doors locked, water leak, thermostat, fridge temperature, etc). And that’s the basis of a solid home automation solution.

Internet-of-Things

Presence on the PubNub Data Stream Network

PubNub Channel Presence is one of the core features of the PubNub Data Stream Network. It enables developers to add user and device detection to their web, mobile, and IoT applications, giving realtime instant detection and notification of user/device status. Built on the global PubNub Data Stream Network, no matter where the devices are located, you can get an accurate and reliable reading on any custom device state you want.

For a quick tutorial on using Presence for IoT devices, whether it’s a network of 1000 connected devices or a single Arduino, check out our blog post: Realtime IoT Monitoring for Devices with PubNub Presence.

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