Skip to content

Latest commit

 

History

History
72 lines (52 loc) · 1.58 KB

README.md

File metadata and controls

72 lines (52 loc) · 1.58 KB

Package senml

godoc cirrus ci goreportcard gocover

Package senml implements the SenML specification, used for sending simple sensor measurements encoded in JSON, CBOR or XML.

The goal of this package is to not only support the specification, but to also make it easy to work with within Go.

Examples

Encoding:

package main

import (
	"fmt"
	"time"

	"github.com/silkeh/senml"
)

func main() {
	now := time.Now()
	list := []senml.Measurement{
		senml.NewValue("sensor:temperature", 23.5, senml.Celsius, now, 0),
		senml.NewValue("sensor:humidity", 33.7, senml.RelativeHumidityPercent, now, 0),
	}

	data, err := senml.EncodeJSON(list)
	if err != nil {
		fmt.Print("Error encoding to JSON:", err)
	}

	fmt.Printf("%s\n", data)
}

Decoding:

package main

import (
	"fmt"

	"github.com/silkeh/senml"
)

func main() {
	json := []byte(`[{"bn":"sensor:","bt":1555487588,"bu":"Cel","n":"temperature","v":23.5},{"n":"humidity","u":"%RH","v":33.7}]`)

	list, err := senml.DecodeJSON(json)
	if err != nil {
		fmt.Print("Error encoding to JSON:", err)
	}

	for _, m := range list {
		fmt.Printf("%#v\n", m)
	}
}