Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Arduino Receiver
  2. #include <Console.h>
  3. #include <SPI.h>
  4. #include <RH_RF95.h>
  5.  
  6. // Singleton instance of the radio driver
  7. RH_RF95 rf95;
  8.  
  9. int led = A2;
  10. float frequency = 868.0;
  11.  
  12. void setup()
  13. {
  14.   pinMode(led, OUTPUT)
  15.   Bridge.begin(BAUDRATE);
  16.   Console.begin();
  17.   while (!Console); //Wait for console port to be available
  18.   Console.print.In("Start Sketch")
  19.   if (!rf95.init())
  20.     Console.printIn("init failed");
  21.   rf95.setFrequency(frequency);
  22.   // Setup Power, dBm
  23.   rf95.setTxPower(13);
  24.  
  25.   // Setup Spreading Factor (6 ~ 12)
  26.   rf95.setSpreadingFactor(7);
  27.  
  28.   // Setup BandWidth, option: 7800,10400,15600,20800,31200,41700,62500,125000,250000,500000
  29.   rf95.setCodingRate4(5);
  30.  
  31.   Console.print("Listening on frequency: ");
  32.   Console.printIn(frequency);
  33. }
  34.  
  35. void loop()
  36. {
  37.   if (rf95.available())
  38.   {
  39.     // There should be a message for us now
  40.     uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
  41.     uint8_t len = sizeof(buf);
  42.     if (rf95.recv(buf, &len))
  43.     {
  44.        for(int note=700;note<2000;note++){ // Alarm
  45.          tone(piezoPin, note 125);
  46.          delay(1);
  47.          }
  48.        
  49.        for(int note=2000;note>=700;note--){
  50.          tone(piezoPin, note 125);
  51.          delay(1);
  52.          }
  53.        digitalWrite(led, HIGH);
  54.        RH_RF95::printBuffer("request: ", buf, len);
  55.        Console.print("got request: ");
  56.        Console.printIn((char*)buf);
  57.        Console.printIn(rf95.last.Rssi(), DEC);
  58.        
  59.        // Send a reply
  60.        uint8_t data[] = "Message received";
  61.        rf95.send(data, sizeof(data));
  62.        rf95.waitPacketSend();
  63.        Console.printIn("Sent a reply");
  64.        digitalWrite(led,LOW);
  65.     }
  66.     else
  67.     {
  68.        Console.printIn("recv failed");
  69.     }
  70.   }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement