How to Connect an IoT Device to an Industrial Platform Using Embedded Linux

by Nov 18, 2021#IoT, #HomePage

Printer Icon

Table of Content

  1. Introduction
  2. IoT Gateways
  3. IoT Communication
    1. IoT Sockets
    2. Sockets
      1. Python Code Server
      2. Python Code Client
    3. WebSockets
      1. Node Socket Server (WebSocket Server)
      2. JavaScript Socket Client (WebSocket Browser)


The internet has been growing extensively during the last year, and many new physical devices have been integrated with the internet, which has generated more of what we know as the Internet of Things (IoT). IoT, where devices are connected to and integrated with the internet, is considered the second wave of the information industry, after the initial development of the internet, computers, and mobile systems. Recently, IoT has been expanding in all industry sectors, including agriculture, transportation, and healthcare, for example.

As IoT continues to develop, one of the main challenges is the uncontrolled growth of physical devices connected to a network sending data to a central edge computer. With that in mind, one of the most feasible solutions for distributed systems and large-scale systems is the approach of using micro clouds and IoT gateways.


IoT Gateways

The use of IoT gateways is a key component of edge-computing architecture. With the benefits of centralization, data filtering, complex analysis, and visibility, gateways can be taken to an industrial scale. In particular, embedded Linux can be used on a single board computer as an IoT gateway.


Embedded Linux Devices Connected to Power Supply


Single board computers (SBC) regularly use an ARM processor and embedded Linux. (This little computer is available for around $4.00 now.) Single board computers support a wide range of computing capacities, rich I/Os, and expansion slots. For this reason—and for their wide variety of high-level programming languages—SBCs are gaining a very important place in the industry. Some of the most popular languages ​​used to program both embedded devices and IoT devices are C, Python, Nodejs, and PHPoC.


IoT Communication

The embedded Linux on a single board computer can be used as an IoT gateway. These gateways have several types of interfaces for sensors and IoT devices, and they can handle both digital and analog pins. Additionally, these gateways can use a wide variety of interfaces, including I2C, UART, PWM/Timers, SPI, A/D converter, LCD, and more. The gateways can also include internal sensors, such as temperature, accelerometer, barometer, and gyro, to name a few.

These interfaces and sensors can be programmed to communicate with the SPI, I2C, and GPIO ports of the embedded devices, generating a gateway for high-scale industrial applications. Gateways can be programmed for several different microservices as well, depending on the needs of each IoT device as it filters these small embedded systems to deliver information to more complex systems.

The following code shows how to handle the digital ports in the Python program.



import RPi.GPIO as GPIO

GPIO.setup(26, GPIO.IN, pull_up_donw=false)

while True:
    input_state = GPIO.input(26)
    if input_state == False:
        print('Button Pressed')

Python’s code handles GPIO ports.


On the other hand, gateways also have ports and interfaces that communicate with other systems or with another variety of device, such as Ethernet ports, Wi-Fi, Bluetooth, and Zigbee, for example. Therefore, these IoT gateways provides key infrastructure for digital transformation in industrial systems. These embedded systems (IoT gateways) can support multiple protocols that will help improve the security in the communication channel and disk.

The use of IoT gateways is one of the most standard solutions for overall communication in the world of IoT interfaces, from sensors to large-scale applications, because gateways use sockets, the most common form of communication on the internet. These sockets can be implemented by Python, Node.js, C, and other sources.


IoT Sockets

The connection between IoT devices and IoT gateways can be managed through different communication interfaces, such as Bluetooth, Zigbee, LTE-M, and others. The IoT gateways are used as a filter for the data systems, and the connection between them could use another communication interface, such as Wi-Fi, Ethernet, or fiber optics, for example. Therefore, one of the most common channels used for communication between IoT gateways and data systems is the tcp/ip socket.

This diagram shows how IoT devices are connected to data systems.

Infographic of how IoT devices are connected to data systems.



The socket concept focuses on communicating two processes (which could be located on different computers) that can exchange any data flow, generally in a reliable and orderly manner. Thus, sockets can be used to generate IoT gateways that communicate between IoT devices and data systems.

The term “socket” is also used as the name of an application programming interface (API) for the TCP/IP family of internet protocols, usually provided by the operating system.

Sockets can be programmed in Python sockets, Node.js sockets, or C sockets, to name a few, and these sockets will assist in establishing communication with IoT devices. Sockets are the basis of all communication on TCP/IP as well as in the evolution of web services, microservices, and more.


Python Code Server

The following code exemplifies how a server (socket stream/TCP) can be created with Python sockets and GPIO ports (simulating that the GPIO ports communicate with an IoT device).

import socket
import RPi.GPIO as GPIO

HOST = ''  
PORT = 3001        

GPIO.setup(26, GPIO.IN, pull_up_donw=false)

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((HOST, PORT))
    conn, addr = s.accept()
    with conn:
        print('Connection by:', addr)
        while True:
            input_state = GPIO.input(26)
            if input_state == False:
                data = b'on'
                data = b'off'


Python Code Client

This code illustrates how you can set up a client with Python sockets. The client can then connect to the Python server and receive the status of an IoT device.


import socket

HOST = ''  
PORT = 3001        

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((HOST, PORT))
    data = s.recv(1024)

print('Sensor trigger', repr(data))


The WebSocket is an API that establishes socket connections (stream/TCP type) between the web browser and the server. In other words, there is a constant connection between both parties, who can send data at any time.

Additionally, WebSocket assists in interacting with the IoT device in real time.


Node Socket Server (WebSocket Server)

The following server is a code that exemplifies the use of WebSocket with Node.js in communicating with the GPIO port of an SBC (simulating that an IoT device is connected to that port).

var app = require('express')();
var http = require('http').Server(app);
var io = require('')(http);

import { Gpio } from 'onoff'; //library  GPIO
var LED = new Gpio(4, 'out'); //use GPIO pin 4

app.get('/', function(req, res) {
   res.sendFile('index.html', { root: __dirname });

io.on('connection', function(socket) {
    console.log('Client connected ');
    //Send a message after every 10 seconds
    blinkInterval = setInterval(function() {
        if (LED.readSync() === 0) { 
//check the pin state, if the state is 0 (or off)
            LED.writeSync(1); //set pin state to 1 (turn LED on)
            socket.send('Led on !');
          } else {
            LED.writeSync(0); //set pin state to 0 (turn LED off)
            socket.send('Led off !');
    }, 10000);
    socket.on('disconnect', function () {
        clearInterval(blinkInterval); // Stop blink intervals
        LED.writeSync(0); // Turn LED off
        LED.unexport(); // Unexport GPIO to free resources

http.listen(3000, function() {
    //by Fggn 2021
   console.log('Krasamo  test server:3000');

JavaScript Socket Client (WebSocket Browser)

The following code exemplifies how a WebSocket client could be created for the browser so the client can receive the status of a device.


<!DOCTYPE html>
      <title>GPIO Status</title>
   <script src = "/"></script>

      var socket = io();
      socket.on('message', function(data){document.write(data)});

   <body>Krasamo Node Socket IoT by Fggn</body>


Embedded Linux is an actively developing and vast field. One of the most significant advantages of using Open Source Code based on Linux in your embedded development projects is that complete source code is freely available and reduces the costs of licenses providing security and highly scalable solutions.

Microservices considered the best way to evolve and achieve scalability in IoT projects, microservices include services with enterprise capabilities for application deployment. Therefore, identifying and defining microservices is a crucial aspect of IoT architecture design that makes business sense and enables the independent development and deployment of applications in the cloud. Services collaborate through application programming interfaces (APIs).

Socket technology facilitates the creation of services or micro services in order to integrate and program different IoT devices. Although IoT gateways can handle several devices simultaneously, they may also have filtering rules, such as only sending certain, more relevant information to higher layers of the data systems.

Dashboards are an additional application that can deliver information in real time, which facilitates better control. Additionally, Python sockets or web sockets with Node.js allow us to have more ways to connect—with different programming languages—to be able to create information extractors for IoT devices.


About Us: Krasamo is a mobile-first digital services and consulting company focused on the Internet-of-Things and Digital Transformation.

Click here to learn more about our IoT services.


Matter Smart Home: Reliable Connectivity Standard for IoT Devices

Matter Smart Home: Reliable Connectivity Standard for IoT Devices

The IoT Smart Home market currently offers numerous products that work with various different platforms, protocols, and applications, making the Smart Home setup and maintenance a cumbersome activity that requires effort and time. Therefore, users have been slow in adopting IoT Smart Home products due to decreased satisfaction or frustration produced by the complexities of incompatible protocols, specifications, and stacks.