Giter VIP home page Giter VIP logo

sds011's Introduction

Simple go package to control / read data from Nova Fitness SDS011 fine dust sensor

Github Release GoDoc Go Report Card Build/Test Status

This package allows to extract structured data from an SDS011 fine dust / particle density sensor device (see here for details / specs). Usage is fairly trivial (see examples directory for a simple console logger implementation).

Features

  • Extraction of firmware version / date
  • Reading / setting of reporting mode (continuous / query-based)
  • Reading / setting of working mode (active / sleep)
  • Polling / query of fine dust data (PM2.5 / PM10) values

Installation

go get -u github.com/fako1024/sds011

Example

// Initialize a new SDS011 sensor
sensor, err := sds011.New("/dev/ttyUSB0")
if err != nil {
  logrus.StandardLogger().Fatalf("Error opening /dev/ttyUSB0: %s", err)
}

// Ensure that device is active, then enable query mode
if err := sensor.SetWorkMode(sds011.WorkModeActive); err != nil {
  logrus.StandardLogger().Errorf("Error setting active mode: %s", err)
}
if err := sensor.SetReportingMode(sds011.ReportingModeQuery); err != nil {
  logrus.StandardLogger().Errorf("Error setting query reporting mode: %s", err)
}

// Ensure that the sensor is put in sleep mode after termination to conserve
// lifetime of the laser
defer func() {
  if err := sensor.SetWorkMode(sds011.WorkModeSleep); err != nil {
    logrus.StandardLogger().Errorf("Error setting sleep mode: %s", err)
  }

  sensor.Close()
}()

// Continuously put the device to active mode for 30 seconds, read out the data
// and put it back to sleep to conserve lifetime of the laser
for {

  // Activate laser and fan, then wait for 30s for the device to settle and for
  // stable air flow
  if err := sensor.SetWorkMode(sds011.WorkModeActive); err != nil {
    logrus.StandardLogger().Errorf("Error setting active mode: %s", err)
  }
  time.Sleep(30 * time.Second)

  // Read single data point
  dataPoint, err := sensor.QueryData()
  if err != nil {
    logrus.StandardLogger().Errorf("Error reading data: %s", err)
  }

  // Log data
  logrus.StandardLogger().Infof("Read data: %s", dataPoint)

  // Put sensor to sleep mode
  if err := sensor.SetWorkMode(sds011.WorkModeSleep); err != nil {
    logrus.StandardLogger().Errorf("Error setting sleep mode: %s", err)
  }

  // Wait 5 minutes to perform the next measurement
  time.Sleep(5 * time.Minute)
}

sds011's People

Contributors

dependabot[bot] avatar fako1024 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.