Advertisement
safwan092

Untitled

Dec 9th, 2023
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. // ask_receiver.pde
  2. // -*- mode: C++ -*-
  3. // Simple example of how to use RadioHead to receive messages
  4. // with a simple ASK transmitter in a very simple way.
  5. // Implements a simplex (one-way) receiver with an Rx-B1 module
  6. // Tested on Arduino Mega, Duemilanova, Uno, Due, Teensy, ESP-12
  7.  
  8. #include <RH_ASK.h>
  9. #ifdef RH_HAVE_HARDWARE_SPI
  10. #include <SPI.h> // Not actually used but needed to compile
  11. #endif
  12.  
  13. RH_ASK driver;
  14. // RH_ASK driver(2000, 4, 5, 0); // ESP8266 or ESP32: do not use pin 11 or 2
  15. // RH_ASK driver(2000, 3, 4, 0); // ATTiny, RX on D3 (pin 2 on attiny85) TX on D4 (pin 3 on attiny85),
  16. // RH_ASK driver(2000, PD14, PD13, 0); STM32F4 Discovery: see tx and rx on Orange and Red LEDS
  17.  
  18. void setup()
  19. {
  20. pinMode(8, OUTPUT);
  21. digitalWrite(8, 0);
  22. #ifdef RH_HAVE_SERIAL
  23. Serial.begin(9600); // Debugging only
  24. #endif
  25. if (!driver.init())
  26. #ifdef RH_HAVE_SERIAL
  27. Serial.println("init failed");
  28. #else
  29. ;
  30. #endif
  31. }
  32. String message;
  33. void loop()
  34. {
  35. uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
  36. uint8_t buflen = sizeof(buf);
  37.  
  38. if (driver.recv(buf, &buflen)) // Non-blocking
  39. {
  40.  
  41.  
  42. // Convert each element of buf to a string and concatenate them
  43. for (int i = 0; i < buflen; i++)
  44. {
  45. message += String(buf[i]);
  46. }
  47.  
  48. // Print the resulting string
  49. Serial.println(message);
  50.  
  51. }
  52. if (message == "49") {
  53. digitalWrite(8, 1);
  54. delay(1000);
  55. message="";
  56. }
  57. else {
  58. digitalWrite(8, 0);
  59. message="";
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement