Python Tkinter Control Arduino LED
需使用Virtual Seria;l Ports Emulator
模擬Pair 方式 COM5<-->COM7
(Python Tkinter Com port = 7)
import serial
import time
import tkinter
def quit():
global tkTop
ser.write(bytes('L', 'UTF-8'))
tkTop.destroy()
def set_button1_state():
global b
b += 1
varLabel.set("LED ON ")
ser.write(bytes('H', 'UTF-8'))
varLabel2.set(b)
print(b)
def set_button2_state():
varLabel.set("LED OFF")
ser.write(bytes('L', 'UTF-8'))
ser = serial.Serial('com7', 9600)
print("Reset Arduino")
time.sleep(3)
ser.write(bytes('L', 'UTF-8'))
tkTop = tkinter.Tk()
tkTop.geometry('300x200')
tkTop.title("IoT24hours")
label3 = tkinter.Label(text = 'Building Python GUI to interface an arduino,'
'\n and control an LED',font=("Courier", 12,'bold')).pack()
tkTop.counter = 0
b = tkTop.counter
varLabel = tkinter.IntVar()
tkLabel = tkinter.Label(textvariable=varLabel, )
tkLabel.pack()
varLabel2 = tkinter.IntVar()
tkLabel2 = tkinter.Label(textvariable=varLabel2, )
tkLabel2.pack()
button1 = tkinter.IntVar()
button1state = tkinter.Button(tkTop,
text="ON",
command=set_button1_state,
height = 4,
fg = "black",
width = 8,
bd = 5,
activebackground='green'
)
button1state.pack(side='top', ipadx=10, padx=10, pady=15)
button2 = tkinter.IntVar()
button2state = tkinter.Button(tkTop,
text="OFF",
command=set_button2_state,
height = 4,
fg = "black",
width = 8,
bd = 5
)
button2state.pack(side='top', ipadx=10, padx=10, pady=15)
tkButtonQuit = tkinter.Button(
tkTop,
text="Quit",
command=quit,
height = 4,
fg = "black",
width = 8,
bg = 'yellow',
bd = 5
)
tkButtonQuit.pack(side='top', ipadx=10, padx=10, pady=15)
tkinter.mainloop()
Arduino程式
const int ledPin = 13; // pin the LED is attached to
int incomingByte; // variable stores serial data
void setup() {
// initialize serial communication:
Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
}
void loop() {
// see if there's incoming serial data:
if (Serial.available() > 0) {
// read the oldest byte in the serial buffer:
incomingByte = Serial.read();
// if it's a capital H (ASCII 72), turn on the LED:
if (incomingByte == 'H') {
digitalWrite(ledPin, HIGH);
Serial.println("Getting H"); //print out to serial monitor to check state
}
// if it's an L (ASCII 76) turn off the LED:
if (incomingByte == 'L') {
digitalWrite(ledPin, LOW);
Serial.println("Getting L"); //print out to serial monitor to check state
}
}
}
int incomingByte; // variable stores serial data
void setup() {
// initialize serial communication:
Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
}
void loop() {
// see if there's incoming serial data:
if (Serial.available() > 0) {
// read the oldest byte in the serial buffer:
incomingByte = Serial.read();
// if it's a capital H (ASCII 72), turn on the LED:
if (incomingByte == 'H') {
digitalWrite(ledPin, HIGH);
Serial.println("Getting H"); //print out to serial monitor to check state
}
// if it's an L (ASCII 76) turn off the LED:
if (incomingByte == 'L') {
digitalWrite(ledPin, LOW);
Serial.println("Getting L"); //print out to serial monitor to check state
}
}
}
沒有留言:
張貼留言