Skip to content

Commit

Permalink
switch between chromecasts with encoder button
Browse files Browse the repository at this point in the history
  • Loading branch information
WebmasterTD committed Dec 8, 2018
1 parent df97169 commit c9bd8cf
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 49 deletions.
16 changes: 6 additions & 10 deletions esp8266/boot.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
# This file is executed on every boot (including wake-boot from deepsleep)
#import esp
#esp.osdebug(None)

import gc
import utime as time
#import webrepl
#webrepl.start()
import wificonf

ssid = wificonf.WIFI_SSID
passw = wificonf.WIFI_PASSWORD

########################
# Wifi passwords here: #
WIFI_SSID = 'TP-LINK_E180'
WIFI_PASSWORD = 'borz52KAI?'
########################
def timed(func):
def wrapper():
start = start = time.ticks_ms() # get millisecond counter
Expand All @@ -28,7 +24,7 @@ def do_connect():
if not sta_if.isconnected():
print('connecting to network...')
sta_if.active(True)
sta_if.connect(WIFI_SSID, WIFI_PASSWORD)
sta_if.connect(ssid, passw)
while not sta_if.isconnected():
pass
#machine.idle()
Expand Down
70 changes: 37 additions & 33 deletions esp8266/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,44 @@
import machine
import esp
import volume

import wificonf
#ToDo
#add exceptions and error handling

cast_ip = ('192.168.0.164', '192.168.0.165')
cast_name = ('SH6', 'Chromecast')
cast_ip = list(wificonf.CHROMECASTS.keys())

cast_name = wificonf.CHROMECASTS



def cycle(p):
try:
len(p)
except TypeError:
# len() is not defined for this type. Assume it is
# a finite iterable so we must cache the elements.
cache = []
for i in p:
yield i
cache.append(i)
p = cache
while p:
yield from p

chromecast = cycle(cast_ip)

def main():
enc = Encoder(12, 13, clicks=2, reverse=True)
np = volume.NeoPixelRing(4, machine.Pin(15), 16)
switch = machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_UP)
button = machine.Pin(5, machine.Pin.IN)
print(cast_ip[switch.value()])

cast = volume.Chromecast(cast_ip[switch.value()])
cast = volume.Chromecast(next(chromecast))

current_switch = switch.value()
print("switch on @ boot", switch.value(), cast_name[switch.value()])

vol = cast.get_volume
current_vol = vol
print('current volume:', current_vol)
enc.set_val(vol)

last_enc_val = vol
current_vol = cast.get_volume
print('Connected to:', cast_name[next(chromecast)], next(chromecast), 'current vol:', current_vol)
enc.set_val(current_vol)
last_enc_val = current_vol
last_change_tick = time.ticks_ms()
np.set_vol(vol)
np.set_vol(current_vol)
req = 1

while True:
Expand All @@ -46,31 +58,23 @@ def main():
current_vol = cast.get_volume
print('current volume:', current_vol)

#CHANGING CHROMECAST
if switch.value() != current_switch:
#####
np.error()
#####
current_switch = switch.value()
cast.disconnect()
cast = volume.Chromecast(cast_ip[current_switch])
vol = cast.get_volume
current_vol = vol
enc.set_val(vol)
last_change_tick = time.ticks_ms()
print('switched to chromecast no:', current_switch, 'current vol:', vol, cast_name[current_switch])

#SLEEP AFTER DELAY
if (time.ticks_diff(time.ticks_ms(), last_change_tick) > 10000): #10 sec
cast.disconnect()
np.turn_off()
print("SLEEP")
esp.deepsleep()

#CHANGING CHROMECAST WITH ENCODER BUTTON
if button.value():
#np.error()
print("BUTTON PRESSED")
last_change_tick = time.ticks_ms()
np.fill_color((255,0,255))
cast.disconnect()
cast = volume.Chromecast(next(chromecast))
current_vol = cast.get_volume
enc.set_val(current_vol)
print('switched to:', cast_name[next(chromecast)], next(chromecast), 'current vol:', current_vol)
last_change_tick = time.ticks_ms()
time.sleep_ms(500)
np.set_vol(val)

Expand Down
12 changes: 6 additions & 6 deletions esp8266/volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
7:b'\x00\x00\x00\x84\x08\x00\x12\x08sender-0\x1a\nreceiver-0"#urn:x-cast:com.google.cast.receiver(\x002C{"type": "SET_VOLUME", "volume": {"level": ###}, "requestId": $$$}',
8:b'\x00\x00\x00\x85\x08\x00\x12\x08sender-0\x1a\nreceiver-0"#urn:x-cast:com.google.cast.receiver(\x002D{"type": "SET_VOLUME", "volume": {"level": ###}, "requestId": $$$}'}

gamma8 = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
GAMMA8 = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5,
Expand All @@ -36,7 +36,7 @@

class Chromecast(object):

def __init__(self, ip):
def __init__(self, ip, demo = None, timeout = None):
self.ip = ip
self.request = 2
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Expand Down Expand Up @@ -85,7 +85,6 @@ def __init__(self, led_v_pin, *args, **kwargs):
super(NeoPixelRing, self).__init__(*args, **kwargs)

def set_vol(self, volume):

n = self.n
for i in range(n):
level = (volume * 40.96) // 256
Expand All @@ -94,11 +93,11 @@ def set_vol(self, volume):
self[i] = color
elif i == level:
num = int((40.96 * volume) % 256)
color = (0, 0, gamma8[num])
color = (0, 0, GAMMA8[num])
self[i] = color

else:
color = (gamma8[30], gamma8[30], gamma8[30])
color = (GAMMA8[30], GAMMA8[30], GAMMA8[30])
self[i] = color
self.write()

Expand All @@ -115,5 +114,6 @@ def turn_on(self):
self.led_v.on()

def fill_color(self, color):
self.fill(color)
a, b, c = color
self.fill((GAMMA8[a], GAMMA8[b], GAMMA8[c]))
self.write()
5 changes: 5 additions & 0 deletions esp8266/wificonf_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
WIFI_SSID = 'YOUR WIFI SSID'
WIFI_PASSWORD = 'YOUR WIFI PASSWORD'

#set chomecasts ips and names here in your network
CHROMECASTS = {'192.168.0.164':'chromecast01', '192.168.0.165':'chromecast01'}

0 comments on commit c9bd8cf

Please sign in to comment.