From 25a6bca31c63840261141d764bbd70331a268f19 Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Tue, 20 Mar 2012 23:25:34 +1300 Subject: Add Webduino library Signed-off-by: Mike Beattie --- .../Web_AjaxRGB_mobile/Web_AjaxRGB_mobile.ino | 140 +++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 Webduino/examples/Web_AjaxRGB_mobile/Web_AjaxRGB_mobile.ino (limited to 'Webduino/examples/Web_AjaxRGB_mobile/Web_AjaxRGB_mobile.ino') diff --git a/Webduino/examples/Web_AjaxRGB_mobile/Web_AjaxRGB_mobile.ino b/Webduino/examples/Web_AjaxRGB_mobile/Web_AjaxRGB_mobile.ino new file mode 100644 index 0000000..aa49ee2 --- /dev/null +++ b/Webduino/examples/Web_AjaxRGB_mobile/Web_AjaxRGB_mobile.ino @@ -0,0 +1,140 @@ +/* Web_AjaxRGB_mobile.pde - example sketch for Webduino library */ +/* - offers web-based slider controllers for RGB led - */ + +#include "SPI.h" +#include "Ethernet.h" +#include "WebServer.h" + +// CHANGE THIS TO YOUR OWN UNIQUE VALUE +static uint8_t mac[6] = { 0x02, 0xAA, 0xBB, 0xCC, 0x00, 0x22 }; + +// CHANGE THIS TO MATCH YOUR HOST NETWORK +static uint8_t ip[4] = { 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[16], value[16]; + 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) = +"" + "" + "Webduino RGB" + "" + "" + "" + "" + "" +"" +"" + "

Webduino RGB

" + "
" + "" + "" + "" + "
" + "" +""; + + 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.print(red); +// Serial.print(" "); +// Serial.print(green); +// Serial.print(" "); +// Serial.println(blue); + analogWrite(RED_PIN, red); + analogWrite(GREEN_PIN, green); + analogWrite(BLUE_PIN, blue); +} -- cgit v1.2.3