
labview 将驱动打包到安装文件中_基于Labview与单片机的无线温度采集与控制系统设计设备篇...
编写一个main.py文件,将上述代码拷贝到里面。 将串口工具与设备接好,然后接到电脑的USB上。打开文件发送工具,点击发送。 等待发送成功,板子会自动重启,并打印如下信息,如果我们不提前打开Labview上位机,连不上服务器会异常退出的。这部分目前还没有做相应的处理。
发布日期:2025-04-04 00:26:52
浏览次数:15
分类:精选文章
本文共 3945 字,大约阅读时间需要 13 分钟。
今天是端午节,祝大家端午节安康。
在上篇文章中,只完成了PC端的软件设计,设备端也是采用软件模拟的方式实现的。今天趁着还没出去的时间,来更新一篇硬件-设备端的实现方法。
硬件准备:ESP8266最小系统开发板,一个温度传感器DS18B20,三个LED模拟开关设备的控制。这是我好久之前自己画来玩的板子,硬件实物如下图所示。
WIFI部分原理图:
目前用到的IO如下所示:
p2_led = Pin(2, Pin.OUT)p4_airconditioner = Pin(4, Pin.OUT)p5_smartsocket = Pin(5, Pin.OUT)# 插座IO温度传感器接在Pin_12引脚dat = machine.Pin(12)
温度传感器实物和原理图:
温度传感器相关代码如下:
import onewire, ds18x20import machine# 定义温度传感器引脚并查找设备dat = machine.Pin(12)ds = ds18x20.DS18X20(onewire.OneWire(dat))roms = ds.scan()print('found devices:', roms)# 采集温度并上传到服务器print('temperatures:', end=' ')ds.convert_temp()time.sleep_ms(500)for rom in roms: print(ds.read_temp(rom), end=' ') val = int(ds.read_temp(rom) * 10) temp_str = 'A' + str(val) + 'B' + '\r\n' s.send(str(temp_str, 'utf8'))print()
硬件端相关的内容已经说完了,接下来就是软件代码的事了。在写代码之前需要说明一下,这里用的esp8266的固件MiroPython的,所以需要烧写对应的固件,对于不知道怎么烧写固件的同学,可以参考这篇文章。
这里的代码是在该文章的代码基础上进行完善了,增加的功能就是一个温度采集以及将温度发送到Labview编写的上位机软件中。
完整的代码如下:
import networkimport socketimport timeimport machinefrom machine import Pinimport onewire, ds18x20# 定义ESP8266 IO口p2_led = Pin(2, Pin.OUT)p4_airconditioner = Pin(4, Pin.OUT)p5_smartsocket = Pin(5, Pin.OUT)# 温度传感器接在Pin_12引脚dat = machine.Pin(12)ds = ds18x20.DS18X20(onewire.OneWire(dat))roms = ds.scan()print('found devices:', roms)# 初始化默认值,低电平有效def default_value_init(): p2_led.value(1) p4_airconditioner.value(1) p5_smartsocket.value(1)# 定义一些设备操作函数def led_control(status): p2_led.value(status)def airconditioner_control(status): p4_airconditioner.value(status)def smart_socket_control(status): p5_smartsocket.value(status)# WIFI连接函数def do_connect(): sta_if = network.WLAN(network.STA_IF) sta_if.active(False) if not sta_if.isconnected(): print("connecting to network ...") sta_if.active(True) sta_if.connect("TP-LINK_FF71", "lwy18079431") while not sta_if.isconnected(): pass if sta_if.isconnected(): print("connect success") print('network config:', sta_if.ifconfig())# TCP客户端连接服务器函数def tcp_connect(host, port): s = socket.socket() s.connect((host, port)) s.settimeout(0) s.send("hello i'm Esp8266 Device\r\n") while True: data = None try: data = s.recv(1024) except Exception as Ex: pass if data: if str(data, 'utf8') == "light_on": led_control(0) s.send(str(data, 'utf8')) elif str(data, 'utf8') == "light_off": led_control(1) s.send(str(data, 'utf8')) elif str(data, 'utf8') == "airconditioner_on": airconditioner_control(0) s.send(str(data, 'utf8')) elif str(data, 'utf8') == "airconditioner_off": airconditioner_control(1) s.send(str(data, 'utf8')) elif str(data, 'utf8') == "socket_on": smart_socket_control(0) s.send(str(data, 'utf8')) elif str(data, 'utf8') == "socket_off": smart_socket_control(1) s.send(str(data, 'utf8')) # 温度采集,打印相关信息,并上传至服务器 print('temperatures:', end=' ') ds.convert_temp() time.sleep_ms(500) for rom in roms: print(ds.read_temp(rom), end=' ') val = int(ds.read_temp(rom) * 10) temp_str = 'A' + str(val) + 'B' + '\r\n' s.send(str(temp_str, 'utf8')) print()if __name__ == "__main__": default_value_init() do_connect() tcp_connect("192.168.0.111", 8888)
烧录并测试:
效果图如下:
如果需要获取这篇文章的所有资料,可回复关键字:labview_esp8266 获取噢!
在使用上如果碰到有问题的,可以底下留言或后台回复,看到会及时帮你解决的。
PS:想获取更多Labview的书籍,可回复关键字:Labview_书籍 获取。
以上源码仅供学习交流,请勿用于商业用途,转载请注明出处。
书籍仅供学习交流,请勿用于商业用途,如侵犯到您的版权,可联系删除!
发表评论
最新留言
不错!
[***.144.177.141]2025年04月28日 08时51分57秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!