python轮询机制控制led

# -*- coding:utf-8 -*- 
# File: ceshitianqi

import urllib2
import json
import time
import datetime
import serial  
import random
import os
import sys


APIKEY = 'ZPdLyl***='  #改成你的APIKEY
ser=serial.Serial("/dev/ttyUSB2",9600,timeout=1)

def read(key):
    ser.write(key)
    print("output:"+key)
    time.sleep(1)
    response = ser.readall()
    print(response)
    print(type(response))
    return response

def http_put(key):
    val = read(key) #获取Arduino的数据
    CurTime = datetime.datetime.now()
    url='http://api.heclouds.com/devices/**1/datapoints'
    #values={'datastreams':[{"id":"temp","datapoints":[{"at":CurTime.isoformat(),"value":val}]}]}
    print(type(val))
    if key== "a" :
      values={'datastreams':[{"id":"humidity","datapoints":[{"at":CurTime.isoformat(),"value":val}]}]}
    if key== "b" :
      values={'datastreams':[{"id":"temperature","datapoints":[{"at":CurTime.isoformat(),"value":val}]}]}
    jdata = json.dumps(values)                  # 对数据进行JSON格式化编码
    #打印json内容
    print jdata
    request = urllib2.Request(url, jdata)
    request.add_header('api-key', APIKEY)
    request.get_method = lambda:'POST'          # 设置HTTP的访问方式
    request = urllib2.urlopen(request)
    return request.read()   

str = ("a","b")
while True:
	for i in str:  
		f = open('1.txt')
		e = f.read()
		if e == "1\n":
			ser.write("c")
		if e == "0\n":
			ser.write("d")

		f.close()   
		resp = http_put(i)
      		time.sleep(2)

轮询1.txt

1则点亮

0则关闭


阅读更多
个人分类: 原创
上一篇Arduino与树莓派配合上传数据至onenet
下一篇arduino控制步进电机
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭