Skip to content

Commit 34a9129

Browse files
authored
Merge pull request #161 from aAAaqwq/feat-temperature-sensor-demo
Fix: Add a temperature sensor demo
2 parents eb4e6e4 + bd4b776 commit 34a9129

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+4249
-0
lines changed

temperature-sensor-demo/Makefile

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
.PHONY: test build-webUI build-modbus build-hardware
2+
3+
test: deploy-hardware deploy-modbus deploy-webUI
4+
5+
clean:
6+
kubectl delete -f ./webUI/resource
7+
kubectl delete -f ./hardware/resource
8+
make -C ./modbus clean
9+
10+
deploy-crds:
11+
kubectl apply -f ./crds/temperature-model.yaml
12+
kubectl apply -f ./crds/temperature-instance.yaml
13+
deploy-webUI:
14+
kubectl apply -f ./webUI/resource
15+
16+
deploy-hardware:
17+
kubectl apply -f ./hardware/resource
18+
19+
deploy-modbus:
20+
make -C ./modbus deploy
21+
22+
build-webUI:
23+
docker build -t temperature-webui:v1.0 ./webUI
24+
# kind load docker-image temperature-webui:v1.0 --nodes=kind-control-plane
25+
build-modbus:
26+
make -C ./modbus docker-build
27+
28+
build-hardware:
29+
docker build -t temperature-sensor:v1.0 ./hardware
30+
# kind load docker-image temperature-sensor:v1.0 --nodes=kind-worker
31+
32+
test-webUI:
33+
go build -o main ./main.go
34+
docker cp ./main kind-control-plane:./srv/temperature/main
35+
docker cp ./static kind-control-plane:./srv/temperature/static

temperature-sensor-demo/README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Temperature Sensor
2+
3+
## Description
4+
This project is used to demonstrate a specific instance of kubeedge. It provides a cloud-edge collaboration example of a temperature sensor.
5+
6+
## Structure
7+
![img](./img/structure.png)
8+
9+
## Prerequisites
10+
* Docker
11+
* KubeEdge v1.21.0+
12+
## Quick Start
13+
### 1. Clone the repository to local
14+
```
15+
git clone https://github.com/kubeedge/examples
16+
cd ./examples/temperature-sensor-demo
17+
```
18+
### 2. Deploy components
19+
20+
#### [2.1 Deploy hardware](hardware/README.md) ✅(At the edge)
21+
22+
```
23+
make build-hardware
24+
make deploy-hardware
25+
```
26+
#### Other options
27+
- Through Physical device
28+
29+
- [Install modbus Slave](https://www.modbustools.com/modbus_slave.html) (only for Windows users)
30+
```
31+
- Create TCP/RTU device connection, mapper uses TCP protocol by default.
32+
- Configure Modbus registers:
33+
- Temperature register: holding register, address 4000, data type: uint16
34+
- Working status register: coil register, address 1001, data type: boolean
35+
```
36+
#### [2.2 Deploy modbus mapper](modbus/README.md) ✅(At the edge)
37+
```
38+
make build-modbus
39+
make deploy-modbus
40+
```
41+
#### [2.3 Deploy webUI](webUI/README.md) ✅(At the cloud)
42+
```
43+
make build-webUI
44+
make deploy-webUI
45+
```
46+
### 3. Test the demo
47+
#### 3.1 View the synchronization of the reported field in the twins field:
48+
```
49+
kubectl get device -o yaml
50+
```
51+
![twinData](./img/twinData.png)
52+
#### 3.2 View the result through the webUI
53+
![test1](./img/test1.png)
54+
![test2](./img/test2.png)
55+
![test3](./img/test3.png)
56+
![test4](./img/test4.png)
57+
58+
59+
60+
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
apiVersion: devices.kubeedge.io/v1beta1
2+
kind: Device
3+
metadata:
4+
name: temperature-instance
5+
spec:
6+
deviceModelRef:
7+
name: temperature-model
8+
protocol:
9+
protocolName: modbus
10+
# Custom protocol configuration
11+
configData:
12+
communicateMode: "TCP" # TCP/RTU
13+
port: "5502" # replace the port with your modbus device port
14+
slaveID: 1
15+
ip: "10.244.1.13" # 1.replace the ip with your modbus device ip
16+
17+
nodeName: "kind-worker" # 2.replace the nodeName with your edge node name
18+
properties:
19+
- name: temperature
20+
visitors:
21+
protocolName: modbus
22+
configData:
23+
dataType: "float" # Define the output format, consistent with the model type definition Enum::int/float/double/string/boolean/bytes
24+
register: "HoldingRegister" # Register Type Enum::CoilRegister/DiscreteInputRegister/HoldingRegister/InputRegister
25+
offset: 0 # Register offset
26+
limit: 1 # Number of registers to read
27+
scale: 0.1 # Scaling factor for temperature values
28+
isSwap: false # Whether to swap bytes
29+
isRegisterSwap: false # Whether to swap registers
30+
max: 100.0 # Maximum value for temperature
31+
min: 1.0 # Minimum value for temperature
32+
collectCycle: 10000
33+
reportCycle: 10000
34+
reportToCloud: true
35+
# Enabling the push function requires deploying related services,like mosquitto broker
36+
# pushMethod:
37+
# mqtt:
38+
# topic: "current temperature"
39+
# qos: 0
40+
# address: "tcp://172.18.0.3:31883" # replace the address with your mqtt broker address
41+
# retained: false
42+
- name: temperature-switch
43+
collectCycle: 10000
44+
reportCycle: 10000
45+
reportToCloud: true
46+
desired:
47+
value: "1"
48+
visitors:
49+
protocolName: modbus
50+
configData:
51+
dataType: "int"
52+
register: "CoilRegister"
53+
offset: 0
54+
limit: 1
55+
scale: 1
56+
isSwap: false
57+
isRegisterSwap: false
58+
methods:
59+
- name: SwitchControl
60+
description: control the switch of the device
61+
propertyNames:
62+
- temperature-switch
63+
- name: UpdateTemperature
64+
description: update the temperature of the device
65+
propertyNames:
66+
- temperature
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
apiVersion: devices.kubeedge.io/v1beta1
2+
kind: DeviceModel
3+
metadata:
4+
name: temperature-model
5+
namespace: default
6+
spec:
7+
protocol: modbus
8+
properties:
9+
- name: temperature
10+
description: actual temperature
11+
type: FLOAT # ENUM: INT,FLOAT,DOUBLE,STRING,BOOLEAN,BYTES
12+
accessMode: ReadWrite
13+
minimum: "0"
14+
maximum: "100.0"
15+
unit: "Celsius"
16+
- name: temperature-switch
17+
description: "the switch of device 0:off,1:on"
18+
type: INT
19+
accessMode: ReadWrite
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM golang:1.22.9-alpine3.19 AS builder
2+
3+
WORKDIR /app
4+
5+
COPY . .
6+
7+
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o main ./main.go
8+
9+
FROM alpine:3.19
10+
11+
COPY --from=builder /app/main .
12+
13+
EXPOSE 5502
14+
15+
CMD ["./main"]
16+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Modbus Simulator
2+
3+
## Description
4+
This is a modbus simulator built using gomodbus dependencies, currently only supports TCP connections
5+
6+
## Quick Start
7+
8+
### 1. Build the image
9+
```
10+
docker build -t temperature-sensor:v1.0 .
11+
```
12+
### 2. Upload image to the edge node
13+
14+
### 3. Deploy the simulator to the edge
15+
```
16+
kubectl apply -f ./resource/deploy.yaml
17+
```
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module hardware
2+
3+
go 1.22.9
4+
5+
require github.com/thinkgos/gomodbus v1.5.2
6+
7+
require github.com/goburrow/serial v0.1.0 // indirect
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
github.com/goburrow/serial v0.1.0 h1:v2T1SQa/dlUqQiYIT8+Cu7YolfqAi3K96UmhwYyuSrA=
2+
github.com/goburrow/serial v0.1.0/go.mod h1:sAiqG0nRVswsm1C97xsttiYCzSLBmUZ/VSlVLZJ8haA=
3+
github.com/thinkgos/gomodbus v1.5.2 h1:zmFEzXILpCtBYB8FwcAuLnb8Rrr3NcdX8oukmyB9hwE=
4+
github.com/thinkgos/gomodbus v1.5.2/go.mod h1:HX6B+W3xV/oKinltblD+aqr9LnQuh9mWT4kMul5hYvE=
5+
github.com/thinkgos/timing v1.1.2/go.mod h1:zS1qzRn2ISTjI1xJc6AHNfWAvCtc7FylwWjt32L8qvo=
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import (
4+
"log"
5+
6+
"github.com/thinkgos/gomodbus"
7+
)
8+
9+
func InitModbusSimulator(address string) {
10+
srv := modbus.NewTCPServer()
11+
srv.AddNodes(
12+
modbus.NewNodeRegister(1, 0, 1, 0, 0, 0,0,0,1),
13+
)
14+
defer srv.Close()
15+
if err := srv.ListenAndServe(address); err != nil {
16+
log.Fatalf("Failed to start modbus simulator: %v", err)
17+
}
18+
}
19+
func main() {
20+
InitModbusSimulator(":5502")
21+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: temperature-sensor
5+
spec:
6+
replicas: 1
7+
selector:
8+
matchLabels:
9+
app: temperature-sensor
10+
template:
11+
metadata:
12+
labels:
13+
app: temperature-sensor
14+
spec:
15+
nodeName: kind-worker
16+
containers:
17+
- name: temperature-sensor
18+
image: temperature-sensor:v1.0
19+
imagePullPolicy: IfNotPresent
20+
resources:
21+
limits:
22+
cpu: 300m
23+
memory: 500Mi
24+
requests:
25+
cpu: 100m
26+
memory: 100Mi

0 commit comments

Comments
 (0)