Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. /*
  2. * Arduino Wireless Communication Tutorial
  3. * Example 1 - Receiver Code
  4. *
  5. * by Dejan Nedelkovski, www.HowToMechatronics.com
  6. *
  7. * Library: TMRh20/RF24, https://github.com/tmrh20/RF24/
  8. */
  9.  
  10. #include <SPI.h>
  11. #include <nRF24L01.h>
  12. #include <RF24.h>
  13.  
  14. #define PAYLOAD_LEN 32
  15. #define MESSAGE_KEY 12345
  16.  
  17. typedef enum {
  18. SENSOR_TEMP = 0,
  19. SENSOR_PRESS
  20. } sensor_type_t;
  21.  
  22. typedef struct {
  23. int key;
  24. sensor_type_t type;
  25. int val;
  26.  
  27. } sensor_data_t;
  28.  
  29. RF24 radio(9, 10); // CE, CSN
  30.  
  31. const byte address[6] = "00001";
  32.  
  33. void setup() {
  34. Serial.begin(9600);
  35. radio.begin();
  36. radio.openReadingPipe(0, address);
  37. radio.setPALevel(RF24_PA_MIN);
  38. radio.startListening();
  39. }
  40.  
  41. void loop() {
  42.  
  43.  
  44. if (radio.available()) {
  45. char data[PAYLOAD_LEN] = {0};
  46.  
  47. radio.read(data, PAYLOAD_LEN);
  48. sensor_data_t *msg = (sensor_data_t *)data;
  49.  
  50. Serial.println("MESSAGE RECEIVED!\n");
  51.  
  52. if (msg->key == MESSAGE_KEY)
  53. Serial.println(" KEY MATCH!\n");
  54.  
  55. Serial.println(msg->key);
  56. Serial.println(msg->type);
  57. Serial.println(msg->val);
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement