Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Intel Edison Projects

You're reading from  Intel Edison Projects

Product type Book
Published in May 2017
Publisher Packt
ISBN-13 9781787288409
Pages 264 pages
Edition 1st Edition
Languages
Concepts
Author (1):
Avirup Basu Avirup Basu
Profile icon Avirup Basu

Table of Contents (7) Chapters

Preface Setting up Intel Edison Weather Station (IoT) Intel Edison and IoT (Home Automation) Intel Edison and Security System Autonomous Robotics with Intel Edison Manual Robotics with Intel Edison

Intel Edison and IoT (Home Automation)

In Chapter 2, Weather Station (IoT), we dealt with transferring data from Edison to the cloud platform. Here, in this chapter, we'll be doing just the opposite. We'll be controlling devices using the Internet. When we talk about IoT, the first thing that usually comes to mind is home automation. Home automation is basically controlling and monitoring home electrical appliances using an interface, which may be a mobile application, a web interface, a wall touch unit, or more simply, your own voice. So, here in this chapter, we'll be dealing with the various concepts of home automation using the MQTT protocol; then, we'll be controlling an electrical load with an Android application and a Windows Presentation Foundation (WPF) application using the MQTT protocol. Some of the topics that we will discuss are:

  • The various concepts of controlling devices using...

Controlling devices using the Internet - concepts

When it comes to control devices using the Internet, some key factors come to play. Firstly, is the technique to be used. There are lot of techniques in this field. A quick workaround is the use of REST services, such as HTTP GET requests, where we get data from an existing database.

Some of the workarounds are discussed here.

REST services

One of the most commonly-used techniques for obtaining the desired data is by an HTTP GET call. Most of the IoT platforms that exist in the market have REST APIs exposed. There, we can send values from the device to the platform using an HTTP POST request, and at the same time get data by an HTTP GET request. Infact, in Chapter 2, Weather Station (IoT), where we used dweet.io to send data from a device, we used an SDK. Internally, the SDK also performs a similar HTTP POST call to send in data.

Instructions or alerts (present on most IoT platforms)

In certain IoT platforms, we have certain ready-made solutions where we just need to call a certain web service and the connection is established. Internally, it may use REST APIs, but for the benefit of the user, they have come up with their own SDK where we implement.

Internally, a platform may follow either a REST call, MQTT, or Web Sockets. However, we just use an SDK where we don't implement it directly, and by using the platform's SDK, we are able to establish a connection. It is entirely platform-specific. Here, we are discussing one of the workarounds,where we use the MQTT protocol to control our devices directly without the use of any IoT platforms.

Architecture

In a typical system, the IoT platform acts as a bridge between the user and the protocols to the controller, as shown in the following figure:

Architecture of the IoT system for controlling devices

The preceding image depicts a typical workflow or architecture of controlling devices using the Internet. It is to be noted that the user may directly control the controller without the use of an IoT platform, as we do here. However, normally a user will use the IoT platform, which also provides more enhanced security. The user may use any web interface, mobile application, or a wall control unit to control the device using any standard protocol. Here in the image, only REST, MQTT, and Web Sockets are included. However, there are more protocols that can be used, such as the AMQP protocol, the MODBUS protocol, and so on. The choice of the protocol depends mainly on how sensitive the system is and how stable...

Using Intel Edison to push data by using the MQTT protocol

As previously mentioned, this short section will show users how to push data from Edison to an Android device using the MQTT protocol. The following screenshot depicts the workflow:

Workflow of pushing data from the Edison to the Android application

From the preceding illustration, it is clear that we first obtain readings from the temperature sensor and then use the MQTT broker to push the readings to the Android application.

Firstly, we are going to connect the temperature sensor to Edison. Make a reference of the circuit from Chapter 2, Weather Station (IoT). After it is connected, fire up your editor to write the following Node.js code:

var mraa = require('mraa'); var mqtt = require('mqtt'); var B=4275;

var R0=100000;

var client = mqtt.connect('mqtt://iot.eclipse.org');
function sendData()

{

var tempPin=new mraa.Aio(0);

//Processing...

Getting data to Edison by using MQTT

We have been talking about home automation controlling electrical loads, but everything has a starting point. The most basic kick-starter is controlling Edison over the Internet—that's what it's all about.

When you have a device that is controllable over the Internet, we recommend controlling the electrical loads. In this other mini-project, we are going to control a simple LED that is already attached to pin 13 of Intel Edison. There is no need for any external hardware for this, as we are using an in-built functionality. Now, open your editor and type in the following code:

var mraa = require('mraa'); var mqtt = require('mqtt');

varledPin=new mraa.Gpio(13); ledPin.dir(mraa.DIR_OUT);

var client = mqtt.connect('mqtt://iot.eclipse.org'); client.subscribe('avirup/control/#') client.handleMessage=function(packet,callback)

{

...

Home automation using Intel Edison, MQTT, Android, and WPF

Until now we have learned about the MQTT protocol and how to subscribe and publish data, both using the application and Edison. Now we will be dealing with a real use case where we'll control an electrical load using Intel Edison, which again will be controlled by the Internet. Here is a quick introduction about what we will be dealing with:

  • Hardware components and circuits
  • Developing a WPF application to control Intel Edison
  • Using MQTT to stitch everything together

Since we've already seen how to control Edison using an Android application, this section won't concentrate on that; instead, it will mainly deal with the WPF application. This is just to give you a brief idea about how a PC can control IoT devices, not only in home automation, but also in various other use cases, both in simple proof of concept scenarios to industry standard...

Android application for controlling Intel Edison using MQTT

In the previous section, we saw how an Android application can be used to subscribe and publish to a channel using a broker. Here, in this section, we'll develop our own Android application for controlling the device using MQTT. The section won't concentrate on the set up of the Android, but will concentrate on the development side of it. We're going to use the Android Studio IDE for the development of the application. Make sure it's configured with all the latest SDKs.

Open your Android Studio:

Android Studio—1

Now, select Start a new Android Studio project:

Android Studio—set up application name

Enter a name for your application; here, we've entered MQTT. Click on Next to continue:

Android Studio: set API level

Now select the Minimum SDK version. Select API 23: Android 6.0 (Marshmallow). Now let's select...

Windows Presentation Foundation application for controlling using MQTT

WPF is a powerful UI framework for building Windows desktop client applications. It supports a broad spectrum of application features including models, controls, graphics layout, data binding, documents, and security. The programming is based on C# for the core logic and XAML for the UI.

Sample "Hello World" application in WPF

Before moving on to the development of an application for controlling Intel Edison, let's have a brief look at how we can integrate certain basic features such as a button click event, handling displaying data, and so on. Open up your Visual Studio and select New Project.

In PCs with low RAM, the installation of Visual Studio may take a while, as will opening...

Open-ended task for the reader

Now, you may have got a brief idea about how things must work in home automation. We have covered multiple areas in this niche. The task that is left for the reader is not only to integrate a single control command, but multiple control commands. This will allow you to control multiple devices. Add more functionality in the Android and the WPF application and go with more string control commands. Connect more relay units to the device for interfacing.

Summary

In this chapter, we've learned about the idea of home automation in its crude form. We also learned about how we can control an electrical load using relays. Not only that, but also we learned how to develop a WPF application and implement the MQTT protocol. On the device end, we used a Node.js code to connect our device to the Internet and subscribe to certain channels using the broker and ultimately receive signals to control itself. In the Android side of the system, we have used an already available MyMqtt application and used it to both to get and publish data. However, we also covered the development of the Android application in detail and showcased the use of it in implementing the MQTT protocol to control devices.

In Chapter 4, Intel Edison and Security System, we are going to learn how to deal with image processing and speech processing applications using Intel Edison. Chapter 4, Intel Edison...

lock icon The rest of the chapter is locked
You have been reading a chapter from
Intel Edison Projects
Published in: May 2017 Publisher: Packt ISBN-13: 9781787288409
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}