Websocketserver to NRF24L01. Strings, pointers and char arrays.

Hello Everyone,

I am tired of racking my brains on this one. Maybe someone can tell me what to do here.

I am receiving a string from the Websocket Client programmed in Android on my WebSocket server in NodeMCU. I want to transfer the string via NRF24L01 to An Arduino Nano.

Android u[/u]--> NodeMCU u[/u]--> Arduino Nano.

Here is my code.

/**
 * NodeMCU as a WebSocket Client. 
 * 
 */

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WebSocketsServer.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <SPI.h>
#include <Hash.h>

#define CE D8
#define CSN D4

RF24 radio(CE,CSN);
const byte address[6] = "00001";

ESP8266WebServer server(80);
WebSocketsServer webSocket = WebSocketsServer(81);


void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {

    switch(type) {
        case WStype_DISCONNECTED:
            Serial.printf("[%u] Disconnected!\n", num);
            break;
        case WStype_CONNECTED: {
            IPAddress ip = webSocket.remoteIP(num);
            Serial.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);

            // send message to client
            webSocket.sendTXT(num, "Connected");
            }
            break;

// This is where I am facing some issues. 

        case WStype_TEXT:
            IPAddress ip = webSocket.remoteIP(num);
            Serial.printf("%s?\n", payload); 
            char char_array[sizeof(payload)] = ; 
            payload.toCharArray(char_array, length);
            //webSocket.sendTXT(num, payload,sizeof(payload),false);
            radio.write(char, length);
            break;
    }
}

void setup() {
    Serial.begin(115200);
    Serial.println();
    Serial.println();

    for(uint8_t t = 4; t > 0; t--) {
        Serial.printf("[SETUP] BOOT WAIT %d...\n", t);
        Serial.flush();
        delay(1000);
    }

   
    Serial.println("Starting AP");
    WiFi.mode(WIFI_AP);
    WiFi.softAP("NodeMCU Access Point","12345678");
    Serial.println("AP Started");

    radio.begin();
    radio.openWritingPipe(address);
    radio.setPALevel(RF24_PA_MIN);
    radio.stopListening();   
    
    Serial.println("Starting Socket");

    // start webSocket server
    webSocket.begin();
    webSocket.onEvent(webSocketEvent);
    Serial.println("Socket Started");

    if(MDNS.begin("simcontrol")) {
        Serial.println("MDNS responder started");
    }
    Serial.println("Starting Server");
    //handle index
    server.on(
      "/", []() {
        server.send(200,"text/plain","You are connected");
    });

    server.begin();
    Serial.println("Server Started");

    //Add service to MDNS
    MDNS.addService("http", "tcp", 80);
    MDNS.addService("ws", "tcp", 81);

}

void loop() {
    webSocket.loop();
    server.handleClient();
}

In the case WStype_TEXT: I am unable to get the string/char array properly formatted to work. I believe I need to send a char array to the Radio.write(). How do I convert the string at payload to the char array required by Radio.write()??

I was never too good at Strings and Char but the Pointers are completely messing me up.

Please help!!!

Regards.

sparishwad:
I believe I need to send a char array to the Radio.write().

You can send any datatype using an nRF24 provided the total length of the message does not exceed 32 bytes. If you want the message to contain different datatypes you need to create a struct.

Be careful to check that the datatypes used on the NodeMCU match the types used on the Arduino.

...R
Simple nRF24L01+ Tutorial

Hello Robin,

thank you soo much for your reply and your tutorial. I tried out the simple one way transmission program and I am not able to make it work satisfactorily. Here are the issues. I see very intermittent transmission.

NodeMCU Tx:

SimpleTx Starting
Data Sent Message 0  Tx failed
Data Sent Message 0  Tx failed
Data Sent Message 0  Acknowledge received
Data Sent Message 1  Tx failed

Data Sent Message 1  Tx failed
Data Sent Message 1  Acknowledge received
Data Sent Message 2  Tx failed
Data Sent Message 2  Acknowledge received
Data Sent Message 3  Acknowledge received
Data Sent Message 4  Tx failed
Data Sent Message 4  Tx failed

Arduino Nano Rx:

SimpleRx Starting
Data received Message 1
Data received
Data received
Data received
Data received Message 0
Data received
Data received Message 0
Data received
Data received
Data received
Data received Message 1
Data received
Data received Message 1
Data received
Data received
Data received
Data received Message 2
Data received
Data received
Data received
Data received Message 3
Data received
Data received Message 4
Data received
Data received Message 4
Data received

So I looked at your reply #29. Here are the results of that program:

NodeMCU Tx:

CheckConnection Starting

FIRST WITH THE DEFAULT ADDRESSES after power on
  Note that RF24 does NOT reset when Arduino resets - only when power is removed
  If the numbers are mostly 0x00 or 0xff it means that the Arduino is not
     communicating with the nRF24

STATUS           = 0x0e RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=7 TX_FULL=0
RX_ADDR_P0-1     = 0x4141417852 0x4141417852
RX_ADDR_P2-5     = 0xc3 0xc4 0xc5 0xc6
TX_ADDR          = 0x4141417852

RX_PW_P0-6       = 0x20 0x20 0x00 0x00 0x00 0x00
EN_AA            = 0x3f
EN_RXADDR        = 0x03
RF_CH            = 0x4c
RF_SETUP         = 0x07
CONFIG           = 0x0e
DYNPD/FEATURE    = 0x00 0x00
Data Rate        = 1MBPS
Model            = nRF24L01+
CRC Length       = 16 bits
PA Power         = PA_MAX

AND NOW WITH ADDRESS AAAxR  0x41 41 41 78 52   ON P1
 and 250KBPS data rate

STATUS           = 0x0e RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=7 TX_FULL=0
RX_ADDR_P0-1     = 0x4141417852 0x4141417852

RX_ADDR_P2-5     = 0xc3 0xc4 0xc5 0xc6
TX_ADDR          = 0x4141417852
RX_PW_P0-6       = 0x20 0x20 0x00 0x00 0x00 0x00
EN_AA            = 0x3f
EN_RXADDR        = 0x03
RF_CH            = 0x4c
RF_SETUP         = 0x27
CONFIG           = 0x0e
DYNPD/FEATURE    = 0x00 0x00
Data Rate        = 250KBPS
Model            = nRF24L01+
CRC Length       = 16 bits
PA Power         = PA_MAX

Arduino Nano Rx:

CheckConnection Starting

FIRST WITH THE DEFAULT ADDRESSES after power on
  Note that RF24 does NOT reset when Arduino resets - only when power is removed
  If the numbers are mostly 0x00 or 0xff it means that the Arduino is not
     communicating with the nRF24

STATUS           = 0x0e RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=7 TX_FULL=0
RX_ADDR_P0-1     = 0xe7e7e7e7e7 0x4141417852
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        = 0x02
RF_CH            = 0x4c
RF_SETUP         = 0x07
CONFIG           = 0x0e
DYNPD/FEATURE    = 0x00 0x00
Data Rate        = 1MBPS
Model            = nRF24L01+
CRC Length       = 16 bits
PA Power         = PA_MAX


AND NOW WITH ADDRESS AAAxR  0x41 41 41 78 52   ON P1
 and 250KBPS data rate

STATUS           = 0x0e RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=7 TX_FULL=0
RX_ADDR_P0-1     = 0xe7e7e7e7e7 0x4141417852
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        = 0x02
RF_CH            = 0x4c
RF_SETUP         = 0x27
CONFIG           = 0x0e
DYNPD/FEATURE    = 0x00 0x00
Data Rate        = 250KBPS
Model            = nRF24L01+
CRC Length       = 16 bits
PA Power         = PA_MAX

Could this have anything to do with the different clock speeds of the two controllers or something like that?

I have no experience of using an nRF24 with a NodeMCU (or any other ESP8266).

Do you have another regular Arduino that you could use to confirm that the nano is working properly?

Do you another NodeMCU to see if nRF24 communication between two of them works OK ?

...R

Hello Robin,

Since I had only one Mini USB cable, I had to patch together a power supply for my second Arduino nano. I think I saw a slight uptick in the received Messages and the acknowledgements. I believe it is a low current issue. However, i do not have the required capacitors at home to test the theory at the moment. I will acquire some capacitors in the upcoming days.

If some other issue comes up, I will get back to you again. For the moment. Thanks.

Kind Regards