Skip to content

add aio post and cheerlights examples #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 13, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions examples/esp32spi_aio_post.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@

import time
import board
import busio
from digitalio import DigitalInOut

from adafruit_esp32spi import adafruit_esp32spi
import adafruit_esp32spi.adafruit_esp32spi_requests as requests


print("ESP32 SPI webclient test")

# Get wifi details and more from a settings.py file
try:
from settings import settings
except ImportError:
print("WiFi settings are kept in settings.py, please add them there!")
raise


esp32_cs = DigitalInOut(board.D9)
esp32_ready = DigitalInOut(board.D10)
esp32_reset = DigitalInOut(board.D5)
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)

requests.set_interface(esp)

if esp.status == adafruit_esp32spi.WL_IDLE_STATUS:
print("ESP32 found and in idle mode")
print("Firmware vers.", esp.firmware_version)
print("MAC addr:", [hex(i) for i in esp.MAC_address])
for ap in esp.scan_networks():
print("\t%s\t\tRSSI: %d" % (str(ap['ssid'], 'utf-8'), ap['rssi']))
while not esp.is_connected:
try:
print("Connecting to AP...")
esp.connect_AP(bytes(settings['ssid'],'utf-8'), bytes(settings['password'],'utf-8'))
except (ValueError, RuntimeError) as e:
print("Failed to connect, retrying\n", e)
continue
print("Connected to", str(esp.ssid, 'utf-8'), "\tRSSI:", esp.rssi)
print("My IP address is", esp.pretty_ip(esp.ip_address))


counter = 0
while True:
try:
while not esp.is_connected:
# settings dictionary must contain 'ssid' and 'password' at a minimum
esp.connect_AP(bytes(settings['ssid'],'utf-8'), bytes(settings['password'],'utf-8'))
# great, lets get the data
print("Posting data...", end='')
data=counter
feed='test'
payload={'value':data}
response=requests.post(
"https://io.adafruit.com/api/v2/"+settings['aio_username']+"/feeds/"+feed+"/data",
json=payload,headers={bytes("X-AIO-KEY","utf-8"):bytes(settings['aio_key'],"utf-8")})
print(response.json())
response.close()
counter = counter + 1
print("OK")
except (ValueError, RuntimeError) as e:
print("Failed to get data, retrying\n", e)
continue
response = None
time.sleep(15)
101 changes: 101 additions & 0 deletions examples/esp32spi_cheerlights.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import time
import board
import busio
from digitalio import DigitalInOut

from adafruit_esp32spi import adafruit_esp32spi
import adafruit_esp32spi.adafruit_esp32spi_requests as requests

import neopixel
import adafruit_fancyled.adafruit_fancyled as fancy



# Get wifi details and more from a settings.py file
try:
from settings import settings
except ImportError:
print("WiFi settings are kept in settings.py, please add them there!")
raise



print("ESP32 SPI webclient test")

DATA_SOURCE = "https://api.thingspeak.com/channels/1417/feeds.json?results=1"
DATA_LOCATION = ["feeds", 0, "field2"]


esp32_cs = DigitalInOut(board.D9)
esp32_ready = DigitalInOut(board.D10)
esp32_reset = DigitalInOut(board.D5)
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)

requests.set_interface(esp)

if esp.status == adafruit_esp32spi.WL_IDLE_STATUS:
print("ESP32 found and in idle mode")
print("Firmware vers.", esp.firmware_version)
print("MAC addr:", [hex(i) for i in esp.MAC_address])
for ap in esp.scan_networks():
print("\t%s\t\tRSSI: %d" % (str(ap['ssid'], 'utf-8'), ap['rssi']))
while not esp.is_connected:
try:
print("Connecting to AP...")
esp.connect_AP(bytes(settings['ssid'],'utf-8'), bytes(settings['password'],'utf-8'))
except (ValueError, RuntimeError) as e:
print("Failed to connect, retrying\n", e)
continue
print("Connected to", str(esp.ssid, 'utf-8'), "\tRSSI:", esp.rssi)
print("My IP address is", esp.pretty_ip(esp.ip_address))


# neopixels
pixels = neopixel.NeoPixel(board.A1, 16, brightness=0.3)
pixels.fill(0)
builtin = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.1)
builtin[0] = 0

# we'll save the value in question
last_value = value = None
the_time = None
times = 0


while True:
try:
while not esp.is_connected:
builtin[0] = (100, 0, 0)
# settings dictionary must contain 'ssid' and 'password' at a minimum
esp.connect_AP(bytes(settings['ssid'],'utf-8'), bytes(settings['password'],'utf-8'))
builtin[0] = (0, 100, 0)
print("Fetching json from", DATA_SOURCE)
builtin[0] = (100, 100, 0)
r = requests.get(DATA_SOURCE)
builtin[0] = (0, 0, 100)
print(r.json())
value=r.json()
for x in DATA_LOCATION:
value = value[x]
print(value)
r.close()
except (ValueError, RuntimeError) as e:
print("Failed to get data, retrying\n", e)
continue

builtin[0] = (100, 100, 100)
if not value:
continue
if last_value != value:
color = int(value[1:],16)
red = color >> 16 & 0xFF
green = color >> 8 & 0xFF
blue = color& 0xFF
gamma_corrected = fancy.gamma_adjust(fancy.CRGB(red, green, blue)).pack()

pixels.fill(gamma_corrected)
last_value = value
times += 1
r = None
time.sleep(60)
10 changes: 10 additions & 0 deletions examples/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file is where you keep secret settings, passwords, and tokens!
# If you put them in the code you risk committing that info or sharing it

settings = {
'ssid' : 'yourssid',
'password' : 'yourpassword',
'timezone' : -5, # this is offset from UTC
'aio_username' : 'youraiousername',
'aio_key' : 'youraiokey',
}