Go Down

Topic: CODE For a Wireless Digital Communication System for Remote Data Processing. (Read 1 time) previous topic - next topic

Allo_Cedric

Does anyone understand how this project cab be implemented in Arduino studio?

The code especially Help !!!!

Consider a wireless data communications network made up of one local node (

zwieblum

What about getting your books out and read the theory first? Might be an enlightening experience!

6v6gt

You have been given an exercise related to least cost routing.
You type 21 bytes into a system. The first 9 bytes are the payload to be sent. The remaining 12 bytes are a 'routing cost' table. You have to analyse the routing cost table to determine which node should receive the payload. Sounds interesting. Let's hope that none of the parameters RN,d or u exceed 1 byte in length.

If by Arduino Studio you mean the Arduino development environment, you can certainly write the code there, but to test it you also need the appropriate hardware (e.g. arduino uno and any wireless devices you need to set up a network)

Allo_Cedric

Good day,
 

For the hardware after spending quite some time researching, I would liek to use the Arduino with the NRF24L01

My problem is only basic knowledge with Arduino developemnt environment so i dont know how how i would write the code for this

6v6gt

Maybe this assignment specifies what hardware you should use.

Anyway, for inputting data, look at this tutorial https://forum.arduino.cc/index.php?topic=288234.0

And for the NRF24L01, see this: https://forum.arduino.cc/index.php?topic=421081.0

My suggestion is that you start with the NRF24L01 and demonstrate that you can send a short test message from 1 Arduino to another. That way you can at least demonstrate that you have done something.

Allo_Cedric

@6v6gt

Up to this far do you understand what needs to be done though ?

thanks for the suggested links i looked them up but i had already looked at examples in the forum
Iam able to type in a string of data which is stored in an array but now im stuck trying to transmit it so that it can be displayed in the serial monitor of the receiver.


This is my code to type in and save in an array
but i want to transmit whatever is stored in my array to the receiver

the problem seems to be at the line i have highlighted

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

RF24 radio(9, 10); // CE, CSN
const byte address[6] = "00001";
const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

void setup() {
    Serial.begin(9600);
    Serial.println("<Arduino is ready>");
    radio.begin();
    radio.openWritingPipe(address);
    radio.setPALevel(RF24_PA_MIN);
    radio.stopListening();
}

void loop() {
    recvWithEndMarker();
    showNewData();
  const char text[32] ={receivedChars};
  radio.write(&text, sizeof(text));
  delay(1000);
}

void recvWithEndMarker() {
    static byte ndx = 0;
    char endMarker = '\n';
    char rc;
   
while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();
 if (rc != endMarker) {
            receivedChars[ndx] = rc;
            ndx++;
            if (ndx >= numChars) {
                ndx = numChars - 1;
            }
        }
        else {
            receivedChars[ndx] = '\0'; // terminate the string
            ndx = 0;
            newData = true;
        }
    }
}
void showNewData() {
    if (newData == true) {
        Serial.print("This just in ... ");
        Serial.println(receivedChars);
        newData = false;
    }
}

6v6gt

Instead of this:

Code: [Select]

void loop() {
    recvWithEndMarker();
    showNewData();
    const char text[32] ={receivedChars};
    radio.write(&text, sizeof(text));
    delay(1000);
}


try:
Code: [Select]

void loop() {
    recvWithEndMarker();
    showNewData();
    // const char text[32] ={receivedChars};
    radio.write(receivedChars, sizeof(receivedChars));
    delay(1000);
}


Allo_Cedric

So iam able with your help to store the 21 characters in an array in the Local Node (First Arduino)

what i have to do next i cant quite put it in code

i have 21 characters 9 out of this will represent my student number the remaining 12 will be as follows

1,x1,x2,2,x3,x4,3,x5,x6 and 4,x7,x8

1,2,3 an 4 are the IDS For nodes RN1,RN2, RN3 and RN4

x1,x2,x3,x4,x5,x6,x7 and x8, are any numbers between 0, 9

For RN1 = X1+X2 =?
      RN2 = X3+X4 =?
      RN3 = X5+X6 =? and lastly
      RN4 = X7+X8 =?

so the addition of these if the answer is least out of all these calculations
the LN will send the first 9 bytes to this node
so we want to only trasnmit throught RN3 but if RN1 gets the least then we dont trasmit anything other than the name of the node but if RN3 is the least then we transmit the first 9 bytes add each individidually so if the first 9 bytes is 213141312 then once weve sent the data to rn3, rn3 will add 2+1+3+1+4+1+3+1+2 = get the answer and transmit it back to LN (first arduino) and this result will be displayed in the serial monitor


no i understand the logic i just cant put it into code for example how do i differentiate between nodes and do the calculations for each to find out which node is the least cost ?

6v6gt

To send only the first 9 characters in the buffer to the receiver, you can do this:

radio.write( receivedChars, 9 ) ;  // send the  chars 0 to 8 in the buffer

The rest of the buffer contains the routing cost information.

To address for example d1 (x11 in the picture in your first post) in the buffer receivedChars , you can use receivedChars[ 10 ]. It is 10 here and not 11 because the array subscripts in C++ start at 0 (not 1).

However, that gives you a character. To do arithmetic with it, you need it to be an integer. The conversion is like this:

int d1 = receivedChars[ 10 ] - 48 ; // convert character representing a number '0' to '9' into an int.
and carry on . . .
int u1 = receivedChars[ 11 ] - 48 ;
etc.
once you have all the individual values, you can do the routing cost calculation.

There are also more advanced ways of doing this with structs, pointer manipulation etc.


Allo_Cedric

I was  able to get the code working

my next question would be how do i add parity to my code ?

so that i would be able to detect if my code has any errorsbut most importantly if someone was to test my code how would he know that i have implemented parity 

6v6gt

Well, you could argue that the NRF24L01 already applies CRC checking to the payload so your own parity checking is not necessary.

I suppose, since you are using the datatype char to represent the data and, therefore, only 7 bits are effectively in use, you could assign the chars to bytes and use the high order bit as a parity bit (set it say to 1 if there are an odd number of 1s in the remaining 7 bits, otherwise set it to 0. Check it at the other end, drop the parity bit and undo the datatype conversion.

Someone testing who was only checking that a payload got from A to B would be unaware of any partiy checking, if all went well and the data arrived. However, correctly implemented, there would be special handling of parity failures (automatic request of transmission etc.) which add significantly to the complexity.

Allo_Cedric

 if((sum_total[0]<sum_total[1])&&(sum_total[0]<sum_total[2])&&(sum_total[0]<sum_total[3]))
        {
          delay(500);
          Serial.print("\nThe lowest cost is ");
          Serial.print(sum_total[0]);
          Serial.print(" which is linked to RN1");
         
          P=0;
          }
        if((sum_total[1]<sum_total[0])&&(sum_total[1]<sum_total[2])&&(sum_total[1]<sum_total[3])){
          delay(500);
          Serial.print("\nThe lowest cost is ");
          Serial.print(sum_total[1]);
          Serial.print(" which is linked to RN2");
         
          P=0;
          }
        if((sum_total[3]<sum_total[0])&&(sum_total[3]<sum_total[1])&&(sum_total[3]<sum_total[2])){
          delay(500);
          Serial.print("\nThe lowest cost is ");
          Serial.print(sum_total[3]);
          Serial.print(" which is linked to RN4");
         
          P=0;
          }
        if((sum_total[0]==sum_total[1])&&(sum_total[0]<sum_total[2])&&(sum_total[0]<sum_total[3])){
          delay(500);
          Serial.print("\nThe lowest costs are ");
          Serial.print(sum_total[0]);
          Serial.print(" and ");
          Serial.print(sum_total[1]);
          Serial.print(" linked to RN1 and RN2 respectively");
}



when i do the above code it displays
 -> The lowest cost is 2 which is linked to RN1
 -> The lowest costs are 2 and 8 linked to RN1 and RN4 respectively

now if statements 1,2,3 work as i expect them to but 4 not so much

when if statement 4 is executed  FOR EXAMPLE if RN1 is equal to RN4 but lower than RN3 and RN2 i want the serial monitor to show that RN1 and RN4 are equal and are least without showing anything else
and if they are not equal or if they are not the least nothing should happen


im struggling with that


6v6gt

I don't think you can do it like that, and even if you succeeded for 4 students, it would create a huge mess if you then had to change it later to include 5 students. Not only that, but at the moment, you have only considered the case where there is 1 clear 'winner'. You are now attempting to cover a '2 winner' case. However, there could be 3 equal best or even all 4 could be equal.

I suggest that you do it like this to create a sorted list

Code: [Select]


  int rank[ 4 ] ;
  . . .
  . . .

  // load rank array
  for ( int i = 0 ; i < 4 ; i++ ) {
    rank[ i ] = i ;
  }

  // repeat 4 times - assumes worst case of a completely inverted initial sort order
  for ( int j = 0 ; j < 4 ; j ++ ) {
    // exchange sort
    for ( int i = 0 ; i < 3 ; i++ ) {
      if ( sum_total[ rank[ i + 1 ] ] < sum_total[ rank[ i ] ] ) {
        // exchange rank [ i ] and rank [ i + 1 ]
        int iTemp = rank[ i ] ;
        rank[ i ] = rank[ i + 1 ] ;
        rank[ i + 1 ] = iTemp ;
      }
    }
  }

  // Now rank[] contains a sorted list where rank[0] is linked to the RN with the lowest cost.
  // Of course there could be multiple "winners" occupying the lower
  // end of the array.

  Serial.println("\nThe lowest cost(s): ") ;
  for ( int i = 0 ; i < 4 ; i++ ) {
    if ( sum_total[ rank[ i ] ] > sum_total[ rank[ 0 ] ] ) break ; // stop as soon as all the "winners" have been printed.
    Serial.print(sum_total[ rank[ i ] ] );
    Serial.print(" which is linked to RN");
    Serial.println( rank[ i ] + 1 ) ;
  }

Go Up