Acquire Temperature and Humidity Sensor Data Using modbus
This chapter demonstrates the demo for collecting information from temperature and humidity sensors through the Modbus module.
I. Prerequisites
-
Hardware preparation:
-
Device connection mode
- Raspberry Pi accesses the network through WiFi.
- The temperature and humidity sensor is connected to the serial port cable through the USB port and then accessed to Raspberry Pi through the USB port.
- The edge node of Raspberry Pi is connected to the Baidu AI Cloud per the Getting Started tutorial (the node name in this article is modbus-test).
II. Simulation Scenario
- The sensor collects the temperature and humidity and sends the data to the broker service of the edge node.
III. Application Description of Edge Node
In addition to two system applications automatically deployed when the edge node is connected to the Baidu AI Cloud, i.e., bartyl-core and baetyl-function, you also need to deploy the following two applications on the edge node.
- broker container application: MQTT broker message module of the edge node
- modbus container application: Collect and parse the data based on the Modbus protocol. This module supports configuring multiple slave devices to collect the data regularly.
IV. Edge Application Configuration
broker configuration
- Create an application broker
- Name: broker
- Image address: hub.baidubce.com/baetyl/broker: v2.0.0
-
Port Mapping:
- Host port: 1883;
- Container port: 1883;
- Protocol: TCP
- The broker service has no user name and password by default.
modbus configuration
- Create modbus-conf-test configuration
- Variable name: service.yml
- Variable value:
broker:
address: tcp://broker:1883 # Connect to the address of the mqtt hub
clientid: modbus-1 # The client id used when connecting to the mqtt hub, it may not be configured when running based on the baetyl framework
slaves:
- id: 1 # slave id
address: /dev/ttyUSB0 # Address when connecting to slave based on tcp
mode: rtu
idletimeout: 1m # Idle tcp connection retention time, the default is 1 minute
baudrate: 9600 # The default rate is 19200
databits: 8 # Data bits, optional (5,6,7,8), the default is 8
stopbits: 1 # Stop bit, optional (1,2) default is 1
parity: N # Parity check type, optional N (none, corresponding stopbits should be configured as 2), E (odd check), O (even check) The default is E
jobs:
- slaveid: 1 # Corresponding equipment for collection tasks
encoding: json # Specify collection and analysis data, the data is sent in JSON, the default is json
maps:
- function: 3 # Function code, corresponding to discrete input
address: 0 # starting address
quantity: 1 # The number of acquisitions, discrete input corresponds to 1bit, the module can automatically determine when the data is parsed
field:
name: temperature # Data field name after parsing
type: int16 # Parse data for float32 data type
- function: 3 # Function code, corresponding to the input register
address: 1 # starting address
quantity: 1 # Acquisition quantity, input register corresponds to 16bit
field:
name: humidity # Data field name after parsing
type: int16 # Parse data for float32 data type
publish:
topic: test # Mqtt topic for collecting data and sending
logger:
filename: var/log/baetyl/service.log # Log path
level: info # Log level
- Create the modbus container application, and add the modbus container service.
- Name: modbus-test
- Image address: baetyltechtest/baetyl-modbus: git-23488d4
-
Add two volume configurations.
- Change the dev directory to the specific device directory: /dev/ttyUSB0
- Add the configuration item of modbus-conf-test. The container directory is /etc/baetyl.
- Privilege mode: Select Yes.
V. Deploy Application to Edge Node
VI. Verify Information Collection Results
Subscribe to the local broker service of the edge node through the MQTT Box. The protocol is mqtt/tcp and the subscription topic is test. The topic is defined in the configuration file. No user name and password are required for this broker application.
VII. Log View
In case of any problem during the test, you can view the running status and log of applications through the following commands.
$ sudo kubectl get pod -A #View the running status of all pods
$ kubectl logs modbus-test-d87cf6d98-ljfnr(The value is the name of the pod) -n baetyl-edge(The value is the NAMESPACE of the pod) View the log of the container application.