wiznet5k_aio_post.py
1 import time 2 import board 3 import busio 4 from digitalio import DigitalInOut 5 from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K 6 import adafruit_wiznet5k.adafruit_wiznet5k_socket as socket 7 import adafruit_requests as requests 8 9 # Get Adafruit.io details from a secrets.py file 10 try: 11 from secrets import secrets 12 except ImportError: 13 print("WiFi secrets are kept in secrets.py, please add them there!") 14 raise 15 16 cs = DigitalInOut(board.D10) 17 spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) 18 19 # Initialize ethernet interface with DHCP 20 eth = WIZNET5K(spi_bus, cs) 21 requests.set_socket(socket, eth) 22 23 counter = 0 24 25 while True: 26 print("Posting data...", end="") 27 data = counter 28 feed = "test" 29 payload = {"value": data} 30 response = requests.post( 31 "http://io.adafruit.com/api/v2/" 32 + secrets["aio_username"] 33 + "/feeds/" 34 + feed 35 + "/data", 36 json=payload, 37 headers={"X-AIO-KEY": secrets["aio_key"]}, 38 ) 39 print(response.json()) 40 response.close() 41 counter = counter + 1 42 print("OK") 43 response = None 44 time.sleep(15)