「樹莓派用於裁線」:修訂間差異
| 第158行: | 第158行: | ||
import time | import time | ||
import spidev | import spidev | ||
#This is Pi : read & send to arduino | #This is Pi : read & send to arduino | ||
GPIO.setmode(GPIO.BCM) | GPIO.setmode(GPIO.BCM) | ||
pipes = [[0xE8, 0xE8, 0xF0, 0xF0, | pipes = [[0xE8, 0xE8, 0xF0, 0xF0, 0xE2],[0xF0, 0xF0, 0xF0, 0xF0, 0xE2]] | ||
# initialization | # initialization | ||
radio = NRF24(GPIO, spidev.SpiDev()) | radio = NRF24(GPIO, spidev.SpiDev()) | ||
radio.begin(0, 17) | radio.begin(0, 17) | ||
radio.setPayloadSize(32) | radio.setPayloadSize(32) | ||
radio.setChannel( | radio.setChannel(0x72) | ||
radio.setDataRate(NRF24.BR_1MBPS) | radio.setDataRate(NRF24.BR_1MBPS) | ||
radio.setPALevel(NRF24. | radio.setPALevel(NRF24.PA_MAX) | ||
radio.setAutoAck(True) | radio.setAutoAck(True) | ||
radio.enableDynamicPayloads() | radio.enableDynamicPayloads() | ||
radio.enableAckPayload() | radio.enableAckPayload() | ||
radio.openWritingPipe(pipes[0]) | radio.openWritingPipe(pipes[0]) | ||
radio.openReadingPipe(1,pipes[1]); | radio.openReadingPipe(1,pipes[1]); | ||
radio.stopListening() | radio.stopListening() | ||
radio.printDetails() | radio.printDetails() | ||
while True: | while True: | ||
message = list(input("Enter a message to send: ")) | message = list(input("Enter a message to send: ")) | ||
while len(message) < 32: | while len(message) < 32: | ||
message.append(0) | message.append(0) | ||
| 第198行: | 第198行: | ||
RF24 rf24(A0, A1); // CE腳, CSN腳 | RF24 rf24(A0, A1); // CE腳, CSN腳 | ||
//Arduino Send to PI | //Arduino Send to PI | ||
const uint64_t addr = | const uint64_t addr = 0xF0F0F0F0E2LL; | ||
const uint64_t pipe = | const uint64_t pipe = 0xE8E8F0F0E2LL; | ||
int i=0; | int i=0; | ||
void setup() { | void setup() { | ||
| 第206行: | 第206行: | ||
rf24.begin(); | rf24.begin(); | ||
rf24.setPALevel(RF24_PA_MAX); // 設定廣播功率 | rf24.setPALevel(RF24_PA_MAX); // 設定廣播功率 | ||
rf24.setChannel( | rf24.setChannel(0x72); // 設定頻道編號 | ||
rf24.setDataRate(RF24_1MBPS); // 設定傳輸速率 | rf24.setDataRate(RF24_1MBPS); // 設定傳輸速率 | ||
rf24.openWritingPipe(addr); // 設定通道位址 | rf24.openWritingPipe(addr); // 設定通道位址 | ||
於 2020年6月4日 (四) 14:56 的修訂
安裝樹派
- 下載Raspbian Buster Lite
- 下載克隆軟體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模組
- 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
- 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
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>