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

by Nov 18, 2021#IoT, #HomePage

Printer Icon
f

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)

Introduction

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.set(GPIO.BCM)
GPIO.setup(26, GPIO.IN, pull_up_donw=false)

while True:
    input_state = GPIO.input(26)
    if input_state == False:
        print('Button Pressed')
        p.barcode('57687','CODE39')
        p.image("logo.png")
        p.cut()
        p.sleep(0.2)

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.

 

Sockets

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 = '127.0.0.1'  
PORT = 3001        

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

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

 

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 = '127.0.0.1'  
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))

WebSockets

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('socket.io')(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>
<html>
   <head>
      <title>GPIO Status</title>
   </head>
   <script src = "/socket.io/socket.io.js"></script>

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

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

Conclusion

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.

RELATED BLOG POSTS

IIoT-Driven Transformation: Boosting Industrial Efficiency & Innovation

IIoT-Driven Transformation: Boosting Industrial Efficiency & Innovation

This paper discusses the transformative potential of the Industrial Internet of Things (IIoT) in enhancing operational efficiency and reducing expenses in plants and buildings. By leveraging wireless sensors, data collection, analytics, and machine learning, IIoT systems create a competitive advantage through improved interoperability and connectivity. We explore the factors driving IIoT adoption, the benefits it offers, and the different types of IIoT software. The paper also highlights Krasamo’s expertise in IoT consulting services and their comprehensive range of IoT offerings to help enterprises implement and benefit from IIoT systems.

Building Machine Learning Features on IoT Edge Devices

Building Machine Learning Features on IoT Edge Devices

Enhance IoT edge devices with machine learning using TensorFlow Lite, enabling businesses to create intelligent solutions for appliances, toys, smart sensors, and more. Leverage pretrained models for object detection, image classification, and other applications. TensorFlow Lite supports iOS, Android, Embedded Linux, and Microcontrollers, offering optimized performance for low latency, connectivity, privacy, and power consumption. Equip your IoT products with cutting-edge machine learning capabilities to solve new problems and deliver innovative, cost-effective solutions for a variety of industries.

IoT Inventory Management for Multi-Channel Retailers and 3PL Services

IoT Inventory Management for Multi-Channel Retailers and 3PL Services

IoT inventory management revolutionizes multi-channel retail and 3PL services by providing real-time tracking, enhanced efficiency, and scalability. IoT technologies like RFID, Barcode, and BLE seamlessly integrate with warehouse management systems, automating processes and optimizing space utilization. This innovative approach facilitates accurate decision-making, improves customer service, and supports business growth in a rapidly evolving fulfillment landscape.

Optimize Business Operations with Customized Asset Tracking App

Optimize Business Operations with Customized Asset Tracking App

Optimize your business operations with a customized asset tracking app. Manage mobile assets in indoor and outdoor spaces using location services, IoT devices, and advanced connectivity standards. Develop tailored solutions for various use cases, including warehouse logistics, outdoor monitoring, and remote healthcare. Harness the power of cloud-based asset management and integrate with the Google Maps platform for dynamic visualization and enhanced fleet optimization. Work with Krasamo’s IoT development professionals to build a comprehensive asset tracking solution tailored to your unique needs.

AI and IoT: Driving Supply Chain Efficiency

AI and IoT: Driving Supply Chain Efficiency

This paper explores the integration of Artificial Intelligence (AI) and the Internet of Things (IoT) in the supply chain. It highlights the benefits of combining AI and IoT, such as increased visibility of assets, reduced costs and downtime, and improved efficiency. The paper also covers the use cases of AI in supply chain management, including warehouse management, supply network risks and predictions, demand forecasting, transportation optimization, and more. By leveraging cloud services, frameworks, and AI platforms, enterprises can build intelligent and autonomous systems that drive supply chain efficiency.

Develop IoT Asset Tracking Solutions with Krasamo

Develop IoT Asset Tracking Solutions with Krasamo

Krasamo specializes in IoT asset tracking solutions, enhancing operational performance by optimizing asset utilization, location monitoring, and condition tracking. Our customized solutions integrate seamlessly with various systems and offer scalability, cost-effectiveness, and advanced features. Krasamo’s IoT engineers help clients build wireless infrastructures tailored to their specific needs, ensuring end-to-end visibility and efficient asset management.