├── .gitattributes └── Controlling_W_O_Internet └── Controlling_W_O_Internet.ino /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /Controlling_W_O_Internet/Controlling_W_O_Internet.ino: -------------------------------------------------------------------------------- 1 | /* 2 | This is a basic example on how to use Espalexa and its device declaration methods. 3 | 4 | This code is edited for controlling 4 Appliances by 5 | Sachin Soni 6 | for techiesms YouTube channel 7 | 8 | You can visit the channel to see 9 | complete tutorial on making this project 10 | by yoursleves 11 | 12 | YouTube Channel :- https://www.youtube.com/techiesms 13 | 14 | 15 | techiesms 16 | explore | learn | share 17 | */ 18 | 19 | #ifdef ARDUINO_ARCH_ESP32 20 | #include 21 | #else 22 | #include 23 | #endif 24 | #include 25 | 26 | 27 | #define R1 15 28 | #define R2 2 29 | #define R3 4 30 | #define R4 22 31 | 32 | // prototypes 33 | boolean connectWifi(); 34 | 35 | //callback functions 36 | void firstLightChanged(uint8_t brightness); 37 | void secondLightChanged(uint8_t brightness); 38 | void thirdLightChanged(uint8_t brightness); 39 | void fourthLightChanged(uint8_t brightness); 40 | 41 | // Change this!! 42 | 43 | // WiFi Credentials 44 | const char* ssid = "SmS_jiofi"; 45 | const char* password = "sms123458956"; 46 | 47 | // device names 48 | String Device_1_Name = "Office light"; 49 | String Device_2_Name = "Studio light"; 50 | String Device_3_Name = "Yellow Bulb"; 51 | String Device_4_Name = "Red bulb"; 52 | 53 | boolean wifiConnected = false; 54 | 55 | Espalexa espalexa; 56 | 57 | void setup() 58 | { 59 | Serial.begin(115200); 60 | 61 | pinMode(R1, OUTPUT); 62 | pinMode(R2, OUTPUT); 63 | pinMode(R3, OUTPUT); 64 | pinMode(R4, OUTPUT); 65 | 66 | // Initialise wifi connection 67 | wifiConnected = connectWifi(); 68 | 69 | if (wifiConnected) 70 | { 71 | 72 | // Define your devices here. 73 | espalexa.addDevice(Device_1_Name, firstLightChanged); //simplest definition, default state off 74 | espalexa.addDevice(Device_2_Name, secondLightChanged); 75 | espalexa.addDevice(Device_3_Name, thirdLightChanged); 76 | espalexa.addDevice(Device_4_Name, fourthLightChanged); 77 | 78 | espalexa.begin(); 79 | 80 | } 81 | 82 | else 83 | { 84 | while (1) 85 | { 86 | Serial.println("Cannot connect to WiFi. Please check data and reset the ESP."); 87 | delay(2500); 88 | } 89 | } 90 | 91 | } 92 | 93 | void loop() 94 | { 95 | espalexa.loop(); 96 | delay(1); 97 | } 98 | 99 | //our callback functions 100 | void firstLightChanged(uint8_t brightness) 101 | { 102 | //Control the device 103 | if (brightness) 104 | { 105 | if (brightness == 255) 106 | { 107 | digitalWrite(R1, HIGH); 108 | Serial.println("Device1 ON"); 109 | } 110 | //Serial.print("ON, brightness "); 111 | //Serial.println(brightness); 112 | } 113 | else 114 | { 115 | digitalWrite(R1, LOW); 116 | Serial.println("Device1 OFF"); 117 | } 118 | } 119 | 120 | void secondLightChanged(uint8_t brightness) 121 | { 122 | 123 | //Control the device 124 | if (brightness) 125 | { 126 | if (brightness == 255) 127 | { 128 | digitalWrite(R2, HIGH); 129 | Serial.println("Device2 ON"); 130 | } 131 | //Serial.print("ON, brightness "); 132 | //Serial.println(brightness); 133 | } 134 | else 135 | { 136 | digitalWrite(R2, LOW); 137 | Serial.println("Device2 OFF"); 138 | } 139 | } 140 | 141 | void thirdLightChanged(uint8_t brightness) 142 | { 143 | 144 | //Control the device 145 | if (brightness) 146 | { 147 | if (brightness == 255) 148 | { 149 | digitalWrite(R3, HIGH); 150 | Serial.println("Device3 ON"); 151 | } 152 | //Serial.print("ON, brightness "); 153 | //Serial.println(brightness); 154 | } 155 | else 156 | { 157 | digitalWrite(R3, LOW); 158 | Serial.println("Device3 OFF"); 159 | } 160 | } 161 | 162 | void fourthLightChanged(uint8_t brightness) 163 | { 164 | 165 | //Control the device 166 | if (brightness) 167 | { 168 | 169 | if (brightness == 255) 170 | { 171 | digitalWrite(R4, HIGH); 172 | Serial.println("Device4 ON"); 173 | } 174 | //Serial.print("ON, brightness "); 175 | //Serial.println(brightness); 176 | } 177 | else 178 | { 179 | digitalWrite(R4, LOW); 180 | Serial.println("Device4 OFF"); 181 | } 182 | } 183 | 184 | // connect to wifi – returns true if successful or false if not 185 | boolean connectWifi() 186 | { 187 | boolean state = true; 188 | int i = 0; 189 | 190 | WiFi.mode(WIFI_STA); 191 | WiFi.begin(ssid, password); 192 | Serial.println(""); 193 | Serial.println("Connecting to WiFi"); 194 | 195 | // Wait for connection 196 | Serial.print("Connecting..."); 197 | while (WiFi.status() != WL_CONNECTED) { 198 | delay(500); 199 | Serial.print("."); 200 | if (i > 20) { 201 | state = false; break; 202 | } 203 | i++; 204 | } 205 | Serial.println(""); 206 | if (state) { 207 | Serial.print("Connected to "); 208 | Serial.println(ssid); 209 | Serial.print("IP address: "); 210 | Serial.println(WiFi.localIP()); 211 | } 212 | else { 213 | Serial.println("Connection failed."); 214 | } 215 | return state; 216 | } 217 | --------------------------------------------------------------------------------