Created
January 12, 2020 01:06
-
-
Save kzrl/976b5591cd1e729c9da0c5a24ebb64fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"strconv" | |
"time" | |
"github.com/tarm/serial" | |
) | |
//Based on https://github.com/piotr-sikora-v/sds011-golang | |
func main() { | |
c := &serial.Config{Name: "/dev/ttyUSB0", Baud: 9600} | |
buf := make([]byte, 10) | |
for { | |
s, err := serial.OpenPort(c) | |
if err != nil { | |
panic(err) | |
} | |
_, err = s.Read(buf) | |
if err != nil { | |
panic(err) | |
} | |
now := time.Now() | |
fmt.Println(now.Format(time.RFC822)) | |
low25, err := strconv.ParseInt(fmt.Sprintf("%d", buf[2]), 10, 32) | |
high25, err := strconv.ParseInt(fmt.Sprintf("%d", buf[3]), 10, 32) | |
calc25 := ((high25 * 256) + low25) / 10 | |
fmt.Print("pm2.5: ") | |
fmt.Println(calc25) | |
low10, err := strconv.ParseInt(fmt.Sprintf("%d", buf[4]), 10, 32) | |
high10, err := strconv.ParseInt(fmt.Sprintf("%d", buf[5]), 10, 32) | |
calc10 := ((high10 * 256) + low10) / 10 | |
fmt.Print("pm10: ") | |
fmt.Println(calc10) | |
s.Close() | |
time.Sleep(5 * time.Minute) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment