Laser_SD_W5500
Dependencies: MQTT SDFileSystem WIZnet_Library mbed
Fork of EthW5500 by
mqtt_laser.py@6:4c80e598d08c, 2018-07-23 (annotated)
- Committer:
- zhangyx
- Date:
- Mon Jul 23 06:28:17 2018 +0000
- Revision:
- 6:4c80e598d08c
- Parent:
- 5:48c44bebe8fb
save to file;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
zhangyx | 6:4c80e598d08c | 1 | #!/usr/bin/env python |
zhangyx | 6:4c80e598d08c | 2 | # -*- coding: UTF-8 -*- |
zhangyx | 6:4c80e598d08c | 3 | |
zhangyx | 6:4c80e598d08c | 4 | ''' |
zhangyx | 6:4c80e598d08c | 5 | File name: laser_server_v2.py |
zhangyx | 6:4c80e598d08c | 6 | Description: 项目激光部分服务器代码 |
zhangyx | 6:4c80e598d08c | 7 | Author: 秦智 |
zhangyx | 6:4c80e598d08c | 8 | Date: June 4, 2018 |
zhangyx | 6:4c80e598d08c | 9 | Others: |
zhangyx | 6:4c80e598d08c | 10 | Dependency: pip install Pillow paho-mqtt |
zhangyx | 6:4c80e598d08c | 11 | |
zhangyx | 6:4c80e598d08c | 12 | ''' |
zhangyx | 6:4c80e598d08c | 13 | |
zhangyx | 6:4c80e598d08c | 14 | from time import sleep |
zhangyx | 6:4c80e598d08c | 15 | import paho.mqtt.client as mqtt |
zhangyx | 6:4c80e598d08c | 16 | import threading |
zhangyx | 6:4c80e598d08c | 17 | import time |
zhangyx | 6:4c80e598d08c | 18 | from PIL import Image |
zhangyx | 6:4c80e598d08c | 19 | |
zhangyx | 5:48c44bebe8fb | 20 | |
zhangyx | 6:4c80e598d08c | 21 | def waitfile(NumofOnce,minvalue): |
zhangyx | 6:4c80e598d08c | 22 | filename = "laser-6-4.JPG" |
zhangyx | 6:4c80e598d08c | 23 | im = Image.open(filename) #打开图片 |
zhangyx | 6:4c80e598d08c | 24 | print("get a picture,the info about filename, im.format, im.size, im.mode:") |
zhangyx | 6:4c80e598d08c | 25 | print(filename, im.format, im.size, im.mode) |
zhangyx | 6:4c80e598d08c | 26 | imL = im.convert("L") |
zhangyx | 6:4c80e598d08c | 27 | x = imL.size[0] #图片的长 |
zhangyx | 6:4c80e598d08c | 28 | y = imL.size[1] #图片的宽 |
zhangyx | 6:4c80e598d08c | 29 | y=int(y*100/x) |
zhangyx | 6:4c80e598d08c | 30 | x=100 |
zhangyx | 6:4c80e598d08c | 31 | imL=imL.resize((x,y)) #缩放图片 |
zhangyx | 6:4c80e598d08c | 32 | datalist = [] #存放坐标点的数组 |
zhangyx | 6:4c80e598d08c | 33 | for iy in range(0,y): |
zhangyx | 6:4c80e598d08c | 34 | for ix in range(0,x): |
zhangyx | 6:4c80e598d08c | 35 | value = imL.getpixel((ix,iy)) #获取像素亮度 |
zhangyx | 6:4c80e598d08c | 36 | if value >=minvalue: #判断阈值 |
zhangyx | 6:4c80e598d08c | 37 | datalist.append([ix,iy,value]) |
zhangyx | 6:4c80e598d08c | 38 | strdatalist=[] |
zhangyx | 6:4c80e598d08c | 39 | sdata = "" |
zhangyx | 6:4c80e598d08c | 40 | nn=0 |
zhangyx | 6:4c80e598d08c | 41 | dotfile = open("write.txt","w") |
zhangyx | 6:4c80e598d08c | 42 | while len(datalist)>0: |
zhangyx | 6:4c80e598d08c | 43 | data = datalist.pop(0) |
zhangyx | 6:4c80e598d08c | 44 | nn=nn+1 |
zhangyx | 6:4c80e598d08c | 45 | # 将坐标转为字符串(x,y,v) |
zhangyx | 6:4c80e598d08c | 46 | sdata = sdata+"("+str(data[0])+","+str(data[1])+","+str(data[2])+")" |
zhangyx | 6:4c80e598d08c | 47 | dotfile.write(sdata) |
zhangyx | 6:4c80e598d08c | 48 | if nn == NumofOnce or len(datalist)==0: |
zhangyx | 6:4c80e598d08c | 49 | # 构造要发送的字符串数组 |
zhangyx | 6:4c80e598d08c | 50 | strdatalist.append(sdata) |
zhangyx | 6:4c80e598d08c | 51 | sdata = "" |
zhangyx | 6:4c80e598d08c | 52 | nn = 0 |
zhangyx | 6:4c80e598d08c | 53 | dotfile.close() |
zhangyx | 6:4c80e598d08c | 54 | strdatalist.append("(0,0,0)") |
zhangyx | 6:4c80e598d08c | 55 | return strdatalist |
zhangyx | 6:4c80e598d08c | 56 | |
zhangyx | 6:4c80e598d08c | 57 | def NewWork(conn): |
zhangyx | 6:4c80e598d08c | 58 | datalist = waitfile(5,30) |
zhangyx | 6:4c80e598d08c | 59 | for datastr in datalist: |
zhangyx | 6:4c80e598d08c | 60 | print("senddata:"+datastr) |
zhangyx | 6:4c80e598d08c | 61 | conn.publish('/control/laser/dat', datastr, qos=1) |
zhangyx | 6:4c80e598d08c | 62 | time.sleep(0.01) |
zhangyx | 6:4c80e598d08c | 63 | |
zhangyx | 6:4c80e598d08c | 64 | def on_message(conn, userdata, msg): |
zhangyx | 6:4c80e598d08c | 65 | print(msg.topic+" "+str(msg.payload)) |
zhangyx | 6:4c80e598d08c | 66 | if msg.payload == 'ready.': |
zhangyx | 6:4c80e598d08c | 67 | print('设备已启动') |
zhangyx | 6:4c80e598d08c | 68 | # 告诉设备,有新任务 |
zhangyx | 6:4c80e598d08c | 69 | conn.publish('/control/laser/cmd', "1", qos=1) |
zhangyx | 6:4c80e598d08c | 70 | elif msg.payload == 'wait data.': |
zhangyx | 6:4c80e598d08c | 71 | print('等待接收数据') |
zhangyx | 6:4c80e598d08c | 72 | # 调用发送数据函数 |
zhangyx | 6:4c80e598d08c | 73 | NewWork(conn) |
zhangyx | 6:4c80e598d08c | 74 | # 告诉设备,数据发送完 |
zhangyx | 6:4c80e598d08c | 75 | conn.publish('/control/laser/cmd', "2", qos=1) |
zhangyx | 6:4c80e598d08c | 76 | elif msg.payload == 'received.': |
zhangyx | 6:4c80e598d08c | 77 | print('数据接收完成') |
zhangyx | 6:4c80e598d08c | 78 | elif msg.payload == 'done.': |
zhangyx | 6:4c80e598d08c | 79 | print('任务完成') |
zhangyx | 6:4c80e598d08c | 80 | |
zhangyx | 6:4c80e598d08c | 81 | def on_connect(client, userdata, flags, rc): |
zhangyx | 6:4c80e598d08c | 82 | print("Connected with result code "+str(rc)) |
zhangyx | 6:4c80e598d08c | 83 | |
zhangyx | 6:4c80e598d08c | 84 | waitfile(10,30); #测试保存文件功能 |
zhangyx | 6:4c80e598d08c | 85 | |
zhangyx | 6:4c80e598d08c | 86 | client = mqtt.Client() |
zhangyx | 6:4c80e598d08c | 87 | client.on_connect = on_connect |
zhangyx | 6:4c80e598d08c | 88 | client.on_message = on_message |
zhangyx | 6:4c80e598d08c | 89 | client.connect("tdxls-iot.xicp.net", 1883, 60) |
zhangyx | 6:4c80e598d08c | 90 | client.subscribe('/values/laser/report', 1) |
zhangyx | 6:4c80e598d08c | 91 | client.loop_start() |
zhangyx | 6:4c80e598d08c | 92 | while True: |
zhangyx | 6:4c80e598d08c | 93 | sleep(0.1) |