Introduction: Home Assistant & Industrial Automation System

About: PLC, Arduino - Do it yourself project

The "Internet of Things" is the biggest current change nowadays, more and more home and industrial devices can be communicated together in its network. The protocol "Message Queue Telemetry Transport" (abbreviated: MQTT) is used in the I.o.T and I.I.o.T as a potential communication protocol.

My project shows how to combine Home Assistant and Industrial Automation System together by MQTT protocol. By this way, all home & industrial devices can be controlled and monitored by Home Assistant dashboard from laptop/ tablet or smart phone, and even they can be controlled by a portable ESP32-powered screen like M5Stack Core 2.

Let's getting started.

Step 1: Supplies

Main components are included as follows:

⦾. 1pcs x Interface module IM151-1.

⦾. 1pcs x PM-E power modules for ET 200S.

⦾. 1pcs x Module 2AI RTD for 2-/3-/4-wire sensors: I used this module to read temperature from PT100.

⦾. And some more ET200S input modules/ ouput modules: 2AI, 2AO ... for futher testing.

Step 2: Softwares, Firmwares and Add-ons.

The softwares required for this project include:

⦾. Node-RED: is a programming tool for wiring together hardware devices, APIs and online services in new and interesting ways. It provides a browser-based editor that makes it easy to wire together flows using the wide range of nodes in the palette that can be deployed to its runtime in a single click. Extension node-red-contrib-s7 need to be install to interact with Siemens S7 PLCs.

⦾. ESPHome: is a system to control your ESP8266/ESP32 by simple yet powerful configuration files and control them remotely through Home Automation systems.

⦾. Mosquitto Broker.

⦾. Home Assistant Community Store HACS: HACS gives us a powerful UI to handle downloads of all our custom needs.

  • HomePoint: It can control your MQTT Home Devices from an ESP32-powered screen.
  • Tasmotizer: The full-featured flashing tool for Tasmota.
  • ESPHome-flasher: It is designed to make flashing ESPs devices with ESPHome.
  • Python.

Step 3: Project Descriptions

There are 3 device groups in my Home Assistant Dashboard, as follows:

1. TASMOTA Group, including:

  • Gate Light is controlled by one Tasmota - SONOFF Basic.
  • Exterior Light is controlled by other Tasmota - SONOFF Basic.

2. ESPHome Group, including:

  • M5StickC shows led control and WIFI level on dashboard.
  • NodeMCU ESP8266 control one Dehumidefier, it's just an example because I don't have it.

3. PLC Group, including:

  • Four PLC lights can be controlled from Home Assistant dashboard.
  • One PLC PT100 temperature data is monitored on Home Assistant dashboard.

Beside Home Assistant dashboard, one small HMI - M5Stack Core 2 - can control 2 lights (Gate Light & Exterior Light) by its touch screen and show PLC PT100 temperature (PLCTemp):

Or the system can be controlled by smart phone:

Step 4: Project Configuration

The project configuration is described below.

Step 5: Project Assembly

All project modules assembled and wires connected are shown below:

1. PLC group assembly and wires connection.

2. Raspberry Pi, ESPHome and Tasmato group assembly.

3. M5Stack Core 2 is added into the system.

Step 6: Raspberry Pi Home Assistant

There are plenty of tutorials on how to install Home Assistant and its add-ons on the Raspberry Pi by Google searching.

1. Installing and setting up Home Assistant on Raspberry Pi 4.

Refer to https://www.home-assistant.io/installation/raspber... to get started with installing Home Assistant.

2. Installing Home Assistant Add-Ons:

Step 7: Siemens PLC S7-300 Configuration & Programming

My PLC connection is shown on below picture:

It is included:

  • Power supply module PS 307, CPU314C-2DP and Ethernet module CP343-1 are mounted on the same rack. CPU314C-2DP digital outpouts control four lights via 4 relays 24VDC.
  • One remote I/O station ET200S communicate with CPU314C-2DP via Profibus cable. The PT100 temperature is read from this remote I/O station.

PLC Ethernet communication is configured in NetPro with IP address: 192.168.1.71.

Main program is written in OB1 (Organization Block 1), lights control logic and temperature data are stored in DB1 (Data Block 1), as follows:

  • DB1.DBX0.0 control Light 0 at Output Q124.0.
  • DB1.DBX0.1 control Light 1 at Output Q124.1.
  • DB1.DBX0.2 control Light 2 at Output Q124.2.
  • DB1.DBX0.3 control Light 3 at Output Q124.3.

PT100 temperature is read from PIW276 and stored at DB1.DBD10.

After downloading hardware config and program to CPU314C-2DP, we can monitor lights status and PT100 value. And we can control the lights by FORCE ON/OFF function in STEP 7.

Make sure that PLC is working smooth locally before connecting it to the Home Assistant.

Step 8: Node-RED, HACS & PLC S7-300

Firstly, we need to install following extensions:

1. Control 4 PLC lights from Home Assistant:

In order for the PLC Lights to be controlled by Home Assistant, I used the node-red-contrib-home-assistant-websocket extension to generate the switch entities from Node-RED.

  • Selecting "entity" node.
  • Choosing Type: Switch.
  • Filling in the name field in the Home Assistant Config (optional): PLC/Light_0.
  • Payload: $entity().state ? "on": "off"
  • Do the same with 3 remaining switches: PLC/Light_1, PLC/Light_2, PLC/Light_3.

After deploying in Node-RED, go to Home Assistant Configuration Integrations Node-RED, we can see 4 light switch entities appear here which have just generated from Node-RED.

  • Name: PLC/Light_0 ‣ Entity ID: switch.plc_light_0
  • Name: PLC/Light_1 ‣ Entity ID: switch.plc_light_1
  • Name: PLC/Light_2 ‣ Entity ID: switch.plc_light_2
  • Name: PLC/Light_3 ‣ Entity ID: switch.plc_light_3

Due to each "switch" entity node in Node-RED has 2 outputs (true and false), so I used "events: state" node to cactch the state of each switch.

Finally, output of "events: state" node is connected to "s7 out" node and "s7 out" node is configured to control logic state of lights in Data Block 1 (DB1) of CPU S7-300.

We can see "switch.plc_light_0" entity ID is finally connected to "DB1, X0.0" in Node-RED. Back to previous step, in PLC, it is define "DB1.DBX0.0". Take note this discrepency.

From "s7 out node" ‣ Go to "s7 endpoint node" ‣ Tab "Connection":

  • Transport: Ethernet (ISO-on-TCP).
  • Address: 192.168.1.71 - Port: 102. Back to previous step, this is the PLC's IP address which I configured in NetPro.
  • Rack: 0 - Slot: 2.
  • Name: PLC0.

From "s7 out node" ‣ Go to "s7 endpoint node" ‣ Tab "Variables":

  • DB1,X0.0 (In PLC: DB1.DBX0.0 control Light 0 at Output Q124.0).
  • DB1,X0.1 (In PLC: DB1.DBX0.1 control Light 1 at Output Q124.1).
  • DB1,X0.2 (In PLC: DB1.DBX0.2 control Light 2 at Output Q124.2).
  • DB1,X0.3 (In PLC: DB1.DBX0.3 control Light 3 at Output Q124.3).
  • DB1,REAL10 (In PLC: DB1.DBD10 store temperature in floating-point).

2. Monitoring PLC PT100 temperature on Home Assistant dashboard and HomePoint screen.

The Node-RED flow is shown below.

The PT100 temperature (DB1,REAL10) is read from "s7 in" node and published to MQTT Broker at "mqtt out" node. Then it can be displayed in the Node-RED dashboard or Home Assistant dashboard by subscribing from the MQTT Broker.

The "mqtt out" node is configured as follow:

  • Server: MQTT-TUENHI
  • Topic: PLC/Temperature
  • QoS: 0 - Retain: false.
  • Name: PLCTemperature.

From "mqtt out" node ‣ Go to "mqtt-broker" node and set up "MQTT-TUENHI" is a MQTT Broker.

  • Server: 192.168.1.14. (This is IP address of Raspberry Pi).
  • Port: 1883.

Since I have already installed the Raspberry Pi (IP address: 192.168.1.14) as a MQTT Broker in Home Assistant, so we can set up Node-RED to use it for publishing and subscribing all topics from connected devices.

If we forgot our MQTT username & password at "Security" tab, they can be found by going to the Home Assistant ‣ Supervisor ‣ Dashboard ‣ Mosquitto broker ‣ Configuration.

In order to display PT100 temperature on the HomePoint screen, I had to convert this temperature data into HomePoint format.

And publish one more topic, named "temperature" to MQTT Broker just for HomePoint.

In the next step, we will know how Homepoint can read this value and display it on the M5Stack Core 2 screen.

Step 9: Tasmota & SONOFF

To flash Tasmota firmware for SONOFF, I did the following steps:

  • Downloading the lastest releases Tasmotizer.
  • Opening SONOFF case and soldering 4 pins male header at reserved 4 pin holes on PCB.

  • Connecting SONOFF header to FT232RL by a XH2.54mm – 4P 10/20cm wire cable double connector. Take note the connection should be correct (3V3/ RX/ TX/ GND).

Connecting FT232RL to the laptop, opening "tasmotizer-xx.exe" in Windows:

  • Select port: choose correct COM port.
  • Select image -> Release (select from a list of available release binaries): tasmota.bin.
  • Select some options based on your need, like: Backup or Erase before flashing...
  • Click Tasmotize! and wait until done.

Tasmota provides a wireless access point for easy Wi-Fi configuration. Finally, we can open web browser based on Tasmota's IP address and configure the MQTT parameters.

I created 2 topics, named: sonoff1 (Gate Light) and sonoff2 (Exterior Light) which are communicated to MQTT Broker at:

  • Host: 192.168.1.14 (IP Address of Raspberry Pi).
  • Port: 1883.

To use sonoff1 & sonoff2 in Home Assistant, I added the following codes in my configuration.yaml.

mqtt:
  broker: 192.168.1.14
  username: YOUR-MQTT-USERNAME
  password: YOUR-MQTT-PASSWORD
  discovery: True
switch:
- platform: mqtt
  name: "GateLight"
  state_topic: "stat/sonoff1/POWER"
  command_topic: "cmnd/sonoff1/POWER"
  qos: 0
  payload_on: "ON"
  payload_off: "OFF"
  payload_available: "Online"
  payload_not_available: "Offline"
  retain: true
- platform: mqtt
  name: "ExteriorLight"
  state_topic: "stat/sonoff2/POWER"
  command_topic: "cmnd/sonoff2/POWER"
  qos: 0
  payload_on: "ON"
  payload_off: "OFF"
  payload_available: "Online"
  payload_not_available: "Offline"
  retain: true 

Step 10: ESPHome & NodeMCU ESP8266 – M5SticC

1. Download ESPHome-Flasher following your PC operating system.

2. To install ESPHome for NodeMCU ESP8266, we did as follows:

  • Firstly we navigate to Home AssistantSupervisor ESPHome OPEN WEB UI ‣ Click on the “+” icon to add a new device.

  • Enter 'Node Name' ‣ Select correct 'Device Type' ‣ Enter WiFI SSID,WiFi Password and OTA Access Password in 'WiFi & Updates'.
  • Click ‘Edit’ on menu device to edit a YAML file currently configured for NodeMCU.

  • Click 'Compile'.

  • When the compilation completes successfully, click on ‘Download Binary’.
  • Open ESPHome-flasher.exe, select correct 'Serial port' and browse to firmware which has just complied, click 'Flash ESP'.
  • Go to Home AssistantConfiguration Integrations ESPHome, we will see that NodeMCU ESP8266 has already been discovered by ESPHome.

3. Since M5StickC hasn't been supported officially by ESPHome, so I referred to below projects to configure it by custom files.

https://github.com/airy10/esphome-m5stickC

https://esphome.io/cookbook/leak-detector-m5stickc

Finally, both NodeMCU and M5StickC have already been discovered by ESPHome.

About configuration file 'm5stickc.yaml', I used this code by airy10 for testing my system.

Step 11: HomePoint & M5Stack Core 2

Please refer to this link for flashing and configuring HomePoint by Sieren, I'd to summarize in the following steps.

1. Downloading the latest Homepoint firmware from GitHub by sieren.

2. Installing the Esptool from Python.

3. Flashing HomePoint firmware for M5Stack Core 2:

  • Connecting M5Stack Core 2 to the laptop via a USB-C cable. Then run this command:

esptool.py write_flash -fs 4MB -fm dout 0x0 homepoint_m5stackcore2_full.bin

  • After flashing, M5Stack Core 2 reboots and we can see a wireless access point name (such as: HomePoint-Config) in our WIFI network and an IP address on the screen (192.168.99.1).

4. WiFi configuration

  • Connecting to the HomePoint-Config access point by laptop or smartphone, go to browser: http://192.168.99.1. We can see a web page for the HomePoint initial configuration.
  • Entering SSID and YOURWIFIPASSWORD of your own WIFI network and setting an USERNAME and PASSWORD to log in to Homepoint web interface.
  • If everything is correct, we will see the new IP address on the top screen after restarting the M5Stack Core2. New IP address in my case is: 192.168.1.12

5. Built-in web server

Entering http://192.168.1.12 in browser, typing USERNAME and PASSWORD, then we can see lot of system files.

6. Display Tasmota switches and MQTT - PLC PT100 temperature on HomePoint screen

From built-in web server (http://192.168.1.12), I edited the /config.json to showTasmota switches of GateLight & ExteriorLight,as well as, PLC temperature value on the on M5Stack Core 2 screen.

My own "/config.json" file is as follow.

{
  "wifi": "SSID",
  "password": "YOURWIFIPASSWORD",
  "login": "USERNAME",
  "webpass": "PASSWORD",
  "mqttbroker": "mqtt://192.168.1.14",
  "mqttusername": "YOUR-MQTT-USERNAME",
  "mqttpasswd": "YOUR-MQTT-PASSWORD",
  "timezone": "CET-1CEST-2,M3.5.0/02:00:00,M10.5.0/03:00:00",
  "scenes": [{
    "name": "GateLight",
    "type": "Light",
    "icon": "livingroom",
    "devices": [{
      "name": "GateLight",
      "setTopic": "cmnd/sonoff1/POWER1",
      "getTopic": "stat/sonoff1/POWER1",
      "onValue": "true",
      "offValue": "false",
      "icon": "bedroom"
    }]
  },
    {
    "name": "ExteriorLight",
    "type": "Light",
    "icon": "livingroom",
    "devices": [{
      "name": "ExteriorLight",
      "setTopic": "cmnd/sonoff2/POWER1",
      "getTopic": "stat/sonoff2/POWER1",
      "onValue": "true",
      "offValue": "false",
      "icon": "bedroom"
    }]
  },
 {
    "name": "Living Room",
    "type": "Sensor",
    "icon": "door",
    "devices": [{
      "name": "temperature",
      "type": "singleValue",
      "jsondata": true,
      "firstIcon": "temperature_small",
      "firstKey": "temperature",
      "getTopic": "temperature"
    }]
  }]
}

For PLC temperature value, as mentioned on previous step, before pupblishing it to MQTT Broker from Node-RED, I had to convert it to HomePoint format, something like:

{"temperature": 56.7}

The result looks really amazing!!!

Step 12: Showing Cards on Home Assistant Dashboard

For TASMOTA GROUP & ESPHOME GROUP, we can easily add them to dashboard by Entities Card supported by Home Assistant.

Home Assistant did not support to add gauge card and button card into one group, so I had to do following steps to create PLC GROUP:

  • Installing 'Stack In Card' in HACS, it allows to group multiple cards into one card without the borders in vertically and horizontally.
  • After installing, then we can see it at Home AssistantHACS Frontend.

  • Typing below codes in Card ConfigurationShow Visual Editor
type: 'custom:stack-in-card'
title: PLC GROUP
mode: vertical
cards:
  - type: horizontal-stack
    cards:
      - type: button
        entity: switch.plc_light_0
        icon_height: 50px
      - type: button
        entity: switch.plc_light_1
        icon_height: 50px
  - type: horizontal-stack
    cards:
      - type: button
        entity: switch.plc_light_2
        icon_height: 50px
      - type: button
        entity: switch.plc_light_3
        icon_height: 50px
  - type: horizontal-stack
    cards:
      - type: gauge
        entity: sensor.plctemperature
        min: 0
        max: 100
  • PLC temperature gauge and light switches are grouped together.

Now my home devices and industrial PLC equipments can be controlled and monitored by Home Assistant dashboard.

Finally, I have added 4 feedback signals from the PLC indicating 4 lights statuses. These feedback signals are published to the MQTT Broker.

Then Home Assistant get and display them on the dashboard. They confirm the lights ON/OFF commands.

Step 13: Node-RED Dashboard and Home Assistant Dashboard

We can use 'Webpage Card' to embed the Node-RED dashboard page into the Home Assistant dashboard.

  • Firstly, we add 4 toggle buttons to control the lights in the Node-RED.

  • Deploying in Node-RED, and go to Node-RED dashboard, now we can control all lights here.

  • Node-RED dashboard can be embedded into Home Assistant dashboard by using 'Webpage Card' as below picture. It is not bad solution!

By the way, my 'configuration.yaml' is attached below:

# MQTT Broker
mqtt:
  broker: 192.168.1.14
  username: YOUR_USER_NAME
  password: YOUR_PASSWORD
  discovery: True
switch:
- platform: mqtt
  name: "GateLight"
  state_topic: "stat/sonoff1/POWER"
  command_topic: "cmnd/sonoff1/POWER"
  qos: 0
  payload_on: "ON"
  payload_off: "OFF"
  payload_available: "Online"
  payload_not_available: "Offline"
  retain: true
- platform: mqtt
  name: "ExteriorLight"
  state_topic: "stat/sonoff2/POWER"
  command_topic: "cmnd/sonoff2/POWER"
  qos: 0
  payload_on: "ON"
  payload_off: "OFF"
  payload_available: "Online"
  payload_not_available: "Offline"
  retain: true
  
sensor:  
- platform: mqtt
  name: "PLC Temperature"
  state_topic: "PLC/Temperature"
  device_class: "temperature"
  unit_of_measurement: "°C"
  value_template: "{{ value | float  | round (2) }}"

binary_sensor:  
- platform: mqtt  
  name: "Status Light 0"
  state_topic: "PLC/Light0"
  value_template: "{{ 'ON' if 'true' in value else 'OFF'}}"
- platform: mqtt  
  name: "Status Light 1"
  state_topic: "PLC/Light1"
  value_template: "{{ 'ON' if 'true' in value else 'OFF'}}"
- platform: mqtt  
  name: "Status Light 2"
  state_topic: "PLC/Light2"
  value_template: "{{ 'ON' if 'true' in value else 'OFF'}}"
- platform: mqtt  
  name: "Status Light 3"
  state_topic: "PLC/Light3"
  value_template: "{{ 'ON' if 'true' in value else 'OFF'}}"

homeassistant:
  name: Home
  latitude: YOUR_LAT
  longitude: YOUR_LONG
  unit_system: metric
  time_zone: YOUR_TIME_ZONE
  customize: !include customize.yaml
# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

# Uncomment this if you are using SSL/TLS, running in Docker container, etc.
http:
  base_url: yoursite.duckdns.org
  ssl_certificate: /ssl/fullchain.pem
  ssl_key: /ssl/privkey.pem
# Text to speech
tts:
  - platform: google_translate
group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml<br>

Step 14: Finish & Development

The MQTT is an open messaging protocol for M2M communication which have been applied in the industrial automation and it actually opens up new possibilities for automation.

Siemens officially offers TIA Portal's libraries to use MQTT for CPUs of the S7-300, S7-1200 or S7-1500 series, as link below:

https://support.industry.siemens.com/cs/document/1...

But for me, to install the latest releases TIA Portal V16 or SIMATIC STEP 7 V16, my laptop hardwares can't meet the following minimum requirements:

  • Processor: Core™ i5-6440EQ (up to 3.4 GHz).
  • RAM: 16 GB (min. 8 GB, 32 GB for large projects).

Therefore, if we can implement a project related to combination of Home Assistant & Siemens PLC System by the official MQTT libraries plus Node-RED, everything is even better and easier.

This is my first time working with the Raspberry Pi and I faced many difficulties with its programming languages☹. My experiment proves that there are no any limits nowadays when a device can be connected to the network

Thank you for reading my work!!!

Automation Contest

Participated in the
Automation Contest