SQL+NRF24 and two data "senders"

Hi! My project consists of one base arduino(W5100 shield+NRF24L01) and two wireless arduinos(NRF24L01). Two wireless stations are sending integer array to base station. Then base station pushes data to SQL database. Everything seems to work ok except when two wireless stations sending array at the exactly same time - MySQL table gets 2 pairs of records (instead of 2 rows I got 4).
I think that there is something in base station...
Here is the code of base station

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

RF24 radio(3, 4);
 unsigned long timeNow;
 unsigned long value;
 int Unit_ID;
unsigned long test[3]={timeNow, value, Unit_ID};
const byte rxAddr[6] = {00010};


byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192,168,0,16);
EthernetClient client;
char server[] = "192.168.0.90"; 
void setup()
{ 
Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, rxAddr[0]);
  radio.startListening();
  Ethernet.begin(mac, ip);
}
void loop()
{
  if (radio.available())
 {  
  radio.read(&test, sizeof(test));
 timeNow=test[0];
  value=test[1];
  Unit_ID=test[2];
  if (client.connect(server, 80)) {
    Serial.println("connected");
    client.print("GET /add_data.php?"); 
    client.print("Unit_ID="); 
    client.print("&&");
    client.print("value="); 
    client.print(value); 
    client.print("&&");
    client.print("timeNow=");
    client.print(timeNow);
    client.println(" HTTP/1.1"); 
    client.println("Host: 192.168.0.90"); 
    client.println("Connection: close"); 
    client.println(); 
    client.println(); 
    client.stop();   
    timeNow=0;
    value=0;
    Unit_ID=0;
  }
  else {
    
    Serial.println("--> connection failed\n");
  }  
  }
}

If both transmitters are transmitting on the same radio channel, there is always a risk of a clash.
One possibility is to set the transmitters to send at random (or different) intervals to minimise the risk.
The receiver has to check if a clash has caused some corruption and reject non-plausible results.
Another approach is to set the two transmitters on different channels and the receiver switches at intervals between the two channels.

Another (and IMHO better) solution is to have the base (master) unit poll the other two (slave) units. That way there will never be a collision.

The code in the pair of programs in this link is designed to work like that.

...R

Robin2:
Another (and IMHO better) solution is to have the base (master) unit poll the other two (slave) units. That way there will never be a collision.

The code in the pair of programs in this link is designed to work like that.

...R

I agree that that is a more elegant solution. However, the topology can sometimes be influenced by the requirement to have maximum battery life in the remote transmitter units which may wake up only occasionally to send some sensor readings, then go back in a sleep state. In your simplified implementation of a slave node, the system is permanently checking for instructions. May be this could be improved by an element of synchronisation between the transmitters and receivers to avoid a long window when the slave has the radio switched on, and in a loop, waiting for instructions.

6v6gt:
I agree that that is a more elegant solution. However, the topology can sometimes be influenced by the requirement to have maximum battery life in the remote transmitter units which may wake up only occasionally to send some sensor readings, then go back in a sleep state.

My system was not designed with battery life in mind as it was/is intended to send/receive data every 100 millisecs or so.

If battery life is an issue I think the nRF24 can be set so it wakes up the Arduino.

If the master is the listener and the slaves are sending (occasionally) the slave code should require an acknowledgement and if it does not get it then it should assume there was a collision and delay a random amount before resending. I suspect it would be best if both slaves transmit at the same interval - but not at the same instant. if the random amount is a fraction of the interval it will stagger the transmissions again and there probably won't be another collision for a significant time.

Of course another option is for the master to listen on two different pipes. That should avoid any conflict.

Lots of scope for interesting experiments. :slight_smile:

...R