nRF24 / RF24

OSI Layer 2 driver for nRF24L01 on Arduino & Raspberry Pi/Linux Devices
https://nrf24.github.io/RF24
GNU General Public License v2.0
2.19k stars 1.01k forks source link

Add support for Arduino Wifi MKR 1010 #602

Closed massivespace closed 4 years ago

massivespace commented 4 years ago

Is your feature request related to a problem? Please describe. I am sending some simple test packets from an Arduino Uno, and receiving them on an Arduino MKR 1010. I have been struggling for a couple of days to understand why during development everything worked, but running stand-alone, the 1010 would not receive anything. At first I thought it was a power supply issue, but I don't believe it is, because I discovered how to reproduce the issue even with the PC connected for power.

I finally narrowed it down to the 1010's USB communication somehow interfering/unlocking the traffic, OR the internal USB libs setting something up in the SPI that maybe the RF24 library isn't (for the Wifi 1010). When plugged into my computer, if I opened the serial terminal window, everything would work as expected, LED would light up to indicate traffic being received. If I select a different port in the IDE, then power cycle the 1010, nothing works. As soon as I select the USB port in the IDE, the traffic starts to be received again. I will post the code below for this scenario, but it's basically the test tx/rx code in the examples.

I'm guessing there is something being called within the USB/serial library internally that is setting up the board's SPI (total guess here) so that communication with the nRF24 begins to work. However, if powered on and no USB connection, that "something" never gets initialized. I tried calling some SPI calls manually to try and get things moving, but I honestly don't know what I'm doing here, and it would just freeze up everything requiring a bootloader boot.

Describe the solution you'd like To be able to receive data on a MKR 1010 via an nRF24 chip without having a serial terminal window open.

Describe alternatives you've considered Running a Nano as a proxy to the 1010, but I'm controlling servos with the RF data and really don't want the added latency.

Additional context I wired pins 8 (MOSI), 9 (SCK), 10 (MISO) to the nRF24 device, and I am using pins 0 and 1 for the CE and CS outputs. I was a little weary of pins 0 and 1 as those are TX/RX and used by USB on other boards, but I also tried pins 2 and 3 to no avail. Pins 13 and 14 are TX/RX on the 1010 device. Also, it's notable that the MOSI, SCK, MISO pins on this device are different from most other Arduino devices.

TX Code:

  #include <SPI.h>
  #include <nRF24L01.h>
  #include <RF24.h>

  RF24 radio(9, 10); // CE, CSN on the Nano
  const byte address[6] = "00001";

  void setup() {
    radio.begin();                  //Starting the Wireless communication
    radio.openWritingPipe(address); //Setting the address where we will send the data
    radio.setPALevel(RF24_PA_MIN);  //You can set it as minimum or maximum depending on the distance between the transmitter and receiver.
    radio.stopListening();          //This sets the module as transmitter
  }

  void loop()
  {
    const char text[] = "Testing 123";
    radio.write(&text, sizeof(text));  //Sending the message to receiver 
    delay(1000);
  }

RX Code:

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(0, 1); // CE, CSN on the Wifi 1010
const byte address[6] = "00001";
int led_counter = 0;

void setup() {
  Serial.begin(9600);
  if (!radio.begin())
    Serial.println("Failed");
  radio.openReadingPipe(0, address);   //Setting the address at which we will receive the data
  radio.setPALevel(RF24_PA_MIN);       //You can set this as minimum or maximum depending on the distance between the transmitter and receiver.
  radio.startListening();              //This sets the module as receiver
}

void loop()
{
  if (radio.available())              //Looking for the data.
  {
    char text[32] = "";                 //Saving the incoming data
    radio.read(&text, sizeof(text));    //Reading the data
    Serial.println(text);
    led_counter = 50;
  }

  int out = false;
  if (led_counter > 0) {
    out = true;
    --led_counter;
  }
  digitalWrite(LED_BUILTIN, out);

  delay(5);
}

When testing with 2 Nanos, or between a Nano and Uno, I can unplug, connect a 9V battery to regulated 3.3 supply, and all works fine.

I'm happy to look and help solve this issue, but I don't really know where to start. I do get the following warnings:

WARNING: library RF24 claims to run on avr, arm, x86, esp8266, esp32, megaavr architecture(s) and may be incompatible with your current board which runs on samd architecture(s).
In file included from /Users/csbyers/Documents/Arduino/libraries/RF24/RF24.h:18:0,
                 from /Users/csbyers/Documents/Arduino/nrf24l01_rx_test/nrf24l01_rx_test.ino:3:
/Users/csbyers/Documents/Arduino/libraries/RF24/RF24_config.h:155:0: warning: "PSTR" redefined
         #define PSTR(x) (x)

In file included from /Users/csbyers/Library/Arduino15/packages/arduino/hardware/samd/1.8.6/cores/arduino/Arduino.h:36:0,
                 from sketch/nrf24l01_rx_test.ino.cpp:1:
/Users/csbyers/Library/Arduino15/packages/arduino/hardware/samd/1.8.6/cores/arduino/avr/pgmspace.h:34:0: note: this is the location of the previous definition
 #define PSTR(str) (str)

In file included from /Users/csbyers/Documents/Arduino/libraries/RF24/RF24.h:18:0,
                 from /Users/csbyers/Documents/Arduino/nrf24l01_rx_test/nrf24l01_rx_test.ino:3:
/Users/csbyers/Documents/Arduino/libraries/RF24/RF24_config.h:157:0: warning: "strlen_P" redefined
         #define strlen_P strlen

In file included from /Users/csbyers/Library/Arduino15/packages/arduino/hardware/samd/1.8.6/cores/arduino/Arduino.h:36:0,
                 from sketch/nrf24l01_rx_test.ino.cpp:1:
/Users/csbyers/Library/Arduino15/packages/arduino/hardware/samd/1.8.6/cores/arduino/avr/pgmspace.h:69:0: note: this is the location of the previous definition
 #define strlen_P(a) strlen((a))

In file included from /Users/csbyers/Documents/Arduino/libraries/RF24/RF24.h:18:0,
                 from /Users/csbyers/Documents/Arduino/nrf24l01_rx_test/nrf24l01_rx_test.ino:3:
/Users/csbyers/Documents/Arduino/libraries/RF24/RF24_config.h:159:0: warning: "pgm_read_word" redefined
         #define pgm_read_word(p) (*(p))

In file included from /Users/csbyers/Library/Arduino15/packages/arduino/hardware/samd/1.8.6/cores/arduino/Arduino.h:36:0,
                 from sketch/nrf24l01_rx_test.ino.cpp:1:
/Users/csbyers/Library/Arduino15/packages/arduino/hardware/samd/1.8.6/cores/arduino/avr/pgmspace.h:103:0: note: this is the location of the previous definition
 #define pgm_read_word(addr) (*(const unsigned short *)(addr))

In file included from /Users/csbyers/Documents/Arduino/libraries/RF24/RF24.h:18:0,
                 from /Users/csbyers/Documents/Arduino/nrf24l01_rx_test/nrf24l01_rx_test.ino:3:
/Users/csbyers/Documents/Arduino/libraries/RF24/RF24_config.h:160:0: warning: "pgm_read_ptr" redefined
         #define pgm_read_ptr(p) (*(p))

In file included from /Users/csbyers/Library/Arduino15/packages/arduino/hardware/samd/1.8.6/cores/arduino/Arduino.h:36:0,
                 from sketch/nrf24l01_rx_test.ino.cpp:1:
/Users/csbyers/Library/Arduino15/packages/arduino/hardware/samd/1.8.6/cores/arduino/avr/pgmspace.h:106:0: note: this is the location of the previous definition
 #define pgm_read_ptr(addr) (*(const void **)(addr))

In file included from /Users/csbyers/Documents/Arduino/libraries/RF24/RF24.cpp:10:0:
/Users/csbyers/Documents/Arduino/libraries/RF24/RF24_config.h:155:0: warning: "PSTR" redefined
         #define PSTR(x) (x)

In file included from /Users/csbyers/Library/Arduino15/packages/arduino/hardware/samd/1.8.6/cores/arduino/Arduino.h:36:0,
                 from /Users/csbyers/Documents/Arduino/libraries/RF24/RF24_config.h:60,
                 from /Users/csbyers/Documents/Arduino/libraries/RF24/RF24.cpp:10:
/Users/csbyers/Library/Arduino15/packages/arduino/hardware/samd/1.8.6/cores/arduino/avr/pgmspace.h:34:0: note: this is the location of the previous definition
 #define PSTR(str) (str)

In file included from /Users/csbyers/Documents/Arduino/libraries/RF24/RF24.cpp:10:0:
/Users/csbyers/Documents/Arduino/libraries/RF24/RF24_config.h:157:0: warning: "strlen_P" redefined
         #define strlen_P strlen

In file included from /Users/csbyers/Library/Arduino15/packages/arduino/hardware/samd/1.8.6/cores/arduino/Arduino.h:36:0,
                 from /Users/csbyers/Documents/Arduino/libraries/RF24/RF24_config.h:60,
                 from /Users/csbyers/Documents/Arduino/libraries/RF24/RF24.cpp:10:
/Users/csbyers/Library/Arduino15/packages/arduino/hardware/samd/1.8.6/cores/arduino/avr/pgmspace.h:69:0: note: this is the location of the previous definition
 #define strlen_P(a) strlen((a))

In file included from /Users/csbyers/Documents/Arduino/libraries/RF24/RF24.cpp:10:0:
/Users/csbyers/Documents/Arduino/libraries/RF24/RF24_config.h:159:0: warning: "pgm_read_word" redefined
         #define pgm_read_word(p) (*(p))

In file included from /Users/csbyers/Library/Arduino15/packages/arduino/hardware/samd/1.8.6/cores/arduino/Arduino.h:36:0,
                 from /Users/csbyers/Documents/Arduino/libraries/RF24/RF24_config.h:60,
                 from /Users/csbyers/Documents/Arduino/libraries/RF24/RF24.cpp:10:
/Users/csbyers/Library/Arduino15/packages/arduino/hardware/samd/1.8.6/cores/arduino/avr/pgmspace.h:103:0: note: this is the location of the previous definition
 #define pgm_read_word(addr) (*(const unsigned short *)(addr))

In file included from /Users/csbyers/Documents/Arduino/libraries/RF24/RF24.cpp:10:0:
/Users/csbyers/Documents/Arduino/libraries/RF24/RF24_config.h:160:0: warning: "pgm_read_ptr" redefined
         #define pgm_read_ptr(p) (*(p))

In file included from /Users/csbyers/Library/Arduino15/packages/arduino/hardware/samd/1.8.6/cores/arduino/Arduino.h:36:0,
                 from /Users/csbyers/Documents/Arduino/libraries/RF24/RF24_config.h:60,
                 from /Users/csbyers/Documents/Arduino/libraries/RF24/RF24.cpp:10:
/Users/csbyers/Library/Arduino15/packages/arduino/hardware/samd/1.8.6/cores/arduino/avr/pgmspace.h:106:0: note: this is the location of the previous definition
 #define pgm_read_ptr(addr) (*(const void **)(addr))

Also, calling radio.printDetails() just comes back blank, possibly due to the #defines for printf in RF24_config.h. I'm looking into that now, as it might help get things moving forward.

massivespace commented 4 years ago

Never mind. I was powering the Wifi 1010 with 3.3V, since I had read everywhere that it ran at 3.3V. I didn't read the specs like I should have. The Vin pin must be 5V. The onboard regulator drops it to 3.3 for everything. Powering it with 5V and the nRF24 with 3.3V fixed the problem. I have no idea why things weren't working with the USB port plugged in and the serial terminal closed. Call it a fluke for now.

massivespace commented 4 years ago

A bit more testing after I closed this revealed that the 5V did not fix the problem. It boiled down to me calling Serial.begin(). When I removed all calls to Serial, the RF24 code works like a charm. If I put Serial.begin() in setup, even without calling any prints, running purely on battery power will not work until I plug in the USB. I'm not sure where that problem lies, or if it's even to be expected, so I'll keep this closed for now but leave this for others if they come across this.