How to build remote configuration for Arduino devices

Arduino-Remote-ConfigurationA ‘smart’ device isn’t smart if you can only control it locally. What good is that? You need the ability to control your devices remotely. And that’s where remote configuration comes into play.

Remote configuration enables developers and users to trigger actions and control their devices from anywhere. The device state interprets a signal that is transferred through a network (in this case, PubNub), and is interpreted to permanently change the state of a device (like a light bulb turning from off to on).

A Messaging Layer for Remote Configuration

In this tutorial, we’ll show you how to make remote configuration a reality for [Atmel based] Arduino connected devices. In developing, we came across two issues.

  1. The need to detect the device state of all connected devices (eg. online/offline status) in realtime
  2. The state of the devices connected to the cloud

The first one can easily be solved using the Presence API, so let’s focus on the second issue. We’ll be using Storage & Playback to store and retrieve the state of connected devices for a home automation solution. In this case, we’ll be able to control smart devices remotely, sending signals in realtime.

A typical example of a state will be a JSON object looking like this:

{"state":[
    {"alive":"true"}, 
    {"CoffeeAmount: Half"},
    {"Temperature":"45"}
]}

Overall Procedure for Arduino Remote Configuration

  • Sign up for a free PubNub account. In doing so, you’ll get your unique publish/subscribe keys which you’ll use to initiate PubNub. These keys can be found in the Developer’s Console.
  • Publish messages through the PubNub IoT Network using Arduino to control smart devices.
  • Access these messages through a web browser.

Onto the tutorial!

Step 1: Connect to PubNub

Say we want to control the temperature on a refrigerator. The following parameters can be stored as the state of the device, for example, seeing if the door is open/closed, temperature, power consumption, and even volume of our fridge.

arduino remote configuration home automation

Run this on the Arduino IDE, and check out the tutorial on connecting PubNub to Arduino in 2 steps for more details.

Step 2: Create a JSON Object in Arduino

To create a json object in Arduino, we use the JSON.h library. The above JSON object can be created using the following:

aJsonObject *msg = aJson.createObject();
	aJson.addStringToObject(msg, "name", "Arduino");
	aJson.addNumberToObject(msg, "TemperatureOutside", 34);

Step 3: Signaling Devices

Other than that, it’s just about publishing a message using the Arduino IDE. The following code lets you do just that:

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char pubkey[] = "demo";
char subkey[] = "demo";
char channel[] = "devices_state";

//setup 
void setup()
{
	Serial.begin(9600);
	Serial.println("Serial set up");

	while (!Ethernet.begin(mac)) {
		Serial.println("Ethernet setup error");
		delay(1000);
	}
	Serial.println("Ethernet set up");

	PubNub.begin(pubkey, subkey);
	Serial.println("PubNub set up");
}

// establishing internet connection to the Arduino and publishing the state
void loop()
{
	Ethernet.maintain();
	EthernetClient *client;
	client = PubNub.publish(channel, msg);
	if (!client) {
		Serial.println("publishing error");
	} else {
		client->stop();
	}
	delay(5000);
}

And one more thing, what if we were able to check the state of devices in simply a web browser? Paste the code below in a browser console and watch the state be filled in.

// Initialize with Publish & Subscribe Keys
 
 var pubnub = PUBNUB.init({
     publish_key: 'PUBLISH_KEY_HERE',
     subscribe_key: 'SUBSCRIBE_KEY_HERE',
     uuid: 'devices_state'
 });
 
 // Retrieving the messages that have been published on to this channel using History. 
 
 pubnub.history({
     channel: 'history_channel',
     callback: function(m){console.log(JSON.stringify(m))},
 });

That’s it! Just a note, PubNub doesn’t limit the number of messages that you can publish or retrieve via history. You can try this out with as many Arduinos as you want, and check their state remotely.

The use cases are endless for remote configuration, from agriculture to shipping and logistics, to home automation. It gives you complete control over your smart devices from anywhere on Earth, and that’s powerful.

IoT Resources

1 thought on “How to build remote configuration for Arduino devices

  1. Pingback: Building an Arduino-powered Smart Home Model | Bits & Pieces from the Embedded Design World

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s