# -*- coding: utf-8 -*-
import RPi.GPIO as GPIO
import requests
import json
import time



def main():
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(17,GPIO.OUT)
    GPIO.output(17,GPIO.HIGH)
    while True:
        print u'当前灯的状态̬...'
        status()
        time.sleep(0.1)
               
def status():
    apiurl = 'http://api.yeelink.net/v1.0/device/340691/sensor/377441/datapoints'
    apiheaders = {'U-ApiKey': '150e5309311dac2cd3163d7aa06714ef', 'content-type': 'application/json'}
    tmp = requests.get(apiurl, headers=apiheaders)
    switch = tmp.json()
    if switch[u'value']==1:
         GPIO.output(17,GPIO.LOW)
         print ("开灯中..")
    else:
         GPIO.output(17,GPIO.HIGH)
         print ("关灯中...")    
if __name__ == '__main__':
    main()

