极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

楼主: 沧海笑1122

关于Webduino--Arduino-based Web Server library(新增控制蜂鸣器+互联网访问)

  [复制链接]
 楼主| 发表于 2012-2-28 00:58:00 | 显示全部楼层
本帖最后由 沧海笑1122 于 2012-2-28 01:00 编辑

项目内容:使用webuino,通过webserver控制RGB灯
试验时间:2012-02-27
板件和元器件:2560+W5100以太网扩展板,RGB LED模块一只
                     RED    6pin
                     BLUE   5pin
                     GREEN 3Pin
试验内容:
通过webserver控制RGB LED亮度。
将W5100和PC分别连接至交换机。编译、上传程序后,LED灯亮度几乎为零,程序初始值为0。(附接线情况及初始状态)

打开串口助手,观察RGB变化情况。

第一步:调整红色,滑动调节棒,在串口助手中观察RED值变大,同时LED灯变为红色,此时其余两色值为0。


第二步:调整绿色,滑动调节棒,在串口助手中观察GREEN值变大,同时LED灯变为绿色,此时其余两色值为0。


第三步:调整蓝色,滑动调节棒,在串口助手中观察BLUE值变大,同时LED灯变为蓝色,此时其余两色值为0。



附:程序代码:
  1. /* Web_AjaxRGB.pde - example sketch for Webduino library */

  2. #include "SPI.h"
  3. #include "Ethernet.h"
  4. #include "WebServer.h"

  5. // CHANGE THIS TO YOUR OWN UNIQUE VALUE
  6. static uint8_t mac[6] = { 0x02, 0xAA, 0xBB, 0xCC, 0x00, 0x22 };

  7. // CHANGE THIS TO MATCH YOUR HOST NETWORK
  8. static uint8_t ip[4] = { 192, 168, 1, 210 }; // area 51!

  9. /* all URLs on this server will start with /rgb because of how we
  10. * define the PREFIX value.  We also will listen on port 80, the
  11. * standard HTTP service port */
  12. #define PREFIX "/rgb"
  13. WebServer webserver(PREFIX, 80);

  14. #define RED_PIN 5
  15. #define GREEN_PIN 3
  16. #define BLUE_PIN 6

  17. int red = 0;            //integer for red darkness
  18. int blue = 0;           //integer for blue darkness
  19. int green = 0;          //integer for green darkness

  20. /* This command is set as the default command for the server.  It
  21. * handles both GET and POST requests.  For a GET, it returns a simple
  22. * page with some buttons.  For a POST, it saves the value posted to
  23. * the red/green/blue variable, affecting the output of the speaker */
  24. void rgbCmd(WebServer &server, WebServer::ConnectionType type, char *, bool)
  25. {
  26.   if (type == WebServer::POST)
  27.   {
  28.     bool repeat;
  29.     char name[16], value[16];
  30.     do
  31.     {
  32.       /* readPOSTparam returns false when there are no more parameters
  33.        * to read from the input.  We pass in buffers for it to store
  34.        * the name and value strings along with the length of those
  35.        * buffers. */
  36.       repeat = server.readPOSTparam(name, 16, value, 16);

  37.       /* this is a standard string comparison function.  It returns 0
  38.        * when there's an exact match.  We're looking for a parameter
  39.        * named red/green/blue here. */
  40.       if (strcmp(name, "red") == 0)
  41.       {
  42.         /* use the STRing TO Unsigned Long function to turn the string
  43.          * version of the color strength value into our integer red/green/blue
  44.          * variable */
  45.         red = strtoul(value, NULL, 10);
  46.       }
  47.       if (strcmp(name, "green") == 0)
  48.       {
  49.         green = strtoul(value, NULL, 10);
  50.       }
  51.       if (strcmp(name, "blue") == 0)
  52.       {
  53.         blue = strtoul(value, NULL, 10);
  54.       }
  55.     } while (repeat);
  56.    
  57.     // after procesing the POST data, tell the web browser to reload
  58.     // the page using a GET method.
  59.     server.httpSeeOther(PREFIX);
  60. //    Serial.print(name);
  61. //    Serial.println(value);

  62.     return;
  63.   }

  64.   /* for a GET or HEAD, send the standard "it's all OK headers" */
  65.   server.httpSuccess();

  66.   /* we don't output the body for a HEAD request */
  67.   if (type == WebServer::GET)
  68.   {
  69.     /* store the HTML in program memory using the P macro */
  70.     P(message) =
  71. "<!DOCTYPE html><html><head>"
  72.   "<title>Webduino AJAX RGB Example</title>"
  73.   "<link href='http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/base/jquery-ui.css' rel=stylesheet />"
  74.   "<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js'></script>"
  75.   "<script src='http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js'></script>"
  76.   "<style> body { background: black; } #red, #green, #blue { margin: 10px; } #red { background: #f00; } #green { background: #0f0; } #blue { background: #00f; } </style>"
  77.   "<script>"

  78. // change color on mouse up, not while sliding (causes much less traffic to the Arduino):
  79. //    "function changeRGB(event, ui) { var id = $(this).attr('id'); if (id == 'red') $.post('/rgb', { red: ui.value } ); if (id == 'green') $.post('/rgb', { green: ui.value } ); if (id == 'blue') $.post('/rgb', { blue: ui.value } ); } "
  80. //    "$(document).ready(function(){ $('#red, #green, #blue').slider({min: 0, max:255, change:changeRGB}); });"

  81. // change color on slide and mouse up (causes more traffic to the Arduino):
  82.     "function changeRGB(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'); if (id == 'red') $.post('/rgb', { red: ui.value } ); if (id == 'green') $.post('/rgb', { green: ui.value } ); if (id == 'blue') $.post('/rgb', { blue: ui.value } ); } "
  83.     "$(document).ready(function(){ $('#red, #green, #blue').slider({min: 0, max:255, change:changeRGB, slide:changeRGB}); });"

  84.   "</script>"
  85. "</head>"
  86. "<body style='font-size:62.5%;'>"
  87.   "<div id=red></div>"
  88.   "<div id=green></div>"
  89.   "<div id=blue></div>"
  90. "</body>"
  91. "</html>";

  92.     server.printP(message);
  93.   }
  94. }

  95. void setup()
  96. {
  97.   pinMode(RED_PIN, OUTPUT);
  98.   pinMode(GREEN_PIN, OUTPUT);
  99.   pinMode(BLUE_PIN, OUTPUT);

  100. Serial.begin(9600);

  101.   // setup the Ehternet library to talk to the Wiznet board
  102.   Ethernet.begin(mac, ip);

  103.   /* register our default command (activated with the request of
  104.    * http://x.x.x.x/rgb */
  105.   webserver.setDefaultCommand(&rgbCmd);

  106.   /* start the server to wait for connections */
  107.   webserver.begin();
  108. }

  109. void loop()
  110. {
  111.   // process incoming connections one at a time forever
  112.   webserver.processConnection();
  113.   Serial.println(red);//调试后可删除
  114.   Serial.println(" ");//调试后可删除
  115.   Serial.println(green);//调试后可删除
  116.   Serial.println(" ");//调试后可删除
  117.   Serial.println(blue);//调试后可删除
  118.   analogWrite(RED_PIN, red);
  119.   analogWrite(GREEN_PIN, green);
  120.   analogWrite(BLUE_PIN, blue);
  121. }
复制代码
代码来源:http://code.google.com/p/webduino/

第一部分:声明网络参数、定义RGB对应pin,连接webserver(注:我在测试中,发现代码中GB是反的,调整过来正常,还没有认真分析)
第二部分:网页代码,http://192.168.1.210/rgb,编写RGB三个滑动棒。
第三部分:主程序,loop部分,webserver.processConnection();
                同时可以从串口助手观察RGB的亮度值,仅供调试,调试成功后,此部分代码连同setup中的打开串口的代码都可以删除。


这个小程序是webuino的实例,并未改编。证明使用webserver.h库,通过webserver调整RGB亮度非常简单。
我家里使用无线路由器上网,在使用动态域名解析后,很容易通过互联网控制arduino的LED灯。
webuino还可以控制蜂鸣器、舵机、PWM。。值得一一尝试。



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

发表于 2012-2-28 09:51:40 | 显示全部楼层
{:soso_e103:}功能太强大了
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-2-29 00:51:12 | 显示全部楼层
本帖最后由 沧海笑1122 于 2012-2-29 00:56 编辑

项目名称:使用webuino库,基于webserver调整无源蜂鸣器音调
时间:2012-02-28
元件:2560+W5100,5V无源蜂鸣器一只
电路:蜂鸣器正极接#3(PWM),负极接GND

访问http://192.168.1.210/buzz

可以通过调整调节棒,来调整蜂鸣器的音调,值越高频率越低(声音低沉);值越低频率越高(声音尖锐),值为0时关闭蜂鸣器。


源代码:
  1. /* Web_Buzzer.pde - example sketch for Webduino library */

  2. #include "SPI.h"
  3. #include "Ethernet.h"
  4. #include "WebServer.h"

  5. // CHANGE THIS TO YOUR OWN UNIQUE VALUE
  6. static uint8_t mac[6] = { 0x02, 0xAA, 0xBB, 0xCC, 0x00, 0x22 };

  7. // CHANGE THIS TO MATCH YOUR HOST NETWORK
  8. static uint8_t ip[4] = { 192, 168, 1, 210 }; // area 51!

  9. /* all URLs on this server will start with /buzz because of how we
  10. * define the PREFIX value.  We also will listen on port 80, the
  11. * standard HTTP service port */
  12. #define PREFIX "/buzz"
  13. WebServer webserver(PREFIX, 80);

  14. /* the piezo speaker on the Danger Shield is on PWM output pin #3 */
  15. #define BUZZER_PIN 3

  16. /* this is the number of microseconds to wait after turning the
  17. * speaker on before turning it off. */
  18. int buzzDelay = 0;

  19. /* toggle is used to only turn on the speaker every other loop
  20. iteration. */
  21. char toggle = 0;

  22. /* This command is set as the default command for the server.  It
  23. * handles both GET and POST requests.  For a GET, it returns a simple
  24. * page with some buttons.  For a POST, it saves the value posted to
  25. * the buzzDelay variable, affecting the output of the speaker */
  26. void buzzCmd(WebServer &server, WebServer::ConnectionType type, char *, bool)
  27. {
  28.   if (type == WebServer::POST)
  29.   {
  30.     bool repeat;
  31.     char name[16], value[16];
  32.     do
  33.     {
  34.       /* readPOSTparam returns false when there are no more parameters
  35.        * to read from the input.  We pass in buffers for it to store
  36.        * the name and value strings along with the length of those
  37.        * buffers. */
  38.       repeat = server.readPOSTparam(name, 16, value, 16);

  39.       /* this is a standard string comparison function.  It returns 0
  40.        * when there's an exact match.  We're looking for a parameter
  41.        * named "buzz" here. */
  42.       if (strcmp(name, "buzz") == 0)
  43.       {
  44.         /* use the STRing TO Unsigned Long function to turn the string
  45.          * version of the delay number into our integer buzzDelay
  46.          * variable */
  47.         buzzDelay = strtoul(value, NULL, 10);
  48.       }
  49.     } while (repeat);
  50.    
  51.     // after procesing the POST data, tell the web browser to reload
  52.     // the page using a GET method.
  53.     server.httpSeeOther(PREFIX);
  54.     return;
  55.   }

  56.   /* for a GET or HEAD, send the standard "it's all OK headers" */
  57.   server.httpSuccess();

  58.   /* we don't output the body for a HEAD request */
  59.   if (type == WebServer::GET)
  60.   {
  61.     /* store the HTML in program memory using the P macro */
  62.     P(message) =
  63. "<!DOCTYPE html><html><head>"
  64.   "<title>Webduino AJAX Buzzer Example</title>"
  65.   "<link href='http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/base/jquery-ui.css' rel=stylesheet />"
  66.   //"<meta http-equiv='Content-Script-Type' content='text/javascript'>"
  67.   "<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js'></script>"
  68.   "<script src='http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js'></script>"
  69.   "<style> #slider { margin: 10px; } </style>"
  70.   "<script>"
  71.     "function changeBuzz(event, ui) { $('#indicator').text(ui.value); $.post('/buzz', { buzz: ui.value } ); }"
  72.     "$(document).ready(function(){ $('#slider').slider({min: 0, max:8000, change:changeBuzz}); });"
  73.   "</script>"
  74. "</head>"
  75. "<body style='font-size:62.5%;'>"
  76.   "<h1>Test the Buzzer!</h1>"
  77.   "<div id=slider></div>"
  78.   "<p id=indicator>0</p>"
  79. "</body>"
  80. "</html>";

  81.     server.printP(message);
  82.   }
  83. }

  84. void setup()
  85. {
  86.   // set the PWM output for the buzzer to out
  87.   pinMode(BUZZER_PIN, OUTPUT);

  88.   // setup the Ehternet library to talk to the Wiznet board
  89.   Ethernet.begin(mac, ip);

  90.   /* register our default command (activated with the request of
  91.    * http://x.x.x.x/buzz */
  92.   webserver.setDefaultCommand(&buzzCmd);

  93.   /* start the server to wait for connections */
  94.   webserver.begin();
  95. }

  96. void loop()
  97. {
  98.   // process incoming connections one at a time forever
  99.   webserver.processConnection();

  100.   /* every other time through the loop, turn on and off the speaker if
  101.    * our delay isn't set to 0. */
  102.   if ((++toggle & 1) && (buzzDelay > 0))
  103.   {
  104.     digitalWrite(BUZZER_PIN, HIGH);
  105.     delayMicroseconds(buzzDelay);
  106.     digitalWrite(BUZZER_PIN, LOW);
  107.   }
  108. }
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-2-29 22:33:45 | 显示全部楼层
本帖最后由 沧海笑1122 于 2012-2-29 22:47 编辑

项目名称:利用花生壳动态域名,从互联网访问2560+W5100
时间:2012-02-29
电路:同上(利用webserver控制无源蜂鸣器)

第一步:在花生壳注册护照,获得一个免费域名net****.****.cc
第二步:在路由器上设置,动态DNS,用花生壳注册的用户名和密码登录,
第三步:在路由器上设置“转发设置”,将广域网访问的80端口与W5100的IP地址建立转发(W5100使用80端口,你也可以改)。
第四步:在浏览器上输入http://net****.****.cc/buzz,就可以使用webserver控制无源蜂鸣器了。


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

发表于 2012-3-1 11:49:58 | 显示全部楼层
{:soso_e179:}收藏
回复 支持 反对

使用道具 举报

发表于 2012-3-1 15:24:21 | 显示全部楼层
不错, 但是就是功能有点简单, arduino的RAM太小了。
回复 支持 反对

使用道具 举报

发表于 2012-3-1 15:56:43 | 显示全部楼层
牛,占座。
回复 支持 反对

使用道具 举报

发表于 2012-6-19 12:20:05 | 显示全部楼层
只支持w5100 能支持nec的网卡不?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-6-19 15:06:31 | 显示全部楼层
yyy_zc 发表于 2012-6-19 12:20
只支持w5100 能支持nec的网卡不?

库是基于5100写的。
回复 支持 反对

使用道具 举报

发表于 2012-6-26 14:35:20 | 显示全部楼层
LZ 我用的是ENC28J60
网络模块是链接到路由器还是电脑?
回复 支持 反对

使用道具 举报

发表于 2012-6-26 16:15:03 | 显示全部楼层
沧海笑1122 发表于 2012-6-19 15:06
库是基于5100写的。

可以ping通 但是浏览器登录不上去{:3_60:}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-7-1 22:29:34 | 显示全部楼层
心之永恒 发表于 2012-6-26 14:35
LZ 我用的是ENC28J60
网络模块是链接到路由器还是电脑?

UNO+以太网扩展5100直接接到路由器上的。
回复 支持 反对

使用道具 举报

发表于 2012-7-3 10:37:20 | 显示全部楼层
沧海笑1122 发表于 2012-7-1 22:29
UNO+以太网扩展5100直接接到路由器上的。

感谢 链接成功了 是淘宝卖家的库 {:3_62:}
回复 支持 反对

使用道具 举报

发表于 2012-9-29 10:54:53 | 显示全部楼层
看起来不错哦,还想不买5100呢,看样子少不了了
回复 支持 反对

使用道具 举报

发表于 2013-3-31 18:17:34 | 显示全部楼层
mark mark
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-5-6 07:44 , Processed in 0.046324 second(s), 24 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表