Example #1
0
func (d *SHT) Parse(key string, input string) data.SensorData {
	str := strings.Split(input, ",")
	temp, _ := strconv.ParseInt(str[0], 16, 16)
	hum, _ := strconv.ParseInt(str[1], 16, 16)

	var result data.SensorData
	result.TimeStamp = time.Now().UTC()
	result.ID = key
	result.Channel = 0
	result.Serial = "0"
	result.Data = make(map[string]float64)
	result.Data["Temperature"] = float64(temp) / 10.0
	result.Data["Humidity"] = float64(hum) / 10.0
	return result
}
Example #2
0
func (b *BMP) Parse(key string, input string) data.SensorData {
	switch key {
	case "BM0", "BM1", "BM2", "BM6", "BM7", "BM8", "BM9":
		val := parseSignedShort(input)
		b.cal[key[2]-'0'] = int32(val)
	case "BM3", "BM4", "BM5":
		val, err := strconv.ParseUint(input, 16, 16)
		if err != nil {
			jww.ERROR.Println(err)
			return data.SensorData{}
		}
		b.cal[key[2]-'0'] = int32(val)
	case "BMA":
		b.cal[10] = int32(parseSignedShort(input))
		b.updateCal()
	case "BMV":
		val, err := strconv.ParseInt(input, 16, 16)
		if err != nil {
			jww.ERROR.Println(err)
			return data.SensorData{}
		}
		b.avgCount = int32(val)
	case "BMO":
		val, err := strconv.ParseInt(input, 16, 16)
		if err != nil {
			jww.ERROR.Println(err)
			return data.SensorData{}
		}
		b.ossMode = int32(val)
	case "BMX":
		if !b.calibrated {
			return data.SensorData{}
		}
		str := strings.Split(input, ",")
		temp, _ := strconv.ParseUint(str[0], 16, 32)
		pres, _ := strconv.ParseUint(str[1], 16, 32)

		t := uint32(temp) / uint32(b.avgCount)
		p := uint32(pres) / uint32(b.avgCount*16.0)

		alpha := b.c5 * (float64(t) - b.c6)
		bmpTemperature := alpha + b.mc/(alpha+b.md)

		s := bmpTemperature - 25.0
		x := (b.x2*s+b.x1)*s + b.x0
		y := (b.y2*s+b.y1)*s + b.y0
		z := (float64(p) - x) / y
		bmpPressure := (b.p2*z+b.p1)*z + b.p0

		elevationAdj := float64(viper.GetInt("elevation")) * 12.0 / 100.0 // 12 hPa/100m
		bmpPressure += elevationAdj

		var result data.SensorData
		result.TimeStamp = time.Now().UTC()
		result.ID = "BMP"
		result.Channel = 0
		result.Serial = "0"
		result.Data = make(map[string]float64)
		result.Data["Temperature"] = float64(bmpTemperature)
		result.Data["Pressure"] = float64(bmpPressure)
		return result
	}
	return data.SensorData{}
}