檢視 樹莓派用於裁線 的原始碼
←
樹莓派用於裁線
跳至導覽
跳至搜尋
由於以下原因,您無權編輯此頁面:
您請求的操作只有這個群組的使用者能使用:
使用者
您可以檢視並複製此頁面的原始碼。
==安裝樹派== #下載[https://www.raspberrypi.org/downloads/raspbian/ Raspbian Buster Lite] #下載[https://www.balena.io/etcher/ 克隆軟體balenaEtcher] #Raspberry Pi設定 ##預設帳密 帳號: pi 密碼: raspberry ##設定指令sudo raspi-config ##變更密碼->1 Change User PW-> ###設定時區I2->Change Timezone->Asia->Taipei ###設定WIFI國別->I4 Change Wi-fi Country->TW Taiwan ###設定WIFI帳密->2 Network Options->N2 Wi-fi->輸入WIFI的SSID->輸入WIFI密碼 ###設定鍵盤 Layout->4 Localisation Options->I3 Change Keyboard Layout->Generic 104-Key PC->US->English (US)->No AltGr Key->Left Logo Key ###啟動SSH->5 Interfacing Options->P2 SSH->YES ###設定auto login->Boot options ##查詢ip->ip a ##切換python 2.7->3.7 ###sudo ln -f -s /usr/bin/python3.7 /usr/bin/python ##安裝pip ###sudo apt-get install python3-pip ##安裝distutils ###sudo apt-get install python3-distutils ###sudo apt-get install python3-dev ##安裝pyserial ###sudo pip3 install pyserial ##修改/boot/cmdline.txt ###dwc_otg.lpm_enable=0 console=ttyAMA0,9600 kgdboc=ttyAMA0,9600 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 rootwait ##樹莓派串口通信開發環境 ###pip3 install RPi.GPIO ##安裝git ###sudo apt-get update ###sudo apt-get install git ##nRF24L01模組 ###[http://invent.module143.com/daskal_tutorial/raspberry-pi-3-wireless-pi-to-pi-python-communication-with-nrf24l01/ Wireless Pi to Pi Python Communication with NRF24L01+] ###sudo raspi-config ###5.Interfacing options->P4 SPI Enable->restart ###On my Pi in python I use that : pipes = [0xF0F0F0F0E1, 0xF0F0F0F0D2] && And On my Arduino Nano : const uint64_t addresses[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL }; ##nRF24L01模組2 py-nrf24 ###pip3 install nrf24 ###https://github.com/bjarne-hansen/py-nrf24 ##nRF24L01模組3 Syrex-o/lib_nrf24 ###enable SPI in raspi-config ###wget https://github.com/Gadgetoid/py-spidev/archive/master.zip ###unzip master.zip ###rm master.zip ###cd ./py-spidev-master ###sudo python3 setup.py install ###mkdir /home/pi/NRF24L01 ###cd /home/pi/NRF24L01 ###git clone https://github.com/Syrex-o/lib_nrf24 ###cd ./lib_nrf24/ ###cp lib_nrf24.py /home/pi/NRF24L01/ ###sudo apt-get install python3-rpi.gpio -y ##[https://www.youtube.com/watch?v=_68f-yp63ds nRF24L01模組4] ###enable SPI ###sudo apt-get update ###sudo apt-get install python-dev python3-dev ###wget https://github.com/doceme/py-spidev/archive/master.zip ###unzip master.zip ###cd py-spidev-master ###sudo python setup.py install ###sudo python3 setup.py install ###cd Desktop ###mkdir NRF24L01 ###cd NRF24L01 ###git clone https://github.com/BLavery/lib_nrf24 ###cd lib_nrf24/ ###cp lib_nrf24.py ~Desktop/NRF24L01 ==Pi4== #hdmi無法顯示時 ##hdmi_drive=2 ##hdmi_group=2 ##hdimi_mode=42 #將內存mount to RAMDISK ##mkdir /tmp/ramdisk ##chmod 777 /tmp/ramdisk ##mount -t tmpfs -o size=16M tmpfs /tmp/ramdisk/ ==python TTS== *[http://yhhuang1966.blogspot.com/2017/08/google-gtts-api.html 利用 Google gTTS 文字轉語音 API 讓電腦說話] *[https://stackoverflow.com/questions/15405082/when-running-a-pygame-script-as-root-no-sound-is-output sudo沒聲音] *pip3 install gTTS <python> from gtts import gTTS tts=gTTS(text='生日快樂', lang='zh-tw') tts.save("\tmp\ramdisk\happybirthday.mp3") </python> *pip3 install pygame *sudo apt-get install libsdl1.2-dev *sudo apt-get install libsdl-mixer1.2 *[https://atceiling.blogspot.com/2014/02/raspberry-pi.html 樹莓派調整音量] **sudo raspi-config ->Advanced Op->A4 Audio->1 Headphones **alsamixer **.asoundrc @home ***defaults.ctl.card 1 defaults.pcm.card 1 <python> from pygame import mixer mixer.init() mixer.music.load('\tmp\ramdisk\happybirthday.mp3') mixer.music.play() </python> *判斷是否下載TTS與播放 <python> from gtts import gTTS from pygame import mixer import os mixer.init() while True: msg=input("Input a string or a value:") mp3="c:\\python\mp3\\" + msg + ".mp3" if not os.path.isfile(mp3): tts=gTTS(text=msg, lang='zh-tw') tts.save(mp3) mixer.music.load(mp3) mixer.music.play() </python> ==WS2812B LED燈條== *[https://learn.adafruit.com/neopixels-on-raspberry-pi/python-usage Python Installation of NeoPixel Library] **sudo pip3 install rpi_ws281x adafruit-circuitpython-neopixel **sudo python3 -m pip install --force-reinstall adafruit-blinka **python <python> import board import neopixel pixels = neopixel.NeoPixel(board.D18, 30) pixels[0] = (255, 0, 0) </python> ==Pi POST== *pip3 install requests2 ==Arduino Send to Pi== *Arduino Send to Pi <c> #include <SPI.h> #include <RF24.h> #include <printf.h> RF24 rf24(A0, A1); // CE腳, CSN腳 //Arduino Send to PI const uint64_t addr = 0xF0F0F0F0E1LL; int i=0; void setup() { Serial.begin(9600); printf_begin(); rf24.begin(); rf24.setChannel(0x76); // 設定頻道編號 rf24.openWritingPipe(addr); // 設定通道位址 rf24.setPALevel(RF24_PA_MAX); // 設定廣播功率 rf24.setDataRate(RF24_1MBPS); // 設定傳輸速率 rf24.enableDynamicPayloads(); rf24.powerUp(); rf24.stopListening(); // 停止偵聽;設定成發射模式 rf24.printDetails(); } void loop(void) { const char text[] = "Hello World!"; rf24.write(&text, sizeof(text)); delay(1000); } </c> *PI Read From Arduino <python> import RPi.GPIO as GPIO from lib_nrf24 import NRF24 import time import spidev #This is Pi & read from arduino GPIO.setmode(GPIO.BCM) pipes = [[0xE8, 0xE8, 0xF0, 0xF0, 0xE1],[0xF0, 0xF0, 0xF0, 0xF0, 0xE1]] # initialization radio = NRF24(GPIO, spidev.SpiDev()) radio.begin(0, 17) radio.setPayloadSize(32) radio.setChannel(0x76) radio.setDataRate(NRF24.BR_1MBPS) radio.setPALevel(NRF24.PA_MIN) radio.setAutoAck(True) radio.enableDynamicPayloads() radio.enableAckPayload() radio.openReadingPipe(1, pipes[1]) #radio.openWritingPipe(pipes[1]) radio.printDetails() radio.startListening() # message = list(input("Enter a message to send: ")) while True: while not radio.available(0): time.sleep(1/100) receiveMessage = [] radio.read(receiveMessage, radio.getDynamicPayloadSize()) print("received: {}".format(receiveMessage)) print("Translating our received Message into unicode characters...") string = "" for n in receiveMessage: if (n>=32 and n<=126): string += chr(n) print("Our received message decodes to: {}".format(string)) </python> ==Pi SENT TO Arduino不確認== *Pi <python> import RPi.GPIO as GPIO from lib_nrf24 import NRF24 import time import spidev #This is Pi : read & send to arduino GPIO.setmode(GPIO.BCM) pipes = [[0xE8, 0xE8, 0xF0, 0xF0, 0xE2],[0xF0, 0xF0, 0xF0, 0xF0, 0xE2]] # initialization radio = NRF24(GPIO, spidev.SpiDev()) radio.begin(0, 17) radio.setPayloadSize(32) radio.setChannel(0x72) radio.setDataRate(NRF24.BR_1MBPS) radio.setPALevel(NRF24.PA_MAX) radio.setAutoAck(True) radio.enableDynamicPayloads() radio.enableAckPayload() radio.openWritingPipe(pipes[0]) radio.openReadingPipe(1,pipes[1]); radio.stopListening() radio.printDetails() while True: message = list(input("Enter a message to send: ")) while len(message) < 32: message.append(0) radio.write(message) </python> *Arduino <c> #include <SPI.h> #include <RF24.h> #include <printf.h> RF24 rf24(A0, A1); // CE腳, CSN腳 //Arduino Send to PI const uint64_t addr = 0xF0F0F0F0E2LL; const uint64_t pipe = 0xE8E8F0F0E2LL; int i=0; void setup() { Serial.begin(9600); printf_begin(); rf24.begin(); rf24.setPALevel(RF24_PA_MAX); // 設定廣播功率 rf24.setChannel(0x72); // 設定頻道編號 rf24.setDataRate(RF24_1MBPS); // 設定傳輸速率 rf24.openWritingPipe(addr); // 設定通道位址 rf24.openReadingPipe(1,pipe); rf24.enableDynamicPayloads(); rf24.powerUp(); rf24.startListening(); rf24.printDetails(); } void loop(void) { if(rf24.available()){ char msg[32] = ""; rf24.read(msg, sizeof(msg)); Serial.println(msg); // 顯示訊息內容 } } </c> ==樹莓派主程式win== <python> from gtts import gTTS from pygame import mixer import requests import re import os mixer.init() #用來執行TTS def MsgtoMp3(text): mp3="c:\\python\mp3\\" + text + ".mp3" print(mp3) if not os.path.isfile(mp3): tts=gTTS(text=text, lang='zh-tw') tts.save(mp3) mixer.music.load(mp3) mixer.music.play() #用來查詢員工姓名並TTS def StafftoMp3(): f = open("C:\\python\\staff.txt") lines = f.readlines() staff='' for line in lines: line=line.replace('\n','') mydata = {'q': line} r = requests.post('http://192.168.1.100:8080/HMSFT/php/findstaff.php', data = mydata) staff += r.text f.close() staff+='已綁定' MsgtoMp3(staff) StafftoMp3() msg=input("input:") print(msg) if(re.match(r'\d{9}',msg,re.IGNORECASE)): MsgtoMp3("製令9碼") elif(re.match(r'CAR\d{3}',msg, re.IGNORECASE)): MsgtoMp3("台車號碼") </python> ==樹莓派主程式pi4== <python> from gtts import gTTS from pygame import mixer import requests import re import RPi.GPIO as GPIO from lib_nrf24 import NRF24 import time import spidev import os mixer.init() GPIO.setmode(GPIO.BCM) pipes = [[0xE8, 0xE8, 0xF0, 0xF0, 0xE2],[0xF0, 0xF0, 0xF0, 0xF0, 0xE2]] radioch=0x72 #用來執行TTS def MsgtoMp3(msg): mp3="./mp3/" + msg + ".mp3" if not os.path.isfile(mp3): tts=gTTS(text=msg, lang='zh-tw') tts.save(mp3) mixer.music.load(mp3) mixer.music.play() #用來查詢員工姓名並TTS def StafftoMp3(): f = open("./staff.txt") lines = f.readlines() staff='' for line in lines: line=line.replace('\n','') mydata = {'q': line} r = requests.post('http://192.168.1.100:8080/HMSFT/php/findstaff.php', data = mydata) staff += r.text f.close() staff+='已綁定' MsgtoMp3(staff) #用來傳輸到看板顯示 def SendtoKanBan(text): # initialization radio = NRF24(GPIO, spidev.SpiDev()) radio.begin(0, 17) radio.setPayloadSize(32) radio.setChannel(radioch) radio.setDataRate(NRF24.BR_1MBPS) radio.setPALevel(NRF24.PA_MAX) radio.setAutoAck(True) radio.enableDynamicPayloads() radio.enableAckPayload() radio.openWritingPipe(pipes[0]) radio.openReadingPipe(1,pipes[1]) radio.stopListening() radio.printDetails() text=list(text) while len(text) < 32: text.append(0) radio.write(text) #用來刪除重覆list def Unique_list(listA): unique_set = set(listA) unique_list = list(unique_set) return unique_list ''' 程式由以下開始, 上段為副程式區 ''' #StafftoMp3() tie=[] staffcode=[] while True: msg=input("input:") if(re.match(r'(\d{1,4}|\d{1,4}\.{1,3})',msg)): MsgtoMp3(msg) SendtoKanBan(msg) elif(re.match(r'\d{9}',msg) or re.match(r'CAR\d{3}',msg,re.IGNORECASE) ): tie.append(msg) tie=Unique_list(tie) print(tie) elif(re.match(r'^\$IN',msg,re.IGNORECASE)): MsgtoMp3("進站成功") elif(re.match(r'^\$OUT',msg,re.IGNORECASE)): MsgtoMp3("出站成功") elif(re.match(r'[A-D]\d{4}',msg,re.IGNORECASE)): if(len(staffcode)==0): staffcode.append(msg) MsgtoMp3("人員一綁定成功") elif(len(staffcode)==1): staffcode.append(msg) staffcode=Unique_list(staffcode) if(len(staffcode)==1): MsgtoMp3("人員重覆綁定") else: fo=open("./staff.txt", "w") content=staffcode[0] + "\n" + staffcode[1] fo.write(content) staffcode=[] fo.close() StafftoMp3() </python> ==樹莓派主程式pi4(增加LED燈)== <python> from gtts import gTTS from pygame import mixer import requests import re import RPi.GPIO as GPIO from lib_nrf24 import NRF24 import time import spidev import os import board import neopixel import math import json GPIO.setmode(GPIO.BCM) pipes = [[0xE8, 0xE8, 0xF0, 0xF0, 0xE2],[0xF0, 0xF0, 0xF0, 0xF0, 0xE2]] radioch=0x72 FRONT_LENGTH = 0 ORDER = neopixel.GRB maxled=1200 pixels = neopixel.NeoPixel(board.D21, maxled, brightness=0.2, auto_write=False, pixel_order=ORDER) stafflist=[] #用來執行TTS def MsgtoMp3(msg): mixer.init() mp3="./mp3/" + msg + ".mp3" if not os.path.isfile(mp3): tts=gTTS(text=msg, lang='zh-tw') tts.save(mp3) mixer.music.load(mp3) mixer.music.play() #os.system("sudo -u pi play -q " + mp3 + ' -t alsa') #用來查詢員工姓名並TTS def StafftoMp3(): global stafflist stafflist=[] f = open("./staff.txt") lines = f.readlines() staff='' for line in lines: line=line.replace('\n','') mydata = {'q': line} r = requests.post('http://192.168.1.100:8080/HMSFT/php/findstaff.php', data = mydata) staff += r.text stafflist.append(line) f.close() staff+='已綁定' MsgtoMp3(staff) #用來傳輸到看板顯示 def SendtoKanBan(text): # initialization radio = NRF24(GPIO, spidev.SpiDev()) radio.begin(0, 17) radio.setPayloadSize(32) radio.setChannel(radioch) radio.setDataRate(NRF24.BR_1MBPS) radio.setPALevel(NRF24.PA_MAX) radio.setAutoAck(True) radio.enableDynamicPayloads() radio.enableAckPayload() radio.openWritingPipe(pipes[0]) radio.openReadingPipe(1,pipes[1]) radio.stopListening() #radio.printDetails() text=list(text) while len(text) < 32: text.append(0) radio.write(text) #用來刪除重覆list def Unique_list(listA): unique_set = set(listA) unique_list = list(unique_set) return unique_list def LightLED(length): pixels.fill((0, 0, 0)) length = float(length) length = (length - FRONT_LENGTH)*60 if length > (maxled - 1) : length = (maxled - 1) if length < 0 : length = 0 uplimit = math.ceil(length); downlimit = math.floor(length); if(uplimit==downlimit): pixels[uplimit] = (255,255,255); else: pixels[uplimit] = (255,0,0) #设置第1个led灯的颜色Red pixels[downlimit] = (0,255,0) #设置第2个led灯的颜色Red pixels.show() ''' 程式由以下開始, 上段為副程式區 ''' StafftoMp3() SendtoKanBan("READY") tie={} staffcode=[] while True: msg=input("input:").upper() if(re.match(r'(^\d{1,2}$|^\d{1,2}\.\d{1,3}$|^\.\d{1,3}$)',msg)): MsgtoMp3(msg) SendtoKanBan(msg) LightLED(msg) elif(re.match(r'(^\d{9}$|^\d{6}$)',msg) or re.match(r'CAR\d{3}',msg,re.IGNORECASE) ): if not tie : tie['time']= time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) tie['station'] = 'HM010' tie['text']=[] tie['car']=[] tie['status']=[] tie['staff'] = stafflist if tie['status']=='IN': MsgtoMp3("已進站請重新確認") elif re.match(r'CAR\d{3}',msg,re.IGNORECASE): if len(tie['car']) > 0: MsgtoMp3("已綁定台車") else: tie['car'].append(msg[-3:]) else: tie['text'].append(msg) tie['text']=Unique_list(tie['text']) print(tie) elif(re.match(r'^IN$',msg,re.IGNORECASE)): if not tie : MsgtoMp3("請綁定製令或台車") elif not tie['text']: MsgtoMp3("未綁到製令") elif not tie['car']: MsgtoMp3("未綁到台車") elif tie['status']=='IN': MsgtoMp3("已進站請重新確認") else: tie['status'] = 'IN' #print(tie) mydata = {'data': json.dumps(tie)} headers = {'Content-Type': 'application/json'} r=requests.post('http://192.168.1.100:8080/HMSFT/tiecar.php', data = mydata) print(r.text) MsgtoMp3("進站成功") elif(re.match(r'^OUT$',msg,re.IGNORECASE)): if not tie : MsgtoMp3("請綁定製令或台車") elif tie['status'] != 'IN': MsgtoMp3("未曾進站無法出站") else: tie['time']= time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) tie['status'] = 'OUT' print(tie) tie={} MsgtoMp3("出站成功") elif(re.match(r'^CONT$',msg,re.IGNORECASE)): if not tie : MsgtoMp3("請綁定製令或台車") else: tie['status']=[] MsgtoMp3("繼續綁定") elif(re.match(r'^RESET$',msg,re.IGNORECASE)): if not tie : MsgtoMp3("請綁定製令或台車") else: tie['text']=[] tie['car']=[] tie['status']=[] MsgtoMp3("請重新綁定台車與製令") elif(re.match(r'[A-D]\d{4}',msg,re.IGNORECASE)): if(len(staffcode)==0): staffcode.append(msg) MsgtoMp3("人員一綁定成功") elif(len(staffcode)==1): staffcode.append(msg) staffcode=Unique_list(staffcode) if(len(staffcode)==1): MsgtoMp3("人員重覆綁定") else: fo=open("./staff.txt", "w") content=staffcode[0] + "\n" + staffcode[1] fo.write(content) staffcode=[] fo.close() StafftoMp3() </python>
返回到「
樹莓派用於裁線
」。
導覽選單
個人工具
建立帳號
登入
命名空間
頁面
討論
臺灣正體
視圖
閱讀
檢視原始碼
檢視歷史
更多
搜尋
導覽
首頁
近期變更
隨機頁面
MediaWiki說明
工具
連結至此的頁面
相關變更
特殊頁面
頁面資訊