-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
351 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#!/usr/bin/python | ||
import smbus | ||
import time | ||
|
||
# Define some constants from the datasheet | ||
|
||
DEVICE = 0x23 # Default device I2C address | ||
|
||
POWER_DOWN = 0x00 # No active state | ||
POWER_ON = 0x01 # Power on | ||
RESET = 0x07 # Reset data register value | ||
|
||
# Start measurement at 4lx resolution. Time typically 16ms. | ||
CONTINUOUS_LOW_RES_MODE = 0x13 | ||
# Start measurement at 1lx resolution. Time typically 120ms | ||
CONTINUOUS_HIGH_RES_MODE_1 = 0x10 | ||
# Start measurement at 0.5lx resolution. Time typically 120ms | ||
CONTINUOUS_HIGH_RES_MODE_2 = 0x11 | ||
# Start measurement at 1lx resolution. Time typically 120ms | ||
# Device is automatically set to Power Down after measurement. | ||
ONE_TIME_HIGH_RES_MODE_1 = 0x20 | ||
# Start measurement at 0.5lx resolution. Time typically 120ms | ||
# Device is automatically set to Power Down after measurement. | ||
ONE_TIME_HIGH_RES_MODE_2 = 0x21 | ||
# Start measurement at 1lx resolution. Time typically 120ms | ||
# Device is automatically set to Power Down after measurement. | ||
ONE_TIME_LOW_RES_MODE = 0x23 | ||
|
||
#bus = smbus.SMBus(0) # Rev 1 Pi uses 0 | ||
bus = smbus.SMBus(1) # Rev 2 Pi uses 1 | ||
|
||
def convertToNumber(data): | ||
# Simple function to convert 2 bytes of data | ||
# into a decimal number | ||
return ((data[1] + (256 * data[0])) / 1.2) | ||
|
||
def readLight(addr=DEVICE): | ||
data = bus.read_i2c_block_data(addr,ONE_TIME_HIGH_RES_MODE_1) | ||
return convertToNumber(data) | ||
|
||
def main(): | ||
|
||
while True: | ||
print ("Light Level : " + str(readLight()) + " lx") | ||
time.sleep(0.5) | ||
|
||
if __name__=="__main__": | ||
main() |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: latin-1 -*- | ||
# | ||
# i2c2mqtt.py | ||
# | ||
# Copyright 2016 Sébastien Lucas <[email protected]> | ||
# | ||
# 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 <http://www.gnu.org/licenses/>. | ||
# | ||
|
||
import bh1750 | ||
import bme280 | ||
import si7201 | ||
import time, json, argparse | ||
import paho.mqtt.publish as publish # pip install paho-mqtt | ||
|
||
verbose = False | ||
|
||
def debug(msg): | ||
if verbose: | ||
print (msg + "\n") | ||
|
||
def getI2cSensors(devices): | ||
tstamp = int(time.time()) | ||
|
||
newObject = {"time": tstamp} | ||
|
||
if not isinstance(devices, list): | ||
newObject['message'] = 'No devices specified.' | ||
return (False, newObject) | ||
|
||
if 'bh750' in devices: | ||
###### Get luminosity ## | ||
lux = bh1750.readLight() | ||
# Drop the first (usually bad) | ||
lux = bh1750.readLight() | ||
newObject['lum'] = int (lux) | ||
debug ("Light Level : " + str(newObject['lum']) + " lx") | ||
|
||
if 'si7201' in devices: | ||
###### Get temperature & humidity from si7201 ## | ||
T = si7201.readTemperature() | ||
newObject['temp'] = round (T, 1) | ||
RH = si7201.readHumidity() | ||
newObject['hum'] = int (RH) | ||
debug ("Temperature : " + str(newObject['temp']) + " °C") | ||
debug ("Humidity : " + str(newObject['hum']) + " %") | ||
|
||
if 'bme280' in devices: | ||
###### Get temperature, pressure & humidity from bme280 ## | ||
T, P, RH = bme280.readBME280All() | ||
newObject['temp'] = round (T, 1) | ||
newObject['hum'] = int (RH) | ||
newObject['pres'] = int(P) | ||
debug ("Temperature : " + str(newObject['temp']) + " °C") | ||
debug ("Humidity : " + str(newObject['hum']) + " %") | ||
debug ("Pressure : " + str(newObject['pres']) + " hPa") | ||
|
||
return (True, newObject) | ||
|
||
parser = argparse.ArgumentParser(description='Read current temperature,illuminance and humidity from i2c sensors and send them to a MQTT broker.') | ||
parser.add_argument('-d', '--device', dest='devices', action="append", | ||
help='Specify the devices to probe in the I2C bus. Can be called many times.') | ||
parser.add_argument('-m', '--mqtt-host', dest='host', action="store", default="127.0.0.1", | ||
help='Specify the MQTT host to connect to.') | ||
parser.add_argument('-n', '--dry-run', dest='dryRun', action="store_true", default=False, | ||
help='No data will be sent to the MQTT broker.') | ||
parser.add_argument('-t', '--topic', dest='topic', action="store", default="sensor/i2c", | ||
help='The MQTT topic on which to publish the message (if it was a success).') | ||
parser.add_argument('-T', '--topic-error', dest='topicError', action="store", default="error/sensor/i2c", metavar="TOPIC", | ||
help='The MQTT topic on which to publish the message (if it wasn\'t a success).') | ||
parser.add_argument('-v', '--verbose', dest='verbose', action="store_true", default=False, | ||
help='Enable debug messages.') | ||
|
||
args = parser.parse_args() | ||
verbose = args.verbose; | ||
|
||
status, data = getI2cSensors(args.devices) | ||
jsonString = json.dumps(data) | ||
if status: | ||
debug("Success with message (for current readings) <{0}>".format(jsonString)) | ||
if not args.dryRun: | ||
publish.single(args.topic, jsonString, hostname=args.host) | ||
else: | ||
debug("Failure with message <{0}>".format(jsonString)) | ||
if not args.dryRun: | ||
publish.single(args.topicError, jsonString, hostname=args.host) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/usr/bin/python | ||
# -*- coding: latin-1 -*- | ||
|
||
import smbus | ||
bus = smbus.SMBus(1) | ||
address = 0x40 | ||
|
||
def crc(data): | ||
rem = 0 | ||
for b in data: | ||
rem ^= b | ||
for bit in range(8): | ||
if rem & 128: | ||
rem = (rem << 1) ^ 0x31 | ||
else: | ||
rem = (rem << 1) | ||
return rem & 0xFF | ||
|
||
def readTemperature(): | ||
r = bus.read_i2c_block_data(address,0xE3) | ||
t_val = (r[0]<<8) + r[1] | ||
return ((175.72 * t_val)/65536.0) - 46.85 | ||
|
||
def readHumidity(): | ||
r = bus.read_i2c_block_data(address,0xE5) | ||
rh_val = (r[0]<<8) + r[1] | ||
return ((125.0 * rh_val)/65536.0) - 6.0 | ||
|
||
def main(): | ||
print ("Temperature : " + str(readTemperature()) + " °C") | ||
print ("Humidity : " + str(readHumidity()) + " %") | ||
|
||
if __name__=="__main__": | ||
main() | ||
|