SHARE
TWEET

Client

Irfandy Jul 20th, 2014 320 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Project: nRF905 AVR/Arduino Library/Driver
  3.  * Author: Zak Kemble, contact@zakkemble.co.uk
  4.  * Copyright: (C) 2013 by Zak Kemble
  5.  * License: GNU GPL v3 (see License.txt)
  6.  * Web: http://blog.zakkemble.co.uk/nrf905-avrarduino-librarydriver/
  7.  */
  8.  
  9. /*
  10.  * Turn the nRF905 on to transmit some data, wait for a reply,
  11.  * turn off and wait for a second.
  12.  * Output power is set to the lowest setting, receive sensitivity is
  13.  * lowered and uses the power up/down feature of the nRF905.
  14.  *
  15.  * 7 -> CE
  16.  * 8 -> PWR
  17.  * 9 -> TXE
  18.  * 2 -> CD
  19.  * 3 -> DR
  20.  * 10 -> CSN
  21.  * 12 -> SO
  22.  * 11 -> SI
  23.  * 13 -> SCK
  24.  */
  25.  
  26. #include <nRF905.h>
  27. #include <SPI.h>
  28.  
  29. #define RXADDR {0xFE, 0x4C, 0xA6, 0xE5} // Address of this device (4 bytes)
  30. #define TXADDR {0x58, 0x6F, 0x2E, 0x10} // Address of device to send to (4 bytes)
  31.  
  32. #define TIMEOUT 1000 // 1 second ping timeout
  33.  
  34. void setup()
  35. {
  36.         // Start up
  37.         nRF905_init();
  38.        
  39.         // Set address of this device
  40.         byte addr[] = RXADDR;
  41.         nRF905_setRXAddress(addr);
  42.  
  43.         // Lowest transmit level -10db
  44.         nRF905_setTransmitPower(NRF905_PWR_n10);
  45.        
  46.         // Reduce receive sensitivity to save a few mA
  47.         nRF905_setLowRxPower(NRF905_LOW_RX_ENABLE);
  48.  
  49.         // Put into receive mode
  50.         nRF905_receive();
  51.  
  52.         Serial.begin(9600);
  53.        
  54.         Serial.println(F("Client started"));
  55. }
  56.  
  57. void loop()
  58. {
  59.         static byte counter;
  60.  
  61.         // Make data
  62.         char data[NRF905_MAX_PAYLOAD] = "test test";
  63.        
  64.  
  65.         // Turn on module
  66.         nRF905_powerUp();
  67.  
  68.         unsigned long startTime = millis();
  69.  
  70.         // Set address of device to send to
  71.         byte addr[] = TXADDR;
  72.         nRF905_setTXAddress(addr);
  73.  
  74.         // Set payload data
  75.         nRF905_setData((byte*)data, sizeof(data));
  76.  
  77.         // Send payload (send fails if other transmissions are going on, keep trying until success)
  78.         while(!nRF905_send());
  79.  
  80.         // Put into receive mode
  81.         nRF905_receive();
  82.  
  83.         // Make buffer for reply
  84.         char buffer[NRF905_MAX_PAYLOAD];
  85.         bool success;
  86.  
  87.         // Wait for reply with timeout
  88.         unsigned long sendStartTime = millis();
  89.         while(1)
  90.         {
  91.                 success = nRF905_getData(buffer, sizeof(buffer));
  92.                 if(success)// Got data
  93.                         break;
  94.  
  95.                 // Timeout
  96.                 if(millis() - sendStartTime > TIMEOUT)
  97.                         break;
  98.         }
  99.  
  100.         // Turn off module
  101.         nRF905_powerDown();
  102.  
  103.         if(success)
  104.         {
  105.                 unsigned int totalTime = millis() - startTime;
  106.                 Serial.print(F("Ping time: "));
  107.                 Serial.print(totalTime);
  108.                 Serial.println(F("ms"));
  109.  
  110.                 // Printout ping contents
  111.                 Serial.print(F("Data from server: "));
  112.                 nRF905_getData(buffer, sizeof(buffer));
  113.                 if(strcmp((char*)buffer, "test test") == 0)
  114.  
  115.                 Serial.println("OK");
  116.  
  117.                 else
  118.  
  119.                 Serial.println("Fail");
  120.         }
  121.         else
  122.                 Serial.println(F("Ping timed out"));
  123.  
  124.         delay(1000);
  125. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top