Skip to content

Commit f4d12dd

Browse files
author
Marty
committed
test yeelight ok
1 parent 4d37fb5 commit f4d12dd

File tree

5 files changed

+74
-65
lines changed

5 files changed

+74
-65
lines changed

Diff for: esp01_mlx90614_yeelight.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import time
2+
import mlx90614
3+
from machine import I2C, Pin
4+
from uyeelight import *
5+
6+
bulbs = Bulb.search(timeout=1)
7+
8+
if len(bulbs)==0:
9+
raise Exception("bulb not found.")
10+
11+
ip = list(bulbs.keys())[0]
12+
bulb = Bulb(ip)
13+
bulb.turn_on()
14+
bulb.set_rgb(255,255,255, effect=EFFECT.SUDDEN, duration=0.1)
15+
time.sleep(1)
16+
bulb.set_rgb(255,100,0, effect=EFFECT.SUDDEN, duration=0.1)
17+
18+
19+
i2c = I2C(scl=Pin(2), sda=Pin(0))
20+
sensor = mlx90614.MLX90614(i2c)
21+
22+
while True:
23+
ambient = sensor.read_ambient_temp()
24+
objTemp = sensor.read_object_temp()
25+
print(objTemp*10)
26+
bulb.set_rgb(255,0,objTemp*10, effect=EFFECT.SUDDEN, duration=0.1)
27+
time.sleep_ms(100)

Diff for: esp01_ssd1306.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from machine import Pin,I2C
2+
import ssd1306
3+
4+
i2c = I2C(scl=Pin(0), sda=Pin(2), freq=100000) #Init i2c
5+
lcd=ssd1306.SSD1306_I2C(64,48,i2c) #create LCD object,Specify col and row
6+
lcd.text("ESP8266",0,0)
7+
lcd.text("test",0,16)
8+
lcd.text("123456",0,32)
9+
lcd.show()

Diff for: esp01_yeelight.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from uyeelight import *
2+
3+
bulbs = Bulb.search(timeout=1)
4+
5+
if len(bulbs)==0:
6+
raise Exception("bulb not found.")
7+
8+
ip = list(bulbs.keys())[0]
9+
bulb = Bulb(ip)
10+
bulb.turn_on()
11+
bulb.set_rgb(255,0,25, effect=EFFECT.SUDDEN, duration=0.1)

Diff for: lib/uyeelight.py

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import usocket as socket
2-
import json
2+
import json, struct
33

44

55
class YeeLightException(Exception):
@@ -232,6 +232,24 @@ def _send_message(self, method, params=None):
232232

233233
return recv_data
234234

235+
def search(timeout=2):
236+
msg = "\r\n".join(["M-SEARCH * HTTP/1.1", "HOST: 239.255.255.250:1982", 'MAN: "ssdp:discover"', "ST: wifi_bulb"])
237+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
238+
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
239+
s.bind(('', 1982))
240+
s.sendto(msg.encode(), ('239.255.255.250', 1982))
241+
s.settimeout(timeout)
242+
bulbs = {}
243+
while True:
244+
try:
245+
data, addr = s.recvfrom(1024)
246+
capabilities = dict([x.strip("\r").split(": ") for x in data.decode().split("\n") if ":" in x])
247+
key = capabilities['Location'].split(':')[1][2:]
248+
bulbs[key] = capabilities
249+
except:
250+
break
251+
return bulbs
252+
235253
def _handle_response(self, response):
236254
response = json.loads(response.decode('utf-8'))
237255

@@ -245,4 +263,4 @@ def _handle_response(self, response):
245263
elif "error" in response:
246264
raise YeeLightException(response["error"])
247265
else:
248-
raise YeeLightException("Unknown Exception occurred.")
266+
raise YeeLightException("Unknown Exception occurred.")

Diff for: main.py

+7-63
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,11 @@
1-
import struct, socket
21
from uyeelight import *
32

4-
# see yeelight app for IP
5-
bulb = Bulb("192.168.0.95")
3+
bulbs = Bulb.search(timeout=1)
64

7-
try:
8-
if not bulb.is_on:
9-
bulb.turn_on()
10-
except YeeLightException as e:
11-
print(e)
12-
13-
bulb.set_rgb(255,0,255, effect=EFFECT.SUDDEN, duration=1)
14-
15-
def discover_bulbs(timeout=2, interface=False):
16-
"""
17-
Discover all the bulbs in the local network.
18-
:param int timeout: How many seconds to wait for replies. Discovery will
19-
always take exactly this long to run, as it can't know
20-
when all the bulbs have finished responding.
21-
:param string interface: The interface that should be used for multicast packets.
22-
Note: it *has* to have a valid IPv4 address. IPv6-only
23-
interfaces are not supported (at the moment).
24-
The default one will be used if this is not specified.
25-
:returns: A list of dictionaries, containing the ip, port and capabilities
26-
of each of the bulbs in the network.
27-
"""
28-
msg = "\r\n".join(["M-SEARCH * HTTP/1.1", "HOST: 239.255.255.250:1982", 'MAN: "ssdp:discover"', "ST: wifi_bulb"])
29-
MCAST_GRP = "239.255.255.250"
30-
MCAST_PORT= 1982
31-
# Set up UDP socket
32-
"""
33-
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
34-
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
35-
s.bind((MCAST_GRP, MCAST_PORT))
36-
# '\xc0\xa8\x01\x01' socket.inet_aton(MCAST_GRP)
37-
mreq = struct.pack("4sl",'\xef\xff\xff\xfa' , socket.INADDR_ANY)
38-
s.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
39-
40-
s.sendto(msg.encode(), (MCAST_GRP, MCAST_PORT))
41-
"""
42-
43-
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
44-
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
45-
s.bind(('', 1982))
46-
s.sendto(msg.encode(), ('239.255.255.250', 1982))
47-
48-
bulbs = []
49-
bulb_ips = set()
50-
while True:
51-
data, addr = s.recvfrom(65507)
52-
53-
capabilities = dict([x.strip("\r").split(": ") for x in data.decode().split("\n") if ":" in x])
54-
parsed_url = urlparse(capabilities["Location"])
55-
56-
bulb_ip = (parsed_url.hostname, parsed_url.port)
57-
if bulb_ip in bulb_ips:
58-
continue
59-
60-
capabilities = {key: value for key, value in capabilities.items() if key.islower()}
61-
bulbs.append({"ip": bulb_ip[0], "port": bulb_ip[1], "capabilities": capabilities})
62-
bulb_ips.add(bulb_ip)
63-
64-
return bulbs
65-
66-
print(discover_bulbs())
5+
if len(bulbs)==0:
6+
raise Exception("bulb not found.")
677

8+
ip = list(bulbs.keys())[0]
9+
bulb = Bulb(ip)
10+
bulb.turn_on()
11+
bulb.set_rgb(255,0,25, effect=EFFECT.SUDDEN, duration=0.1)

0 commit comments

Comments
 (0)