Overview

树莓派+二氧化碳传感器.png

Snap

Raspberry

IMG_2581.png

CO2 Sensor

image.png

4G HAT

[Raspberry] JX-CO2-101 CO2 Sensor with GPIO use UART - 图4

Reading Data

  1. cat /dev/ttyAMA0

image.png

Step

RasberryPi 3B+ install golang

  1. wget https://golang.org/dl/go1.15.8.linux-armv6l.tar.gz
  2. tar -C /usr/local -xzf go1.15.8.linux-armv6l.tar.gz && rm -f go1.15.8.linux-armv6l.tar.gz
  3. echo "export PATH=$PATH:/usr/local/go/bin" >> ~/.bash_profile
  4. export PATH=$PATH:/usr/local/go/bin
  5. go version

Read Serial Data

  1. package main
  2. import (
  3. "log"
  4. "time"
  5. "github.com/tarm/serial"
  6. )
  7. // pi3 should open uart and communicate with device: /dev/ttyAMA0 | /dev/serial0
  8. func main() {
  9. c := &serial.Config{Name: "/dev/ttyAMA0", Baud: 9600, ReadTimeout: time.Second * 5}
  10. s, err := serial.OpenPort(c)
  11. if err != nil {
  12. log.Fatal(err)
  13. }
  14. n, err := s.Write([]byte{0xff, 0x01, 0x03, 0x02, 0x00, 0x00, 0x00, 0x00, 0xfb})
  15. log.Println("write n", n)
  16. if err != nil {
  17. log.Fatal(err)
  18. }
  19. buf := make([]byte, 128)
  20. n, err = s.ReadAll(buf)
  21. if err != nil {
  22. log.Fatal(err)
  23. }
  24. log.Printf("%q", buf[:n])
  25. }

Server

receive data from edge device

  1. func (d dioxideDensity) RegistRouter(r gin.IRouter) {
  2. r.POST("/dioxide", d.Add)
  3. }
  4. func (d dioxideDensity) Add(c *gin.Context) {
  5. var req struct {
  6. DioxideDensity int `json: "dioxide" binding:"required"`
  7. DeviceId string `json: "deviceId" binding:"required"`
  8. // Status int `json: "status" binding:"required"`
  9. ZoneName string `json: "zoneName" binding:"required"`
  10. }
  11. if err := c.ShouldBind(&req); err != nil {
  12. c.Error(err)
  13. c.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest})
  14. return
  15. }
  16. err := mysql.InsertDioxide(d.db, req.DioxideDensity, 0, req.ZoneName, req.DeviceId)
  17. if err != nil {
  18. log.Println(err)
  19. }
  20. c.JSON(http.StatusOK, gin.H{"status": http.StatusOK})
  21. }

Data Model

A. 园区省市模式
image.png
B. 经纬度地点模式

  • 需要通过 4G 模块上报 GPS 位置

Display

image.png

Resource

适合嵌入 PPT 的代码图片生成