我就废话不多说了,大家还是直接看代码吧!
# -*- 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则关闭
补充知识:python笔记(轮询、长轮询)
一、轮询
views.py
from flask import Flask,render_template,request,jsonify app = Flask(__name__) USERS = { '1':{'name':'贝贝','count':1}, '2':{'name':'小东北','count':0}, '3':{'name':'何伟明','count':0}, } @app.route('/user/list') def user_list(): import time return render_template('user_list.html',users=USERS) @app.route('/vote',methods=['POST']) def vote(): uid = request.form.get('uid') USERS[uid]['count'] += 1 return "投票成功" @app.route('/get/vote',methods=['GET']) def get_vote(): return jsonify(USERS) if __name__ == '__main__': app.run(threaded=True)
html
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>Title</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> li{ cursor: pointer; } </style> </head> <body> <ul id="userList"> {% for key,val in users.items() %} <li uid="{{key}}">{{val.name}} ({{val.count}})</li> {% endfor %} </ul> <script src="/UploadFiles/2021-04-08/jquery.min.js">二、长轮询
views.py
from flask import Flask,render_template,request,jsonify,session import uuid import queue app = Flask(__name__) app.secret_key = 'asdfasdfasd' USERS = { '1':{'name':'贝贝','count':1}, '2':{'name':'小东北','count':0}, '3':{'name':'何伟明','count':0}, } QUEQUE_DICT = { } @app.route('/user/list') def user_list(): user_uuid = str(uuid.uuid4()) QUEQUE_DICT[user_uuid] = queue.Queue() session['current_user_uuid'] = user_uuid return render_template('user_list.html',users=USERS) @app.route('/vote',methods=['POST']) def vote(): uid = request.form.get('uid') USERS[uid]['count'] += 1 for q in QUEQUE_DICT.values(): q.put(USERS) return "投票成功" @app.route('/get/vote',methods=['GET']) def get_vote(): user_uuid = session['current_user_uuid'] q = QUEQUE_DICT[user_uuid] ret = {'status':True,'data':None} try: users = q.get(timeout=5) ret['data'] = users except queue.Empty: ret['status'] = False return jsonify(ret) if __name__ == '__main__': app.run(threaded=True)html
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>Title</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> li{ cursor: pointer; } </style> </head> <body> <ul id="userList"> {% for key,val in users.items() %} <li uid="{{key}}">{{val.name}} ({{val.count}})</li> {% endfor %} </ul> <script src="/UploadFiles/2021-04-08/jquery.min.js">以上这篇python轮询机制控制led实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com
暂无“python轮询机制控制led实例”评论...
更新日志
2024年11月08日
2024年11月08日
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]