IoT in my life. How to create a smart office, as well as track the growth of potatoes in your apartment thanks to IoT

I greet you, Khabrovchians!

In 2020, we all know what the Internet of Things is and why it is needed. But how many of us are familiar with cloud platforms that represent one of the most significant layers in IoT? Let's get it right.

It is no secret that the heterogeneity of the protocols significantly complicates the processes of connecting smart devices, their configuration and data processing. Similar problems are being addressed through the cloud of the Internet of Things. Today, using one of the Russian Internet of Things platforms as an example, I will show how easy it is to connect devices with different protocols, as well as use the information received to build automation processes.

In the platform that I usually use for my tasks, I have already implemented interaction with devices working using protocols such as MQTT, Wialon Combine, Wialon IPS, Galileosky, Modbus and some others.

In addition to using the presented protocols, for devices that do not have Internet access, it is possible to write software agents - some intermediaries between the equipment and the platform that are installed on another device (for example, Raspberry Pi) and connected to this equipment.

Suppose you want to ensure interaction with a device that works on one of the presented protocols. In this case, it will be enough to complete three steps:

  1. configure the model with the desired parameters and commands;
  2. create an object with a unique identifier in the platform;
  3. configure the device to connect to the platform.

Let's analyze a few cases and see how it all connects.

Case No. 1 Agile-gong


To begin with, one day our team seriously thought about how to automate work processes in the office.

So, in accordance with the Agile concept, at noon, all employees gather at the Daily meeting. It’s easy to miss a notification in Slack about an upcoming meeting during the work and it’s not very convenient to be distracted by the watch ... So the idea was born to create Agile-gong - an automated sound notification system.

How is it implemented? Iron is NodeMCU (a miniature analog of Arduino with a built-in Wi-Fi module), a servo drive and a capacitor. Every weekday at 12 o’clock you need to ensure that the output shaft of the servo drive with impact equipment at the end is turned at an angle sufficient to ring the gong and notify everyone of the rise.

The connection diagram for iron is quite simple:

image

Code wired on NodeMCU provides:

  1. installation of Wi-Fi connection and connection to the platform using the MQTT protocol;
  2. setting the initial position of the servo in 0 degrees;
  3. publication of messages with data on the current situation;
  4. subscribing to commands and turning the servo angle by command.

#include "Arduino.h"
#include "EspMQTTClient.h" /* https://github.com/plapointe6/EspMQTTClient */
// Servo library
#include <Servo.h>

// Object Servo with name myservo
Servo myservo;
int pos;

EspMQTTClient client(
  "<wifi-ssid>",
  "<wifi-password>",

  "<MQTT Broker server ip>",
  "<ric-mqtt-client-id>"
  );


void setup() {
  Serial.begin(9600);
  move(0);
}

void onConnectionEstablished() {
  Serial.println("connected");
  client.subscribe("move", [] (const String& payload)  {
	int angle = payload.toInt();
	if (angle != pos) {
  	move(angle);
	}
	client.publish("position", payload);
  });
}

void loop() {
  client.loop();
}

void move(const int angle)
{
	myservo.attach(5);
	myservo.write(angle);
	delay(800);
	myservo.detach();
	pos = angle;
}


On the platform side, a device model has been developed. It describes the parameters that can be received from the device, and the commands that can be sent to it. In the interpretation of the MQTT command, these are messages for the client with a certain topic and data, in our case, the data contains the necessary rotation angle.

image

Then an object was created with an identifier by which authorization on the platform occurs. After connecting, the display looks as follows:

image

In teams there is an option to send a rotation command at an angle of 0 and 90 degrees.

image

Now you need to add automation scripts. We will create an automatic machine that, when the right time comes, will go into a 90-degree rotation state, then in the cycle for a configurable number of repetitions, it will make the necessary number of strokes and return to the initial waiting state of 12 hours.

Each automation scenario is a certain block diagram that defines the logic of the object’s behavior. Having registered such a scenario, you can take into account all the changes that occur with the device, and based on exactly what changes have occurred, the device will be able to perform appropriate actions automatically, without sending a command to the user.
The resulting machine can be used not only for a particular device.

For example, you can make the exact same system with a gong and install it in another office in your office. Then you will have the same model, two different objects and one machine running on two objects.

image

Case No. 2 Carbon Dioxide Sensor


The second useful solution for us was to connect a carbon dioxide sensor. Also connected via MQTT. Again, the iron assembly scheme is trivial.

image

Yes, by the way, we were engaged in the implementation of both the first and second case within the hackathon inside the company. And none of us was immersed in the work of iron, and there was no need for this.

image

Further the procedure is the same. The model includes the ppm parameter (1000 ppm = 0.1% of the 2 content), which the device transmits, but it is not very obvious, therefore, another parameter was immediately displayed in the model - the percentage of CO2 content. It is calculated as ppm divided by 10000.

image

Here you can also notice two commands to turn on the light bulb. They decided to use it for indication. And we manage it, of course, from the platform machine. After connecting the device, the display of the parameters is as follows. These values ​​are accepted and displayed in real time, but you can also view past accumulated packets in history or display a graph of parameter changes over a certain period.

image

The automaton for this object works as follows. In the upper state, the light turns off. In the bottom - start the timer for a minute and turn on the light bulb. The transition from the first state to the second occurs by the event of receiving data from the device with the condition that the ppm value is more than 600 units. Return (transition from the second state to the first) occurs when the timer is triggered.

image

You may have two questions.

  1. Why automatic? Isn’t it easier to register such conditions on the hardware itself? After all, everything is so simple here.
  2. Why is there a timer?

In fact, the benefit of the machine is even in such a simple case. I put this sensor with a light bulb on my desk for debugging, and every time I came to work, the light came on, as the threshold value in the machine was quite low. For a while I tried different values ​​in the machine and as a result I came to the optimal value of 600 units. To select the desired value, I just needed to change the value in the machine and save it. No flashing of the device. And if we transfer this device to an office where it is necessary to maintain the best air condition and frequent ventilation is necessary, then the value can again be simply changed. Fast and convenient.

Here is a timer for a minute. This is necessary so that for a minute we are in a state of high CO2 and not react to the fact that for a while the high value continues to come. Otherwise, we would constantly blink a light bulb, making transitions until the air condition normalizes. You could already guess that it is possible to make a transition to the initial state in a different way. Also on the event of receiving data, but in which the opposite condition is true - ppm <600. Then we will be in the second state exactly until the normal value comes.

Case No. 3 ACS


The most complex example will be the description of working with an access control and management system, which is an electronic module designed to control access to premises, accounting for passage times and events.

image

The controller processes the information coming from the reader with the Wiegand output interface and, using the built-in relay, commutes the actuator - the electromagnetic lock. It has no Internet connection and no visible connectivity to the platform. However, it has its own protocol for exchanging data with the control computer, thanks to which it is possible to send commands to the controller, such as reading from the controller, writing to the controller, opening / closing the lock, and others. Therefore, in this case, a non-standard approach was organized - the use of the agent, which I mentioned at the beginning of the article.

Work on the controller protocol was implemented in C ++ code and launched for execution on the Raspberry Pi, which in turn was connected to the controller via RS-485 via an interface converter. The main task of the program is to connect to the platform, serialize commands and deserialize the data received from the controller. Thus, we were able to make the device “smart” using a small software layer.

The device model is as follows:

image

The main information from the controller is events. It comes to the platform in JSON format and includes fields:

  • event time,
  • event code
  • employee card number.

A model is also used to parse JSON fields for various parameters.

image

In the object’s interface, it looks like this:

image

And this is the interface for sending commands:

image

You may notice that there is a command not only to read the event buffer, but also to write new boundaries. The buffer boundaries are stored in the controller memory - the beginning and the end. When a read command arrives at the device, these boundaries are read and within these limits, reading from the event buffer takes place. The end of the buffer boundary is shifted automatically on the controller when new events are received. But the initial border of the buffer needs to be rewritten (indicating the final border after the last read) so as not to read the same data again. But this needs to be done only after the event data has been successfully sent to the platform. It is also convenient to lock the receipt of data and then send a command to rewrite the boundaries in the machine.

image

This project found its continuation in integration with our internal CRM system, in which on the information page about employees we always see up-to-date information about who is or is not in the office. Also, the time of entry / exit from the office is displayed, the total number of hours per month is considered.

image

image

Data is taken from the platform using the RESTful API. The platform API provides the ability to work, interact and use the platform entities and their data in such external systems as web portals, mobile and web applications, or, as in our case, CRM systems.

There are also cases when a guest / food delivery person or someone else who needs to open the door has come to the company. In order not to use your card and thereby not transmit incorrect readings about your status, you can use the “Unlock” button on the platform. And if a person needs to be met at the door, then it’s convenient to do the same from a mobile application.

Case No. 4 Smart Garden


My personal story with the garden in the apartment began against the backdrop of a crazy panic of people and buying products. Once again, going to the store and seeing empty shelves where the potatoes should be, I decided to use the last found potato in the refrigerator not for its intended purpose. I planted this potato in a huge pot. With such a naive experiment, my garden on the windowsill began, which two months later already looks like this:

image

Since I don’t know which grower, and the garden needs even more water than flowers, I quickly encountered a problem that I forget to water it. I won’t talk about automatic irrigation systems, this is too much of a topic, and it’s quite difficult to organize its work qualitatively. Instead, I had the following ideas:

  1. - , - . , .
  2. , , , — . , , , . , .
  3. – 18:00, 6:00. , — . , , / , .

The interface will look like this: The

image

machine for the first case looks like this. The transition to the state in which the notification is sent is made under a difficult condition - in one of the plants the humidity is below normal. The link between the conditions is OR.

image

The return to the initial state occurs according to the condition - in all plants the soil moisture is higher than normal, bunch I. The

image

machine for the second case is as follows. The transition is carried out according to the scheduler, the reset is the unconditional transition.

image

And finally, the machine for the latter case:

image

These machines are running on the same object and work in parallel.

image

Perhaps this is all that I wanted to cover in my article. The main idea that I wanted to convey was that working with the Internet of things platform incredibly facilitates the creation of business processes of any complexity, because in this case you need to study only one interface - the platform interface, which avoids deep immersion in the work of iron and its programming.

All Articles