iT邦幫忙

0

有關Python樹梅派的問題

  • 分享至 

  • xImage
import spidev # To communicate with SPI devices
from numpy import interp  # To scale values
from time import sleep  # To add delay
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
relaypin_light = 5
relaypin_Moisture=6
GPIO.setup(relaypin_light,GPIO.OUT) #設定GPIO5 為輸出
GPIO.setup(relaypin_Moisture,GPIO.OUT) #設定GPIO6 為輸出
# Start SPI connection
spi = spidev.SpiDev() # Created an object
spi.open(0,0)
# Read MCP3008 data
def analogInput(channel):
  spi.max_speed_hz = 1350000
  adc = spi.xfer2([1,(8+channel)<<4,0])
  data = ((adc[1]&3) << 8) + adc[2]
  return data
while True:  
  
  output_Moisture = analogInput(0) # Reading from MCP3008 CH0 
  output_Moisture = interp(output_Moisture, [0, 1023], [100, 0])
  output_Moisture = int(output_Moisture) 
  print("Moisture:", output_Moisture)
  GPIO.output(relaypin_Moisture,0)
  sleep(0.5)

  if output_Moisture >= 40: 
    GPIO.output(relaypin_Moisture,1)
    time.sleep(0.5)
  else:
    GPIO.output(relaypin_Moisture,0)
    time.sleep(10)
    GPIO.output(relaypin_Moisture,1)
    
    
  output_light = analogInput(1) # Reading from MCP3008 CH1
  output_light = interp(output_light, [0, 1023], [0, 500])
  output_light = int(output_light)
  print("light:", output_light)
  sleep(0.5)

  if  output_light>= 400:
    GPIO.output(relaypin_light,1) 
    time.sleep(0.5)
  else:
    GPIO.output(relaypin_light ,0) 
    time.sleep(0.5)
  


請問各位大神,要怎麼把output_Moisture和output_light的資料繪製成圖
去網上看了一下 不知道要怎麼去指定這些數值
往上大都好像都是給固定的數字繪圖

haward79 iT邦研究生 1 級 ‧ 2021-06-18 08:29:34 檢舉
可以用 matplotlib 來畫。
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
I code so I am
iT邦高手 1 級 ‧ 2021-06-18 16:21:53
最佳解答
import spidev # To communicate with SPI devices
from numpy import interp  # To scale values
from time import sleep  # To add delay
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
relaypin_light = 5
relaypin_Moisture=6
GPIO.setup(relaypin_light,GPIO.OUT) #設定GPIO5 為輸出
GPIO.setup(relaypin_Moisture,GPIO.OUT) #設定GPIO6 為輸出
# Start SPI connection
spi = spidev.SpiDev() # Created an object
spi.open(0,0)
# Read MCP3008 data
def analogInput(channel):
  spi.max_speed_hz = 1350000
  adc = spi.xfer2([1,(8+channel)<<4,0])
  data = ((adc[1]&3) << 8) + adc[2]
  return data
  
# 我加的  
output_Moisture_list = []
output_light_list = []
  
while True:  
  
  output_Moisture = analogInput(0) # Reading from MCP3008 CH0 
  output_Moisture = interp(output_Moisture, [0, 1023], [100, 0])
  output_Moisture = int(output_Moisture) 
  print("Moisture:", output_Moisture)
  GPIO.output(relaypin_Moisture,0)
  sleep(0.5)

  if output_Moisture >= 40: 
    GPIO.output(relaypin_Moisture,1)
    time.sleep(0.5)
  else:
    GPIO.output(relaypin_Moisture,0)
    time.sleep(10)
    GPIO.output(relaypin_Moisture,1)
    
    
  output_light = analogInput(1) # Reading from MCP3008 CH1
  output_light = interp(output_light, [0, 1023], [0, 500])
  output_light = int(output_light)
  print("light:", output_light)
  sleep(0.5)

  if  output_light>= 400:
    GPIO.output(relaypin_light,1) 
    time.sleep(0.5)
  else:
    GPIO.output(relaypin_light ,0) 
    time.sleep(0.5)
    
  # 我加的  
  output_Moisture_list.append(output_Moisture)
  output_light_list.append(output_light)
  
# 我加的  
import matplotlib.pyplot as plt
plt.plot(output_Moisture_list)
plt.plot(output_light_list)
plt.show

我要發表回答

立即登入回答