예제 #1
0
# 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:
        print('received <- ON\n')
    elif int(feed_data['value']) == 0:
        print('received <= OFF\n')

    # Set the LED to the feed value
    LED.value = int(feed_data['value'])

    time.sleep(5)
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'])
예제 #3
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)