This is an old revision of the document!
Radio Transmission
We will use the SparrowTransfer library to establish a radio link between two Sparrow nodes. The class implements a protocol over some bsic transmit and receive functions and can be downloaded from here.
To install the library, just unzip it to your Arduino\libraries folder
We have below an example of two Arduino sketches that allow transmitting and receiving data packets over the radio interface. You will need two nodes for this example, one of them will act as a transmitter (Tx), and the second one will receive the data the first node is sending (Rx).
The Transmitter code:
#include "SparrowTransfer.h" //create object SparrowTransfer ST; struct SEND_DATA_STRUCTURE{ //put your variable definitions here for the data you want to send //THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO uint16_t data; }; //give a name to the group of data SEND_DATA_STRUCTURE mydata; void blinkLED() //blinks the LED { digitalWrite(8,LOW); delay(20); digitalWrite(8,HIGH); } void setup(){ Serial.begin(9600); //start the library, pass in the data details ST.begin(details(mydata)); pinMode(8, OUTPUT); digitalWrite(8, LOW); mydata.data = 0; } void loop(){ mydata.data++; //send the data ST.sendData(); blinkLED(); delay(1000); }
And the Receiver code:
#include "SparrowTransfer.h" //create object SparrowTransfer ST; struct RECEIVE_DATA_STRUCTURE{ //put your variable definitions here for the data you want to send //THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO uint16_t data; }; //give a name to the group of data RECEIVE_DATA_STRUCTURE mydata; uint16_t old_index, received_index, lost; void setup(){ Serial.begin(9600); //start the library, pass in the data details ST.begin(details(mydata)); pinMode(11, OUTPUT); digitalWrite(11, HIGH); } void blinkLED() { digitalWrite(11, LOW); delay(20); digitalWrite(11, HIGH); } void loop(){ //check and see if a data packet has come in. if(ST.receiveData()){ blinkLED(); received_index++; if(old_index != 0) lost += mydata.data - old_index - 1; Serial.print("Frame arrived: "); Serial.print(mydata.data); Serial.print(" "); Serial.print(", lost: "); Serial.print(lost); Serial.print(", loss: "); Serial.print(lost*100.0/(lost+received_index), 3); Serial.println("%"); old_index = mydata.data; } //you should make this delay shorter than your transmit delay or else messages could be lost delay(250); }