lukbe

NodeMCU -ESP8266 - RGB LED control

Feb 16th, 2016
1,533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2.  
  3. const char* ssid = "xxxx";
  4. const char* password = "xxxx";
  5. String number;
  6. float color;
  7.  
  8. #define blue_pin 14
  9. #define green_pin 12
  10. #define red_pin 13
  11.  
  12. WiFiServer server(80);
  13.  
  14. void setup() {
  15.  
  16. pinMode(blue_pin,OUTPUT);
  17. pinMode(green_pin,OUTPUT);
  18. pinMode(red_pin,OUTPUT);
  19. digitalWrite(blue_pin,HIGH);
  20. digitalWrite(green_pin,HIGH);
  21. digitalWrite(red_pin,HIGH);
  22.  
  23.  
  24. Serial.begin(115200);
  25. delay(10);
  26.  
  27.  
  28. // Connect to WiFi network
  29. Serial.println();
  30. Serial.println();
  31. Serial.print("Connecting to ");
  32. Serial.println(ssid);
  33.  
  34. WiFi.begin(ssid, password);
  35.  
  36. while (WiFi.status() != WL_CONNECTED) {
  37. delay(500);
  38. Serial.print(".");
  39. }
  40. Serial.println("");
  41. Serial.println("WiFi connected");
  42.  
  43. // Start the server
  44. server.begin();
  45. Serial.println("Server started");
  46.  
  47. // Print the IP address
  48. Serial.print("Use this URL to connect: ");
  49. Serial.print("http://");
  50. Serial.print(WiFi.localIP());
  51. Serial.println("/");
  52.  
  53. }
  54.  
  55. void loop() {
  56.  
  57. WiFiClient client = server.available();
  58. if (!client) {
  59. return;
  60. }
  61.  
  62. Serial.println("new client");
  63. while (!client.available()) {
  64. delay(1);
  65. }
  66.  
  67. String request = client.readStringUntil('\r');
  68. Serial.println(request);
  69. client.flush();
  70. number= request.substring(5, 10);
  71. Serial.println(number);
  72. color=number.toFloat();
  73. Serial.println(color);
  74.  
  75.  
  76. if (request.indexOf("/off") > 0) {
  77. analogWrite(blue_pin,0);
  78. analogWrite(green_pin,0);
  79. analogWrite(red_pin,0);
  80. }
  81.  
  82. if (request.indexOf("/white") > 0) {
  83. analogWrite(blue_pin,255);
  84. analogWrite(green_pin,255);
  85. analogWrite(red_pin,255);
  86. }
  87. if (color >= 1000 && color <=1255){
  88. int blue = color;
  89. blue = map(blue, 1000,1255,0,255);
  90. analogWrite(blue_pin,blue);
  91. Serial.println("modra");
  92. Serial.println(blue);
  93. delay(10);
  94.  
  95. }
  96.  
  97. if (color >=2000 && color <=2255){
  98. int green = color;
  99. green = map(green,2000,2255,0,255);
  100. analogWrite(green_pin,green);
  101. Serial.println("zelena");
  102. Serial.println(green);
  103. delay(10);
  104.  
  105. }
  106.  
  107. if (color >=3000 && color <= 3255){
  108. int red = color;
  109. red = map(red, 3000, 3255,0,255);
  110. analogWrite(red_pin,red);
  111. Serial.println("cervena");
  112. Serial.println(red);
  113. delay(10);
  114. }
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment