Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
tutorials_radio [2016/02/24 06:00]
admin created
tutorials_radio [2016/11/25 17:46] (current)
admin
Line 1: Line 1:
 ===== Radio Transmission ===== ===== Radio Transmission =====
  
-We will use the [[https://​code.google.com/​p/​zigduino-radio/​|ZigduinoRadio]] ​library ​in order to send data between two or more Sparrow nodes. ​You can download the library ​from {{::zigduinoradio_201111130010.zip|here}}.+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 {{::sparrowradio.zip|here}}.
  
-Here's an example code: +To install the library, just unzip it to your Arduino\libraries folder or use Arduino IDE'​s ​Library Installer. 
 + 
 +The API for interfacing with the Atmega128RFA1 radio is derived from [[https://​learn.sparkfun.com/​tutorials/​atmega128rfa1-dev-board-hookup-guide/​example-code|Sparkfun'​s implementation]]. 
 + 
 +Here is a code example of two nodes communicating via the protocol. You will need two Sparrow sensor nodes: the first one acts as a sender and the second one will receive the data. 
 + 
 +The code from the Sender runs on a stand-alone Sparrow node, while the Receiver needs to be connected to a computer in order to view the received data on the Serial Terminal. 
 + 
 +<​imgcaption protocol | Simple setup for testing the transmission protocol>​{{ :​protocol.jpg?​700 |}}</​imgcaption>​ 
 + 
 + 
 +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). 
 + 
 +You can send virtually any data between the two nodes, as long as the code on each node uses the same data structure. The protocol sends the structure as a whole, so it is important that all variables in the structure are defined in the same order and have the same data types both at the sender and at the receiver. 
 + 
 +There is a very simple data error detection mechanism implemented as a checksum byte obtained by XOR-ing all of the bytes in a data frame. Received data frames that have the wrong checksum are automatically dropped. It is possible to also add a more reliable CRC check, but from what I have seen, the XOR checksum does a decent job. 
 + 
 +The Transmitter ​code:
  
 <code C> <code C>
-/*+#include "​SparrowTransfer.h"​
    
-Run this sketch on two Zigduinos, open the serial monitor at 9600 baud +//create object 
-and type in stuff. Watch the Rx Zigduino output what you've input into +SparrowTransfer ST; 
-the serial port of the Tx Zigduino.+
    
-*/+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; 
 +};
    
-#include <​ZigduinoRadio.h>​+//give a name to the group of data 
 +SEND_DATA_STRUCTURE mydata;
    
-void setup()+void blinkLED() //blinks the LED
 { {
-  ​ZigduinoRadio.begin(11); +  ​digitalWrite(8,LOW); 
-  Serial.begin(9600);​+  ​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;
    
-  ZigduinoRadio.attachError(errHandle);​ 
-  ZigduinoRadio.attachTxDone(onXmitDone);​ 
 } }
    
-void loop() +void loop(){
-{ +
-  if (Serial.available()) +
-  { +
-    ZigduinoRadio.beginTransmission();​+
    
-    Serial.println();​ +  mydata.data++;
-    Serial.print("​Tx:​ ");+
    
-    while(Serial.available()) +  //send the data 
-    { +  ST.sendData(); 
-      char c = Serial.read(); +  ​blinkLED();
-      ​Serial.write(c); +
-      ZigduinoRadio.write(c);​ +
-    }+
    
-    Serial.println(); +  delay(1000); 
 +
 +</​code>​ 
 + 
 +And the Receiver code: 
 + 
 +<code C> 
 +#include "​SparrowTransfer.h"​
    
-    ZigduinoRadio.endTransmission();​ +//create object 
-  }+SparrowTransfer ST; 
    
-  if (ZigduinoRadio.available()) +struct RECEIVE_DATA_STRUCTURE{ 
-  ​{ +  ​//put your variable definitions here for the data you want to send 
-    ​Serial.println();​ +  //​THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO 
-    ​Serial.print("​Rx:​ ");+  ​uint16_t data;
    
-    while(ZigduinoRadio.available()) +}; 
-      ​Serial.write(ZigduinoRadio.read());+//give a name to the group of data 
 +RECEIVE_DATA_STRUCTURE mydata;
    
-    Serial.println();​ +uint16_t old_indexreceived_indexlost;
-    Serial.print("​LQI:​ "); +
-    Serial.print(ZigduinoRadio.getLqi()10); +
-    Serial.print("​RSSI: "); +
-    Serial.print(ZigduinoRadio.getLastRssi(),​ 10); +
-    Serial.print("​ dBm, ED: "); +
-    Serial.print(ZigduinoRadio.getLastEd(),​ 10); +
-    Serial.println("​dBm"​); +
-  }+
    
 +void setup(){
 +  Serial.begin(9600);​
 + 
 +  //start the library, pass in the data details  ​
 +  ST.begin(details(mydata));​
 + 
 +  pinMode(11, OUTPUT);
 +  digitalWrite(11,​ HIGH); ​
    
-  delay(1000);​ 
 } }
    
-void errHandle(radio_error_t err)+void blinkLED()
 { {
-  ​Serial.println(); +  ​digitalWrite(11, LOW); 
-  ​Serial.print("​Error:​ "); +  ​delay(20); 
-  ​Serial.print((uint8_t)err10); +  ​digitalWrite(11HIGH);  ​
-  Serial.println();​+
 } }
    
-void onXmitDone(radio_tx_done_t x) +void loop(){ 
-+  ​//check and see if a data packet has come in.  
-  Serial.println(); +  if(ST.receiveData()){ 
-  Serial.print("​TxDone: "); +  
-  Serial.print((uint8_t)x10); +    blinkLED();​ 
-  Serial.println();​+  
 +    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);
 } }
 </​code>​ </​code>​