Sending data from python script to arduino and then arduino to nrf24l01

Hi,

I have been working on a project where I am doing some image processing using python, OpenCV package. After processing, I need to send the data from python script to Arduino connected to the system. From this Arduino, a NRF24L01 is connected which transmits the data to another Arduino which is doing some job.

I have tried the pyserial package and went through lot of different blogs. I can communicate the between the python script when there is no nrf24l01 but when I connect the same, couldn't receive anything on the other end.

Any help on this will be greatly appreciated.
Thank You

Seeing your code and schematics would be helpful

nitinsaini2909:
I have tried the pyserial package and went through lot of different blogs.

The pySerial package is ok, but it’s a pretty low level processing package. I suggest using a Python package designed for communicating with Arduinos specifically: pySerialTransfer. This package will automatically packetize/parse serial data packets between Python and Arduinos.

To install:

pip install pySerialTransfer

Example Python code:

from pySerialTransfer import pySerialTransfer as txfer

if __name__ == '__main__':
    try:
        link = txfer.SerialTransfer('COM13')
    
        link.txBuff[0] = 'h'
        link.txBuff[1] = 'i'
        link.txBuff[2] = '\n'
        
        link.send(3)
        
        while not link.available():
            if link.status < 0:
                print('ERROR: {}'.format(link.status))
            
        print('Response received:')
        
        response = ''
        for index in range(link.bytesRead):
            response += chr(link.rxBuff[index])
        
        print(response)
        link.close()
        
    except KeyboardInterrupt:
        link.close()

The pySerialTransfer library has an Arduino compatible library, SerialTransfer.h. This library also packetizes/parses serial data packets from Python and other Arduinos.

To install:

  • Download and install via the Arduino IDE’s Libraries Manager (search “SerialTransfer”)

Example Arduino code:

#include "SerialTransfer.h"
#include <SoftwareSerial.h>


SoftwareSerial mySerial(2, 3); // RX, TX
SerialTransfer myTransfer;


void setup()
{
  Serial.begin(115200);
  mySerial.begin(9600);
  myTransfer.begin(mySerial);
}

void loop()
{
  myTransfer.txBuff[0] = 'h';
  myTransfer.txBuff[1] = 'i';
  myTransfer.txBuff[2] = '\n';
 
  myTransfer.sendData(3);
  delay(100);

  if(myTransfer.available())
  {
    Serial.println("New Data");
    for(byte i = 0; i < myTransfer.bytesRead; i++)
      Serial.write(myTransfer.rxBuff[i]);
    Serial.println();
  }
  else if(myTransfer.status < 0)
  {
    Serial.print("ERROR: ");
    Serial.println(myTransfer.status);
  }
}

As for the other parts of the project, we’ll have to see your entire codebase (as UKHeliBob said)

Hi,

As suggested I tried the pyserialtrasnfer and serialTransfer.h as u suggested but no help. Also, the python script opens the serial com port for the Arduino connected to the system. So, I cannot see if the data is been received by the same Arduino to the system. but I was transferring data to through nrf to the other Arduino to receive and display it on the system via the serial monitor.

the code I was using earlier on python script was

import serial


trans = serial.Serial("/dev/ttyUSB0", baudrate=9600)

data = "2"
trans.writ

from this code, I was able to transfer the code to the Arduino and toggle the LED on pin 13.

but when I try to transfer the same Through nrf after initializing it, couldn’t receive anything on the other end.

Arduino transmitter code I was using is

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

RF24 radio(9, 10); // CE, CSN         
const byte address[6] = "00001";
//Byte of array representing the address. 
//This is the address where we will send the data. This should be same on the receiving side.

void setup() {
  radio.begin();                  
  radio.openWritingPipe(address); 
  radio.setPALevel(RF24_PA_MIN);  
  radio.stopListening();          
  Serial.begin(9600);
  char data = '3';
}

void loop(){
  char data = '3';
  while(Serial.available() > 0){
    data = Serial.read();
  }
  radio.write(&data, sizeof(data));
}

and code on the receiver end is

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

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

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address);   
  radio.setPALevel(RF24_PA_MIN);       
  radio.startListening();
  Serial.println("starting the serial comm");
}

void loop(){
  if (radio.available()){      
    char data;                 
    radio.read(&data, sizeof(data));
    Serial.println(data);
  }
  delay(1000);
}

Have a look at this Simple nRF24L01+ Tutorial.

Wireless problems can be very difficult to debug so get the wireless part working on its own before you start adding any other features.

The examples are as simple as I could make them and they have worked for other Forum members. If you get stuck it will be easier to help with code that I am familiar with. Start by getting the first example to work

There is also a connection test program to check that the Arduino can talk to the nRF24 it is connected to.

A common problem with nRF24 modules is insufficient 3.3v current from the Arduino 3.3v pin. At least for testing try powering the nRF24 with a pair of AA alkaline cells (3v) with the battery GND connected to the Arduino GND.

...R

nitinsaini2909:
As suggested I tried the pyserialtrasnfer and serialTransfer.h as u suggested but no help.

Can you post the code? The two libraries work flawless for me - I'd be interested in why it isn't working for you.

nitinsaini2909:
but I was transferring data to through nrf to the other Arduino to receive and display it on the system via the serial monitor.

Why not get a USB to TTL converter instead? It's a lot easier than having a second Arduino and radio module...