本帖最后由 云天 于 2021-8-4 19:25 编辑
【项目意图】
使用Mind+Python模式下,利用tkinter库,Easy Iot物联网,在窗口界面中利用按钮对大麦进行控制。
【软件】
Mind+Python模式
Mind+最近更新了V1.7.1 RC1.0版本。增加内置扩展库:MQTT、Pygame;我本次使用了这个MQTT。
Tkinter 是 Python 的标准 GUI 库。Python 使用 Tkinter 可以快速的创建 GUI 应用程序。
【硬件】
1、麦昆Plus
[麦昆 Plus]【试用测评】麦昆Plus与麦昆 精华
2、WIFI IoT模块
用极简的方式连接物联网平台与硬件
【Python代码】
有相应代码注释
-
- # -*- coding: UTF-8 -*-
-
- # MindPlus
- # Python
- import siot
- import tkinter as tk
- from tkinter import *
- from PIL import Image, ImageTk#图像控件
- #连接物联网
- siot.init(client_id="sxs12345678",server="iot.dfrobot.com.cn",port=1883,user="RNZYsQuIWh",password="gHWuLsiwIoWgz")
- siot.connect()
- siot.loop()
- #定义函数,向物联网发送相应控制指令
- def forward():#前进
- siot.publish(topic="5n92uqBMg", data="A")
- def back():#后退
- siot.publish(topic="5n92uqBMg", data="B")
- def left():#左转
- siot.publish(topic="5n92uqBMg", data="C")
- def right():#右转
- siot.publish(topic="5n92uqBMg", data="D")
- def stop():#停止
- siot.publish(topic="5n92uqBMg", data="E")
- def open_s():#张开
- siot.publish(topic="5n92uqBMg", data="F")
- def stop_s():#闭合
- siot.publish(topic="5n92uqBMg", data="G")
- def light_open():#双闪开
- siot.publish(topic="5n92uqBMg", data="H")
- def light_close():#双闪关
- siot.publish(topic="5n92uqBMg", data="I")
-
- #生成窗体,布置相应文本框和按钮
- top = tk.Tk()
- top.title('控制窗口')
- top.geometry('900x600')
- image_width = 900
- image_height = 600
- canvas = Canvas(top,bg = 'white',width = image_width,height = image_height )#绘制画布
- img = Image.open('bg.jpg')
- bg = ImageTk.PhotoImage(img)
- bgid = canvas.create_image(0, 0, image=bg, anchor='nw')
- canvas.place(x = 0,y = 0)
- #产生标签文本背景透明效果
- txtid=canvas.create_text(350,20, fill = 'red',font=("黑体", 35),anchor="nw")
- canvas.insert(txtid,1,"物联网大麦")
- #Label(top,text = '物联网大麦!',font = ("黑体",14),width =15,height = 1).place(x =400,y = 20,anchor = 'nw')
- E1 = Entry(top, bd =5)
- E1.pack(side = RIGHT)
- E1.place(x=100,y=100)
- wx=20
- hy=200
- def setV():#设置车速
- siot.publish(topic="dH6_dBZ7g", data=str(E1.get()))
-
- #生成按钮,并指定相应功能
- left = tk.Button(top,text='左转',height=2,width=15,command=left)
- left.place(x=wx,y=hy+100)
- forward = tk.Button(top,text='前进',height=2,width=15,command=forward)
- forward.place(x=wx+150,y=hy)
- right = tk.Button(top,text='右转',height=2,width=15,command=right)
- right.place(x=wx+300,y=hy+100)
- back = tk.Button(top,text='后退',height=2,width=15,command=back)
- back.place(x=wx+150,y=hy+200)
- stop = tk.Button(top,text='停止',height=2,width=15,command=stop)
- stop.place(x=wx+150,y=hy+100)
-
- wx=450
- hy=200
- open_s = tk.Button(top,text='张开',height=2,width=15,command=open_s)
- open_s.place(x=wx+150,y=hy)
- stop_s = tk.Button(top,text='闭合',height=2,width=15,command=stop_s)
- stop_s.place(x=wx+150,y=hy+200)
- light_open = tk.Button(top,text='闪开',height=2,width=15,command=light_open)
- light_open.place(x=wx,y=hy+100)
- light_close = tk.Button(top,text='闪关',height=2,width=15,command=light_close)
- light_close.place(x=wx+300,y=hy+100)
- setV = tk.Button(top,text='设速',height=2,width=15,command=setV)
- setV.place(x=wx+150,y=hy+100)
- while True:
-
- top.update()
- top.after(100)
-
-
- top.mainloop()
-
复制代码
【micro:bit】
上传到大麦上Microbit程序代码。
Arduino C 代码(功能同上)
-
- /*!
- * MindPlus
- * microbit
- *
- */
- #include <DFRobot_Obloq_I2C.h>
- #include <DFRobot_MaqueenPlus.h>
-
- // 动态变量
- String mind_s_MingLing;
- volatile float mind_n_CheSu, mind_n_ShuangShan;
- // 函数声明
- void obloqMqttEventT0(String& obloq_message);
- void obloqMqttEventT1(String& obloq_message);
- // 静态常量
- const String topics[5] = {"5n92uqBMg","dH6_dBZ7g","","",""};
- const MsgHandleCb msgHandles[5] = {obloqMqttEventT0,obloqMqttEventT1,NULL,NULL,NULL};
- // 创建对象
- DFRobot_Obloq_I2C olq;
- DFRobot_MaqueenPlus maqueenPlus;
-
-
- // 主程序开始
- void setup() {
- olq.registerMqttHandle(msgHandles);
- olq.startConnect("sxs", "*******", "ReNZYsrQIWg", "gHrWLfgswIhWgz", topics, "iot.dfrobot.com.cn", 1883);
- maqueenPlus.writePid(maqueenPlus.ON);
- maqueenPlus.servoRun(maqueenPlus.S3, 110);
- mind_n_CheSu = 30;
- mind_n_ShuangShan = 0;
- Serial.begin(9600);
- }
- void loop() {
- if ((mind_n_ShuangShan==1)) {
- maqueenPlus.setRGB(maqueenPlus.ALL, maqueenPlus.RED);
- delay(500);
- maqueenPlus.setRGB(maqueenPlus.ALL, maqueenPlus.PUT);
- delay(500);
- }
- }
-
-
- // 事件回调函数
- void obloqMqttEventT0(String& obloq_message) {
- mind_s_MingLing = obloq_message;
- Serial.println(mind_s_MingLing);
- if ((mind_s_MingLing==String("A"))) {
- maqueenPlus.motorRun(maqueenPlus.ALL, maqueenPlus.CW, mind_n_CheSu);
- maqueenPlus.setRGB(maqueenPlus.ALL, maqueenPlus.PUT);
- }
- if ((mind_s_MingLing==String("B"))) {
- maqueenPlus.motorRun(maqueenPlus.ALL, maqueenPlus.CCW, mind_n_CheSu);
- maqueenPlus.setRGB(maqueenPlus.ALL, maqueenPlus.RED);
- }
- if ((mind_s_MingLing==String("C"))) {
- maqueenPlus.motorStop(maqueenPlus.LEFT);
- maqueenPlus.motorRun(maqueenPlus.RIGHT, maqueenPlus.CW, mind_n_CheSu);
- maqueenPlus.setRGB(maqueenPlus.LEFT, maqueenPlus.RED);
- maqueenPlus.setRGB(maqueenPlus.RIGHT, maqueenPlus.PUT);
- }
- if ((mind_s_MingLing==String("D"))) {
- maqueenPlus.motorStop(maqueenPlus.RIGHT);
- maqueenPlus.motorRun(maqueenPlus.LEFT, maqueenPlus.CW, mind_n_CheSu);
- maqueenPlus.setRGB(maqueenPlus.RIGHT, maqueenPlus.RED);
- maqueenPlus.setRGB(maqueenPlus.LEFT, maqueenPlus.PUT);
- }
- if ((mind_s_MingLing==String("E"))) {
- maqueenPlus.motorStop(maqueenPlus.ALL);
- maqueenPlus.setRGB(maqueenPlus.ALL, maqueenPlus.PUT);
- }
- if ((mind_s_MingLing==String("F"))) {
- maqueenPlus.servoRun(maqueenPlus.S3, 50);
- }
- if ((mind_s_MingLing==String("G"))) {
- maqueenPlus.servoRun(maqueenPlus.S3, 110);
- }
- if ((mind_s_MingLing==String("H"))) {
- mind_n_ShuangShan = 1;
- }
- if ((mind_s_MingLing==String("I"))) {
- mind_n_ShuangShan = 0;
- }
- }
- void obloqMqttEventT1(String& obloq_message) {
- mind_n_CheSu = (String(obloq_message).toInt());
- }
-
复制代码
【演示视频】
|