Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions temperature-sensor-demo/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
.PHONY: test build-webUI build-modbus build-hardware

test: deploy-hardware deploy-modbus deploy-webUI

clean:
kubectl delete -f ./webUI/resource
kubectl delete -f ./hardware/resource
make -C ./modbus clean

deploy-crds:
kubectl apply -f ./crds/temperature-model.yaml
kubectl apply -f ./crds/temperature-instance.yaml
deploy-webUI:
kubectl apply -f ./webUI/resource

deploy-hardware:
kubectl apply -f ./hardware/resource

deploy-modbus:
make -C ./modbus deploy

build-webUI:
docker build -t temperature-webui:v1.0 ./webUI
# kind load docker-image temperature-webui:v1.0 --nodes=kind-control-plane
build-modbus:
make -C ./modbus docker-build

build-hardware:
docker build -t temperature-sensor:v1.0 ./hardware
# kind load docker-image temperature-sensor:v1.0 --nodes=kind-worker

test-webUI:
go build -o main ./main.go
docker cp ./main kind-control-plane:./srv/temperature/main
docker cp ./static kind-control-plane:./srv/temperature/static
60 changes: 60 additions & 0 deletions temperature-sensor-demo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Temperature Sensor

## Description
This project is used to demonstrate a specific instance of kubeedge. It provides a cloud-edge collaboration example of a temperature sensor.

## Structure
![img](./img/structure.png)

## Prerequisites
* Docker
* KubeEdge v1.21.0+
## Quick Start
### 1. Clone the repository to local
```
git clone https://github.com/kubeedge/examples
cd ./examples/temperature-sensor-demo
```
### 2. Deploy components

#### [2.1 Deploy hardware](hardware/README.md) ✅(At the edge)

```
make build-hardware
make deploy-hardware
```
#### Other options
- Through Physical device

- [Install modbus Slave](https://www.modbustools.com/modbus_slave.html) (only for Windows users)
```
- Create TCP/RTU device connection, mapper uses TCP protocol by default.
- Configure Modbus registers:
- Temperature register: holding register, address 4000, data type: uint16
- Working status register: coil register, address 1001, data type: boolean
```
#### [2.2 Deploy modbus mapper](modbus/README.md) ✅(At the edge)
```
make build-modbus
make deploy-modbus
```
#### [2.3 Deploy webUI](webUI/README.md) ✅(At the cloud)
```
make build-webUI
make deploy-webUI
```
### 3. Test the demo
#### 3.1 View the synchronization of the reported field in the twins field:
```
kubectl get device -o yaml
```
![twinData](./img/twinData.png)
#### 3.2 View the result through the webUI
![test1](./img/test1.png)
![test2](./img/test2.png)
![test3](./img/test3.png)
![test4](./img/test4.png)




66 changes: 66 additions & 0 deletions temperature-sensor-demo/crds/temperature-instance.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
apiVersion: devices.kubeedge.io/v1beta1
kind: Device
metadata:
name: temperature-instance
spec:
deviceModelRef:
name: temperature-model
protocol:
protocolName: modbus
# Custom protocol configuration
configData:
communicateMode: "TCP" # TCP/RTU
port: "5502" # replace the port with your modbus device port
slaveID: 1
ip: "10.244.1.13" # 1.replace the ip with your modbus device ip

nodeName: "kind-worker" # 2.replace the nodeName with your edge node name
properties:
- name: temperature
visitors:
protocolName: modbus
configData:
dataType: "float" # Define the output format, consistent with the model type definition Enum::int/float/double/string/boolean/bytes
register: "HoldingRegister" # Register Type Enum::CoilRegister/DiscreteInputRegister/HoldingRegister/InputRegister
offset: 0 # Register offset
limit: 1 # Number of registers to read
scale: 0.1 # Scaling factor for temperature values
isSwap: false # Whether to swap bytes
isRegisterSwap: false # Whether to swap registers
max: 100.0 # Maximum value for temperature
min: 1.0 # Minimum value for temperature
collectCycle: 10000
reportCycle: 10000
reportToCloud: true
# Enabling the push function requires deploying related services,like mosquitto broker
# pushMethod:
# mqtt:
# topic: "current temperature"
# qos: 0
# address: "tcp://172.18.0.3:31883" # replace the address with your mqtt broker address
# retained: false
- name: temperature-switch
collectCycle: 10000
reportCycle: 10000
reportToCloud: true
desired:
value: "1"
visitors:
protocolName: modbus
configData:
dataType: "int"
register: "CoilRegister"
offset: 0
limit: 1
scale: 1
isSwap: false
isRegisterSwap: false
methods:
- name: SwitchControl
description: control the switch of the device
propertyNames:
- temperature-switch
- name: UpdateTemperature
description: update the temperature of the device
propertyNames:
- temperature
19 changes: 19 additions & 0 deletions temperature-sensor-demo/crds/temperature-model.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: devices.kubeedge.io/v1beta1
kind: DeviceModel
metadata:
name: temperature-model
namespace: default
spec:
protocol: modbus
properties:
- name: temperature
description: actual temperature
type: FLOAT # ENUM: INT,FLOAT,DOUBLE,STRING,BOOLEAN,BYTES
accessMode: ReadWrite
minimum: "0"
maximum: "100.0"
unit: "Celsius"
- name: temperature-switch
description: "the switch of device 0:off,1:on"
type: INT
accessMode: ReadWrite
16 changes: 16 additions & 0 deletions temperature-sensor-demo/hardware/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM golang:1.22.9-alpine3.19 AS builder

WORKDIR /app

COPY . .

RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o main ./main.go

FROM alpine:3.19

COPY --from=builder /app/main .

EXPOSE 5502

CMD ["./main"]

17 changes: 17 additions & 0 deletions temperature-sensor-demo/hardware/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Modbus Simulator

## Description
This is a modbus simulator built using gomodbus dependencies, currently only supports TCP connections

## Quick Start

### 1. Build the image
```
docker build -t temperature-sensor:v1.0 .
```
### 2. Upload image to the edge node

### 3. Deploy the simulator to the edge
```
kubectl apply -f ./resource/deploy.yaml
```
7 changes: 7 additions & 0 deletions temperature-sensor-demo/hardware/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module hardware

go 1.22.9

require github.com/thinkgos/gomodbus v1.5.2

require github.com/goburrow/serial v0.1.0 // indirect
5 changes: 5 additions & 0 deletions temperature-sensor-demo/hardware/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
github.com/goburrow/serial v0.1.0 h1:v2T1SQa/dlUqQiYIT8+Cu7YolfqAi3K96UmhwYyuSrA=
github.com/goburrow/serial v0.1.0/go.mod h1:sAiqG0nRVswsm1C97xsttiYCzSLBmUZ/VSlVLZJ8haA=
github.com/thinkgos/gomodbus v1.5.2 h1:zmFEzXILpCtBYB8FwcAuLnb8Rrr3NcdX8oukmyB9hwE=
github.com/thinkgos/gomodbus v1.5.2/go.mod h1:HX6B+W3xV/oKinltblD+aqr9LnQuh9mWT4kMul5hYvE=
github.com/thinkgos/timing v1.1.2/go.mod h1:zS1qzRn2ISTjI1xJc6AHNfWAvCtc7FylwWjt32L8qvo=
21 changes: 21 additions & 0 deletions temperature-sensor-demo/hardware/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"log"

"github.com/thinkgos/gomodbus"
)

func InitModbusSimulator(address string) {
srv := modbus.NewTCPServer()
srv.AddNodes(
modbus.NewNodeRegister(1, 0, 1, 0, 0, 0,0,0,1),
)
defer srv.Close()
if err := srv.ListenAndServe(address); err != nil {
log.Fatalf("Failed to start modbus simulator: %v", err)
}
}
func main() {
InitModbusSimulator(":5502")
}
26 changes: 26 additions & 0 deletions temperature-sensor-demo/hardware/resource/deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: temperature-sensor
spec:
replicas: 1
selector:
matchLabels:
app: temperature-sensor
template:
metadata:
labels:
app: temperature-sensor
spec:
nodeName: kind-worker
containers:
- name: temperature-sensor
image: temperature-sensor:v1.0
imagePullPolicy: IfNotPresent
resources:
limits:
cpu: 300m
memory: 500Mi
requests:
cpu: 100m
memory: 100Mi
Binary file added temperature-sensor-demo/img/getTemp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added temperature-sensor-demo/img/structure.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added temperature-sensor-demo/img/test1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added temperature-sensor-demo/img/test2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added temperature-sensor-demo/img/test3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added temperature-sensor-demo/img/test4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added temperature-sensor-demo/img/twinData.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions temperature-sensor-demo/modbus/Dockerfile_nostream
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM golang:1.22.9-alpine3.19 AS builder

WORKDIR /build

ENV GO111MODULE=on \
GOPROXY=https://goproxy.cn,direct

COPY . .

RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o main ./cmd/main.go

FROM ubuntu:22.04
Comment on lines +5 to +12

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This Dockerfile has a couple of issues that should be addressed:

  1. It hardcodes GOPROXY=https://goproxy.cn,direct, which might not be accessible or desirable for all users. It's better to make this configurable via a build argument.
  2. The final image is based on ubuntu:22.04, while the builder uses alpine. Using a large base image like Ubuntu when a smaller one like Alpine is available (and used in the builder stage) unnecessarily increases the final image size. Consider using alpine for the final stage as well to keep the image small.


RUN mkdir -p kubeedge

COPY --from=builder /build/main kubeedge/
COPY ./config.yaml kubeedge/

WORKDIR kubeedge
35 changes: 35 additions & 0 deletions temperature-sensor-demo/modbus/Dockerfile_stream
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
FROM golang:1.22.9-bullseye AS builder

WORKDIR /build

ENV GO111MODULE=on \
GOPROXY=https://goproxy.cn,direct

COPY . .

RUN apt-get update && \
apt-get install -y bzip2 curl upx-ucl gcc-aarch64-linux-gnu libc6-dev-arm64-cross gcc-arm-linux-gnueabi libc6-dev-armel-cross libva-dev libva-drm2 libx11-dev libvdpau-dev libxext-dev libsdl1.2-dev libxcb1-dev libxau-dev libxdmcp-dev yasm

RUN curl -sLO https://ffmpeg.org/releases/ffmpeg-4.1.6.tar.bz2 && \
tar -jx --strip-components=1 -f ffmpeg-4.1.6.tar.bz2 && \
./configure && make && \
make install

RUN GOOS=linux go build -o main cmd/main.go

FROM ubuntu:22.04

RUN mkdir -p kubeedge

RUN apt-get update && \
apt-get install -y bzip2 curl upx-ucl gcc-aarch64-linux-gnu libc6-dev-arm64-cross gcc-arm-linux-gnueabi libc6-dev-armel-cross libva-dev libva-drm2 libx11-dev libvdpau-dev libxext-dev libsdl1.2-dev libxcb1-dev libxau-dev libxdmcp-dev yasm

RUN curl -sLO https://ffmpeg.org/releases/ffmpeg-4.1.6.tar.bz2 && \
tar -jx --strip-components=1 -f ffmpeg-4.1.6.tar.bz2 && \
./configure && make && \
make install

COPY --from=builder /build/main kubeedge/
COPY ./config.yaml kubeedge/

WORKDIR kubeedge
Loading