Skip to content

Commit

Permalink
Merge pull request #23 from caternuson/new_example
Browse files Browse the repository at this point in the history
Add multi sensor example
  • Loading branch information
caternuson authored Sep 21, 2021
2 parents de7a46c + ab0bc47 commit 64b85cd
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ repos:
name: pylint (examples code)
description: Run pylint rules on "examples/*.py" files
entry: /usr/bin/env bash -c
args: ['([[ ! -d "examples" ]] || for example in $(find . -path "./examples/*.py"); do pylint --disable=missing-docstring,invalid-name $example; done)']
args: ['([[ ! -d "examples" ]] || for example in $(find . -path "./examples/*.py"); do pylint --disable=missing-docstring,invalid-name,consider-using-f-string $example; done)']
language: system
40 changes: 40 additions & 0 deletions examples/ds18x20_multi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# SPDX-FileCopyrightText: 2020 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

# Example of specifying multiple sensors using explicit ROM codes.
# These ROM codes need to be determined ahead of time. Use `ow_bus.scan()`.
#
# (1) Connect one sensor at a time
# (2) Use `ow_bus.scan()[0].rom` to determine ROM code
# (3) Use ROM code to specify sensors (see this example)

import time
import board
from adafruit_onewire.bus import OneWireBus, OneWireAddress
from adafruit_ds18x20 import DS18X20

# !!!! REPLACE THESE WITH ROM CODES FOR YOUR SENSORS !!!!
ROM1 = b"(\xbb\xfcv\x08\x00\x00\xe2"
ROM2 = b"(\xb3t\xd3\x08\x00\x00\x9e"
ROM3 = b"(8`\xd4\x08\x00\x00i"
# !!!! REPLACE THESE WITH ROM CODES FOR YOUR SENSORS !!!!

# Initialize one-wire bus on board pin D5.
ow_bus = OneWireBus(board.D5)

# Uncomment this to get a listing of currently attached ROMs
# for device in ow_bus.scan():
# print(device.rom)

# Use pre-determined ROM codes for each sensors
temp1 = DS18X20(ow_bus, OneWireAddress(ROM1))
temp2 = DS18X20(ow_bus, OneWireAddress(ROM2))
temp3 = DS18X20(ow_bus, OneWireAddress(ROM3))

# Main loop to print the temperatures every second.
while True:
print("Temperature 1 = {}".format(temp1.temperature))
print("Temperature 2 = {}".format(temp2.temperature))
print("Temperature 3 = {}".format(temp3.temperature))
print("-" * 20)
time.sleep(1)

0 comments on commit 64b85cd

Please sign in to comment.