今日讀的進度,以及筆記 For python3的部分

現在已經可以利用python讀取DHT11的值並且畫出圖形,主要程式部分我會列在下方,大致的作法是分析資料和切開資料

1.先確認Arduino部分送出來的值 可以發現是有下列三個值

  • temperature:溫度值
  • Humidity:濕度值
  • Flag:控制指標值
在Arduino程式碼的部分可以看到有serial write


#include 

#define dht_dpin A0 //定義訊號要從Pin A0 進來  

dht DHT;   
boolean flagPlot = true;

void setup(){   
Serial.begin(9600);   
delay(300);             //Let system settle   
//Serial.println("Humidity and temperature\n\n");   
             //Wait rest of 1000ms recommended delay before   
                        //accessing sensor   
}

void loop(){   
DHT.read11(dht_dpin);   //去library裡面找DHT.read11  
Serial.print("temperature:");   
Serial.println(DHT.temperature);   
delay(200); 
Serial.print("Humidity:");   
Serial.println(DHT.humidity);   
//Serial.print("% ");   
delay(200); 
Serial.print("Flag:");
Serial.println(flagPlot);
delay(200);   


}  

在python程式的部分程式有使用Tkinter

#!/usr/bin/python3

import serial
import sys
import numpy as np
import tkinter
from matplotlib import pyplot, rcParams

port = serial.Serial('COM6', 9600, timeout=1)

# Tkinter canvas
top = tkinter.Tk()
top.title("Tkinter + matplotlib")

# Create flag to work with indefinite while loop
flag = tkinter.BooleanVar(top)
flag.set(True)


def cleanText(data):
    data = data.replace("\r\n", "")
    return data


def onStartButtonPress():
    while True:
        if flag.get():
            line = port.readline().decode('UTF-8')
            reading = line.split(':')
        #    print (reading)
            if len(reading) == 2:
                for i in range(2):
                    if reading[0] == "temperature":
                        TempLabel.config(text=cleanText(reading[1]))
                        TempUnitLabel.config(text="C")
                        TempUnitLabel.update_idletasks()
  #                  if reading[0] == "Temperature(F)":
 #                       TempLabel.config(text=cleanText(reading[1]))
 #                       TempUnitLabel.config(text="F")
#                        TempUnitLabel.update_idletasks()
                    if reading[0] == "Humidity":
                        HumdLabel.config(text=cleanText(reading[1]))
                        pData.append(float(reading[1]))
                        del pData[0]
#                    if reading[0] == "Light(lx)":
  #                      LighLabel.config(text=cleanText(reading[1]))
                    if reading[0] == "Flag":
#                        print (reading[1])
                        if int(reading[1]) == 1:
                            try:
                                print ("In flag")
                                print ("In flag -> if ")
                                l1.set_xdata(np.arange(len(pData)))
                                l1.set_ydata(pData)  # update the data
                                pyplot.ylim([0, 100])
                                pyplot.draw()  # update the plot
                            except:
                                print ("In flag except")
                                pyplot.figure()
                                pyplot.title('Humidity')
                                ax1 = pyplot.axes()
                                l1, = pyplot.plot(pData)
                                pyplot.ylim([0, 100])
                        if int(reading[1]) == 0:
                            try:
                                pyplot.close('all')
                                l1 = None
                            except:
                                continue
            port.flushInput()
            top.update()
        else:
            flag.set(True)
            break


def onExitButtonPress():
    print ("Exiting....")
    flag.set(False)
    port.close()
    top.quit()
    top.destroy()
    print ("Done.")
    sys.exit()

pyplot.ion()
rcParams['toolbar'] = 'None'
pData = [0] * 25

tkinter.Label(top, text="Temperature").grid(column=1, row=1)
tkinter.Label(top, text="Humidity").grid(column=1, row=2)
tkinter.Label(top, text="Light").grid(column=1, row=3)

TempLabel = tkinter.Label(top, text=" ")
TempLabel.grid(column=2, row=1)
HumdLabel = tkinter.Label(top, text=" ")
HumdLabel.grid(column=2, row=2)
LighLabel = tkinter.Label(top, text=" ")
LighLabel.grid(column=2, row=3)

TempUnitLabel = tkinter.Label(top, text=" ")
TempUnitLabel.grid(column=3, row=1)
HumdUnitLabel = tkinter.Label(top, text="%")
HumdUnitLabel.grid(column=3, row=2)
LighUnitLabel = tkinter.Label(top, text="lx")
LighUnitLabel.grid(column=3, row=3)

# Create Start button and associate with onStartButtonPress method
StartButton = tkinter.Button(top,
                             text="Start",
                             command=onStartButtonPress)
StartButton.grid(column=1, row=4)

# Create Exit button and destroy the window
ExitButton = tkinter.Button(top,
                            text="Exit",
                            command=onExitButtonPress)
ExitButton.grid(column=2, row=4)

top.mainloop()

可以得到這兩個畫面

底下為實際接線圖


Comments

comments powered by Disqus