sensor

package module
v0.0.0-...-4e862df Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 8, 2021 License: MIT Imports: 5 Imported by: 0

README

jx-co2-101-sensor

Preview

image

Sensor code

树莓派 Raspberry model 3B+ 和 JX-CO2-101 传感器. 更多文档

package main

import (
	"log"
	"time"

	. "github.com/abserari/jx-co2-101-sensor"
	"github.com/tarm/serial"
)

// pi3 should open uart and communicate with device: /dev/ttyAMA0 | /dev/serial0
func main() {
	c := &serial.Config{Name: "/dev/ttyAMA0", Baud: 9600, ReadTimeout: time.Second * 5}
	s, err := serial.OpenPort(c)
	if err != nil {
		log.Fatal(err)
	}

	sensor := NewCO2Sensor(s)
	sensor.SendActiveModeChange()

	// go func() {
	// 	for {
	// 		sensor.SendQuery()
	// 		time.Sleep(3 * time.Second)
	// 	}
	// }()
	for {
		data, _, err := sensor.ReadLine()
		if err != nil {
			LogError(err)
		}
		value, err := ReadData(data)
		LogInfo(value)
	}

}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ActiveModeChange = []byte{0xff, 0x05, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0xf8}
View Source
var Correct = []byte{0xff, 0x05, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8}
View Source
var Logmode = DebugLog
View Source
var MODBUS_RTU = []byte{0x05, 0x03, 0x00, 0x05, 0x00, 0x01, 0x94, 0x07}
View Source
var QueryModeChange = []byte{0xff, 0x05, 0x03, 0x02, 0x00, 0x00, 0x00, 0x00, 0xf7}
View Source
var QueryPPM = []byte{0xff, 0x05, 0x03, 0x03, 0x01, 0x00, 0x00, 0x00, 0xf5}

Functions

func CRC

func CRC(b []byte, check []byte) bool

CRC not implement from: https://github.com/sigurn/crc8

func LogDebug

func LogDebug(v ...interface{})

func LogError

func LogError(v ...interface{})

func LogInfo

func LogInfo(v ...interface{})

func ReadData

func ReadData(raw []byte) (int, error)

Types

type CO2Sensor

type CO2Sensor struct {
	*serial.Port
	*bufio.Reader
}

func NewCO2Sensor

func NewCO2Sensor(s *serial.Port) *CO2Sensor

func (*CO2Sensor) SendActiveModeChange

func (s *CO2Sensor) SendActiveModeChange() error

func (*CO2Sensor) SendCorrect

func (s *CO2Sensor) SendCorrect() error

func (*CO2Sensor) SendMODBUS_RTU

func (s *CO2Sensor) SendMODBUS_RTU() error

func (*CO2Sensor) SendQuery

func (s *CO2Sensor) SendQuery() (int, error)

func (*CO2Sensor) SendQueryModeChange

func (s *CO2Sensor) SendQueryModeChange() error

type LogMode

type LogMode int
const DebugLog LogMode = 0
const ErrLog LogMode = 2
const InfoLog LogMode = 1

Directories

Path Synopsis
model

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL