Debounce, Gunnebo Business Solutions, Internet of Things (IoT), Microsoft Azure, Node RED

Debounce Algorithms in IoT

Working in IoT we sometimes need to handle large data streams of information, that might or might not be totally accurate. Streams might contain noise, inaccurate/unreal readings and other unwanted data.

Digital oscilloscope-1

Switch debouncing

Debouncing can be done on the hardware itself, or in software. Hardware debouncing can be done either using an S-R circuit or an R-C circuit. Two famous algorithms to do software debouncing is vertical counter and shift registers. Despite being well-known, in literature, these methods are typically presented as a code dump with little or no explanation. In this article, I will touch upon these circuits, methods and other algorithms and their use in IoT debouncing.

Understanding Switch Bounce

When the contacts of mechanical switches toggle from one position to another, these contacts bounce (or “chatter”) for a brief moment. During the first millisecond, the bounces are closely spaced and irregular, and although all of it happens in the course of milliseconds, high-speed logic will detect these bounces as genuine presses and releases.

Electrical panel. Low voltage device. Electrical equipment. Power supply. Electro substation. Power net.jpg

A button release produces bounces too, but it is common for a switch release to produce less bounce than for a switch press.

Switches usually become stable after 5-20ms depending on the quality, size and electronics of the hardware.

Hardware Debouncing

Debouncing using S-R circuits

Switch debouncing using S-R circuit is one of the earliest hardware debouncing methods. In this circuit, S-R latch avoids bounces in the circuit along with the pull-up resistor. It is still the most effective debouncing approach.

The figure below depicts a simple digital debouncing circuit which is used quite often.

1

The circuit utilizes two cross-coupled NAND gates which aim to create an S-R latch, A SPDT (Single Pole Double Throw) switch and two pull up resistors. Then the resistor produces and generates a logic ‘one’ for the gates and the Switch pulls one of the inputs to ground.

If the switch is kept in a position as  seen in the figure, the output of the upper gate is ‘1’ regardless of the input of the other gate and the one created by the bottom pull up resistor which stimulates the lower NAND gate to zero, rapidly in turn hustles back to the other gate. If the switch moves back and forth like a pendulum between the contacts and is suspended or halted for a while in neither one of the regions amidst the terminals, the latch preserves its’ state because ‘0’ from the bottom NAND gate is fed back. The switch may move between the contacts but the latch’s output assures that not in any way it would bang back and therefore, the switch is bounce free.

R-C Debouncing

Although S-R is still common, it’s bulkiness cause problems when you try to use it frequently. You can see that it uses many hardware pieces. Another drawback to using S-R circuits is SPDT switches are more expensive than SPST switches. Thus, a new approach of debouncing emerged using an R-C circuit. The basic principle behind it is to use a capacitor to filter out swift adjustments or changes in the switch signal.

The following image demonstrates a basic R-C circuit which is used for debouncing.

2

It is a simple circuit which uses two Resistors, a Capacitor, a Schmidt trigger hex inverter and an SPST switch.

  • In the event where the switch opens, the voltage across the capacitor which is initially zero begins to charge to Vcc through R1 & R2. The voltage at Vin is higher and hence, the output of the inverting Schmitt trigger is low (logic 0)
  • When the switch is closed, the capacitor discharges to zero and subsequently, the voltage at Vin is ‘0’ and output of the inverting Schmidt trigger is high (logic 1)

At the time of the bouncing condition, the capacitor will halt the voltage at Vin when it comes to either Vcc or Gnd.

You may wonder why a standard inverter is not used. There is a problem for using the standard inverter gate here. TTL defines a zero input when the applied voltage is between 0 and 0.8 and the output in certain circumstances or situations is very unpredictable or unforeseeable. Thus, we must use a Scmitt trigger hex inverter. Thereby, the output remains constant even if the inputs vary or dither and it also ensures to prevent the output from switching due to its’ hysteresis trait.

Software Debouncing

We can debounce switches using the software as well. The basic principle is still to switch signals and filter out glitches if any. The most used algorithms used for that are counters and shift registers.

Counter Method

The first approach uses a counter to time how long the switch signal has been low. If the signal has been low continuously for a set amount of time, then it is considered pressed and stable. 

Let’s see the steps in the Counter method. 

First, we set up the count value to Zero. Then set up a sampling event with a certain period, say 1 ms. You can use a timer for that. On the sample event, Do the following things.

If the switch signal is high, reset the counter variable to 0 and set the internal switch state to ‘released’. If the switch signal is low, increment the counter variable by 1 until it reaches 10. once the counter reached 10, set the internal switch state to ‘pressed’. 

Shift Register Method

Similar to that of the counter method. The only difference is that it uses a shift register. The algorithm assumes unsigned 8-bit reg value usually found in microcontrollers

First, set up the shift register variable to xFF. Set up a sampling event of period 1 ms with the help of a timer. On the sample event, Do the following things.

First, shift the variable towards MSB, the most significant bit. Set LSB, the least significant bit to the current switch value. if the shift register value is equal to 0, set internal switch state to ‘pressed’. otherwise, set internal switch state to ‘released’. 

IoT Sensor Bounce

Recently my team has been working on telemetry involving OCR decoding of License Plates. I consider data from an OCR routine, a temperature sensor or a push button the same thing and debouncing the telemetry can be done very much in the same way.

Collection of European license plates from different countries

First of all, we needed to clean up the data stream by filtering out incorrect values. Since there are not control digits on license plates, we chose to trust the result if the camera would return three similar plates within five iterations.

If you want to know more about how to debounce data streams or if you have any questions, please reach out to me: bjorn.nostdahl@gunnebo.com

 

Gunnebo Business Solutions, IBM International Business Machines, Node RED

Node-RED on SIMATIC IoT 2040

With the high pace the technology industry is moving in towards development, many different fields and areas have been considered as hot zones. The different innovations motivates researchers to create and develop better devices and technologies that are helpful. However, the more we advance, the more technology gets complicated and sophisticated. This is observed much more in hardware developments as the number of components used keeps increasing year by year to keep up with the need.

SIMATIC IOT2040 By Siemens

A leading company in innovations and developments is Siemens, which is German based. Siemens is specialized in technologies that would impact the Industry, Energy, Healthcare, and Infrastructure & Cities fields. With their many powerful groundbreaking products in the market, Siemens have been interested in the IoT field, releasing their SIMATIC IOT2000 series. This series targets the industry field, allowing different machines to analyse and utilize data sources from all around the globe.

iot2000-2040

Current issues include weak communication with overseas machinery due to different use of languages and different source codes. SIMATIC IOT2040 is one of the series by Siemens which is the up-to-date version to the SMATIC series. This version includes the following:

  • Energy-saving processor, with many compatible interfaces including: Intel Quark x1020 (+Secure Boot), 1 GB RAM, 2 Ethernet ports, 2 x RS232/485 interfaces, battery-backed RTC.
  • Supports Yocto Linux.
  • Arduino shields und miniPCIe cards can be used for expansions.
  • Programming with high-level languages
  • Compact design and DIN rail mounting
  • Proven SIMATIC quality offers great ruggedness, reliability and longevity

This version in particular is worth mentioning due to its ability to be used with many different hardware and solutions. This product is mostly used with different other add-ons, which help deliver the target efficiently.

Setup: SIMATIC IOT2040

A very common application for the SIMATIC IOT 2040 is the use in Micro-SD cards (minimum capacity of 16GB). Many of the previously stated features of the series can be used with the card, helping in making the experience much better. The following is a tutorial guide, explaining how to successfully prepare and install SIMATIC IOT2040:

Preparation:

  1. Download the following image from Siemens source: https://support.automation.siemens.com
  2. Remove all trash and deleted files fomr your SD-Adaptor and flash the image to your disk imager.
  3. You can now safely insert the SD-card in the SIMATIC IOT 2040.
  4. You should then connect your devices to the computer/laptop using an Ethernet cable.
  5. A strong internet connection should be available at all times. Adjust your Ethernet IP address to an IP around 192.168.200.1, which is considered as the static IP for IOT2040. (Subnet mask 255.255.255.0)
  6. Use the Secure Shell Protocol with the following IP: 168.200.1, using user root. Insert your own password for the root.
  7. SSH into 192.168.200.1 with user root. Set a password for root:
  8. Then run:
  9. Now you are ready to run the installation for SIMATIC IOT2040 successfully.

Installation:

  1. Edit file /etc/opkg/base-feeds.conf  and add these lines:
  2. Now you can run the file with the name “opkg update” and install the git directly.

Node-Red: Flow Based Solution

One of the most modern tools that is considered a breakthrough is Node-Red. Node-Red is a development tool, created by IBM initially, to wire hardware devices together with APIs and online services. This technology is flow based, which was inspired by the use of Internet of Things (IOT). In simple terms, the technology focuses on the use of  browser software that helps users to develop different tools using flow diagrams. It was created as a means to simplify development; making it available for users with basic knowledge. The tool focuses on the ease of use of software and online services, using a direct connection through the internet.

Untitled-3

The previously stated technologies could come really handy when they are used together. Both tools have the capability to be used in parallel aiming towards the same final result. NodeRed will be used to ease the use of SIMATC through simple flow diagrams. SIMATIC is generally a very important tool, yet too complicated for normal users. Thus, the use of NodeRed is crucial in this case, where you will need to control the development process and wiring of the hardware with the online services as smooth as possible.

Setup: Node-Red

The following is a tutorial guide, explaining how to successfully prepare and install Node-Red:

  1. Through the menu named software, you can move forward to the Manage packages page.
  2. Set Node-Red on Auto-start together with Mosquitto MQTT Broker.
  3. Here is where we integrate SIMATIC IOT 2000 with Nod- Red. You are expected to install the nodes for the SIMAMTIC.
  4. In the following directory “/home/root/.node-red” create a root folder by the name of nodes; where you will place the installed nodes in the previous step.
  5. Put custom nodes here if needed. For example from Git repository:

    Dependencies and nodes under npm can be installed directly to  /home/root/.node-red
  6. In the following directory “/home/root/.node-red” create a root folder by the name of nodes; where you will place the installed nodes in the previous step.

 

If you want to know more about IoT and Node-RED, feel free to contact me at: bjorn.nostdahl@gunnebo.com 🙂

Gunnebo Business Solutions, IBM International Business Machines, Mender, Node RED, Technical

Mender IoT Device Management

With the progress in humanity, innovations are starting to move towards being digitalized. The vast majority of all human data are stored using digital methods. This would involve the use of computers, cloud computing and the Internet of Things (IoT) which is one of the latest technological disruption. These technologies are used to connect devices through digital channels, which are used to transfer data back and forth. On the other hand, the digital world is in constant need for updates. These updates are essential to cope with the increase in data and the overall customers’ requirements.

Update Software Computer Program Upgrade Business technology Internet Concept-1

Why is Software Update Essential?

  • Bugs: One of the main problems in computing technology is the number of bugs that rise due to weak developing skills or high amounts of data; which was not accounted for. Updating your software will acts as means to fix and overstep such bugs.
  • Security: Unfortunately, cyber security is a huge issue in this era. With many threats rising in the field, updates are always released with better security settings in hopes to lower and eliminate threats.
  • Features: The most common reason for software updates is to release new competitive features that copes with the customers’ requirements.

However, with the high numbers of devices invading our planet, it is impossible to provide these software updates through physical means. This is why, Over-the-air (OTA) methods are the most efficient way to deliver software updates. In some cases, the only available method to use is OTA; where physical means can’t be used. The process of software update through OTA is a very complex process where the data is delivered over networks and digital channels to reach your device. This is a very delicate process where you have to ensure proper connection and power connectivity to avoid any errors in the process.

Mender: Your New Solution

With the intensity of such transactions, you should always look for the best service out there to implement the process as efficient as possible. Trying to build your own infrastructure that will achieve efficient OTA might be a real hassle. The amount of time and work spent on the process is way more than you can handle, so is the cost you are about to pay too.

Mender

This is why, companies should look for the different software update solution companies out there. Here is where Mender kicks in; it is an end-to-end open source software update solution for connected devices and IoT. You can consider Mender as a ready-made infrastructure that will solve all your software update issues.

Why we use Mender?

No Vendor lock-ins: One thing to look forward too while using Mender is the fact that we won’t face any vendor lock-ins. Mender is an open source, licensed under Apache 2.0. This gives it the complete freedom of being used by the customers without the interference of vendors or other third parties. With Mender, you should no longer worry about getting locked in.

Global communication network concept.png

Reduction in customer support issues: Mender focuses on their customers’ experience, making it as smooth as possible. This is achieved through strong security protocols during the update process. The process is focused to be as efficient and optimal as possible, compensating for any pitfalls in the connection. Mender uses image-based updates which acts as a safety net when connectivity problems rise. It will ensure full device connectivity at all times leading to a decrement in system failures and device recalls.

Features and Functionality

The developers of Mender are very concerned with the common software update issues and the hassle customers go through. This has helped them develop Mender with more features that other software update solution provider that surely helps in making the process simpler and more effective for the users. The following is a list of some of the features you can enjoy while using Mender:

  • Intuitive UI
  • Deployment Reports
  • Custom checks via scripting support
  • Code signing

Anticipated Progress and Updates

With Mender, there is still much more to look forward too. Gunnebo, which is a multinational business specialized in security services, are of course interested in contributing to Mender, possibly to help implementing the features we and other companies like us needs.

Our first project will be updating Node-RED flows from the Mender v.2 update module. If you are interested in contributing or want to know more – feel free to contact me at: bjorn.nostdahl@gunnebo.com

Gunnebo Business Solutions, IBM International Business Machines, Microsoft Azure, Node RED, Technical

Node-RED deployment on Azure

Today I would like to talk about the process of deployment Node-RED instances on Azure platform.

The initial tasks were:

  1. Deploy Node-RED instance to Azure cloud and provide public IP address/ DNS name to it.
  2. Secure Node-RED instance access with user credentials.
  3. Update instance with actual node set and provide ability to keep them up to date.

Let’s discuss all steps one by one.

Azure deployment

The most common and convenient way to deploy your application on Azure platform is by using Azure Resource Manager. It enables you to use all application resources as a group and to deploy, manage or delete them just in one operation. With Resource Manager, you can create a template (Azure Resource Manager template) that defines the infrastructure and configuration of your Azure solution. It allows you to deploy your solution repeatedly throughout its lifecycle being confident that your resources are deployed in a consistent state.

Resource Manager template is a JSON file that defines resources which you need to deploy to a resource group. Resource Manager analyzes the template and then converts its syntax into REST API operations for the appropriate resource providers. For the resources to be deployed in correct order, you can set dependencies between them.  It is done when one resource relies on a value from another resource, for example, in case of a virtual machine which needs a storage account for disks.

You may wonder, “What resources are and why we need them? We just want to deploy NodeJS application (Node-RED) on Azure”.  Well, a resource is a manageable item that is available on Azure. Some common resources are a virtual machine, a storage account and a virtual network, but there are much more. To start Node-RED in the cloud, we need to create VM and deploy a Docker container(image) with Node-RED inside. Since one resource relates to another one, we should create a bunch of resources in our resource group (that is a container holding related resources for our Azure solution). It includes:

  • Storage account
  • Public IP address
  • Virtual Network
  • Network interface
  • Network security group
  • Virtual Machine
  • Extensions

Resource Manager provides extensions for scenarios in case you need additional operations such as installing particular software which is not included in the setup. We used Docker Extension in order to setup Docker container on VM.

Ok, so now we are ready to create a template. The detailed description can be found here.

Here I would like to  talk only about extension section:

At this stage, we define DockerExtension resource that depends on our Virtual Machine resource. We specify to use “nodered/node-red-docker” image from DokerHub

Also, we need to enable Docker Remote API for further use:

Since we need to get access to the API we also expose port in Network Security Group:

Also, we need to map 80 VM port to 1880 (default port for Node-RED):

After defining the template, we are ready to deploy the resources to Azure. There are several ways to do that: PowerShell, Azure CLI, Azure Portal, REST API or Azure SDK.

Since we want to develop automation solution for application deployment, REST API and Azure SDK seem to be most suitable for us.  The reason why I want to highlight the Azure SDK for .NET is that it is much easier to build an application using existing wrapper classes for the API than to create your own REST wrappers and methods

Take these four steps to deploy your template with C# SDK:

1. To be able to make any requests to the API, first we need to authenticate and authorize our request. Let’s create the management client:

azureauth.properties  –  authorization file. Before you can deploy a template, you need to acquire a token for authenticating requests to Azure Resource Manager. You should also record the application ID, the authentication key, and the tenant ID which you need in the authorization file.

2. Create resource group and storage account:

3. Upload your template file to Azure:

4. Deploy template:

That’s it. On the whole, the deployment process in our case takes about 3-5 mins.

To retrieve public  IP address our Docker container is available on:

So now we have Node-RED instance up running on Azure cloud and accessible via public IP/DNS name. Let’s proceed to the next step.

Secure Node-RED instance

Node-RED Editor supports two types of authentication:

  • username/password credential based authentication
  • starting from Node-RED 0.17, authentication against any OAuth/OpenID provider such as, for example, GitHub or Twitter 

If we choose the first option, we need to add the following to our settings.js file:

Since we want to make this credentials customizable for each deployment we can’t embed this configuration in Docker file. So we need a way to execute commands inside Docker container after deployment. That’s why we use Docker Remote API to adjust credentials settings. And this is the reason to expose additional port in our template, as mentioned above.

Here is a command example to setup credentials for Node-RED:

We used .NET Client for Docker Remote API as a wrapper to REST API:

Now we have secured our Node-RED editor with custom username and password.

Keeping nodes and flows up to date

Now we need a way to provide our cloud Node-RED instance with custom node’s set and keep it up to date. We already have all tools for that. Custom nodes are stored in separate Git repository. A few options are available:

  1. Execute npm install <git repo url>  inside Node-RED userDir ( /data  for "nodered/node-red-docker"  container)
  2. Copy custom nodes to /data/nodes inside a container.

Node-RED flows can be synchronized in a similar way. By default, Node-RED Docker container stores flows data in /data/flows.json. The flows configuration file is set using an environment parameter ( FLOWS), This can be changed by setting environment variables in docker-compose configuration section:

Using this approach we can put nodes and flows file under version control inside a container and synchronize them with a remote repository.

All commands can be executed via Docker Remote API in the same way, as described in the previous section.

Each time we need to update our nodes, we just call Docker API and pull updates from repository. Also, we can backup our flows.json  by committing and pushing it into the repository.

As an improvement, we can create Git hook in order to update our Node-RED instances once some changes are pushed to our node’s repo. But this is out of the scope of this post.

Summary

Here we make a short overview of how to automate your deployments on Azure cloud with Azure Resource Manager and Azure SDK for .NET. In our example, we set up Node-RED docker container in the cloud but all mentioned steps are applicable to any similar Docker deployments.