Advertisement
pleasedontcode

"Bluetooth Relay" rev_02

Jul 12th, 2024
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "Bluetooth Relay"
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-07-13 00:47:32
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Disconnecting and connecting the power supply, */
  21.     /* when the character string "soldier: connected" is */
  22.     /* received, the power supply should be turned on, */
  23.     /* and if "soldier; off" is received, cut off the */
  24.     /* power supply. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <SoftwareSerial.h>
  30. #include <Relay.h>  //https://github.com/rafaelnsantos/Relay
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35. void updateOutputs(void);
  36.  
  37. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  38. const uint8_t soldier_RelayModule_Signal_PIN_D2 = 2;
  39.  
  40. /***** DEFINITION OF Software Serial *****/
  41. const uint8_t myHC05_HC05_mySerial_PIN_SERIAL_TX_A0 = A0;
  42. const uint8_t myHC05_HC05_mySerial_PIN_SERIAL_RX_A1 = A1;
  43. SoftwareSerial myHC05_HC05_mySerial(myHC05_HC05_mySerial_PIN_SERIAL_RX_A1, myHC05_HC05_mySerial_PIN_SERIAL_TX_A0);
  44.  
  45. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  46. /***** used to store raw data *****/
  47. bool soldier_RelayModule_Signal_PIN_D2_rawData = 0;
  48.  
  49. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  50. /***** used to store data after characteristic curve transformation *****/
  51. float soldier_RelayModule_Signal_PIN_D2_phyData = 0.0;
  52.  
  53. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  54. Relay light(soldier_RelayModule_Signal_PIN_D2, true); // Initialize relay on pin 2, Normally Open
  55.  
  56. void setup(void)
  57. {
  58.     // put your setup code here, to run once:
  59.  
  60.     light.begin(); // Initialize the relay pin
  61.  
  62.     myHC05_HC05_mySerial.begin(9600); // Initialize software serial communication
  63. }
  64.  
  65. void loop(void)
  66. {
  67.     // put your main code here, to run repeatedly:
  68.  
  69.     updateOutputs(); // Refresh output data
  70.  
  71.     // Check if data is available on the serial port
  72.     if (myHC05_HC05_mySerial.available()) {
  73.         String receivedString = myHC05_HC05_mySerial.readStringUntil('\n'); // Read the incoming string until newline character
  74.  
  75.         // Check for specific commands and update the relay state
  76.         if (receivedString == "soldier: connected") {
  77.             soldier_RelayModule_Signal_PIN_D2_rawData = true; // Turn on the relay
  78.         } else if (receivedString == "soldier: off") {
  79.             soldier_RelayModule_Signal_PIN_D2_rawData = false; // Turn off the relay
  80.         }
  81.     }
  82. }
  83.  
  84. void updateOutputs()
  85. {
  86.     if (soldier_RelayModule_Signal_PIN_D2_rawData) {
  87.         light.turnOn(); // Turn relay on
  88.     } else {
  89.         light.turnOff(); // Turn relay off
  90.     }
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement