Sebuahhobi98

SingleNRF24L01

Nov 3rd, 2020 (edited)
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Arduino.h>
  2. /*OLED Display*/
  3. #include <Wire.h>
  4. #include <Adafruit_GFX.h>
  5. #include <Adafruit_SSD1306.h>
  6.  
  7. // Color definitions
  8. #define BLACK 0x0000
  9. #define BLUE 0x001F
  10. #define RED 0xF800
  11. #define GREEN 0x07E0
  12. #define CYAN 0x07FF
  13. #define MAGENTA 0xF81F
  14. #define YELLOW 0xFFE0
  15. #define SCREEN_WIDTH 128 // OLED display width, in pixels
  16. #define SCREEN_HEIGHT 64 // OLED display height, in pixels
  17.  
  18. // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
  19. #define SDA A4
  20. #define SCL A5
  21. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
  22.  
  23. #include <SPI.h>
  24. #include <Wire.h>
  25. #include "nRF24L01.h"
  26. #include "RF24.h"
  27.  
  28.  
  29. RF24 radio(0, 2, 14, 12, 13);
  30.  
  31. const byte pipes[][6] = {"1000", "2000", "3000", "4000", "5000", "6000"};
  32.  
  33. //#include <WiFi.h>
  34. //#include <HTTPClient.h>
  35.  
  36. const char* ssid = "UIR WIFI";
  37. const char* password =  "";
  38.  
  39. byte channel = 101;
  40. const String this_node = "F";
  41.  
  42. void radioConfig() {
  43.   //  radio.begin();
  44.   display.clearDisplay();
  45.   display.setTextColor(WHITE);
  46.   display.setTextSize(1);
  47.   display.setCursor(0, 0);
  48.   if (!radio.begin()) {
  49.     display.println("Radio failed");
  50.     display.display();
  51.     for (;;);
  52.   }
  53.   if (radio.isChipConnected()) {
  54.     display.println("Radio is connected");
  55.     display.display();
  56.   }
  57.   radio.setChannel(channel);
  58.   radio.setPayloadSize(32);
  59.   radio.setPALevel(RF24_PA_MAX);
  60.   radio.setDataRate(RF24_250KBPS);
  61.   radio.openReadingPipe(0, pipes[5]); //F
  62.   radio.startListening();
  63.   //  //Serial.println();
  64.   //  //Serial.println("Radio Received Start Listening");
  65. }
  66.  
  67. void setup(void) {
  68.   Serial.begin(115200);
  69.  
  70.   if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
  71.     ////Serial.println(F("SSD1306 allocation failed"));
  72.     for (;;);
  73.   }
  74.  
  75.   display.clearDisplay();
  76.   display.display();
  77.   delay(1000);
  78.  
  79.   radioConfig();
  80.   delay(2000);
  81. }
  82.  
  83. String d;
  84. void loop() {
  85.   while (Serial.available()) {
  86.     d = Serial.readString();
  87.     radio.stopListening();
  88.     radio.openWritingPipe(pipes[4]);
  89.     char msg[33];
  90.     d.toCharArray(msg, sizeof(msg));
  91.     byte cek = radio.write(msg, sizeof(msg));
  92.     radio.startListening();
  93.   }
  94.   readRadio();
  95. }
  96.  
  97. char msg[33];
  98. String data;
  99. void readRadio() {
  100.   delay(5);    
  101.   byte pipe_num = 0;
  102.   if (radio.available(&pipe_num)) {
  103.     display.clearDisplay();
  104.     display.setTextColor(WHITE);
  105.     display.setTextSize(1);
  106.       display.setCursor(0, 0);
  107.       display.print("Reading...");
  108.       radio.read(msg, sizeof(msg));
  109.       data = String(msg);
  110.    
  111.     Serial.println("#################################");
  112.     Serial.print("Data: ");
  113.     Serial.println(msg);
  114.      
  115.     display.setCursor(0, 10);
  116.     display.print("Message: ");
  117.     display.println(msg);
  118.     display.display();
  119.     delay(2000);
  120.   } else {
  121.     display.clearDisplay();
  122.     display.setTextColor(WHITE);
  123.     display.setTextSize(1);
  124.     display.setCursor(0, 50);
  125.     display.println("Radio no available");
  126.     display.display();
  127.   }
  128. }
Add Comment
Please, Sign In to add comment