Beispiel #1
0
    def push_to_io(self, feed_key, data):
        # pylint: disable=line-too-long
        """Push data to an adafruit.io feed

        :param str feed_key: Name of feed key to push data to.
        :param data: data to send to feed

        """
        # pylint: enable=line-too-long

        try:
            aio_username = secrets['aio_username']
            aio_key = secrets['aio_key']
        except KeyError:
            raise KeyError("Adafruit IO secrets are kept in secrets.py, please add them there!\n\n")

        wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(self._esp, secrets, None)
        io_client = RESTClient(aio_username, aio_key, wifi)

        while True:
            try:
                feed_id = io_client.get_feed(feed_key)
            except AdafruitIO_RequestError:
                # If no feed exists, create one
                feed_id = io_client.create_new_feed(feed_key)
            except RuntimeError as exception:
                print("An error occured, retrying! 1 -", exception)
                continue
            break

        while True:
            try:
                io_client.send_data(feed_id['key'], data)
            except RuntimeError as exception:
                print("An error occured, retrying! 2 -", exception)
                continue
            except NameError as exception:
                print(feed_id['key'], data, exception)
                continue
            break
Beispiel #2
0
class AIOHandler(LoggingHandler):

    def __init__(self, name):
        """Create an instance."""
        # PyPortal ESP32 Setup
        esp32_cs = DigitalInOut(board.ESP_CS)
        esp32_ready = DigitalInOut(board.ESP_BUSY)
        esp32_reset = DigitalInOut(board.ESP_RESET)
        spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
        esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
        status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
        wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)

        # Set your Adafruit IO Username and Key in secrets.py
        # (visit io.adafruit.com if you need to create an account,
        # or if you need your Adafruit IO key.)
        ADAFRUIT_IO_USER = secrets['adafruit_io_user']
        ADAFRUIT_IO_KEY = secrets['adafruit_io_key']

        # Create an instance of the Adafruit IO REST client
        self._io = RESTClient(ADAFRUIT_IO_USER, ADAFRUIT_IO_KEY, wifi)

        self._name = '{0}-logging'.format(name)
        try:
            # Get the logging feed from Adafruit IO
            self._log_feed = self._io.get_feed(self._name)
        except AdafruitIO_RequestError:
            # If no logging feed exists, create one
            self._log_feed = self._io.create_new_feed(self._name)

    def emit(self, level, msg):
        """Generate the message and write it to the UART.

        :param level: The level at which to log
        :param msg: The core message

        """
        self._io.send_data(self._log_feed['key'], self.format(level, msg))
Beispiel #3
0
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
"""

# Set your Adafruit IO Username and Key in secrets.py
# (visit io.adafruit.com if you need to create an account,
# or if you need your Adafruit IO key.)
aio_username = secrets['aio_username']
aio_key = secrets['aio_key']

# Create an instance of the Adafruit IO REST client
io = RESTClient(aio_username, aio_key, wifi)

# Create a new 'circuitpython' feed with a description
print('Creating new Adafruit IO feed...')
feed = io.create_new_feed('circuitpython', 'a Adafruit IO CircuitPython feed')

# List a specified feed
print('Retrieving new Adafruit IO feed...')
specified_feed = io.get_feed('circuitpython')
print(specified_feed)

# Delete a specified feed by feed key
print('Deleting feed...')
io.delete_feed(specified_feed['key'])
print('Feed deleted!')
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
"""

# Set your Adafruit IO Username and Key in secrets.py
# (visit io.adafruit.com if you need to create an account,
# or if you need your Adafruit IO key.)
ADAFRUIT_IO_USER = secrets['adafruit_io_user']
ADAFRUIT_IO_KEY = secrets['adafruit_io_key']

# Create an instance of the Adafruit IO REST client
io = RESTClient(ADAFRUIT_IO_USER, ADAFRUIT_IO_KEY, wifi)

try:
    # Get the 'temperature' feed from Adafruit IO
    temperature_feed = io.get_feed('temperature')
except AdafruitIO_RequestError:
    # If no 'temperature' feed exists, create one
    temperature_feed = io.create_new_feed('temperature')

# Send random integer values to the feed
random_value = randint(0, 50)
print('Sending {0} to temperature feed...'.format(random_value))
io.send_data(temperature_feed['key'], random_value)
print('Data sent!')

# Retrieve data value from the feed
print('Retrieving data from temperature feed...')
received_data = io.receive_data(temperature_feed['key'])
print('Data from temperature feed: ', received_data['value'])
Beispiel #5
0
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
"""

# Set your Adafruit IO Username and Key in secrets.py
# (visit io.adafruit.com if you need to create an account,
# or if you need your Adafruit IO key.)
aio_username = secrets['aio_username']
aio_key = secrets['aio_key']

# Create an instance of the Adafruit IO REST client
io = RESTClient(aio_username, aio_key, wifi)

try:
    # Get the 'digital' feed from Adafruit IO
    digital_feed = io.get_feed('digital')
except AdafruitIO_RequestError:
    # If no 'digital' feed exists, create one
    digital_feed = io.create_new_feed('digital')

# Set up LED
LED = DigitalInOut(board.D13)
LED.direction = Direction.OUTPUT

while True:
    # Get data from 'digital' feed
    print('getting data from IO...')
    feed_data = io.receive_data(digital_feed['key'])

    # Check if data is ON or OFF
    if int(feed_data['value']) == 1:
Beispiel #6
0
class BabyClock:
    def __init__(self):
        self.splash = displayio.Group(max_size=5)
        self.bg_group = displayio.Group(max_size=1)

        board.DISPLAY.auto_brightness = True

        self.bg_file = None

        self.texts = []

        # touchscreen
        self.touchscreen = adafruit_touchscreen.Touchscreen(board.TOUCH_XL, board.TOUCH_XR,
                                               board.TOUCH_YD, board.TOUCH_YU,
                                               calibration=((5200, 59000),(5800, 57000)),
                                               size=(320, 240))

        # connecting wifi
        self.wifi = connect_wifi()

        # connecting io
        self.connect_io()

        # setting local time
        set_local_time()

        # getting last feed time from adafruit io
        self.prev_feed_end_time, self.prev_feed_start_time, self.prev_feed_count = self.get_last_feed_time()

        board.DISPLAY.show(self.splash)

    def connect_io(self):
        print('Connecting adafruit io...')
        self.io = RESTClient(secrets['aio_username'], secrets['aio_key'], self.wifi)
        self.feed_start_time_f = self.io.get_feed('feed-start')
        self.feed_end_time_f = self.io.get_feed('feed-end')
        self.feed_count_f = self.io.get_feed('feed-count')
        print('Done.')

    """Send feed time with count of the day to adafruit io
    """
    def new_feed_time(self):
        print('Loading history...')
        received_data = self.io.receive_data(self.feed_time_f['key'])
        print('Data from temperature feed: ', received_data['value'])

    """Get from adafruit io last feed time & count
    """
    def get_last_feed_time(self):
        data = self.io.receive_data(self.feed_start_time_f['key'])
        prev_feed_start_time = str_to_time(data['value'])
        data = self.io.receive_data(self.feed_end_time_f['key'])
        prev_feed_end_time = str_to_time(data['value'])
        data = self.io.receive_data(self.feed_count_f['key'])
        prev_feed_count = int(data['value'])
        return prev_feed_end_time, prev_feed_start_time, prev_feed_count

    def clear_display(self):
        # remove all from group
        while self.bg_group:
            self.bg_group.pop()
        while self.splash:
            self.splash.pop()
        self.splash.append(self.bg_group)
        self.texts = []

    def set_background(self, image_file):
        # close if open
        if self.bg_file:
            self.bg_file.close()
        self.bck_file = open(image_file, 'rb')
        bg = displayio.OnDiskBitmap(self.bck_file)
        bg_sprite = displayio.TileGrid(bg, pixel_shader=displayio.ColorConverter(), x=0, y=0)
        self.bg_group.append(bg_sprite)
        board.DISPLAY.refresh_soon()
        board.DISPLAY.wait_for_frame()

    def set_to_info(self, max_glyphs=2):
        self.dim_down()

        self.clear_display()
        self.set_background('back_info.bmp')
        # hour since, min since, count
        positions = [(210,45),(250,45),(230,110)]
        font = bitmap_font.load_font('/fonts/Nunito-Black-17.bdf')
        #font.load_glyphs(b'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')
        font.load_glyphs(b'0123456789:/-_,. ')
        for x, y in positions:
            text = Label(font, text='', max_glyphs=max_glyphs)
            text.x = x
            text.y = y
            text.color = 0x000000
            self.splash.append(text)
            self.texts.append(text)

        # :
        text = Label(font, text=':')
        text.x = 238
        text.y = 43
        text.color =0x000000
        self.splash.append(text)

        self.dim_up()

    def set_to_timer(self):
        self.dim_down()
        self.feed_start_time = time.localtime()

        if self.feed_start_time.tm_mday != self.prev_feed_start_time.tm_mday:
            self.prev_feed_count = 0

        self.clear_display()
        self.set_background('back_timer.bmp')
        # hour since, min since, sec_since
        positions = [(100-50+10,120),(220-50+10,120)]
        font = bitmap_font.load_font('/fonts/Nunito-Light-75.bdf')
        #font.load_glyphs(b'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')
        font.load_glyphs(b'0123456789: ')
        for x, y in positions:
            text = Label(font, text='', max_glyphs=2)
            text.x = x
            text.y = y
            text.color = 0xffffff # white
            self.splash.append(text)
            self.texts.append(text)

        # add : between numbers
        text = Label(font, text=':')
        text.x = 160-7
        text.y = 120-5
        text.color = 0xffffff # white
        self.splash.append(text)

        self.dim_up()

    def set_texts(self, texts):
        for text_area, text in zip(self.texts, texts):
            text_area.text = str(text)

    def dim_down(self):
        board.DISPLAY.auto_brightness = False
        for i in range(100, -1, -1):  # dim down
            board.DISPLAY.brightness = i/100
            time.sleep(0.005)

    def dim_up(self):
        board.DISPLAY.auto_brightness = False
        for i in range(101):  # dim down
            board.DISPLAY.brightness = (i)/100
            time.sleep(0.005)
        board.DISPLAY.auto_brightness = True

    def send_to_io(self):
        # feed_start_time
        # feed_end_time
        # count

        # set previous
        self.prev_feed_end_time = time.localtime()
        self.prev_feed_start_time = self.feed_start_time
        self.prev_feed_count += 1

        # send to io
        self.io.send_data(self.feed_start_time_f['key'], time_to_str(self.feed_start_time))
        self.io.send_data(self.feed_end_time_f['key'], time_to_str(self.prev_feed_end_time))
        self.io.send_data(self.feed_count_f['key'], self.prev_feed_count)
Beispiel #7
0
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(
    esp, secrets, status_light)

# Set your Adafruit IO Username and Key in secrets.py
# (visit io.adafruit.com if you need to create an account,
# or if you need your Adafruit IO key.)
aio_username = secrets['aio_username']
aio_key = secrets['aio_key']

# Create an instance of the Adafruit IO REST client
io = RESTClient(aio_username, aio_key, wifi)

# Get the weight feed from IO
weight_feed = io.get_feed('weight')

# initialize the dymo scale
units_pin = digitalio.DigitalInOut(board.D3)
units_pin.switch_to_output()
dymo = adafruit_dymoscale.DYMOScale(board.D4, units_pin)

# take a reading of the current time, used for toggling the device out of sleep
time_stamp = time.monotonic()

while True:
    try:
        reading = dymo.weight
        text = "%0.1f g" % reading.weight
        print(text)
        weight_label.text = text
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
"""

# Set your Adafruit IO Username and Key in secrets.py
# (visit io.adafruit.com if you need to create an account,
# or if you need your Adafruit IO key.)
aio_username = secrets['aio_username']
aio_key = secrets['aio_key']

# Create an instance of the Adafruit IO REST client
io = RESTClient(aio_username, aio_key, wifi)

try:
    # Get the 'light' feed from Adafruit IO
    light_feed = io.get_feed('light')
except AdafruitIO_RequestError:
    # If no 'light' feed exists, create one
    light_feed = io.create_new_feed('light')

# Set up an analog light sensor on the PyPortal
adc = AnalogIn(board.LIGHT)

while True:
    light_value = adc.value
    print('Light Level: ', light_value)
    print('Sending to Adafruit IO...')
    io.send_data(light_feed['key'], light_value)
    print('Sent!')
    # delay sending to Adafruit IO
    time.sleep(SENSOR_DELAY)
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
"""

# Set your Adafruit IO Username and Key in secrets.py
# (visit io.adafruit.com if you need to create an account,
# or if you need your Adafruit IO key.)
ADAFRUIT_IO_USER = secrets['adafruit_io_user']
ADAFRUIT_IO_KEY = secrets['adafruit_io_key']

# Create an instance of the Adafruit IO REST client
io = RESTClient(ADAFRUIT_IO_USER, ADAFRUIT_IO_KEY, wifi)

try:
    # Get the 'location' feed from Adafruit IO
    location_feed = io.get_feed('location')
except AdafruitIO_RequestError:
    # If no 'location' feed exists, create one
    location_feed = io.create_new_feed('location')

# Set data
data_value = 42

# Set up metadata associated with data_value
metadata = {'lat': 40.726190, 'lon': -74.005334, 'ele': -6, 'created_at': None}

# Send data and location metadata to the 'location' feed
print('Sending data and location metadata to IO...')
io.send_data(location_feed['key'], data_value, metadata)
print('Data sent!')
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(
    esp, secrets, status_light)

# Set your Adafruit IO Username and Key in secrets.py
# (visit io.adafruit.com if you need to create an account,
# or if you need your Adafruit IO key.)
ADAFRUIT_IO_USER = secrets['adafruit_io_user']
ADAFRUIT_IO_KEY = secrets['adafruit_io_key']

# Create an instance of the Adafruit IO REST client
io = RESTClient(ADAFRUIT_IO_USER, ADAFRUIT_IO_KEY, wifi)

try:
    # Get the 'temperature' feed from Adafruit IO
    temperature_feed = io.get_feed('temperature')
    light_feed = io.get_feed('light')
except AdafruitIO_RequestError:
    # If no 'temperature' feed exists, create one
    temperature_feed = io.create_new_feed('temperature')
    light_feed = io.create_new_feed('light')

# Set up ADT7410 sensor
i2c_bus = busio.I2C(board.SCL, board.SDA)
adt = adafruit_adt7410.ADT7410(i2c_bus, address=0x48)
adt.high_resolution = True

# Set up an analog light sensor on the PyPortal
adc = AnalogIn(board.LIGHT)

while True: