Skip to content

Instantly share code, notes, and snippets.

@qoobaa
Created April 9, 2016 12:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qoobaa/c5f2f74a421881e2113362b3bcddbdc7 to your computer and use it in GitHub Desktop.
Save qoobaa/c5f2f74a421881e2113362b3bcddbdc7 to your computer and use it in GitHub Desktop.
require "json"
require "net/http"
SDS011_DEV = "/dev/ttyUSB0"
FIREBASE_HOST = "airsensor.firebaseio.com"
AUTH_TOKEN = "B1K9LMmwtYR8EwXtSTztUUwhaC7IjE5HWUOOuSfk"
header, command, pm25l, pm25h, pm10l, pm10h, id1, id2, sum, tail = IO.read(SDS011_DEV, 10).unpack("CCCCCCCCCC")
if header == 0xAA && command == 0xC0 && (pm25l + pm25h + pm10l + pm10h + id1 + id2) % 256 == sum && tail == 0xAB
pm25 = (pm25h * 256 + pm25l) / 10.0
pm10 = (pm10h * 256 + pm10l) / 10.0
data = JSON.generate({pm10: pm10, pm25: pm25, timestamp: {".sv": "timestamp"}})
Net::HTTP.start(FIREBASE_HOST, 443, use_ssl: true) do |http|
response = http.post("/measurements.json?auth=#{AUTH_TOKEN}", data)
raise "Firebase communication error #{response.code}" if response.code != "200"
end
else
raise "SDS011 communication error"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment