本帖最后由 沧海笑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。
附:程序代码:/* Web_AjaxRGB.pde - example sketch for Webduino library */
#include "SPI.h"
#include "Ethernet.h"
#include "WebServer.h"
// CHANGE THIS TO YOUR OWN UNIQUE VALUE
static uint8_t mac = { 0x02, 0xAA, 0xBB, 0xCC, 0x00, 0x22 };
// CHANGE THIS TO MATCH YOUR HOST NETWORK
static uint8_t ip = { 192, 168, 1, 210 }; // area 51!
/* all URLs on this server will start with /rgb because of how we
* define the PREFIX value.We also will listen on port 80, the
* standard HTTP service port */
#define PREFIX "/rgb"
WebServer webserver(PREFIX, 80);
#define RED_PIN 5
#define GREEN_PIN 3
#define BLUE_PIN 6
int red = 0; //integer for red darkness
int blue = 0; //integer for blue darkness
int green = 0; //integer for green darkness
/* This command is set as the default command for the server.It
* handles both GET and POST requests.For a GET, it returns a simple
* page with some buttons.For a POST, it saves the value posted to
* the red/green/blue variable, affecting the output of the speaker */
void rgbCmd(WebServer &server, WebServer::ConnectionType type, char *, bool)
{
if (type == WebServer::POST)
{
bool repeat;
char name, value;
do
{
/* readPOSTparam returns false when there are no more parameters
* to read from the input.We pass in buffers for it to store
* the name and value strings along with the length of those
* buffers. */
repeat = server.readPOSTparam(name, 16, value, 16);
/* this is a standard string comparison function.It returns 0
* when there's an exact match.We're looking for a parameter
* named red/green/blue here. */
if (strcmp(name, "red") == 0)
{
/* use the STRing TO Unsigned Long function to turn the string
* version of the color strength value into our integer red/green/blue
* variable */
red = strtoul(value, NULL, 10);
}
if (strcmp(name, "green") == 0)
{
green = strtoul(value, NULL, 10);
}
if (strcmp(name, "blue") == 0)
{
blue = strtoul(value, NULL, 10);
}
} while (repeat);
// after procesing the POST data, tell the web browser to reload
// the page using a GET method.
server.httpSeeOther(PREFIX);
// Serial.print(name);
// Serial.println(value);
return;
}
/* for a GET or HEAD, send the standard "it's all OK headers" */
server.httpSuccess();
/* we don't output the body for a HEAD request */
if (type == WebServer::GET)
{
/* store the HTML in program memory using the P macro */
P(message) =
"<!DOCTYPE html><html><head>"
"<title>Webduino AJAX RGB Example</title>"
"<link href='http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/base/jquery-ui.css' rel=stylesheet />"
"<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js'></script>"
"<script src='http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js'></script>"
"<style> body { background: black; } #red, #green, #blue { margin: 10px; } #red { background: #f00; } #green { background: #0f0; } #blue { background: #00f; } </style>"
"<script>"
// change color on mouse up, not while sliding (causes much less traffic to the Arduino):
// "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 } ); } "
// "$(document).ready(function(){ $('#red, #green, #blue').slider({min: 0, max:255, change:changeRGB}); });"
// change color on slide and mouse up (causes more traffic to the Arduino):
"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 } ); } "
"$(document).ready(function(){ $('#red, #green, #blue').slider({min: 0, max:255, change:changeRGB, slide:changeRGB}); });"
"</script>"
"</head>"
"<body style='font-size:62.5%;'>"
"<div id=red></div>"
"<div id=green></div>"
"<div id=blue></div>"
"</body>"
"</html>";
server.printP(message);
}
}
void setup()
{
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
Serial.begin(9600);
// setup the Ehternet library to talk to the Wiznet board
Ethernet.begin(mac, ip);
/* register our default command (activated with the request of
* http://x.x.x.x/rgb */
webserver.setDefaultCommand(&rgbCmd);
/* start the server to wait for connections */
webserver.begin();
}
void loop()
{
// process incoming connections one at a time forever
webserver.processConnection();
Serial.println(red);//调试后可删除
Serial.println(" ");//调试后可删除
Serial.println(green);//调试后可删除
Serial.println(" ");//调试后可删除
Serial.println(blue);//调试后可删除
analogWrite(RED_PIN, red);
analogWrite(GREEN_PIN, green);
analogWrite(BLUE_PIN, blue);
}代码来源: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。。值得一一尝试。
{:soso_e103:}功能太强大了
本帖最后由 沧海笑1122 于 2012-2-29 00:56 编辑
项目名称:使用webuino库,基于webserver调整无源蜂鸣器音调
时间:2012-02-28
元件:2560+W5100,5V无源蜂鸣器一只
电路:蜂鸣器正极接#3(PWM),负极接GND
访问http://192.168.1.210/buzz
可以通过调整调节棒,来调整蜂鸣器的音调,值越高频率越低(声音低沉);值越低频率越高(声音尖锐),值为0时关闭蜂鸣器。
源代码:/* Web_Buzzer.pde - example sketch for Webduino library */
#include "SPI.h"
#include "Ethernet.h"
#include "WebServer.h"
// CHANGE THIS TO YOUR OWN UNIQUE VALUE
static uint8_t mac = { 0x02, 0xAA, 0xBB, 0xCC, 0x00, 0x22 };
// CHANGE THIS TO MATCH YOUR HOST NETWORK
static uint8_t ip = { 192, 168, 1, 210 }; // area 51!
/* all URLs on this server will start with /buzz because of how we
* define the PREFIX value.We also will listen on port 80, the
* standard HTTP service port */
#define PREFIX "/buzz"
WebServer webserver(PREFIX, 80);
/* the piezo speaker on the Danger Shield is on PWM output pin #3 */
#define BUZZER_PIN 3
/* this is the number of microseconds to wait after turning the
* speaker on before turning it off. */
int buzzDelay = 0;
/* toggle is used to only turn on the speaker every other loop
iteration. */
char toggle = 0;
/* This command is set as the default command for the server.It
* handles both GET and POST requests.For a GET, it returns a simple
* page with some buttons.For a POST, it saves the value posted to
* the buzzDelay variable, affecting the output of the speaker */
void buzzCmd(WebServer &server, WebServer::ConnectionType type, char *, bool)
{
if (type == WebServer::POST)
{
bool repeat;
char name, value;
do
{
/* readPOSTparam returns false when there are no more parameters
* to read from the input.We pass in buffers for it to store
* the name and value strings along with the length of those
* buffers. */
repeat = server.readPOSTparam(name, 16, value, 16);
/* this is a standard string comparison function.It returns 0
* when there's an exact match.We're looking for a parameter
* named "buzz" here. */
if (strcmp(name, "buzz") == 0)
{
/* use the STRing TO Unsigned Long function to turn the string
* version of the delay number into our integer buzzDelay
* variable */
buzzDelay = strtoul(value, NULL, 10);
}
} while (repeat);
// after procesing the POST data, tell the web browser to reload
// the page using a GET method.
server.httpSeeOther(PREFIX);
return;
}
/* for a GET or HEAD, send the standard "it's all OK headers" */
server.httpSuccess();
/* we don't output the body for a HEAD request */
if (type == WebServer::GET)
{
/* store the HTML in program memory using the P macro */
P(message) =
"<!DOCTYPE html><html><head>"
"<title>Webduino AJAX Buzzer Example</title>"
"<link href='http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/base/jquery-ui.css' rel=stylesheet />"
//"<meta http-equiv='Content-Script-Type' content='text/javascript'>"
"<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js'></script>"
"<script src='http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js'></script>"
"<style> #slider { margin: 10px; } </style>"
"<script>"
"function changeBuzz(event, ui) { $('#indicator').text(ui.value); $.post('/buzz', { buzz: ui.value } ); }"
"$(document).ready(function(){ $('#slider').slider({min: 0, max:8000, change:changeBuzz}); });"
"</script>"
"</head>"
"<body style='font-size:62.5%;'>"
"<h1>Test the Buzzer!</h1>"
"<div id=slider></div>"
"<p id=indicator>0</p>"
"</body>"
"</html>";
server.printP(message);
}
}
void setup()
{
// set the PWM output for the buzzer to out
pinMode(BUZZER_PIN, OUTPUT);
// setup the Ehternet library to talk to the Wiznet board
Ethernet.begin(mac, ip);
/* register our default command (activated with the request of
* http://x.x.x.x/buzz */
webserver.setDefaultCommand(&buzzCmd);
/* start the server to wait for connections */
webserver.begin();
}
void loop()
{
// process incoming connections one at a time forever
webserver.processConnection();
/* every other time through the loop, turn on and off the speaker if
* our delay isn't set to 0. */
if ((++toggle & 1) && (buzzDelay > 0))
{
digitalWrite(BUZZER_PIN, HIGH);
delayMicroseconds(buzzDelay);
digitalWrite(BUZZER_PIN, LOW);
}
}
本帖最后由 沧海笑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控制无源蜂鸣器了。
{:soso_e179:}收藏
不错, 但是就是功能有点简单, arduino的RAM太小了。
牛,占座。
只支持w5100 能支持nec的网卡不?
yyy_zc 发表于 2012-6-19 12:20 static/image/common/back.gif
只支持w5100 能支持nec的网卡不?
库是基于5100写的。
LZ 我用的是ENC28J60
网络模块是链接到路由器还是电脑?
沧海笑1122 发表于 2012-6-19 15:06 static/image/common/back.gif
库是基于5100写的。
可以ping通 但是浏览器登录不上去{:3_60:}
心之永恒 发表于 2012-6-26 14:35 static/image/common/back.gif
LZ 我用的是ENC28J60
网络模块是链接到路由器还是电脑?
UNO+以太网扩展5100直接接到路由器上的。
沧海笑1122 发表于 2012-7-1 22:29 static/image/common/back.gif
UNO+以太网扩展5100直接接到路由器上的。
感谢 链接成功了 是淘宝卖家的库 {:3_62:}
看起来不错哦,还想不买5100呢,看样子少不了了:lol
mark mark;P