Example #1
0
def getDevice():
    # This function is used to query the OneWireBus (OWB) to grab a list
    # of devices. This function expects that the sensor is wired up to
    # Digital pin 2. If you used a different pin, change the following
    # line of code:
    owb = OneWireBus(board.D2)

    devices = owb.scan()
    for device in devices:

        idString = "ROM = {} \tFamily = 0x{:02x}".format(
            [hex(i) for i in device.rom], device.family_code)
        #
        # This is the particular sensor identification string returned by the DS18B20 that I am using.
        # You might have to modify this string if it doesn't work for you. You can log output to Mu serial console
        # to see the idStrings for all your OneWire devices.
        # e.g.
        # print idString
        if idString == "ROM = ['0x28', '0xaa', '0x81', '0x2', '0x38', '0x14', '0x1', '0xfe'] 	Family = 0x28":
            ds18b20 = adafruit_ds18x20.DS18X20(owb, device)
            #
            # There are several resolution settings (e.g. how many significant digits of temperature are sent)
            # The settings are 9,10,11,12. Just FYI, the larger the resolution, the longer the time to generate
            # the data (we're talking 100-700ms) but for our purposes I would rather have ALL THE SIGNIFICANT DIGITS!
            # See here for more info: https://www.maximintegrated.com/en/app-notes/index.mvp/id/4377 or https://cdn-shop.adafruit.com/datasheets/DS18B20.pdf
            # So, 12 it is. :-)
            ds18b20.resolution = 12
            return ds18b20
    return None
Example #2
0
    def __init__(self):
        # Set up watchdog timer
        self.watchdog = microcontroller.watchdog
        self.watchdog.deinit()
        self.watchdog.timeout = WATCHDOG_TIMEOUT
        self.watchdog.mode = WATCHDOG_MODE

        # Set up heartbeat output (i.e red LED)
        self._heartbeat = digitalio.DigitalInOut(HEARTBEAT_PIN)
        self._heartbeat.direction = digitalio.Direction.OUTPUT
        self._heartbeat_duration = HEARTBEAT_DURATION

        # Set up I2C bus
        i2c = busio.I2C(board.SCL, board.SDA)
        # Set up SPI bus
        spi = busio.SPI(board.SCK, board.MOSI, board.MISO)

        # Set up real time clock as source for time.time() or time.localtime() calls.
        print("Initialising real time clock.\n\n\n\n")
        clock = PCF8523(i2c)
        rtc.set_time_source(clock)

        print("Initialising display.\n\n\n\n")
        self.display = Display(self, DISPLAY_TIMEOUT, i2c, spi)

        print("Initialising lights.\n\n\n\n")
        self.lights = Lights(LIGHTS_ON_TIME, LIGHTS_OFF_TIME, LIGHTS_ENABLE_PIN, LIGHTS_DISABLE_PIN)

        print("Initialising feeder.\n\n\n\n")
        self.feeder = Feeder(FEEDING_TIMES, PORTIONS_PER_MEAL, FEEDER_MOTOR, FEEDER_STEPS_PER_ROTATION,
                             FEEDER_STEP_DELAY, FEEDER_STEP_STYLE, i2c)

        print("Initialising temperature sensors.\n\n\n\n")
        ow_bus = OneWireBus(OW_PIN)
        self.water_sensor = TemperatureSensor(ow_bus, WATER_SN, WATER_OFFSET)
        self.air_sensor = TemperatureSensor(ow_bus, AIR_SN, AIR_OFFSET)

        # Set up SD card
        print("Setting up logging.\n\n\n\n")
        cs = digitalio.DigitalInOut(SD_CS)
        sdcard = SDCard(spi, cs)
        vfs = storage.VfsFat(sdcard)
        storage.mount(vfs, "/sd")
        self._log_data = LOG_DATA
        self._log_interval = time_tuple_to_secs(LOG_INTERVAL)
        self._last_log = None

        print("Initialising Bluetooth.\n\n\n\n")
        self._ble = BLERadio()
        self._ble._adapter.name = BLE_NAME
        self._ble_uart = UARTService()
        self._ble_ad = ProvideServicesAdvertisement(self._ble_uart)
Example #3
0
import board
import busio
import digitalio
import time
from adafruit_onewire.bus import OneWireBus
from adafruit_ds18x20 import DS18X20

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

ds18_bus = ow_bus.scan()
print(ds18_bus)

ds18 = []
for probe in ds18_bus:
    print(probe)
    ds18.append(DS18X20(ow_bus, probe))

print(ds18)
Example #4
0
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

# This example shows how to access the DS2413 pins and use them for both input
# and output. In this example, it is assumed an LED is attached to IOA and a
# button is attached to IOB. See the datasheet for details about how to
# interface the external hardware (it is different than most Arduino examples).
import time
import board
from adafruit_onewire.bus import OneWireBus
import adafruit_ds2413

# Create OneWire bus
ow_bus = OneWireBus(board.D2)

# Create the DS2413 object from the first one found on the bus
ds = adafruit_ds2413.DS2413(ow_bus, ow_bus.scan()[0])

# LED on IOA
led = ds.IOA

# button on IOB
button = ds.IOB
button.direction = adafruit_ds2413.INPUT

# Loop forever
while True:
    # Check for button press
    if button.value:
        # Print a message.
        print("Button pressed!")
Example #5
0
# This example shows how to access the DS2413 pins and use them for both input
# and output. In this example, it is assumed an LED is attached to IOA and a
# button is attached to IOB. See the datasheet for details about how to
# interface the external hardware (it is different than most Arduino examples).
import time
import board
from adafruit_onewire.bus import OneWireBus
import adafruit_ds2413

# Create OneWire bus
ow_bus = OneWireBus(board.D2)

# Create the DS2413 object from the first one found on the bus
ds = adafruit_ds2413.DS2413(ow_bus, ow_bus.scan()[0])

# LED on IOA
led = ds.IOA

# button on IOB
button = ds.IOB
button.direction = adafruit_ds2413.INPUT

# Loop forever
while True:
    # Check for button press
    if button.value:
        # Print a message.
        print("Button pressed!")
        # Toggle LED
        led.value = not led.value
        # A little debounce
Example #6
0
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import board
from adafruit_onewire.bus import OneWireBus

# Create the 1-Wire Bus
# Use whatever pin you've connected to on your board
ow_bus = OneWireBus(4)

# Reset and check for presence pulse.
# This is basically - "is there anything out there?"
print("Resetting bus...", end="")
if ow_bus.reset():
    print("OK.")
else:
    raise RuntimeError("Nothing found on bus.")

# Run a scan to get all of the device ROM values
print("Scanning for devices...", end="")
devices = ow_bus.scan()
print("OK.")
print("Found {} device(s).".format(len(devices)))

# For each device found, print out some info
for i, d in enumerate(devices):
    print("Device {:>3}".format(i))
    print("\tSerial Number = ", end="")
    for byte in d.serial_number:
        print("0x{:02x} ".format(byte), end="")
    print("\n\tFamily = 0x{:02x}".format(d.family_code))
# Simple demo of printing the temperature from the first found DS18x20 sensor every second.
# Author: Tony DiCola

# A 4.7Kohm pullup between DATA and POWER is REQUIRED!

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

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

# Scan for sensors and grab the first one found.
ds18 = DS18X20(ow_bus, ow_bus.scan()[0])

# Main loop to print the temperature every second.
while True:
    print("Temperature: {0:0.3f}C".format(ds18.temperature))
    time.sleep(1.0)
Example #8
0
                [hex(i) for i in dev.rom], dev.family_code))
    return devices[0] if devices else None


def measure_temps(sensor_list=None):
    names = sensors.keys() if sensor_list is None else sensor_list
    readings = OrderedDict()
    pixel[0] = READING
    for s_name in names:
        readings[s_name] = sensors[s_name]()
    pixel[0] = BLACK
    return readings


i2c = board.I2C()
ow_bus = OneWireBus(DS18X20_PIN)
bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c)
sht31d = adafruit_sht31d.SHT31D(i2c)
ds18b20 = adafruit_ds18x20.DS18X20(ow_bus, find_DS18X20(ow_bus,
                                                        verbose=VERBOSE))
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)

ntc_ain = AnalogIn(NTC_PIN)
tmp36_ain = AnalogIn(TMP36_PIN)
lm35_ain = AnalogIn(LM35_PIN)

vref = ntc_ain.reference_voltage

### Review PAD_TO_LEN if extending this dict
sensors = OrderedDict([("bmp280", lambda: bmp280.temperature),
                       ("sht31d", lambda: sht31d.temperature),
Example #9
0
    from secrets import secrets
except ImportError:
    print("WiFi secrets are kept in secrets.py, please add them there!")
    raise

try:
    from data import data
except ImportError:
    print("Metadata is kept in Data.py, please add it there!")
    raise

location = data['location']
latitude = data['latitude']
longitude = data['longitude']
# Initialize one-wire bus on board pin D5.
ow_bus = OneWireBus(board.A3)
oneWire = ow_bus.scan()

# Scan for sensors and grab the first one found.
sensors = []

#Fill sensors array with oneWire objects
for i, d in enumerate(oneWire):
    serial = ""
    for byte in d.serial_number:
        #print("{:02x}".format(byte), end='')
        serial = serial + "{:02x}".format(byte)
    sensor = {"Serial Number": serial, "ow": DS18X20(ow_bus, ow_bus.scan()[i])}
    sensors.append(sensor)
    t = '{0:0.3f}'.format(DS18X20(ow_bus, ow_bus.scan()[i]).temperature)
    ds18 = DS18X20(ow_bus, ow_bus.scan()[i])
Example #10
0
# Simple demo of printing the temperature from the first found DS18x20 sensor every second.
# Author: Tony DiCola
import time

import board

from adafruit_onewire.bus import OneWireBus
from adafruit_ds18x20 import DS18X20


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

# Scan for sensors and grab the first one found.
ds18 = DS18X20(ow_bus, ow_bus.scan()[0])

# Main loop to print the temperature every second.
while True:
    print('Temperature: {0:0.3f}C'.format(ds18.temperature))
    time.sleep(1.0)
import board
import busio
import digitalio
import time
from adafruit_onewire.bus import OneWireBus

spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
cs = digitalio.DigitalInOut(board.RFM9X_CS)
reset = digitalio.DigitalInOut(board.RFM9X_RST)
import adafruit_rfm9x
rfm9x = adafruit_rfm9x.RFM9x(spi, cs, reset, 868.0)

ow_bus=OneWireBus(board.D5)
devices=ow_bus.scan()
import adafruit_ds18x20
ds18b20 = adafruit_ds18x20.DS18X20(ow_bus, devices[0])

while True:
    temp=ds18b20.temperature
    rfm9x.send(str(temp))
    time.sleep(5)
Example #12
0
import board
import time
from adafruit_onewire.bus import OneWireBus
from adafruit_ds18x20 import DS18X20
import digitalio

bus = OneWireBus(board.D5)
sensor = DS18X20(bus, bus.scan()[0])

rele = digitalio.DigitalInOut(board.D6)
rele.direction = digitalio.Direction.OUTPUT

while True:
    t = sensor.temperature
    print('Temperatura: {0:0.3f} °C'.format(t))

    if t < 60:
        rele.value = False
    else:
        rele.value = True

    time.sleep(1.0)
Example #13
0
#DS18X20库模块
from adafruit_onewire.bus import OneWireBus
from adafruit_ds18x20 import DS18X20

#构建I2C对象
i2c = busio.I2C(board.SCK, board.MOSI)

#构建oled对象,01Studio配套的OLED地址为0x3C
display = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c, addr=0x3C)

#清屏
display.fill(0)
display.show()

# 初始化单总线对象,引脚为D5.
ow = OneWireBus(board.D5)

# 搜索传感器,返回第1个
ds = DS18X20(ow, ow.scan()[0])

while True:

    temp = round(ds.temperature, 2)  #保留2位小数

    display.fill(0)  #清屏
    display.text('01Studio', 0, 0, 1, font_name='font5x8.bin')
    display.text('Temp Test', 0, 20, 1, font_name='font5x8.bin')
    display.text(str(temp) + ' C', 0, 40, 1, font_name='font5x8.bin')
    display.show()

    print(str(temp) + ' C')
Example #14
0
# (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))
Example #15
0
import time
import board
from adafruit_onewire.bus import OneWireBus
from adafruit_ds18x20 import DS18X20

# Initialize one-wire bus on board D5.
ow_bus = OneWireBus(board.GP2)
# Scan for sensors and grab them all
ds18 = [DS18X20(ow_bus, found) for found in ow_bus.scan()]
# 12-bit resolution (default)
ds18.resolution = 10

# read (request) the temperature every 5 seconds
TEMPERATURE_DELAY = 5

# time when the next read should occur.
ds18_next = time.monotonic()
# should we start the async read or do the actual read ?
ds18_do_read = False
# delay for async read (default, real value will be set later)
ds18_async_delay = 1

# Main loop to print the temperature every 5 second.
while True:
    now = time.monotonic()

    # Is it time to read ?
    if now > ds18_next:
        # if we did request the temperature, now do the read
        if ds18_do_read:
            # fetch the temperatures from the sensor
import board
from adafruit_onewire.bus import OneWireBus

# Create the 1-Wire Bus
# Use whatever pin you've connected to on your board
ow_bus = OneWireBus(board.D2)

# Reset and check for presence pulse.
# This is basically - "is there anything out there?"
print("Resetting bus...", end="")
if ow_bus.reset():
    print("OK.")
else:
    raise RuntimeError("Nothing found on bus.")

# Run a scan to get all of the device ROM values
print("Scanning for devices...", end="")
devices = ow_bus.scan()
print("OK.")
print("Found {} device(s).".format(len(devices)))

# For each device found, print out some info
for i, d in enumerate(devices):
    print("Device {:>3}".format(i))
    print("\tSerial Number = ", end="")
    for byte in d.serial_number:
        print("0x{:02x} ".format(byte), end="")
    print("\n\tFamily = 0x{:02x}".format(d.family_code))

# Usage beyond this is device specific. See a CircuitPython library for a 1-Wire
# device for examples and how OneWireDevice is used.
Example #17
0
import board
import busio
import digitalio
from digitalio import DigitalInOut
import time
import gc
import adafruit_bme280
from adafruit_onewire.bus import OneWireBus
ow_bus = OneWireBus(board.A5)

devices = ow_bus.scan()
for device in devices:
    print("ROM = {} \tFamily = 0x{:02x}".format([hex(i) for i in device.rom],
                                                device.family_code))

import adafruit_ds18x20
ds18b20 = adafruit_ds18x20.DS18X20(ow_bus, devices[0])

# Get Wifi and FarmOS details
try:
    from secrets import secrets
except ImportError:
    print("WiFi secrets are kept in secrets.py, please add them there!")
    raise

WIFI_ESSID = secrets['ssid']
WIFI_PASS = secrets['password']
farmos_pubkey = secrets['farmos_pubkey']
farmos_privkey = secrets['farmos_privkey']

base_url = "https://edgecollective.farmos.net/farm/sensor/listener/"