Tag Archives: ATmega256RFR2 Xplained Pro

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).


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.


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:


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) {
     public void errorCallback(String channel, PubnubError 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++){
		    	uuid1 = uuid1 + temp.charAt(index);
    	String replaced = uuid1.replace("\""," ");
    	String[] uuidlist = replaced.split("\\s+");
    	for (String tempstring : uuidlist){
	public void errorCallback(String channel, PubnubError error){
	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.


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:

     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 =[];


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

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

A closer look at Atmel’s Xplained Pro kits

Atmel’s comprehensive lineup of Xplained Pro boards offers engineers everything they need to start designing microcontroller (MCU) applications in minutes. First off, the boards are quite easy to connect, linking to PCs with just a USB cable.

As expected, the boards are automatically recognized by Atmel Studio, facilitating direct access to example projects and documentation. Meanwhile, hardware extension boards provide easy access to all functionality of the MCU.

Currently, Xplained Pro kits are grouped into three primary categories:

  • Evaluation kits – Lowest cost kits starting at $39 for evaluating MCUs and developing with example projects in Atmel Studio.
  • Starter kits – Low-cost bundle of MCU and extension boards starting at $99 for rapid application prototyping and development with Atmel Studio and Atmel Software Framework.
  • Extension kits – Boards with additional functionality, connecting to Xplained Pro MCU boards through standardized connectors.

On the evaluation side, Atmel offers the SAM D20 Xplained Pro, SAM4N Xplained Pro, SAM4S Xplained Pro, SAM4L Xplained Pro and the ATmega256RFR2 Xplained Pro.

In terms of extension boards, there is the I/O1 Xplained Pro, OLED1 Xplained Pro, SLCD1 Xplained Pro and the PROTO1 Xplained Pro.

Interested in learning more? Be sure to stay tuned, because next time we’ll be getting up close and personal with Atmel’s MCU Xplained (evaluation) kits.