Commit 309cc235 authored by marble's avatar marble
Browse files

added code

parent 55b73426
# This file is executed on every boot (including wake-boot from deepsleep
from machine import reset, Pin
from network import WLAN, STA_IF, phy_mode, MODE_11N
from time import ticks_ms
from gc import collect as gc_collect
import ujson as json
from socket import socket as sock, AF_INET, SOCK_DGRAM, IPPROTO_UDP, getaddrinfo
from neopixel import NeoPixel
import webrepl
STA_CONF_ARR_PATH = "/sta_conf_arr.json"
PHY_MODE = MODE_11N
STA_TIMEOUT = 5000
AF_INET_BROADCAST = "255.255.255.255"
ADDR_ANNOUNCE_PORT = 1337
def scan_wifi():
sta_if = WLAN(STA_IF)
sta_if.active(True)
scan = sta_if.scan()
print("\nWiFi Scan:\n\t{:35}, {:14}, {:2}, {:4}, {:4}, {:6}".format("ssid", "bssid", "ch", "rssi", "auth", "hidden"))
for ssid, bssid, channel, rssi, authmode, hidden in scan:
print("\t{:35}, 0x{:12}, {:2}, {:4}, {:4}, {:6}".format(ssid, "".join(["{:02x}".format(b) for b in bssid])[:-1], channel, rssi, authmode, hidden))
print("")
return scan
def connect_to_ap(ssid, psk):
sta_if = WLAN(STA_IF)
sta_if.active(True)
sta_if.connect(ssid, psk)
now = ticks_ms()
while (not sta_if.isconnected()) and ((ticks_ms()-now < STA_TIMEOUT)):
pass
if not sta_if.isconnected():
raise OSError("Could not connect to AP")
def deactivate_sta():
sta_if = WLAN(STA_IF)
sta_if.active(True)
sta_if.connect("", "")
sta_if.active(False)
def connect_to_wifi():
sta_conf_arr = json.load(open(STA_CONF_ARR_PATH))
for ssid, _, _, _, _, _ in scan_wifi():
for sta_conf in sta_conf_arr:
if ssid.decode() == sta_conf["ssid"]:
print("Connecting to "+sta_conf["ssid"])
try:
connect_to_ap(sta_conf["ssid"], sta_conf["psk"])
except OSError as e:
pass
else:
print("Connected to "+sta_conf["ssid"])
return
deactivate_sta()
raise OSError("No access point available.")
if __name__ == '__main__':
# turn light on
np = NeoPixel(Pin(13), 135, bpp=4)
for i in range(np.n):
np[i] = (255, 255, 255, 255)
np.write()
phy_mode(PHY_MODE)
try:
connect_to_wifi()
s = sock(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
s.connect((AF_INET_BROADCAST, ADDR_ANNOUNCE_PORT))
s.send("{}\n".format(WLAN().ifconfig()[0]))
s.close()
except KeyError as err:
print("Key Error: {0}".format(err))
except OSError as err:
print("OS Error: {0}".format(err))
gc.collect()
webrepl.start()
from math import pi, cos
HUE_MAX = tau = pi+pi
def _hue_to_rgb(angle):
# make 0 <= angle < tau
angle = angle % HUE_MAX
# shift and compress angle for each color
angle_red = 3*(angle+tau)/2 if (angle < tau/3) or (angle > 2*tau/3) else 0
angle_green = 3* angle /2 if (angle < 2*tau/3) else 0
angle_blue = 3*(angle-tau)/2 if (angle > tau/3) else 0
# use nice cos wave for transition
red = (1 - cos(angle_red))/2
green = (1 - cos(angle_green))/2
blue = (1 - cos(angle_blue))/2
return [red, green, blue]
def hsb_to_rgb(hue, saturation=1, brightness=1):
rgb = _hue_to_rgb(hue)
rgb[:] = [value*saturation+1-saturation for value in rgb]
rgb[:] = [round(255*value*brightness) for value in rgb]
return rgb
def hsb_to_rgbw(hue, saturation=1, brightness=1):
rgb = _hue_to_rgb(hue)
rgb[:] = [value*saturation for value in rgb] + [1-saturation]
rgb[:] = [round(255*value*brightness) for value in rgb]
return rgb
from math import pi
from machine import reset
from hsb import hsb_to_rgb, hsb_to_rgbw, HUE_MAX
from neopixel import NeoPixel
tau = pi+pi
PIN_NEOPIXEL = const(13)
NUMBER_NEOPIXEL = const(135)
STEPS_ANIMATION = const(NUMBER_NEOPIXEL)
np = NeoPixel(Pin(PIN_NEOPIXEL), NUMBER_NEOPIXEL, bpp=4)
def main():
np = NeoPixel(Pin(PIN_NEOPIXEL), NUMBER_NEOPIXEL, bpp=4)
phase = 0
# while True:
# for i in range(NUMBER_NEOPIXEL):
# angle = i*tau/NUMBER_NEOPIXEL+phase
# np[i] = hsb_to_rgbw(angle, 0.75, 1)
# np.write()
# phase += tau/STEPS_ANIMATION
# phase %= tau
if __name__ == '__main__':
main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment