├── README.md └── esp8266Server_RGBLED.ino /README.md: -------------------------------------------------------------------------------- 1 | # ESP8266WiFi RGBLED 2 | This project we use ESP8266 WiFi Module to control RGB LED via WiFi Network. 3 | 4 | Hardware 5 | 1.ESP8266 WiFi Module ( or NodeMCU ) 6 | 2.RGB LED 7 | 3.OLED Display 8 | 9 | Wiring Diagram 10 | ESP8266 Pin GPIO_14 to LED R 11 | ESP8266 Pin GPIO_12 to LED G 12 | ESP8266 Pin GPIO_13 to LED B 13 | 14 | Option OLED Display 15 | ESP8266 Pin GPIO_05 ( SCL ) to OLED pin SCL 16 | ESP8266 Pin GPIO_04 ( SDA ) to OLED pin SDA 17 | 18 | BLOG 19 | http://microcontrollerkits.blogspot.com/2016/05/esp8266-wifi-control-rgbled.html 20 | -------------------------------------------------------------------------------- /esp8266Server_RGBLED.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #define OLED_RESET 4 9 | Adafruit_SSD1306 display(OLED_RESET); 10 | 11 | #if (SSD1306_LCDHEIGHT != 64) 12 | #error("Height incorrect, please fix Adafruit_SSD1306.h!"); 13 | #endif 14 | 15 | const char* ssid = "Connectify-me"; 16 | const char* password = "12345678"; 17 | 18 | ESP8266WebServer server(80); 19 | 20 | const int R = 14; 21 | const int G = 12; 22 | const int B = 13; 23 | 24 | void handleRoot() { 25 | 26 | String red = server.arg(0); // read RGB arguments 27 | String green = server.arg(1); 28 | String blue = server.arg(2); 29 | 30 | analogWrite(R, red.toInt()); 31 | analogWrite(G, green.toInt()); 32 | analogWrite(B, blue.toInt()); 33 | 34 | Serial.println(red.toInt()); // for TESTING 35 | Serial.println(green.toInt()); // for TESTING 36 | Serial.println(blue.toInt()); // for TESTING 37 | 38 | display.clearDisplay(); 39 | display.setTextColor(WHITE); 40 | 41 | display.setTextSize(2); 42 | display.setTextColor(WHITE); 43 | display.setCursor(0,0); 44 | display.print("R = "); display.println(red.toInt()); 45 | display.setCursor(0,18); 46 | display.print("G = "); display.println(green.toInt()); 47 | display.setCursor(0,36); 48 | display.print("B = "); display.println(blue.toInt()); 49 | display.display(); 50 | 51 | String webpage; 52 | webpage += "\r\n"; 53 | webpage += "\r\n"; 54 | webpage += "
ESP8266 RGB LED

ESP8266 RGBLED

"; 55 | 56 | webpage += ""; 57 | webpage += ""; 58 | webpage += ""; 59 | webpage += ""; 60 | 61 | webpage += ""; 62 | webpage += ""; 63 | 64 | webpage += "\r\n"; 129 | 130 | server.send(200, "text/html", webpage); 131 | } 132 | 133 | 134 | void testRGB() { // fade in and out of Red, Green, Blue 135 | analogWrite(R, 1023); // R off 136 | analogWrite(G, 1023); // G off 137 | analogWrite(B, 1023); // B off 138 | fade(R); // R 139 | fade(G); // G 140 | fade(B); // B 141 | } 142 | 143 | void fade(int pin) { 144 | for (int u = 0; u < 1024; u++) { 145 | analogWrite(pin, 1023 - u); 146 | delay(1); 147 | } 148 | for (int u = 0; u < 1024; u++) { 149 | analogWrite(pin, u); 150 | delay(1); 151 | } 152 | } 153 | 154 | void handleNotFound(){ 155 | //digitalWrite(led, 1); 156 | String message = "File Not Found\n\n"; 157 | message += "URI: "; 158 | message += server.uri(); 159 | message += "\nMethod: "; 160 | message += (server.method() == HTTP_GET)?"GET":"POST"; 161 | message += "\nArguments: "; 162 | message += server.args(); 163 | message += "\n"; 164 | for (uint8_t i=0; i>1); // init done 187 | 188 | display.clearDisplay(); // Clear the buffer. 189 | display.setTextSize(2); 190 | display.setTextColor(WHITE); 191 | //display.setTextColor(BLACK, WHITE); // 'inverted' text 192 | display.setCursor(0,0); 193 | display.println(" ESP8266"); 194 | 195 | display.setTextSize(3); //Size4 = 5 digit , size3 = 7 digits 196 | //display.setTextColor(BLACK, WHITE); // 'inverted' text 197 | display.setTextColor(WHITE); 198 | display.setCursor(0,20); 199 | display.println("RGB LED"); 200 | 201 | display.setTextSize(1); 202 | display.setTextColor(WHITE); 203 | //display.setTextColor(BLACK, WHITE); // 'inverted' text 204 | display.setCursor(0,52); 205 | display.println("Version 0.1"); 206 | 207 | display.display(); 208 | delay(2000); 209 | 210 | display.clearDisplay(); 211 | display.setTextSize(2); 212 | display.setTextColor(WHITE); 213 | //display.setTextColor(BLACK, WHITE); // 'inverted' text 214 | display.setCursor(0,0); 215 | display.println("Connecting"); 216 | 217 | 218 | // Wait for connection 219 | while (WiFi.status() != WL_CONNECTED) { 220 | delay(500); 221 | Serial.print("."); 222 | display.print("."); 223 | display.display(); 224 | } 225 | Serial.println(""); 226 | Serial.print("Connected to "); 227 | Serial.println(ssid); 228 | Serial.print("IP address: "); 229 | Serial.println(WiFi.localIP()); 230 | 231 | display.clearDisplay(); 232 | display.setTextSize(1); display.setTextColor(WHITE); 233 | display.setCursor(0,0); display.print("SSID:"); display.println(ssid); 234 | display.setTextSize(2); display.setTextColor(WHITE); 235 | display.setCursor(0,18); display.println(WiFi.localIP()); 236 | display.display(); 237 | 238 | if (MDNS.begin("esp8266")) { 239 | Serial.println("MDNS responder started"); 240 | } 241 | 242 | server.on("/", handleRoot); 243 | testRGB(); 244 | 245 | server.onNotFound(handleNotFound); 246 | server.begin(); 247 | Serial.println("HTTP server started"); 248 | } 249 | 250 | void loop(void){ 251 | server.handleClient(); 252 | } 253 | --------------------------------------------------------------------------------