Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*********************************************************************
- ***************************** TRASMITTER******************************
- *********************************************************************/
- //Version 2.0
- /* We use bit shifting "<<" to get the buttons state value as a single byte of data
- where the first 4 bits are the for buttons states.
- Because we are uing "PULLUP" on our inputs we invert the logic to work "ACTIVE HIGH"
- this also lets us use the same state for our indicator LED's
- Using this method the task of ignoring illegal press's is greatly simplified.
- The only allowed press is one button at a time. If you want to change this add
- the combination value to the condition on line 128 */
- #define PRINT //Comment out for no debug serial prints
- #include "BluetoothSerial.h"
- #define Button1_PIN 4
- #define Button2_PIN 16
- #define Button3_PIN 17
- #define Button4_PIN 23
- #define LED1 22
- #define LED2 21
- #define LED3 19
- #define LED4 18
- #define LED 2
- #define COUNT 4
- //#define USE_NAME // Comment this to use MAC address instead of a slaveName
- const char *pin = "1234"; // Change this to reflect the pin expected by the real slave BT device
- #if !defined(CONFIG_BT_SPP_ENABLED)
- #error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
- #endif
- BluetoothSerial SerialBT;
- #ifdef USE_NAME
- String slaveName = "ESP32-BT-Slave"; // Change this to reflect the real name of your slave BT device
- #else
- String MACadd = "24:62:AB:E0:84:9E"; // Write Slave side MAC address
- uint8_t address[6] = {0x24, 0x62, 0xAB, 0xE0, 0x84, 0x9E}; // Write Slave side MAC address in HEX
- #endif
- String slaveName = "ESP32-BT-Slave";
- bool connected;
- int8_t state, lastState, leds[]{LED1, LED2, LED3, LED4},
- buttons[]{Button4_PIN, Button3_PIN, Button2_PIN, Button1_PIN};
- uint32_t lastMillis = 0;
- #define FLUSH_DELAY 60000UL
- String myName = "ESP32-BT-Master";
- void setup() {
- #ifdef PRINT
- Serial.begin(115200);
- #endif
- for(byte i = 0; i < COUNT; i++){
- pinMode(buttons[i], INPUT_PULLUP);
- pinMode(leds[i], OUTPUT);
- digitalWrite(leds[i], HIGH);
- delay(300);
- digitalWrite(leds[i], LOW);
- }
- pinMode(LED, OUTPUT);
- SerialBT.begin(myName, true);
- #ifdef PRINT
- Serial.printf("The device \"%s\" started in master mode, make sure slave BT device is on!\n", myName.c_str());
- #endif
- #ifndef USE_NAME
- SerialBT.setPin(pin);
- #ifdef PRINT
- Serial.println("Using PIN");
- #endif
- #endif
- bool connected = startConnection();
- digitalWrite(LED, connected);
- }
- bool startConnection(){
- bool connected;
- // This would reconnect to the slaveName(will use address, if resolved) or address used with connect(slaveName/address).
- #ifdef USE_NAME
- connected = SerialBT.connect(slaveName);
- #ifdef PRINT
- Serial.printf("Connecting to slave BT device named \"%s\"\n", slaveName.c_str());
- #endif
- #else
- connected = SerialBT.connect(address);
- #ifdef PRINT
- Serial.print("Connecting to slave BT device with MAC "); Serial.println(MACadd);
- #endif
- #endif
- if(connected) {
- #ifdef PRINT
- Serial.println("Connected Successfully!");
- #endif
- return true;
- } else {
- while(!SerialBT.connected(0)) {
- #ifdef PRINT
- Serial.println("Failed to reconnect. Make sure remote device is available and in range, then restart app.");
- #endif
- delay(2000);
- ESP.restart();
- }
- }
- return false;
- }
- int8_t getState(){
- int8_t i, j=0;
- for(i = 0; i < COUNT; i++){
- j = (j << 1) | digitalRead(buttons[i]); // read each input pin
- }
- j = -j + 0xf; //inverts logic because we are using "PULLUP on inputs"
- return j;
- }
- void loop() {
- int8_t state = getState();
- if(millis() - lastMillis >= FLUSH_DELAY){
- SerialBT.flush();
- lastMillis = millis();
- }
- if(!SerialBT.connected(0)){
- digitalWrite(LED, LOW);
- delay(2000);
- SerialBT.disconnect();
- #ifdef PRINT
- Serial.println("Disconnected Successfully!");
- #endif
- SerialBT.end();
- #ifdef PRINT
- Serial.println("End Service Successfully!\n\nTrying to restart Connection");
- #endif
- bool restartLink = startConnection();
- digitalWrite(2, restartLink);
- }else{
- uint8_t send_data[2];
- send_data[0] = 'T';
- send_data[1] = state;
- SerialBT.write(send_data, 2);
- if(state != lastState){
- #ifdef PRINT
- if(state == 1){
- Serial.println(F("Button 1 Pressed"));
- }else if(state == 2){
- Serial.println(F("Button 2 Pressed"));
- }else if(state == 4){
- Serial.println(F("Button 3 Pressed"));
- }else if(state == 8){
- Serial.println(F("Button 4 Pressed"));
- }else{
- Serial.println(F("Button\t Released"));
- }
- #endif
- for(byte i = 0; i < COUNT; i++){
- digitalWrite(leds[i], bitRead(state, i));
- }
- }
- delay(20);
- }
- lastState = state;
- }
- //-----------------------------END TRANSMITTER CODE-------------------------------------
- /*********************************************************************
- ***************************** RECEIVER ******************************
- *********************************************************************/
- //Version 2.0 (see TRANSMITTER notes for functionality)
- #define PRINT
- #include "BluetoothSerial.h"
- #define Relay1_PIN 22
- #define Relay2_PIN 21
- #define Relay3_PIN 19
- #define Relay4_PIN 18
- #define FLUSH_DELAY 60000UL //Flush buffers every 60sec (30 x 2000 = 60,000)
- #define COUNT 4
- bool linkState, lastLinkState;
- uint32_t lastMillis = 0;
- uint8_t state, lastState,
- relays[]{Relay1_PIN, Relay2_PIN, Relay3_PIN, Relay4_PIN};
- #define USE_PIN // Uncomment this to use PIN during pairing. The pin is specified on the line below
- const char *pin = "1234"; // Change this to more secure PIN.
- String device_name = "ESP32-BT-Slave";
- #if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
- #error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
- #endif
- #if !defined(CONFIG_BT_SPP_ENABLED)
- #error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
- #endif
- BluetoothSerial SerialBT;
- void setup() {
- SerialBT.begin(device_name); //Bluetooth device name
- #ifdef PRINT
- Serial.begin(115200);
- Serial.printf("The device with name \"%s\" is started.\nNow you can pair it with Bluetooth!\n", device_name.c_str());
- #endif
- //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
- #ifdef USE_PIN
- SerialBT.setPin(pin);
- #ifdef PRINT
- Serial.println("Using PIN");
- #endif
- #endif
- pinMode(2, OUTPUT);
- for(uint8_t i = 0; i < COUNT; i++){
- pinMode(relays[i], OUTPUT);
- }
- linkState = SerialBT.connected();
- }
- void loop() {
- if (SerialBT.available()) {
- linkState = 1;
- uint8_t data[2];
- SerialBT.readBytes(data, 2);
- if (data[0] == 'T') {
- state = data[1];
- if(state != lastState){
- for(uint8_t i = 0; i < COUNT; i++){
- digitalWrite(relays[i], bitRead(state, i));
- // Print relay statuses for debugging
- }
- #ifdef PRINT
- if(state == 1){
- Serial.println(F("Relay 1 ON"));
- }else if(state == 2){
- Serial.println(F("Relay 2 ON"));
- }else if(state == 4){
- Serial.println(F("Relay 3 ON"));
- }else if(state == 8){
- Serial.println(F("Relay 4 ON"));
- }else{
- Serial.println(F("Relay's OFF"));
- }
- #endif
- }
- }
- }else{
- linkState = 0;
- }
- if(millis() - lastMillis > FLUSH_DELAY){
- Serial.flush();
- SerialBT.flush();
- lastMillis = millis();
- }
- if(linkState != lastLinkState){
- digitalWrite(2, linkState);
- #ifdef PRINT
- Serial.printf("The device %s\n", linkState ? "Connected" : "NOT Connected");
- #endif
- if(!linkState){
- while (!SerialBT.connected(3000)) {
- ESP.restart();
- }
- }
- }
- delay(20);
- lastLinkState = linkState;
- lastState = state;
- }
- //-----------------------------END RECEIVER CODE-------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment