Advertisement
CuriousScientist

RF 433 Receiver code

Apr 4th, 2020
725
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //If you found this code useful, please subscribe to my channel: https://www.youtube.com/c/CuriousScientist?sub_confirmation=1
  2. //It took several hours for me to prepare everything, but subscription is just a click for you. Thank you!
  3. //This code belongs to the following tutorial video: https://youtu.be/lGR8yVVkEL0
  4. //RECEIVER
  5.  
  6.  
  7. #include <RH_ASK.h>//RadioHead Amplitude Shift Keying Library
  8. #include <SPI.h> // SPI Library
  9. #include <Wire.h> //Wire Library (I2C)
  10.  
  11. RH_ASK rf_driver; //Instatiating a rf_driver for the receiver
  12.  
  13. //ASCII-based OLED library
  14. //Its huge advantage that it is _not_ graphical, so it is using much less memory
  15. #include "SSD1306Ascii.h"
  16. #include "SSD1306AsciiAvrI2c.h"
  17. #define I2C_ADDRESS 0x3C //Address
  18. #define RST_PIN -1 //For OLED with no reset pin
  19.  
  20. SSD1306AsciiAvrI2c display;
  21.  
  22.  
  23. // Set buffer to size of expected message
  24.     uint8_t buf[24];
  25.     uint8_t buflen = sizeof(buf);
  26.  
  27.     String receivedString; //String for long messages
  28.  
  29. //REC pin = D11
  30. int redLED = 5; //on, when it is too loud
  31. int greenLED = 6; //on when it is OK
  32.  
  33.  
  34. void setup()
  35. {
  36.     // Initialize ASK Object
  37.     rf_driver.init();
  38.    
  39.   #if RST_PIN >= 0
  40.   display.begin(&Adafruit128x64, I2C_ADDRESS, RST_PIN);
  41.     #else // RST_PIN >= 0
  42.   display.begin(&Adafruit128x64, I2C_ADDRESS);
  43.     #endif // RST_PIN >= 0
  44.   // Call oled.setI2cClock(frequency) to change from the default frequency.
  45.  
  46.   display.setFont(System5x7);
  47.   display.set1X();
  48.   display.clear();
  49.  
  50.    //Managing the LEDs
  51.    pinMode(redLED, OUTPUT);
  52.    pinMode(greenLED, OUTPUT);
  53.  
  54.    digitalWrite(redLED, LOW);
  55.    digitalWrite(greenLED, HIGH);
  56.    //----------------------------
  57.  
  58.   //Serial.begin(9600);
  59.   //Serial.println("Receiver started");
  60. }
  61.  
  62. void loop()
  63. {
  64.    
  65.     // Check if received packet is correct size
  66.     if (rf_driver.recv(buf, &buflen))
  67.     {    
  68.  
  69.       for (int i = 0; i < buflen-1; i++)
  70.       {    
  71.         receivedString += String(buf[i]); //building up a string from the received data            
  72.       }
  73.      
  74.       PrintScreen(); //print to OLED  
  75.       receivedString="";  //reset to zero, so we don't mix the messages
  76.      
  77.     }
  78.    
  79. }
  80.  
  81. void PrintScreen()
  82. {
  83.   display.clear();  
  84.   display.setCursor(0, 0);
  85.   display.println("Status: ");
  86.   display.println();
  87.  
  88.   if(receivedString == "0")
  89.   {
  90.     display.println("Waiting...");
  91.     digitalWrite(redLED, LOW);
  92.     digitalWrite(greenLED, HIGH); //Only green is on, while we are waiting
  93.   }
  94.   if(receivedString == "1")
  95.   {  
  96.     display.println("Check device!");
  97.     digitalWrite(redLED, HIGH); //Red goes on, when the trigger level is crossed on the transmitter side
  98.     digitalWrite(greenLED, LOW);
  99.   }
  100.  
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement