nRF24L01+をArduinoで動かす
むかーし買った、SparkfunのnRF24L01+での通信テストをやってみた。
(これも実は昔テストして動かなくってあきらめたものの1つである...)
https://www.sparkfun.com/products/691
サンプルコードは下記のものを使用した。
やり方は、prinft.hはそれぞれのフォルダにコピーして、
pro_micro_receive_nrf24l01.ino, pro_micro_transmit_nrf24L01.inoを送信・受信にそれぞれ書き込み。
https://gist.github.com/bryanthompson/ef4ecf24ad36410f077b
- printf.h
/* Copyright (C) 2011 J. Coliz <maniacbug@ymail.com> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation. */ /** * @file printf.h * * Setup necessary to direct stdout to the Arduino Serial library, which * enables 'printf' */ #ifndef __PRINTF_H__ #define __PRINTF_H__ #ifdef ARDUINO int serial_putc( char c, FILE * ) { Serial.write( c ); return c; } void printf_begin(void) { fdevopen( &serial_putc, 0 ); } #else #error This example is only for use on Arduino. #endif // ARDUINO #endif // __PRINTF_H__
- pro_micro_receive_nrf24l01.ino
#include <SPI.h> #include <RF24.h> #include "printf.h" #define RF_CS 9 #define RF_CSN 10 RF24 radio(RF_CS, RF_CSN); const uint64_t pipes[2] = { 0xe7e7e7e7e7LL, 0xc2c2c2c2c2LL }; struct sensor_struct{ int sensor_id; float temp; float soil_temp; float humid; float pres; }; void setup() { Serial.begin(9600); printf_begin(); radio.begin(); radio.openWritingPipe(pipes[1]); // note that our pipes are the same above, but that radio.openReadingPipe(1, pipes[0]); // they are flipped between rx and tx sides. radio.startListening(); radio.printDetails(); } void loop() { if (radio.available()) { Serial.println("--------------------------------------------------------------------------------"); uint8_t rx_data[32]; // we'll receive a 32 byte packet bool done = false; while (!done) { done = radio.read( &rx_data, sizeof(rx_data) ); printf("Got payload @ %lu...\r\n", millis()); } // echo it back real fast radio.stopListening(); radio.write( &rx_data, sizeof(rx_data) ); Serial.println("Sent response."); radio.startListening(); // do stuff with the data we got. Serial.print("First Value: "); Serial.println(rx_data[0]); } } // https://gist.github.com/bryanthompson/ef4ecf24ad36410f077b
- pro_micro_transmit_nrf24L01.ino
#include <SPI.h> #include <RF24.h> #include "printf.h" #define LED 2 #define RF_CS 9 #define RF_CSN 10 RF24 radio(RF_CS, RF_CSN); const uint64_t pipes[2] = { 0xe7e7e7e7e7LL, 0xc2c2c2c2c2LL }; void setup() { Serial.begin(9600); printf_begin(); pinMode(LED, OUTPUT); radio.begin(); radio.openWritingPipe(pipes[0]); radio.openReadingPipe(1, pipes[1]); radio.startListening(); radio.printDetails(); } void loop() { unsigned long time = millis(); uint8_t data[32]; // we'll transmit a 32 byte packet data[0] = 99; // our first byte in the pcaket will just be the number 99. // transmit the data radio.stopListening(); radio.write( &data, sizeof(data) ); radio.startListening(); // listen for acknowledgement from the receiver unsigned long started_waiting_at = millis(); bool timeout = false; while (!radio.available() && ! timeout) if (millis() - started_waiting_at > 250 ) timeout = true; if (timeout){ Serial.println("Failed, response timed out."); } else { // the receiver is just going to spit the data back radio.read( &data, sizeof(data) ); digitalWrite(LED, HIGH); delay(100); // light up the LED for 100ms if it worked. digitalWrite(LED, LOW); Serial.print("Got response, round trip delay: "); Serial.print(millis() - started_waiting_at); } delay(1000); // wait a second and do it again. } // https://gist.github.com/bryanthompson/ef4ecf24ad36410f077b
ピンアサインは下記に書いてある通りに行った.
(上のurlのものと若干違う気がするけど、こっちだとうまくいったよ)
https://arduinoexamples.wordpress.com/2012/11/11/nrf24l01-com-arduino-leonardo-nrf24l01-with-arduino-leonardo/
nRF24L01+ | Arduino |
GND | GND |
VCC | 3.3V |
CE | D9 |
CSN | D10(SS) |
SCK | D13(SCK) |
MOSI | D11(MOSI) |
MISO | D12(MISO) |
IRQ | Not used |