# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries
# SPDX-License-Identifier: MIT

import os
import sys
import time
import ssl
import wifi
import socketpool
import microcontroller
import board
import busio
import adafruit_requests
import adafruit_dht
from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError
from microcontroller import cpu


wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))

aio_username = os.getenv('ADAFRUIT_IO_USERNAME')
aio_key = os.getenv('ADAFRUIT_IO_KEY')

pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
# Initialize an Adafruit IO HTTP API object
io = IO_HTTP(aio_username, aio_key, requests)
print("connected to io")


try:
# get feed
    picowServo = io.get_feed("myservo")
except AdafruitIO_RequestError:
# if no feed exists, create one
    print("Feed error")
    sys.exit(-1)

#  pack feed names into an array for the loop
feed_names = [picowServo]
print("Connected to feed.")

clock = 11


while True:
    #  when the clock runs out..
    print("") # Prints a blank line after clock print.
    #  read sensor
    servoData = clock
    data = [servoData]
    #  send sensor data to respective feeds
#            io.send_data(picowServo,servoData)
    io.send_data("myservo",servoData)
    print("sent")
    time.sleep(15)
    #  print sensor data to the REPL
    print()

    #time.sleep(1)
    time.sleep(1)
    print(clock)
    clock = clock + 1
    if clock >50:
        clock = 11
        


