Advertisement
computermuseo

controllo led RGB con arduino web ethernet

Mar 11th, 2015
1,081
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. /* Web_AjaxRGB_mobile.pde - example sketch for Webduino library */
  2. /* - offers web-based slider controllers for RGB led - */
  3.  
  4. #include "SPI.h"
  5. #include "Ethernet.h"
  6. #include "WebServer.h"
  7.  
  8. // CHANGE THIS TO YOUR OWN UNIQUE VALUE
  9. static uint8_t mac[6] = { 0x02, 0xAA, 0xBB, 0xCC, 0x00, 0x22 };
  10.  
  11. // CHANGE THIS TO MATCH YOUR HOST NETWORK
  12. static uint8_t ip[4] = { 192, 168, 1, 210 }; // area 51!
  13.  
  14. /* all URLs on this server will start with /rgb because of how we
  15. * define the PREFIX value. We also will listen on port 80, the
  16. * standard HTTP service port */
  17. #define PREFIX "/rgb"
  18. WebServer webserver(PREFIX, 80);
  19.  
  20. #define RED_PIN 5
  21. #define GREEN_PIN 3
  22. #define BLUE_PIN 6
  23.  
  24. int red = 0; //integer for red darkness
  25. int blue = 0; //integer for blue darkness
  26. int green = 0; //integer for green darkness
  27.  
  28. /* This command is set as the default command for the server. It
  29. * handles both GET and POST requests. For a GET, it returns a simple
  30. * page with some buttons. For a POST, it saves the value posted to
  31. * the red/green/blue variable, affecting the output of the speaker */
  32. void rgbCmd(WebServer &server, WebServer::ConnectionType type, char *, bool)
  33. {
  34. if (type == WebServer::POST)
  35. {
  36. bool repeat;
  37. char name[16], value[16];
  38. do
  39. {
  40. /* readPOSTparam returns false when there are no more parameters
  41. * to read from the input. We pass in buffers for it to store
  42. * the name and value strings along with the length of those
  43. * buffers. */
  44. repeat = server.readPOSTparam(name, 16, value, 16);
  45.  
  46. /* this is a standard string comparison function. It returns 0
  47. * when there's an exact match. We're looking for a parameter
  48. * named red/green/blue here. */
  49. if (strcmp(name, "red") == 0)
  50. {
  51. /* use the STRing TO Unsigned Long function to turn the string
  52. * version of the color strength value into our integer red/green/blue
  53. * variable */
  54. red = strtoul(value, NULL, 10);
  55. }
  56. if (strcmp(name, "green") == 0)
  57. {
  58. green = strtoul(value, NULL, 10);
  59. }
  60. if (strcmp(name, "blue") == 0)
  61. {
  62. blue = strtoul(value, NULL, 10);
  63. }
  64. } while (repeat);
  65.  
  66. // after procesing the POST data, tell the web browser to reload
  67. // the page using a GET method.
  68. server.httpSeeOther(PREFIX);
  69. // Serial.print(name);
  70. // Serial.println(value);
  71.  
  72. return;
  73. }
  74.  
  75. /* for a GET or HEAD, send the standard "it's all OK headers" */
  76. server.httpSuccess();
  77.  
  78. /* we don't output the body for a HEAD request */
  79. if (type == WebServer::GET)
  80. {
  81. /* store the HTML in program memory using the P macro */
  82. P(message) =
  83. "<!DOCTYPE html><html><head>"
  84. "<meta charset=\"utf-8\"><meta name=\"apple-mobile-web-app-capable\" content=\"yes\" /><meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\"><meta name=\"viewport\" content=\"width=device-width, user-scalable=no\">"
  85. "<title>Webduino RGB</title>"
  86. "<link rel=\"stylesheet\" href=\"http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.css\" />"
  87. "<script src=\"http://code.jquery.com/jquery-1.6.4.min.js\"></script>"
  88. "<script src=\"http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.js\"></script>"
  89. "<style> body, .ui-page { background: black; } .ui-body { padding-bottom: 1.5em; } div.ui-slider { width: 88%; } #red, #green, #blue { display: block; margin: 10px; } #red { background: #f00; } #green { background: #0f0; } #blue { background: #00f; } </style>"
  90. "<script>"
  91. // causes the Arduino to hang quite frequently (more often than Web_AjaxRGB.pde), probably due to the different event triggering the ajax requests
  92. "$(document).ready(function(){ $('#red, #green, #blue').slider; $('#red, #green, #blue').bind( 'change', function(event, ui) { jQuery.ajaxSetup({timeout: 110}); /*not to DDoS the Arduino, you might have to change this to some threshold value that fits your setup*/ var id = $(this).attr('id'); var strength = $(this).val(); if (id == 'red') $.post('/rgb', { red: strength } ); if (id == 'green') $.post('/rgb', { green: strength } ); if (id == 'blue') $.post('/rgb', { blue: strength } ); });});"
  93. "</script>"
  94. "</head>"
  95. "<body>"
  96. "<div data-role=\"header\" data-position=\"inline\"><h1>Webduino RGB</h1></div>"
  97. "<div class=\"ui-body ui-body-a\">"
  98. "<input type=\"range\" name=\"slider\" id=\"red\" value=\"0\" min=\"0\" max=\"255\" />"
  99. "<input type=\"range\" name=\"slider\" id=\"green\" value=\"0\" min=\"0\" max=\"255\" />"
  100. "<input type=\"range\" name=\"slider\" id=\"blue\" value=\"0\" min=\"0\" max=\"255\" />"
  101. "</div>"
  102. "</body>"
  103. "</html>";
  104.  
  105. server.printP(message);
  106. }
  107. }
  108.  
  109. void setup()
  110. {
  111. pinMode(RED_PIN, OUTPUT);
  112. pinMode(GREEN_PIN, OUTPUT);
  113. pinMode(BLUE_PIN, OUTPUT);
  114.  
  115. // Serial.begin(9600);
  116.  
  117. // setup the Ehternet library to talk to the Wiznet board
  118. Ethernet.begin(mac, ip);
  119.  
  120. /* register our default command (activated with the request of
  121. * http://x.x.x.x/rgb */
  122. webserver.setDefaultCommand(&rgbCmd);
  123.  
  124. /* start the server to wait for connections */
  125. webserver.begin();
  126. }
  127.  
  128. void loop()
  129. {
  130. // process incoming connections one at a time forever
  131. webserver.processConnection();
  132. // Serial.print(red);
  133. // Serial.print(" ");
  134. // Serial.print(green);
  135. // Serial.print(" ");
  136. // Serial.println(blue);
  137. analogWrite(RED_PIN, red);
  138. analogWrite(GREEN_PIN, green);
  139. analogWrite(BLUE_PIN, blue);
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement