基于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