基于kubeedge examples temperature-demo,并参考traffic-light例程,实现边缘kubeedge DHT11温湿度同时获取。
1 修改crds model.yaml和instance.yaml
model.yaml
kind: DeviceModel
metadata:
name: temp-rh-model
namespace: default
spec:
properties:
- name: temperature-status
description: Temperature collected from the edge device
type:
string:
accessMode: ReadOnly
defaultValue: ''
- name: humidity-status
description: Relative humidity collected from the edge device
type:
string:
accessMode: ReadOnly
defaultValue: ''
instance.yaml
apiVersion: devices.kubeedge.io/v1alpha2
kind: Device
metadata:
name: temp-rh
labels:
description: 'temp-rh'
manufacturer: 'test'
spec:
deviceModelRef:
name: temp-rh-model
nodeSelector:
nodeSelectorTerms:
- matchExpressions:
- key: ''
operator: In
values:
- calrbpi1
status:
twins:
- propertyName: temperature-status
desired:
metadata:
type: string
value: ''
- propertyName: humidity-status
desired:
metadata:
type: string
value: ''
注:calrbpi1是我的部署edgenode,,修改为自己的。
2 Cloud端应用设备模型与设备
$ kubectl apply -f model.yaml
$ sed -i "s#edge-node#<your-edge-node-name>#g" instance.yaml
$ kubectl apply -f instance.yaml
3 修改temperature-mapper/main.go
cp -r temperature-mapper temp-rh-mapper
vim temp-rh-mapper/main.go
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"strconv"
"syscall"
"time"
"github.com/d2r2/go-dht"
"github.com/d2r2/go-shell"
"github.com/yosssi/gmq/mqtt"
"github.com/yosssi/gmq/mqtt/client"
logger "github.com/d2r2/go-logger"
)
const (
DEVICE_ID = "temp-rh"
TEMPERATURE_STATE = "temperature-status"
HUMIDITY_STATE = "humidity-status"
)
var lg = logger.NewPackageLogger("main",
logger.DebugLevel,
// logger.InfoLevel,
)
//DeviceStateUpdate is the structure used in updating the device state
type DeviceStateUpdate struct {
State string `json:"state,omitempty"`
}
//BaseMessage the base struct of event message
type BaseMessage struct {
EventID string `json:"event_id"`
Timestamp int64 `json:"timestamp"`
}
//TwinValue the struct of twin valu

本文介绍了如何基于kubeedge的temperature-demo和traffic-light例子,修改并实现在边缘节点raspberry pi 3B上通过DHT11传感器获取温湿度数据。内容包括修改CRDs文件、设备模型、mapper代码、生成镜像、部署配置以及查看结果的过程。
最低0.47元/天 解锁文章
26

被折叠的 条评论
为什么被折叠?



