Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Need advice in nRF24l01+ programming (a rather unique project)

Status
Not open for further replies.

swordz60

Newbie level 6
Joined
Mar 17, 2016
Messages
13
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
223
Hi guys,
I'm currently working on a project involving 2 arduinos + 2 LCD screens + 2 GPS modules + 2 nRF24l01 modules. The idea is to create a warning system for trains. It's based on the speed of the locomotive and the speed of the last wagon. The point of that is knowing whether any of the wagons got decoupled (I know that this doesn't happen often, but I work for a railway company and it seemed like an original project for my bachelor's thesis).
I've got both arduinos up and running (they output speed, coordinates, visible satellites on the LCDs), but i can't seem to figure out how to transmit GPS data (in this case speed) from one arduino to the other via the nRF24l01+. I have succeeded flashing an LED and transmitting a text message so far.

TL;DR: How do I transfer real time data from one arduino to another using nRF24l01+?

I'd greatly appreciate your input and I'm sorry if I missed a post on a similar subject.
I'm a newbie in C# language, just a warning. :)


TRANSMITTER:

Code C++ - [expand]
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <SoftwareSerial.h>
#include <TinyGPS.h>
#include <LiquidCrystal.h>
 
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
 
int repeat = 0; 
 
LiquidCrystal lcd(7, 6, 5, 4, 3, 2); // Address used for the 16x2 LCD
RF24 radio(9, 10);   // 9 - CE, 10 - CSN
 
const byte rxAddr[6] = "12345"; 
TinyGPS gps;
SoftwareSerial ss(1, 0); // Define as software serial  mounted as tx and rx 
 
void setup()
{
  ss.begin(9600); // Serial communication at baud rate 9600 GPS to MCU
  lcd.begin(16,2);     
 
  radio.begin();  
  radio.setRetries(20, 10);  
  radio.openWritingPipe(rxAddr);  
  radio.stopListening();  
 
  
  for (int count=0; count <= 5; count++)
  {
    lcd.setCursor(0,0);
    lcd.print("Starting...");
    delay(800);
    lcd.clear();
    delay(800);
  }
 
  lcd.setCursor(0,0);
  lcd.print("Starting GPS..");
  lcd.setCursor(0,1);
  lcd.print("----------------");
  delay(10000);
  lcd.clear();
  
}
 
void loop()
{
  bool data = false;  
  unsigned long chars;    
  unsigned short sentences, failed;  
 
  // One second we parse GPS data and report some key values
  for (unsigned long start = millis(); millis() - start < 1000;) 
  {
    while (ss.available())
    {
      char c = ss.read();
       
      if (gps.encode(c))  
        data = true;  
    }
  }
 
  if (data)           
  {                             
    float flat, flon, speed;
    unsigned long age;        
    gps.f_get_position(&flat, &flon, &age);  
    
   
    if (repeat < 2) 
     { 
      lcd.setCursor(0,0);
      lcd.print("Coordinates:");
       lcd.setCursor(0,1);
       lcd.print(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 4); 
         lcd.setCursor(7,1);
         lcd.print(", ");
         lcd.setCursor(9,1);
         lcd.print(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 4); 
      delay(5000);
      lcd.clear();
      
      repeat++;
     }
    
    lcd.setCursor(0,0);
    lcd.print("Speed: ");
     lcd.setCursor(6,0);
     lcd.print(gps.f_speed_kmph() == TinyGPS::GPS_INVALID_F_SPEED ? 0 : gps.f_speed_kmph(), 1);
      lcd.setCursor(12,0);
      lcd.print("km/h");
        lcd.setCursor(0,1);
        lcd.print("Satellites: ");
         lcd.setCursor(11,1);
         lcd.print(gps.satellites() == TinyGPS::GPS_INVALID_SATELLITES ? 0 : gps.satellites());
 
      if (radio.available()) 
  {
    char rad_spd = (gps.f_speed_kmph() == TinyGPS::GPS_INVALID_F_SPEED ? 0 : gps.f_speed_kmph(), 1); 
    char rad_sats = gps.satellites() == TinyGPS::GPS_INVALID_SATELLITES ? 0 : gps.satellites();  
 
    radio.write(&rad_spd, sizeof(rad_spd));  
    radio.write(&rad_sats, sizeof(rad_sats)); 
    
  }
      
      if (repeat < 2)   
      {       
        delay(2000);
        lcd.clear();
        repeat++;
      }
    
  }
        
  else 
  {   
    gps.stats(&chars, &sentences, &failed);
    lcd.setCursor(0,0);
    lcd.print("Chars: ");
     lcd.setCursor(7,0);
     lcd.print(chars);
       lcd.setCursor(0,1);
       lcd.print("Sentences: ");
        lcd.setCursor(11,1);
        lcd.print(sentences);
     delay(1000);
     lcd.clear();
   }  
}




RECEIVER:


Code C++ - [expand]
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <SoftwareSerial.h>
#include <TinyGPS.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
 
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
 
int repeat = 0; 
 
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Address used for the 20x4 LCD
RF24 radio(9, 10);   // 9 - CE, 10 - CSN
 
const byte rxAddr[6] = "12345";
TinyGPS gps;
SoftwareSerial ss(1, 0); // Define as software serial  mounted as tx and rx 
 
void setup()
{
  ss.begin(9600); // Serial communication at baud rate 9600 GPS to MCU
  lcd.begin(20,4);     
 
  radio.begin();  
  radio.openReadingPipe(0, rxAddr); 
  radio.startListening(); 
  
  for (int count=0; count <= 5; count++)
  {
    lcd.setCursor(0,0);
    lcd.print("Starting...");
    delay(800);
    lcd.clear();
    delay(800);
  }
 
  lcd.setCursor(0,0);
  lcd.print("Starting GPS..");
  lcd.setCursor(0,1);
  lcd.print("----------------");
  delay(10000);
  lcd.clear();  
}
 
void loop()
{
  bool data = false; 
  unsigned long chars;   
  unsigned short sentences, failed;  
 
  for (unsigned long start = millis(); millis() - start < 1000;) 
  {
    while (ss.available())
    {
      char c = ss.read();
       
      if (gps.encode(c))  
        data = true;  
    }
  }
 
  if (data)          
  {
                               
    float flat, flon, speed;
    unsigned long age;       
    gps.f_get_position(&flat, &flon, &age);  
    
   
    if (repeat < 2)
     { 
      lcd.setCursor(0,0);
      lcd.print("Coordinates:");
       lcd.setCursor(0,1);
       lcd.print(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 4); 
         lcd.setCursor(7,1);
         lcd.print(", ");
         lcd.setCursor(9,1);
         lcd.print(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 4); 
      delay(5000);
      lcd.clear();
      
      repeat++;
     }
    
       lcd.setCursor(0,0);
    lcd.print("Speed: ");
     lcd.setCursor(6,0);
     lcd.print(gps.f_speed_kmph() == TinyGPS::GPS_INVALID_F_SPEED ? 0 : gps.f_speed_kmph(), 1);
      lcd.setCursor(12,0);
      lcd.print("km/h");
        lcd.setCursor(0,1);
        lcd.print("Satellites: ");
         lcd.setCursor(11,1);
         lcd.print(gps.satellites() == TinyGPS::GPS_INVALID_SATELLITES ? 0 : gps.satellites());
 
      if (radio.available())  
    char rad_spd[32] = {0};  
    char rad_sats[32] = {0};  
    radio.read(&rad_spd, sizeof(rad_spd));  
    //radio.read(&rad_sats, sizeof(rad_sats));  
    
    lcd.setCursor(0,2);
    lcd.print(rad_spd);  
    //lcd.setCursor(0,3);
    //lcd.print(rad_sats); 
  }
      
      if (repeat < 2)     
      {       
        delay(2000);
        lcd.clear();
        repeat++;
      }
    
  }
 
   else 
   {   
    gps.stats(&chars, &sentences, &failed);
    lcd.setCursor(0,0);
    lcd.print("Chars: ");
     lcd.setCursor(7,0);
     lcd.print(chars);
       lcd.setCursor(0,1);
       lcd.print("Sentences: ");
        lcd.setCursor(11,1);
        lcd.print(sentences);
     delay(1000);
     lcd.clear();
 
   }  
}


P.S. I use 2 LCDs for testing purposes, after the project is complete, there will be only 1 LCD left (the 20x4 one). I will also add some other functions later (comparation of the speeds, a buzzer to warn about the difference in speeds etc.)
 
Last edited by a moderator:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top