macca-nz

no_rules_4_relays_ctl_using_BTclassic

Mar 25th, 2024 (edited)
2,391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 8.59 KB | Source Code | 0 0
  1. /*********************************************************************
  2. ***************************** TRASMITTER******************************
  3. *********************************************************************/
  4.  
  5. //Version 2.0
  6.  
  7. /* We use bit shifting "<<" to get the buttons state value as a single byte of data
  8.    where the first 4 bits are the for buttons states.
  9.    Because we are uing "PULLUP" on our inputs we invert the logic to work "ACTIVE HIGH"
  10.    this also lets us use the same state for our indicator LED's
  11.    Using this method the task of ignoring illegal press's is greatly simplified.
  12.    The only allowed press is one button at a time. If you want to change this add
  13.    the combination value to the condition on line 128               */
  14.  
  15. #define PRINT                       //Comment out for no debug serial prints
  16.  
  17. #include "BluetoothSerial.h"
  18.  
  19. #define Button1_PIN 4
  20. #define Button2_PIN 16
  21. #define Button3_PIN 17
  22. #define Button4_PIN 23
  23. #define LED1        22    
  24. #define LED2        21      
  25. #define LED3        19    
  26. #define LED4        18  
  27. #define LED         2
  28.  
  29. #define COUNT  4
  30.  
  31. //#define USE_NAME // Comment this to use MAC address instead of a slaveName
  32. const char *pin = "1234"; // Change this to reflect the pin expected by the real slave BT device
  33.  
  34. #if !defined(CONFIG_BT_SPP_ENABLED)
  35. #error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
  36. #endif
  37.  
  38. BluetoothSerial SerialBT;
  39.  
  40. #ifdef USE_NAME
  41.   String slaveName = "ESP32-BT-Slave"; // Change this to reflect the real name of your slave BT device
  42. #else
  43.   String MACadd = "24:62:AB:E0:84:9E"; // Write Slave side MAC address
  44.   uint8_t address[6] = {0x24, 0x62, 0xAB, 0xE0, 0x84, 0x9E}; // Write Slave side MAC address in HEX
  45. #endif
  46.  
  47. String slaveName = "ESP32-BT-Slave";
  48.  
  49. bool connected;
  50. int8_t state, lastState, leds[]{LED1, LED2, LED3, LED4},
  51.        buttons[]{Button4_PIN, Button3_PIN, Button2_PIN, Button1_PIN};
  52. uint32_t lastMillis = 0;
  53. #define FLUSH_DELAY 60000UL
  54.  
  55. String myName = "ESP32-BT-Master";
  56.  
  57. void setup() {
  58.   #ifdef PRINT
  59.     Serial.begin(115200);
  60.   #endif
  61.   for(byte i = 0; i < COUNT; i++){
  62.     pinMode(buttons[i], INPUT_PULLUP);
  63.     pinMode(leds[i], OUTPUT);
  64.     digitalWrite(leds[i], HIGH);
  65.     delay(300);
  66.     digitalWrite(leds[i], LOW);
  67.   }
  68.  
  69.   pinMode(LED, OUTPUT);
  70.  
  71.   SerialBT.begin(myName, true);
  72.   #ifdef PRINT
  73.     Serial.printf("The device \"%s\" started in master mode, make sure slave BT device is on!\n", myName.c_str());
  74.   #endif
  75.   #ifndef USE_NAME
  76.     SerialBT.setPin(pin);
  77.     #ifdef PRINT
  78.       Serial.println("Using PIN");
  79.     #endif
  80.   #endif
  81.  
  82.   bool connected = startConnection();
  83.   digitalWrite(LED, connected);
  84. }
  85.  
  86. bool startConnection(){
  87.   bool connected;
  88.   // This would reconnect to the slaveName(will use address, if resolved) or address used with connect(slaveName/address).
  89.   #ifdef USE_NAME
  90.     connected = SerialBT.connect(slaveName);
  91.     #ifdef PRINT
  92.       Serial.printf("Connecting to slave BT device named \"%s\"\n", slaveName.c_str());
  93.     #endif
  94.   #else
  95.     connected = SerialBT.connect(address);
  96.     #ifdef PRINT
  97.       Serial.print("Connecting to slave BT device with MAC "); Serial.println(MACadd);
  98.     #endif
  99.   #endif
  100.   if(connected) {
  101.      #ifdef PRINT  
  102.       Serial.println("Connected Successfully!");
  103.     #endif
  104.     return true;
  105.   } else {
  106.     while(!SerialBT.connected(0)) {
  107.       #ifdef PRINT  
  108.         Serial.println("Failed to reconnect. Make sure remote device is available and in range, then restart app.");
  109.       #endif
  110.       delay(2000);
  111.       ESP.restart();
  112.     }
  113.   }
  114.   return false;
  115. }
  116.  
  117. int8_t getState(){
  118.   int8_t i, j=0;
  119.     for(i = 0; i < COUNT; i++){
  120.       j = (j << 1) | digitalRead(buttons[i]);   // read each input pin
  121.     }
  122.     j = -j + 0xf;       //inverts logic because we are using "PULLUP on inputs"
  123.   return j;
  124. }
  125.  
  126. void loop() {
  127.   int8_t state = getState();
  128.   if(millis() - lastMillis >= FLUSH_DELAY){
  129.     SerialBT.flush();
  130.     lastMillis = millis();
  131.   }
  132.   if(!SerialBT.connected(0)){
  133.     digitalWrite(LED, LOW);
  134.     delay(2000);
  135.     SerialBT.disconnect();
  136.     #ifdef PRINT
  137.       Serial.println("Disconnected Successfully!");
  138.     #endif
  139.     SerialBT.end();
  140.     #ifdef PRINT
  141.       Serial.println("End Service Successfully!\n\nTrying to restart Connection");
  142.     #endif
  143.     bool restartLink = startConnection();
  144.     digitalWrite(2, restartLink);
  145.   }else{  
  146.     uint8_t send_data[2];
  147.     send_data[0] = 'T';
  148.     send_data[1] = state;    
  149.     SerialBT.write(send_data, 2);
  150.       if(state != lastState){
  151.         #ifdef PRINT
  152.           if(state == 1){
  153.             Serial.println(F("Button 1 Pressed"));
  154.           }else if(state == 2){
  155.             Serial.println(F("Button 2 Pressed"));
  156.           }else if(state == 4){
  157.             Serial.println(F("Button 3 Pressed"));
  158.           }else if(state == 8){
  159.             Serial.println(F("Button 4 Pressed"));
  160.           }else{
  161.             Serial.println(F("Button\t Released"));
  162.           }
  163.         #endif
  164.           for(byte i = 0; i < COUNT; i++){
  165.             digitalWrite(leds[i], bitRead(state, i));
  166.         }
  167.     }
  168.     delay(20);
  169.   }
  170.   lastState = state;
  171. }
  172.  
  173. //-----------------------------END TRANSMITTER CODE-------------------------------------
  174.  
  175.  
  176. /*********************************************************************
  177. ***************************** RECEIVER ******************************
  178. *********************************************************************/
  179.  
  180. //Version 2.0 (see TRANSMITTER notes for functionality)
  181.  
  182. #define PRINT
  183.  
  184. #include "BluetoothSerial.h"
  185.  
  186. #define Relay1_PIN 22
  187. #define Relay2_PIN 21
  188. #define Relay3_PIN 19
  189. #define Relay4_PIN 18
  190.  
  191. #define FLUSH_DELAY 60000UL     //Flush buffers every 60sec (30 x 2000 = 60,000)
  192. #define COUNT 4
  193.  
  194. bool linkState, lastLinkState;
  195. uint32_t lastMillis = 0;
  196. uint8_t state, lastState,
  197.         relays[]{Relay1_PIN, Relay2_PIN, Relay3_PIN, Relay4_PIN};
  198.  
  199. #define USE_PIN // Uncomment this to use PIN during pairing. The pin is specified on the line below
  200. const char *pin = "1234"; // Change this to more secure PIN.
  201.  
  202. String device_name = "ESP32-BT-Slave";
  203.  
  204. #if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
  205. #error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
  206. #endif
  207.  
  208. #if !defined(CONFIG_BT_SPP_ENABLED)
  209. #error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
  210. #endif
  211.  
  212. BluetoothSerial SerialBT;
  213.  
  214. void setup() {
  215.   SerialBT.begin(device_name); //Bluetooth device name
  216.   #ifdef PRINT
  217.     Serial.begin(115200);
  218.     Serial.printf("The device with name \"%s\" is started.\nNow you can pair it with Bluetooth!\n", device_name.c_str());
  219.   #endif
  220.  
  221.   //Serial.printf("The device with name \"%s\" and MAC address %s is started.\nNow you can pair it with Bluetooth!\n", device_name.c_str(), SerialBT.getMacString()); // Use this after the MAC method is implemented
  222.   #ifdef USE_PIN
  223.     SerialBT.setPin(pin);
  224.     #ifdef PRINT
  225.       Serial.println("Using PIN");
  226.     #endif
  227.   #endif
  228.   pinMode(2, OUTPUT);
  229.   for(uint8_t i = 0; i < COUNT; i++){
  230.     pinMode(relays[i], OUTPUT);
  231.   }
  232.   linkState = SerialBT.connected();
  233. }
  234.  
  235. void loop() {
  236.   if (SerialBT.available()) {
  237.     linkState = 1;
  238.       uint8_t data[2];
  239.       SerialBT.readBytes(data, 2);
  240.       if (data[0] == 'T') {
  241.         state = data[1];
  242.           if(state != lastState){
  243.             for(uint8_t i = 0; i < COUNT; i++){
  244.               digitalWrite(relays[i], bitRead(state, i));
  245.               // Print relay statuses for debugging
  246.             }
  247.             #ifdef PRINT
  248.               if(state == 1){
  249.                 Serial.println(F("Relay 1 ON"));
  250.               }else if(state == 2){
  251.                 Serial.println(F("Relay 2 ON"));
  252.               }else if(state == 4){
  253.                 Serial.println(F("Relay 3 ON"));
  254.               }else if(state == 8){
  255.                 Serial.println(F("Relay 4 ON"));
  256.               }else{
  257.                 Serial.println(F("Relay's OFF"));
  258.               }
  259.             #endif
  260.           }
  261.       }
  262.   }else{
  263.     linkState = 0;
  264.   }
  265.   if(millis() - lastMillis > FLUSH_DELAY){  
  266.     Serial.flush();
  267.     SerialBT.flush();
  268.     lastMillis = millis();
  269.   }
  270.   if(linkState != lastLinkState){
  271.     digitalWrite(2, linkState);
  272.     #ifdef PRINT
  273.       Serial.printf("The device %s\n", linkState ? "Connected" : "NOT Connected");
  274.     #endif
  275.     if(!linkState){
  276.       while (!SerialBT.connected(3000)) {
  277.         ESP.restart();
  278.       }
  279.     }
  280.   }
  281.   delay(20);
  282.   lastLinkState = linkState;
  283.   lastState = state;
  284. }
  285.  
  286. //-----------------------------END RECEIVER CODE-------------------------------------
  287.  
Advertisement
Add Comment
Please, Sign In to add comment