Advertisement
Guest User

RaspberryPi_Mailbox_Receiver

a guest
Mar 22nd, 2015
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.08 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <sstream>
  4. #include <string>
  5. #include "./RF24.h"
  6. #include <ctime>
  7.  
  8.  
  9. using namespace std;
  10. //
  11. // Hardware configuration
  12. // Configure the appropriate pins for your connections
  13.  
  14. /****************** Raspberry Pi ***********************/
  15.  
  16. // Setup for GPIO 15 CE and CE0 CSN with SPI Speed @ 8Mhz
  17. RF24 radio(RPI_V2_GPIO_P1_15, RPI_V2_GPIO_P1_24, BCM2835_SPI_SPEED_8MHZ);
  18.  
  19. // Radio pipe addresses for the 2 nodes to communicate.
  20. //const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
  21. const uint64_t pipes[1] = { 0xCAFECAFEA1LL };
  22.  
  23. const int max_payload_size = 32;
  24. char receive_payload[max_payload_size+1]; // +1 to allow room for a terminating NULL char
  25.  
  26. // Time
  27. time_t rawtime;
  28. struct tm* timeinfo;
  29. char time_buffer[80];
  30.  
  31. void update_time(char* time_buffer) {
  32.     time(&rawtime);
  33.     timeinfo = localtime(&rawtime);
  34.     strftime(time_buffer,80,"%d-%m-%Y %I:%M:%S",timeinfo);
  35. }
  36.  
  37. int main(int argc, char** argv) {
  38.  
  39.     // Setup and configure rf radio
  40.     radio.begin();
  41.     radio.enableDynamicPayloads();
  42.     radio.setDataRate(RF24_250KBPS);
  43.     radio.setPALevel(RF24_PA_MAX);
  44.     //radio.disableCRC();
  45.     radio.printDetails();
  46.  
  47.     /***********************************/
  48.  
  49.     radio.openReadingPipe(1,pipes[0]);
  50.     radio.startListening();
  51.  
  52.     // forever loop
  53.     while (1)
  54.     {
  55.         // if there is data ready
  56.         if ( radio.available() )
  57.         {
  58.             // Dump the payloads until we've gotten everything
  59.             uint8_t len = 0;
  60.  
  61.             while (radio.available())
  62.             {
  63.                 // Fetch the payload, and see if this was the last one.
  64.                 len = radio.getDynamicPayloadSize();
  65.                 radio.read( receive_payload, len );
  66.  
  67.                 // Put a zero at the end for easy printing
  68.                 receive_payload[len] = 0;
  69.  
  70.                 // Spew it
  71.                 update_time(time_buffer);
  72.                 printf("%s  Got payload size=%i value=%s\n\r",time_buffer,len,receive_payload);
  73.             }
  74.  
  75.             usleep(100000);
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement