forked from dotnet/aspnetcore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
led
82 lines (69 loc) · 2.53 KB
/
led
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/python
import requests
import socket
import threading
import logging
import RPi.GPIO as GPIO
# change this to the values from MCS web console
DEVICE_INFO = {
'device_id' : 'DqVTl80X',
'device_key' : 'iCEWVMZYw5r9Xfk6'
}
# change 'INFO' to 'WARNING' to filter info messages
logging.basicConfig(level='INFO')
heartBeatTask = None
def establishCommandChannel():
# Query command server's IP & port
connectionAPI = 'https://api.mediatek.com/mcs/v2/devices/%(device_id)s/connections.csv'
r = requests.get(connectionAPI % DEVICE_INFO,
headers = {'deviceKey' : DEVICE_INFO['device_key'],
'Content-Type' : 'text/csv'})
logging.info("Command Channel IP,port=" + r.text)
(ip, port) = r.text.split(',')
# Connect to command server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((ip, int(port)))
s.settimeout(None)
# Heartbeat for command server to keep the channel alive
def sendHeartBeat(commandChannel):
keepAliveMessage = '%(device_id)s,%(device_key)s,0' % DEVICE_INFO
commandChannel.sendall(keepAliveMessage)
logging.info("beat:%s" % keepAliveMessage)
def heartBeat(commandChannel):
sendHeartBeat(commandChannel)
# Re-start the timer periodically
global heartBeatTask
heartBeatTask = threading.Timer(40, heartBeat, [commandChannel]).start()
heartBeat(s)
return s
def waitAndExecuteCommand(commandChannel):
while True:
command = commandChannel.recv(1024)
logging.info("recv:" + command)
# command can be a response of heart beat or an update of the LED_control,
# so we split by ',' and drop device id and device key and check length
fields = command.split(',')[2:]
if len(fields) > 1:
timeStamp, dataChannelId, commandString = fields
if dataChannelId == 'LEDControl':
# check the value - it's either 0 or 1
commandValue = int(commandString)
logging.info("led :%d" % commandValue)
setLED(commandValue)
pin = None
def setupLED():
global pin
# on LinkIt Smart 7699, pin 44 is the Wi-Fi LED.
GPIO.setmode(GPIO.BCM)
GPIO.setup(17,GPIO.OUT)
def setLED(state):
# Note the LED is "reversed" to the pin's GPIO status.
# So we reverse it here.
if state:
GPIO.output(17,1)
else:
GPIO.output(17,0)
if __name__ == '__main__':
setupLED()
channel = establishCommandChannel()
waitAndExecuteCommand(channel)