Tag Archives: ATSAM R21

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

Atmel’s SmartConnect lineup targets the IoT

Atmel has expanded its wireless product portfolio with a new family of solutions targeting the rapidly evolving Internet of Things (IoT). Essentially, the new SmartConnect family combines Atmel’s ultra-low power MCUs with its wireless solutions and complementary software into a single package, allowing designers to easily add wireless connectivity to any embedded system. This new lineup includes Atmel’s SmartConnect Wi-Fi modules, an integration of the company’s ultra-low power Wi-Fi System-on-Chip with a Cortex M0+ ARM-based MCU, and the SmartConnect ZigBee SAM R21, a single-chip integrating Atmel’s ultra-low power ZigBee solution with a Cortex-M0+-based MCU.

SmartConnect_chip

Atmel’s SmartConnect solutions can be deployed across a wide range of markets, helping to accelerate development time for cost-effective, battery-operated applications in the residential, healthcare, industrial, smart energy and wearable spaces.

“Atmel’s Wi-Fi solutions deliver industry-leading low-power consumption and are the leading market solution for power-constraint applications such as remote controls,” explained Reza Kazerounian, Sr. Vice President and General Manager, Microcontroller Business Unit, Atmel Corporation.

“Atmel also has a long history of providing ultra-low power wireless connectivity for sophisticated applications that are IEEE 802.15.4-compliant, IPv6/6LoWPAN-based and ZigBee-certified for nearly a decade. [Our] broad portfolio of wireless products combine the company’s rich family of RF transceivers with 8- and 32-bit AVRs and ARM-based MCUs.”

As Kazerounian notes, ultra-low power wireless connectivity is critical for embedded applications in the era of the Internet of Things.

“[That is why] Atmel’s SmartConnect technology is about simplifying the use of embedded wireless connectivity technologies and enabling users to accelerate their time-to-market. This simplicity allows all players to participate in the IoT market, fueling the innovation needed to accelerate adoption,” he added.

Greg Potter, Analyst, SNL Kagan MRG, expressed similar sentiments.

“With over 50 billion devices predicted to be connected by 2020, it’s important for embedded companies to provide an ultra-low power turnkey solution that brings a complex mix of embedded and connectivity technologies in a single package,” he said.

“Atmel’s new SmartConnect family does just that by coupling ultra-low power MCUs with Wi-Fi and ZigBee connectivity into a single package. The company’s breadth of easy-to-use IoT solutions, ranging from embedded processing to connectivity and software/tools, will enable more designers to bring their connected devices to market.”

Key facts about Atmel’s SmartConnect family

SmartConnect Wi-Fi

Providing designers with flexibility to help accelerate development, this new family of modules bring wireless Internet connectivity to any embedded design. 

The first products are an ultra-low power lineup of Wi-Fi modules that enable battery-powered IoT endpoints such as thermostats, temperature sensors through Wi-Fi connectivity – without compromising battery life.

These highly integrated modules will enable designers to lower their overall bill of materials while integrating IEEE 802.11 a/b/g wireless connectivity. 

Additionally, the new Wi-Fi modules provide an integrated software solution with application and security protocols such as TLS, integrated network services (TCP/IP stack) and standard Real Time Operating System (RTOS) which are all available via Atmel’s Studio 6 integrated development platform (IDP). 

SmartConnect Wi-Fi is slated to kick off mass production in May 2014.

SmartConnect ZigBee SAM R21



Building on Atmel’s long history of ultra-low power ZigBee solutions, these new products integrate Atmel’s ARM Cortex-M0+-based MCUs with a robust peripheral set and its high-performance RF transceiver. 

The new single-chip series is available in extremely small 5x5mm 32-pin and 7x7mm 48-pin package, effectively saving board space and reducing the overall bill of materials. The devices ship in a variety of memory densities and are qualified for industrial temperature grades up to 125C, making them ideal for wireless lighting control applications such as ZigBee Light Link.

These new devices are fully supported by the wireless composer in Atmel’s Studio 6 IDP and help accelerate development time. 

The ATSAM R21 Xplained PRO board is already available at the official Atmel Store, with Atmel currently sampling the ATSAM R21 series to select customers. Public sampling will be available at the end of March with production quantities slated for July 2014. Pricing for the SAM R21? Starting at $2.75 in 10,000-piece quantities.