├── .gitattributes └── WiFi_Bluetooth_ESP32 └── WiFi_Bluetooth_ESP32.ino /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /WiFi_Bluetooth_ESP32/WiFi_Bluetooth_ESP32.ino: -------------------------------------------------------------------------------- 1 | /* 2 | This is the code for the project called 3 | 4 | WiFi & Bluetooth controlled Home Automation using ESP32 5 | 6 | This code is written by Sachin Soni on 14.08.2020 7 | 8 | The tutorial Video for the project is uploaded on 9 | our YouTube channel called "techiesms" 10 | 11 | Channel Link - https://www.youtube.com/techiesms 12 | 13 | 14 | techiesms 15 | explore | learn | share 16 | */ 17 | 18 | #define BLYNK_PRINT Serial 19 | 20 | #include // https://github.com/blynkkk/blynk-library 21 | #include "BluetoothSerial.h" // https://github.com/espressif/arduino-esp32/tree/master/libraries/BluetoothSerial 22 | 23 | 24 | #if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED) 25 | #error Bluetooth is not enabled! Please run 26 | `make menuconfig` to and enable it 27 | #endif 28 | 29 | 30 | BluetoothSerial SerialBT; 31 | 32 | 33 | int bluedata; // variable for storing bluetooth data 34 | 35 | int relay1 = 15; 36 | int relay2 = 2; 37 | int relay3 = 4; 38 | int relay4 = 22; 39 | 40 | char auth[] = "AUTH_TOKEN"; 41 | char ssid[] = "SSID"; 42 | char pass[] = "PASS"; 43 | 44 | BLYNK_WRITE(V1) 45 | { 46 | int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable 47 | digitalWrite(relay1, !pinValue); 48 | // process received value 49 | } 50 | 51 | BLYNK_WRITE(V2) 52 | { 53 | int pinValue = param.asInt(); // assigning incoming value from pin V2 to a variable 54 | digitalWrite(relay2, !pinValue); 55 | // process received value 56 | } 57 | 58 | BLYNK_WRITE(V3) 59 | { 60 | int pinValue = param.asInt(); // assigning incoming value from pin V3 to a variable 61 | digitalWrite(relay3, !pinValue); 62 | // process received value 63 | } 64 | 65 | BLYNK_WRITE(V4) 66 | { 67 | int pinValue = param.asInt(); // assigning incoming value from pin V4 to a variable 68 | digitalWrite(relay4, !pinValue); 69 | // process received value 70 | } 71 | 72 | 73 | 74 | void setup() 75 | { 76 | Serial.begin(115200); 77 | 78 | btStart(); Serial.println("Bluetooth On"); 79 | 80 | SerialBT.begin("ESP32_Bluetooth"); //Bluetooth device name 81 | Serial.println("The device started, now you can pair it with bluetooth!"); 82 | delay(10000); 83 | 84 | pinMode(relay1, OUTPUT); 85 | pinMode(relay2, OUTPUT); 86 | pinMode(relay3, OUTPUT); 87 | pinMode(relay4, OUTPUT); 88 | digitalWrite(relay1, LOW); 89 | digitalWrite(relay2, LOW); 90 | digitalWrite(relay3, LOW); 91 | digitalWrite(relay4, LOW); 92 | 93 | Serial.println("Connecting to Internet"); 94 | delay(2000); 95 | 96 | WiFi.begin(ssid, pass); Serial.println("WiFi On"); 97 | Blynk.config(auth); 98 | 99 | delay(2000); 100 | 101 | } 102 | void loop() 103 | { 104 | 105 | if (WiFi.status() != WL_CONNECTED) 106 | { 107 | Serial.println("Not Connected"); 108 | } 109 | else 110 | { 111 | Serial.println(" Connected"); 112 | Blynk.run(); 113 | } 114 | 115 | if (SerialBT.available()) 116 | { 117 | 118 | Bluetooth_handle(); 119 | 120 | } 121 | 122 | } 123 | 124 | void Bluetooth_handle() 125 | { 126 | //char bluedata; 127 | bluedata = SerialBT.parseInt(); 128 | //Serial.println(bluedata); 129 | delay(20); 130 | if (1 == bluedata) { 131 | digitalWrite(relay1, LOW); 132 | Blynk.virtualWrite(V1, 1); 133 | SerialBT.println("relay1 on"); 134 | Serial.print("relay1 on\n"); 135 | } 136 | else if (2 == bluedata) { 137 | digitalWrite(relay1, HIGH); 138 | Blynk.virtualWrite(V1, 0); 139 | SerialBT.println("relay1 off"); 140 | Serial.print("relay1 off\n"); 141 | } 142 | else if (3 == bluedata) { 143 | digitalWrite(relay2, LOW); 144 | Blynk.virtualWrite(V2, 1); 145 | SerialBT.println("relay2 on"); 146 | Serial.print("relay2 on\n"); 147 | } 148 | else if (4 == bluedata) { 149 | digitalWrite(relay2, HIGH); 150 | Blynk.virtualWrite(V2, 0); 151 | SerialBT.println("relay2 off"); 152 | Serial.print("relay2 off\n"); 153 | } 154 | else if (5 == bluedata) { 155 | digitalWrite(relay3, LOW); 156 | Blynk.virtualWrite(V3, 1); 157 | SerialBT.println("relay3 on"); 158 | Serial.print("relay3 on\n"); 159 | } 160 | else if (6 == bluedata) { 161 | digitalWrite(relay3, HIGH); 162 | Blynk.virtualWrite(V3, 0); 163 | SerialBT.println("relay3 off\n"); 164 | Serial.print("relay3 off\n"); 165 | } 166 | else if (7 == bluedata) { 167 | digitalWrite(relay4, LOW); 168 | Blynk.virtualWrite(V4, 1); 169 | SerialBT.println("relay4 on\n"); 170 | Serial.print("relay4 on\n"); 171 | } 172 | else if (bluedata == 8) { 173 | digitalWrite(relay4, HIGH); 174 | Blynk.virtualWrite(V4, 0); 175 | SerialBT.println("relay4 off"); 176 | Serial.print("relay4 off\n"); 177 | } 178 | else 179 | { 180 | } 181 | } 182 | --------------------------------------------------------------------------------