Last active
April 8, 2016 02:15
-
-
Save awong1900/a9dde81fc5b29f4e0781 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding=utf-8 | |
import time | |
import json | |
import requests | |
#wiolink api配置 | |
wiolink_api = "https://cn.iot.seeed.cc/v1/node/GroveTempHumD0/temperature" | |
wiolink_token = "xxx" #请填入你的wiolink的token | |
#yeelink api配置 | |
api_url='http://api.yeelink.net/v1.0' | |
api_key='xxx' #请填入专属的api key | |
api_headers={'U-ApiKey':api_key,'content-type': 'application/json'} | |
raspi_device_id=344292 | |
cpu_sensor_id=382635 | |
def get_temp(): | |
url = "%s?access_token=%s" %(wiolink_api, wiolink_token) | |
temp = None | |
try: | |
r = requests.get(url) | |
json_response = r.json() | |
temp = json_response.get("celsius_degree", None) | |
except Exception as e: | |
print e | |
return temp | |
#上传温度到yeelink | |
def upload_temp_to_yeelink(): | |
url=r'%s/device/%s/sensor/%s/datapoints' % (api_url,raspi_device_id,cpu_sensor_id) | |
strftime=time.strftime("%Y-%m-%dT%H:%M:%S") | |
print "time:",strftime | |
temp=get_temp() | |
print "temp:",temp | |
if not temp: | |
retrun | |
data={"timestamp":strftime , "value": temp} | |
res=requests.post(url,headers=api_headers,data=json.dumps(data)) | |
print "status_code:",res.status_code | |
def main(): | |
while True: | |
upload_temp_to_yeelink() | |
#休眠60秒 | |
time.sleep(60) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment