Notifications
Clear all

Structures, Serialisation, XOR Checksums etc.

91 Posts
7 Users
8 Likes
26.4 K Views
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
Topic starter  

@frogandtoad

In your latest code "print2Digits" function you could change:

From:
    String tString = String(number);
To:
    String tString(number);

Point taken, I love compact code.

 

I guess the following would useful if used in a recursive manner, i.e. to resend the data if sendMyData() comes back false.

bool sendMyData(int outgoingData, uint16_t target){
  RF24NetworkHeader header(target); // (Address where the data is going) 
  return network.write(header, &outgoingData, sizeof(outgoingData)); // Send the data  
 }

 

 

Unfortunately, the following code set my brain to explode mode. ? 

struct SensorStation {
  uint16_t StationID;
  float Temperature;
  float Pressure;
  float Humidity;
  SensorStation(uint16_t sensorid, float temperature, float pressure, float humidity)
    : StationID(sensorid), Temperature(temperature), 
Pressure(pressure), Humidity(humidity) {} }; struct BaseStation : public SensorStation {  uint16_t BaseStationID;// Additional info, we could combine it with checksum if desired byte Length;  byte Checksum;    BaseStation(uint16_t address)    : SensorStation(0, 0.00f, 0.00f, 0.00f),
BaseStationID(address), Length(sizeof(*this)), Checksum(0) {}

 

I won't be employing this just yet, for two reasons

 

  1. If it ain't broke don't fix it,
  2. and I am not quite sure what is going on here or how to interact with this code.

 

This certainly beyond me for the meantime!


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 
Posted by: @pugwash

Point taken, I love compact code.

Me too... I always challenge myself to make it as compact as I can - Your "sendMyData" function should be used to make checks irrespective of application, that's the whole reason the writers gave it a Boolean return value (will get to this checking stuff later).

Posted by: @pugwash

Unfortunately, the following code set my brain to explode mode. ? 

Lol, OK, I'll try to explain a little - Feel free to ask questions about it.

This is inheritance in it's very basic and public form.  In C++, structures are also classes, and the only difference is that structures are public by default, otherwise they are identical as objects - I could use the class keyword, but then I would have to add public internal specifiers to make the data members accessible, otherwise I would need to add a bunch of getters and setters.

In a nutshell, I am still just sending one object, but made up of two classes, essentially extending the functionality of the base class - Here's another example that might be easier to relate to:

#include <Servo>

struct BetterServo : public Servo {
  String ServoID;
  String ServoName;
  BetterServo() {}
  BetterServo(String servoid, String servoname) : ServoID(servoid), ServoName(servoname) {} 
 };

BetterServo MyServo(1, "ShedWindow");
MyServo.attach(3);
MyServo.write(42);

Now we have the all the functionality of the original servo, with identity information added to it, so again, we have extended the functionality of the original servo object to suit our needs... make sense?

It's not to difficult, and you'll love the possibilities it can give you, I'm sure 😉

Cheers!

 


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
Topic starter  

   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@pugwash

Posted by: @pugwash

@frogandtoad

The first thing I have done, before getting into the OOP stuff, is to incorporate the boolean function, which now looks like this. The changes lie between the sections marked with asterisks.

Yes, that's exactly how it should be used, and you just reminded me, that I said earlier I would get back to the checking part.  I'm sure it was a good learning experience to incorporate your own checksum routine, but I was looking through the documentation, and I learn't that there is a built in automated checksum feature of the NRF24L01, and as such can be set to automatically resend messages up to 15 times.

Were you aware of that?
Just a thought and maybe worth looking into.

Cheers!


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
Topic starter  

@frogandtoad

Yes, that's exactly how it should be used, and you just reminded me, that I said earlier I would get back to the checking part.  I'm sure it was a good learning experience to incorporate your own checksum routine, but I was looking through the documentation, and I learn't that there is a built in automated checksum feature of the NRF24L01, and as such can be set to automatically resend messages up to 15 times.

Were you aware of that?

Yes, I was aware of that. But in this sort application, where the data coming back is not controlling anything, I feel that the use of checksums is really "overkill"!

I just left it in there as an example for anyone wanting to use XOR checksums!


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
Topic starter  

@frogandtoad

Yes, that's exactly how it should be used

And this proves that it is working!

serial monitor

   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@pugwash

OK, no worries... just wanted to check if you knew of it so you don't try to reinvent the wheel 🙂

Happy coding!

 


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 
Posted by: @pugwash

And this proves that it is working!

 

serial monitor

Excellent - We'll end up refactoring this code by 50% 😛

Update:

String print2digits(const int number){
  String tString(number);
  return (tString.length() < 2) ? ("0" + tString) : tString;
 }

   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
Topic starter  

@frogandtoad

Despite enabling CRC, I am still getting checksum errors! But only from one remote sensor, i.e. the sensor that is furthest away when talking about signal strength. When looking at the line of sight, the closest sensor is only one wall and a ceiling away, but the furthest has four walls between it and the local receiver.

Just to be clear, I am only using breakout boards with an onboard antenna, I haven't unpacked the units with stubby antennas yet!

Guess I will start playing about with the PA level and if I must, reduce the system-wide data rate to 250 kbit/s.

I have already connected the nrf24l01 to the output voltage of a 3.3V voltage regulator instead of the Nano 3.3V output, so I don't believe I should have power issues setting the PA to MAX.

And for anyone else following this thread, the is the best resource I have found yet, on the RF24 library.

https://maniacbug.github.io/RF24/index.html


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@pugwash

Posted by: @pugwash

Despite enabling CRC, I am still getting checksum errors!

Hard to say why that may be happening, but it could be noisy channel selection or other factors.

Certainly you could play around with PA level and other settings to narrow down any encountered bottlenecks.

Interesting to see what you find out in your experiments.


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
Topic starter  

@frogandtoad

I messed around with PA level and data rates, but with no noticeable improvement.

I ran a scanner sketch for about an hour and found that most of the traffic was in the lower half of the 2.4 to 2.5GHz range. That is why I had set the channel to 0x5a(90) after checking local Radio Regs for ISM bands.

Moving the remote sensor closer i.e. one less wall to penetrate, significantly reduces the number of checksum errors.

My conclusion is that I must be close to the limit that I can expect from an nrf24l01 module without an external antenna!


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@pugwash

Posted by: @pugwash

My conclusion is that I must be close to the limit that I can expect from an nrf24l01 module without an external antenna!

Quite possible... I have the one with the antenna, which is supposed to theoretically have a range of ~1km:

nrf24l01

 


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
Topic starter  

@frogandtoad

That's odd, mine still look like this! ? ? ? 

IMG 4248

Haven't removed the condom yet!


   
ReplyQuote
Page 5 / 7