Advertisement
pleasedontcode

"Bluetooth Relay" rev_01

Jul 12th, 2024
111
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 NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-07-13 00:46:07
  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. /****** DEFINITION OF LIBRARIES *****/
  28. #include <SoftwareSerial.h>
  29. #include <Relay.h>  //https://github.com/rafaelnsantos/Relay
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void updateOutputs(void);
  35.  
  36. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  37. const uint8_t soldier_RelayModule_Signal_PIN_D2 = 2;
  38.  
  39. /***** DEFINITION OF Software Serial *****/
  40. const uint8_t myHC05_HC05_mySerial_PIN_SERIAL_TX_A0 = A0;
  41. const uint8_t myHC05_HC05_mySerial_PIN_SERIAL_RX_A1 = A1;
  42. SoftwareSerial myHC05_HC05_mySerial(myHC05_HC05_mySerial_PIN_SERIAL_RX_A1, myHC05_HC05_mySerial_PIN_SERIAL_TX_A0);
  43.  
  44. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  45. /***** used to store raw data *****/
  46. bool soldier_RelayModule_Signal_PIN_D2_rawData = 0;
  47.  
  48. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  49. /***** used to store data after characteristic curve transformation *****/
  50. float soldier_RelayModule_Signal_PIN_D2_phyData = 0.0;
  51.  
  52. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  53. Relay light(soldier_RelayModule_Signal_PIN_D2, true); // Initialize relay on pin 2, Normally Open
  54.  
  55. void setup(void)
  56. {
  57.     // put your setup code here, to run once:
  58.  
  59.     light.begin(); // Initialize the relay pin
  60.  
  61.     myHC05_HC05_mySerial.begin(9600); // Initialize software serial communication
  62. }
  63.  
  64. void loop(void)
  65. {
  66.     // put your main code here, to run repeatedly:
  67.  
  68.     updateOutputs(); // Refresh output data
  69.  
  70.     // Check if data is available on the serial port
  71.     if (myHC05_HC05_mySerial.available()) {
  72.         String receivedString = myHC05_HC05_mySerial.readStringUntil('\n'); // Read the incoming string until newline character
  73.  
  74.         // Check for specific commands and update the relay state
  75.         if (receivedString == "soldier: connected") {
  76.             soldier_RelayModule_Signal_PIN_D2_rawData = true; // Turn on the relay
  77.         } else if (receivedString == "soldier: off") {
  78.             soldier_RelayModule_Signal_PIN_D2_rawData = false; // Turn off the relay
  79.         }
  80.     }
  81. }
  82.  
  83. void updateOutputs()
  84. {
  85.     if (soldier_RelayModule_Signal_PIN_D2_rawData) {
  86.         light.turnOn(); // Turn relay on
  87.     } else {
  88.         light.turnOff(); // Turn relay off
  89.     }
  90. }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement