Book Image

Learning Internet of Things

By : Peter Waher
Book Image

Learning Internet of Things

By: Peter Waher

Overview of this book

<p>This book starts by exploring the popular HTTP, UPnP, CoAP, MQTT, and XMPP protocols. You will learn how protocols and patterns can put limitations on network topology and how they affect the direction of communication and the use of firewalls. Thing registries and delegation of trust are introduced as important tools to secure the life cycle of Things on the Internet. Once the fundamentals have been mastered, your focus will move to the Internet of Things architecture. A secure architecture is proposed that will take full advantage of the power of Internet of Things and at the same time protect end user integrity and private personal data without losing flexibility and interoperability.</p> <p>This book provides you with a practical overview of the existing protocols, communication patterns, architectures, and security issues important to Internet of Things.</p>
Table of Contents (16 chapters)
Learning Internet of Things
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating a controller


We have a sensor that provides sensing and an actuator that provides actuating. But none have any intelligence yet. The controller application provides intelligence to the network. It will consume data from the sensor, then draw logical conclusions and use the actuator to inform the world of its conclusions.

The controller we create will read the ambient light and motion detection provided by the sensor. If it is dark and there exists movement, the controller will sound the alarm. The controller will also use the LEDs of the controller to display how much light is being reported.

Tip

Of the three applications we have presented thus far, this application is the simplest to implement since it does not publish any information that needs to be protected. Instead, it uses two other applications through the interfaces they have published. The project does not use any particular hardware either.

Representing sensor values

The first step toward creating a controller is to access sensors from where relevant data can be retrieved. We will duplicate sensor data into these private member variables:

private static bool motion = false;
private static double lightPercent = 0;
private static bool hasValues = false;

In the following chapter, we will show you different methods to populate these variables with values by using different communication protocols. Here, we will simply assume the variables have been populated by the correct sensor values.

Parsing sensor data

We get help from Clayster.Library.IoT.SensorData to parse data in XML format, generated by the sensor data export we discussed earlier. So, all we need to do is loop through the fields that are received and extract the relevant information as follows. We return a Boolean value that would indicate whether the field values read were different from the previous ones:

private static bool UpdateFields(XmlDocument Xml)
{
  FieldBoolean Boolean;
  FieldNumeric Numeric;
  bool Updated = false;

  foreach (Field F in Import.Parse(Xml))
  {
    if(F.FieldName == "Motion" && (Boolean = F as FieldBoolean) != null)
    {
      if(!hasValues || motion != Boolean.Value)
      {
        motion = Boolean.Value;
        Updated = true;
      }
    } else if(F.FieldName == "Light" && (Numeric = F as FieldNumeric) != null && Numeric.Unit == "%")
    {
      if(!hasValues || lightPercent != Numeric.Value)
      {
        lightPercent = Numeric.Value;
        Updated = true;
      }
    }
  }

  return Updated;
}

Calculating control states

The controller needs to calculate which LEDs to light along with the state of the alarm output based on the values received by the sensor. The controlling of the actuator can be done from a separate thread so that communication with the actuator does not affect the communication with the sensor, and the other way around. Communication between the main thread that is interacting with the sensor and the control thread is done using two AutoResetEvent objects and a couple of control state variables:

private static AutoResetEvent updateLeds = new AutoResetEvent(false);
private static AutoResetEvent updateAlarm = new AutoResetEvent(false);
private static int lastLedMask = -1;
private static bool? lastAlarm = null;
private static object synchObject = new object();

We have eight LEDs to control. We will turn them off if the sensor reports 0 percent light and light them all if the sensor reports 100 percent light. The control action we will use takes a byte where each LED is represented by a bit. The alarm is to be sounded when there is less than 20 percent light reported and the motion is detected. The calculations are done as follows:

private static void CheckControlRules()
{
  int NrLeds = (int)System.Math.Round((8 * lightPercent) / 100);
  int LedMask = 0;
  int i = 1;
  bool Alarm;

  while(NrLeds > 0)
  {
    NrLeds--;
    LedMask |= i;
    i <<= 1;
  }

  Alarm = lightPercent < 20 && motion;

We then compare these results with the previous ones to see whether we need to inform the control thread to send control commands:

  lock(synchObject)
  {
    if(LedMask != lastLedMask)
    {
      lastLedMask = LedMask;
      updateLeds.Set();
    }

    if (!lastAlarm.HasValue || lastAlarm.Value != Alarm)
    {
      lastAlarm = Alarm;
      updateAlarm.Set();
    }
  }
}