A '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).
In this tutorial, we'll show you how to make remote configuration a reality for [Microchip-based] Arduino connected devices. In developing, we came across two issues.
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"} ]}
Onto the tutorial!
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.
Run this on the Arduino IDE, and check out the tutorial on connecting PubNub to Arduino in 2 steps for more details.
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);
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.