• Has anyone managed to make this work?

    I'm using the RF24 arduino library from here:
    https://github.com/TMRh20/RF24

    Espruino:

    SPI1.setup({sck:A5, miso:A6, mosi:A7}); // B0, 1 used for other pins.
    
    
    var nrf1 = require("http://localhost/NRF24L01P.min.­js").connect( SPI1, B0, B1, 32);//Arduino library defaults 32 byte packet length. 
    nrf1.init([0xE7,0xE7,0xE7,0xE7,0xE7], [0xE8,0xE8,0xF0,0xF0,0xE1]);
    
    nrf1.setReg(0x05,76); //The arduino library uses channel 76
    
    var test=[222,173,190,239,222,173,190,239,22­2,173,190,239,222,173,190,239];
    var test2=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,­15,16,17,18,19,20,21,22,23,24,25,26,27,2­8,29,30,31,32];
    
    

    My version of the NRF module was modified to change the BASE_CONFIG to use 2-byte CRC, since the arduino library wants that; no other changes.

    Code on Arduino side (hacked up from an example)

    [#include](http://forum.espruino.com/sea­rch/?q=%23include) <SPI.h>
    [#include](http://forum.espruino.com/sea­rch/?q=%23include) "nRF24L01.h"
    [#include](http://forum.espruino.com/sea­rch/?q=%23include) "RF24.h"
    [#include](http://forum.espruino.com/sea­rch/?q=%23include) "printf.h"
    
    //
    // Hardware configuration
    //
    
    // Set up nRF24L01 radio on SPI bus plus pins 9 & 10
    
    RF24 radio(9,10);
    
    
    
    //
    // Topology
    //
    
    // Single radio pipe address for the 2 nodes to communicate.
    const uint64_t pipe = 0xE8E8F0F0E1LL;
    
    uint8_t payload_data[16];
    
    
    //
    // Setup
    //
    
    void setup(void)
    {
      //
      // Role
      //
    
      Serial.begin(57600);
      printf_begin();
      printf("\n\rRF24/examples/led_remote/\n\­r");
    
      //
      // Setup and configure rf radio
      //
    
      radio.begin();
    
      //
      // Open pipes to other nodes for communication
      //
    
      // This simple sketch opens a single pipes for these two nodes to communicate
      // back and forth.  One listens on it, the other talks to it.
    
      
      radio.openReadingPipe(1,pipe);
      radio.startListening();
      radio.printDetails();
    
    
    }
    
    //
    // Loop
    //
    
    void loop(void)
    {
    
      // if there is data ready
      if ( radio.available() )
      {
        // Dump the payloads until we've gotten everything
        bool done = false;
        while (!done)
        {
          // Fetch the payload, and see if this was the last one.
          radio.read( payload_data, 16 );
          done=radio.available();
          // Spew it
          printf("Got payload\n\r");
          int i;
          for (i = 0; i < 16; i = i + 1) {
            Serial.print(payload_data[i]);
          }
        }
      }
    }
    

    Serial output is this - which all looks right:

    STATUS       = 0x0e RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=7 TX_FULL=0
    RX_ADDR_P0-1     = 0xe7e7e7e7e7 0xe8e8f0f0e1
    RX_ADDR_P2-5    = 0xc3 0xc4 0xc5 0xc6
    TX_ADDR      = 0xe7e7e7e7e7
    RX_PW_P0-6  = 0x00 0x20 0x00 0x00 0x00 0x00
    EN_AA        = 0x3f
    EN_RXADDR   = 0x03
    RF_CH       = 0x4c
    RF_SETUP    = 0x07
    CONFIG       = 0x0f
    DYNPD/FEATURE   = 0x00 0x00
    Data Rate    = 1MBPS
    Model        = nRF24L01+
    CRC Length   = 16 bits
    PA Power     = PA_MAX
    

    On the Espruino, I try to do:

    nrf1.send(test2);
    TX not received 30
    =false

    I remember reading a post by someone else doing this - I can't seem to find it now though. I'm wondering if they ever got it working.... The NRF24 should be a great way to do simple communications between an Espruino and one or more less-capable Arduino-like devices tucked into corners, or located outdoors, etc.

  • I'm not sure anyone got it working, which does seem particularly strange. What was the BASE_CONFIG change you made? It'd be good to update the NRF24 initialisation to bring it more in line with Arduino's (and maybe automatically set the channel on startup).

    Just wondering, are the payload sizes the same? Looks like Arduino's is 16 and Espruino's is 32?

  • RX_PW_P1 is 0x20 (32) on arduino, that's 32 byte payload by my understanding? I had tried it with 16 bytes as well.

    My modified version of the module

    function b(a,c,e,b){this.CSN=c;this.CE=e;this.PAY­LOAD=b?b:16;this.cmd="";this.spi=a;this.­callbacks=[]}b.prototype.init=function(a­,c){digitalWrite(this.CE,0);digitalWrite­(this.CSN,1);this.setRXAddr(a);this.setT­XAddr(c);for(var b=17;22>=b;b++)this.setReg(b,this.PAYLOA­D);this.setReg(0,15);this.setReg(4,95);d­igitalWrite(this.CE,1)};b.prototype.setE­nabled=function(a){digitalWrite(this.CE,­a)};b.prototype.setReg=function(a,c){thi­s.spi.send([32|a,c],this.CSN)};b.prototy­pe.setAddr=function(a,c){c=
    c.clone();c.splice(0,0,32|a);this.spi.se­nd(c,this.CSN)};b.prototype.setRXAddr=fu­nction(a,c){void 0===c?c=1:this.setReg(2,this.getReg(2)|1­<<c);this.setAddr(10+c,2>c?a:[a[0]])};b.­prototype.setTXAddr=function(a){this.set­Addr(10,a);this.setAddr(16,a)};b.prototy­pe.getReg=function(a){return this.spi.send([0|a,0],this.CSN)[1]};b.pr­ototype.getAddr=function(a){a=this.spi.s­end([0|a,0,0,0,0,0],this.CSN);a.splice(0­,1);return a};b.prototype.getStatus=function(a){ret­urn this.getReg(7)};b.prototype.setDataRate=­
    function(a){var c={25E4:32,1E6:0,2E6:8};!a in c&&console.log("Unknown rate");this.setReg(6,this.getReg(6)&-41|­c[a])};b.prototype.getDataPipe=function(­){var a=this.getReg(7)&14;return 14==a?void 0:a>>1};b.prototype.getData=function(){f­or(var a=[97],c=0;c<this.PAYLOAD;c++)a.push(0);­a=this.spi.send(a,this.CSN);a.splice(0,1­);this.setReg(7,64);return a};b.prototype.send=function(a){this.set­Reg(7,48);digitalWrite(this.CE,0);this.s­etReg(0,14);this.spi.send(225,this.CSN);­a=a.clone();a.splice(0,0,160);this.spi.s­end(a,
    this.CSN);digitalWrite(this.CE,1);for(a=­1E3;a--&&!(this.getReg(7)&48););0>=a&&pr­int("TX timeout");a=!0;this.getReg(7)&16&&(print­("TX not received "+this.getReg(7)),a=!1);digitalWrite(thi­s.CE,0);this.setReg(0,15);digitalWrite(t­his.CE,1);this.setReg(7,48);return a};b.prototype.slaveHandler=function(){f­or(;void 0!==this.getDataPipe();){var a=this.getData(),c;for(c in a){var b=a[c];if(0===b&&""!==this.cmd){var d=this.cmd;this.cmd="";var f=this;setTimeout(function(){print("...>­"+d);var a=""+eval(d);print("...="+
    a);f.sendStringTimeout(a,500)},1)}else 0!==b&&(this.cmd+=String.fromCharCode(b)­)}}};b.prototype.masterHandler=function(­){for(;void 0!==this.getDataPipe();){var a=this.getData(),c;for(c in a){var b=a[c];if(0===b&&""!==this.cmd){b=this.c­md;this.cmd="";var d=this.callbacks.splice(0,1)[0];void 0!==d&&d(b)}else 0!==b&&(this.cmd+=String.fromCharCode(b)­)}}};b.prototype.sendString=function(a){­for(var c=0;c<=a.length;c+=this.PAYLOAD){for(var­ b=[],d=0;d<this.PAYLOAD;d++)b[d]=a.charC­odeAt(c+d);if(!this.send(b))return!1}ret­urn!0};
    b.prototype.sendCommand=function(a,b){th­is.callbacks.push(b);this.sendString(a)}­;b.prototype.sendStringTimeout=function(­a,b){var e=this;setTimeout(function(){e.sendStrin­g(a)},b)};exports.connect=function(a,c,e­,d){return new b(a,c,e,d)}
    

    BASE_CONFIG was 8, is now 12

    Difference in the minified code is the two setReg(0,11) calls are now setReg(0,15), and setReg(0,10) changed to setReg(0,14)

  • did you manage to link the modules? If yes, is there an example?

  • Fri 2019.11.22

    @kazmikro it appears the minified code shown in #3 is what would be used on the Espruino side using the examples at:

    https://www.espruino.com/NRF24L01P

    The source for Arduino is at the GitHub link provided in #1

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

NRF24 to communicate between Espruino and Arduino

Posted by Avatar for DrAzzy @DrAzzy

Actions