From 1b54888661d8457a12f5134432e5d7bb8ade6d37 Mon Sep 17 00:00:00 2001 From: servetcos Date: Mon, 4 Mar 2019 16:01:57 +0100 Subject: [PATCH 1/8] run commands as sudo --- README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 85d60ea..40eb472 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,11 @@ Software to read out [Sensirion SCD30](https://www.sensirion.com/en/environmenta This software is licenced under GPLv3 by [UnravelTEC OG](https://unraveltec.com) (https://unraveltec.com), 2018. -## Prerequsites +## Prerequsites -### Python +You might need to run the following commands as root e.g. by typing `sudo` before running a specific command. + +### Python Install the following python-libraries: @@ -57,6 +59,8 @@ Remember: Maximum I2C speed for SCD30 is 100kHz. # Run program +You might need to run the following as root e.g. by typing `sudo` before running the script. + ``` python scd30.py ``` From d396ae585e51f08106bfcb25ba8238293842670a Mon Sep 17 00:00:00 2001 From: servetcos Date: Mon, 4 Mar 2019 16:07:09 +0100 Subject: [PATCH 2/8] example wiring scd30->rpi3b --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 40eb472..f1aae8f 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,12 @@ Software to read out [Sensirion SCD30](https://www.sensirion.com/en/environmenta This software is licenced under GPLv3 by [UnravelTEC OG](https://unraveltec.com) (https://unraveltec.com), 2018. +### Wiring example of the SCD30 to Raspberry Pi 3 B +SCD30: RX/SDA -> Pi: I2C1 SDA (GPIO2) +SCD30: TX/SCL -> Pi: I2C1 SCL (GPIO3) +SCD30: VIN -> Pi: 3.3V/5.5V (use one of PWR pinouts) +SCD30: GND -> Pi: GND (use one of GND pinouts) + ## Prerequsites You might need to run the following commands as root e.g. by typing `sudo` before running a specific command. From 2a592f8ae95306aca3837ae8e492e6e3102a6b14 Mon Sep 17 00:00:00 2001 From: servetcos Date: Mon, 4 Mar 2019 16:10:03 +0100 Subject: [PATCH 3/8] wiring section formatting --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index f1aae8f..f5aefeb 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,11 @@ This software is licenced under GPLv3 by [UnravelTEC OG](https://unraveltec.com) ### Wiring example of the SCD30 to Raspberry Pi 3 B SCD30: RX/SDA -> Pi: I2C1 SDA (GPIO2) + SCD30: TX/SCL -> Pi: I2C1 SCL (GPIO3) + SCD30: VIN -> Pi: 3.3V/5.5V (use one of PWR pinouts) + SCD30: GND -> Pi: GND (use one of GND pinouts) ## Prerequsites From 6f6dbaad11e81f196d32dedae7a2c326c9a66d11 Mon Sep 17 00:00:00 2001 From: servetcos Date: Mon, 4 Mar 2019 16:12:06 +0100 Subject: [PATCH 4/8] wiring moved to prereq section --- README.md | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index f5aefeb..298a28d 100644 --- a/README.md +++ b/README.md @@ -4,17 +4,14 @@ Software to read out [Sensirion SCD30](https://www.sensirion.com/en/environmenta This software is licenced under GPLv3 by [UnravelTEC OG](https://unraveltec.com) (https://unraveltec.com), 2018. -### Wiring example of the SCD30 to Raspberry Pi 3 B -SCD30: RX/SDA -> Pi: I2C1 SDA (GPIO2) - -SCD30: TX/SCL -> Pi: I2C1 SCL (GPIO3) - -SCD30: VIN -> Pi: 3.3V/5.5V (use one of PWR pinouts) - -SCD30: GND -> Pi: GND (use one of GND pinouts) - ## Prerequsites +### Wiring example of the SCD30 to Raspberry Pi 3 B +- SCD30: RX/SDA -> Pi: I2C1 SDA (GPIO2) +- SCD30: TX/SCL -> Pi: I2C1 SCL (GPIO3) +- SCD30: VIN -> Pi: 3.3V/5.5V (use one of PWR pinouts) +- SCD30: GND -> Pi: GND (use one of GND pinouts) + You might need to run the following commands as root e.g. by typing `sudo` before running a specific command. ### Python From 05a9cdbc367d7757f8725aea6da1b5ed503d4bd4 Mon Sep 17 00:00:00 2001 From: servetcos Date: Mon, 4 Mar 2019 16:12:58 +0100 Subject: [PATCH 5/8] formatting --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 298a28d..be39023 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ This software is licenced under GPLv3 by [UnravelTEC OG](https://unraveltec.com) ## Prerequsites -### Wiring example of the SCD30 to Raspberry Pi 3 B +### Wiring SCD30 to Raspberry Pi 3 B - SCD30: RX/SDA -> Pi: I2C1 SDA (GPIO2) - SCD30: TX/SCL -> Pi: I2C1 SCL (GPIO3) - SCD30: VIN -> Pi: 3.3V/5.5V (use one of PWR pinouts) From 7820a7dec6f9a2aaeb766c25c0baa2f61653bb64 Mon Sep 17 00:00:00 2001 From: servetcos Date: Mon, 4 Mar 2019 17:00:33 +0100 Subject: [PATCH 6/8] python3 version of scd30.py called py3scd30.py --- py3scd30.py | 183 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100755 py3scd30.py diff --git a/py3scd30.py b/py3scd30.py new file mode 100755 index 0000000..feef0e7 --- /dev/null +++ b/py3scd30.py @@ -0,0 +1,183 @@ +#!/usr/bin/env python +# coding=utf-8 +# +# Copyright © 2018 UnravelTEC +# Michael Maier +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# If you want to relicense this code under another license, please contact info+github@unraveltec.com. + +# hints from https://www.raspberrypi.org/forums/viewtopic.php?p=600515#p600515 + +from __future__ import print_function + +# This module uses the services of the C pigpio library. pigpio must be running on the Pi(s) whose GPIO are to be manipulated. +# cmd ref: http://abyz.me.uk/rpi/pigpio/python.html#i2c_write_byte_data +import pigpio # aptitude install python-pigpio +import time +import struct +import sys +import crcmod # aptitude install python-crcmod + + +def eprint(*args, **kwargs): + print(*args, file=sys.stderr, **kwargs) + +PIGPIO_HOST = '::1' +PIGPIO_HOST = '127.0.0.1' + +pi = pigpio.pi(PIGPIO_HOST) +if not pi.connected: + eprint("no connection to pigpio daemon at " + PIGPIO_HOST + ".") + exit(1) + +I2C_SLAVE = 0x61 +I2C_BUS = 1 + +try: + pi.i2c_close(0) +except: + if sys.exc_info()[1] and str(sys.exc_info()[1]) != "'unknown handle'": + eprint("Unknown error: ", sys.exc_type, ":", sys.exc_info()[1]) + +try: + h = pi.i2c_open(I2C_BUS, I2C_SLAVE) +except: + eprint("i2c open failed") + exit(1) + +# read meas interval (not documented, but works) + +def read_n_bytes(n): + + try: + (count, data) = pi.i2c_read_device(h, n) + except: + eprint("error: i2c_read failed") + exit(1) + + if count == n: + return data + else: + eprint("error: read measurement interval didnt return " + str(n) + "B") + return False + +def i2cWrite(data): + try: + pi.i2c_write_device(h, data) + except: + eprint("error: i2c_write failed") + return -1 + return True + + +def read_meas_interval(): + ret = i2cWrite([0x46, 0x00]) + if ret == -1: + return -1 + + try: + (count, data) = pi.i2c_read_device(h, 3) + except: + eprint("error: i2c_read failed") + exit(1) + + if count == 3: + if len(data) == 3: + interval = int(data[0])*256 + int(data[1]) + #print "measurement interval: " + str(interval) + "s, checksum " + str(data[2]) + return interval + else: + eprint("error: no array len 3 returned, instead " + str(len(data)) + "type: " + str(type(data))) + else: + "error: read measurement interval didnt return 3B" + + return -1 + +read_meas_result = read_meas_interval() +if read_meas_result == -1: + exit(1) + +if read_meas_result != 2: +# if not every 2s, set it + eprint("setting interval to 2") + ret = i2cWrite([0x46, 0x00, 0x00, 0x02, 0xE3]) + if ret == -1: + exit(1) + read_meas_interval() + + +#trigger cont meas +# TODO read out current pressure value +pressure_mbar = 972 +LSB = 0xFF & pressure_mbar +MSB = 0xFF & (pressure_mbar >> 8) +#print ("MSB: " + hex(MSB) + " LSB: " + hex(LSB)) +#pressure_re = LSB + (MSB * 256) +#print("press " + str(pressure_re)) +pressure = [MSB, LSB] + +pressure_array = ''.join(chr(x) for x in [pressure[0], pressure[1]]) + +#pressure_array = ''.join(chr(x) for x in [0xBE, 0xEF]) # use for testing crc, should be 0x92 +#print(pressure_array) + +f_crc8 = crcmod.mkCrcFun(0x131, 0xFF, False, 0x00) + +#print("CRC: " + hex(f_crc8)) +i2cWrite([0x00, 0x10, pressure[0], pressure[1], f_crc8]) + +# read ready status +while True: + ret = i2cWrite([0x02, 0x02]) + if ret == -1: + exit(1) + + data = read_n_bytes(3) + if data == False: + time.sleep(0.1) + continue + + if data[1] == 1: + #print "data ready" + break + else: + #eprint(".") + time.sleep(0.1) + +#read measurement +i2cWrite([0x03, 0x00]) +data = read_n_bytes(18) + +#print "CO2: " + str(data[0]) +" "+ str(data[1]) +" "+ str(data[3]) +" "+ str(data[4]) + +struct_co2 = struct.pack('>BBBB', data[0], data[1], data[3], data[4]) +float_co2 = struct.unpack('>f', struct_co2)[0] + +struct_T = struct.pack('>BBBB', data[6], data[7], data[9], data[10]) +float_T = struct.unpack('>f', struct_T) + +struct_rH = struct.pack('>BBBB', data[12], data[13], data[15], data[16]) +float_rH = struct.unpack('>f', struct_rH)[0] + +if float_co2 > 0.0: + print("gas_ppm{sensor=\"SCD30\",gas=\"CO2\"} %f" % float_co2) + +print("temperature_degC{sensor=\"SCD30\"} %f" % float_T) + +if float_rH > 0.0: + print("humidity_rel_percent{sensor=\"SCD30\"} %f" % float_rH) + +pi.i2c_close(h) From 72d3ef78b1aaa1eb768cba54ffdbdf3a3092d8f8 Mon Sep 17 00:00:00 2001 From: servetcos Date: Mon, 4 Mar 2019 22:14:18 +0100 Subject: [PATCH 7/8] prereq enable i2c through sudo raspi-config --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index be39023..abb3712 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,9 @@ This software is licenced under GPLv3 by [UnravelTEC OG](https://unraveltec.com) ## Prerequsites +### Enable I2C interface on Raspberry Pi +`sudo raspi-config` navigate to `P5 I2C` and select ``. + ### Wiring SCD30 to Raspberry Pi 3 B - SCD30: RX/SDA -> Pi: I2C1 SDA (GPIO2) - SCD30: TX/SCL -> Pi: I2C1 SCL (GPIO3) From 72c3526543cc875440487c21b54a57e35614cf83 Mon Sep 17 00:00:00 2001 From: servetcoskun Date: Mon, 4 Mar 2019 21:31:35 +0000 Subject: [PATCH 8/8] fixed encoding error crcmod.CrcFun python2-to-python3 --- py3scd30.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py3scd30.py b/py3scd30.py index feef0e7..9522865 100755 --- a/py3scd30.py +++ b/py3scd30.py @@ -137,7 +137,7 @@ def read_meas_interval(): f_crc8 = crcmod.mkCrcFun(0x131, 0xFF, False, 0x00) #print("CRC: " + hex(f_crc8)) -i2cWrite([0x00, 0x10, pressure[0], pressure[1], f_crc8]) +i2cWrite([0x00, 0x10, pressure[0], pressure[1], f_crc8(b'123456789')]) # read ready status while True: