Lisbon
Offline
Newbie
Karma: 0
Posts: 12
|
 |
« on: March 29, 2011, 10:11:48 am » |
Hi I find what i do but have a question I see the project "BlinkLed" in "Arduino0022" How put one more comand code : // -*- c++ -*- // // Copyright 2010 Ovidiu Predescu <ovidiu@gmail.com> // Date: December 2010 //
#include <SPI.h> #include <Ethernet.h> #include <Flash.h> #include <SdFat.h> #include <SdFatUtil.h> #include <TinyWebServer.h>
// The LED attached in PIN 13 on an Arduino board. const int ledPin = 7;
// The initial state of the LED int ledState = LOW;
void setLedEnabled(boolean state) { ledState = state; digitalWrite(ledPin, ledState); }
inline boolean getLedState() { return ledState; }
boolean file_handler(TinyWebServer& web_server); boolean blink_led_handler(TinyWebServer& web_server); boolean led_status_handler(TinyWebServer& web_server); boolean index_handler(TinyWebServer& web_server);
TinyWebServer::PathHandler handlers[] = { // Work around Arduino's IDE preprocessor bug in handling /* inside // strings. // // `put_handler' is defined in TinyWebServer {"/", TinyWebServer::GET, &index_handler }, {"/upload/" "*", TinyWebServer::PUT, &TinyWebPutHandler::put_handler }, {"/blinkled", TinyWebServer::POST, &blink_led_handler }, {"/ledstatus" "*", TinyWebServer::GET, &led_status_handler }, {"/" "*", TinyWebServer::GET, &file_handler }, {NULL}, };
const char* headers[] = { "Content-Length", NULL };
TinyWebServer web = TinyWebServer(handlers, headers);
boolean has_filesystem = true; Sd2Card card; SdVolume volume; SdFile root; SdFile file;
static uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// Don't forget to modify the IP to an available one on your home network byte ip[] = { 192, 42, 172, 237 };
void send_file_name(TinyWebServer& web_server, const char* filename) { if (!filename) { web_server.send_error_code(404); web_server << F("Could not parse URL"); } else { TinyWebServer::MimeType mime_type = TinyWebServer::get_mime_type_from_filename(filename); web_server.send_error_code(200); web_server.send_content_type(mime_type); web_server.end_headers(); if (file.open(&root, filename, O_READ)) { Serial << F("Read file "); Serial.println(filename); web_server.send_file(file); file.close(); } else { web_server << F("Could not find file: ") << filename << "\n"; } } }
boolean file_handler(TinyWebServer& web_server) { char* filename = TinyWebServer::get_file_from_path(web_server.get_path()); send_file_name(web_server, filename); free(filename); return true; }
boolean blink_led_handler(TinyWebServer& web_server) { web_server.send_error_code(200); web_server.send_content_type("text/plain"); web_server.end_headers(); // Reverse the state of the LED. setLedEnabled(!getLedState()); Client& client = web_server.get_client(); if (client.available()) { char ch = (char)client.read(); if (ch == '0') { setLedEnabled(false); } else if (ch == '1') { setLedEnabled(true); } } }
boolean led_status_handler(TinyWebServer& web_server) { web_server.send_error_code(200); web_server.send_content_type("text/plain"); web_server.end_headers(); Client& client = web_server.get_client(); client.println(getLedState(), DEC); }
boolean index_handler(TinyWebServer& web_server) { send_file_name(web_server, "INDEX.HTM"); return true; }
void file_uploader_handler(TinyWebServer& web_server, TinyWebPutHandler::PutAction action, char* buffer, int size) { static uint32_t start_time; static uint32_t total_size;
switch (action) { case TinyWebPutHandler::START: start_time = millis(); total_size = 0; if (!file.isOpen()) { // File is not opened, create it. First obtain the desired name // from the request path. char* fname = web_server.get_file_from_path(web_server.get_path()); if (fname) { Serial << F("Creating ") << fname << "\n"; file.open(&root, fname, O_CREAT | O_WRITE | O_TRUNC); free(fname); } } break;
case TinyWebPutHandler::WRITE: if (file.isOpen()) { file.write(buffer, size); total_size += size; } break;
case TinyWebPutHandler::END: file.sync(); Serial << F("Wrote ") << file.fileSize() << F(" bytes in ") << millis() - start_time << F(" millis (received ") << total_size << F(" bytes)\n"); file.close(); } }
void setup() { Serial.begin(115200); Serial << F("Free RAM: ") << FreeRam() << "\n";
pinMode(ledPin, OUTPUT); setLedEnabled(false);
// initialize the SD card Serial << F("Setting up SD card...\n"); pinMode(10, OUTPUT); // set the SS pin as an output (necessary!) digitalWrite(10, HIGH); // but turn off the W5100 chip! if (!card.init(SPI_FULL_SPEED, 4)) { Serial << F("card failed\n"); has_filesystem = false; } // initialize a FAT volume if (!volume.init(&card)) { Serial << F("vol.init failed!\n"); has_filesystem = false; } if (!root.openRoot(&volume)) { Serial << F("openRoot failed"); has_filesystem = false; }
if (has_filesystem) { // Assign our function to `upload_handler_fn'. TinyWebPutHandler::put_handler_fn = file_uploader_handler; }
Serial << F("Setting up the Ethernet card...\n"); Ethernet.begin(mac, ip);
// Start the web server. Serial << F("Web server starting...\n"); web.begin();
Serial << F("Ready to accept HTTP requests.\n"); }
void loop() { if (has_filesystem) { web.process(); } } I would like to create more ON/OFF , who can me send a example of 2 leds thks
|
|
|
|
« Last Edit: September 06, 2011, 09:06:56 am by AWOL »
|
Logged
|
Regwards Nuno Rodrigues
Use : Arduino Uno Ethernet Shield ( Mini SDcard )
Software : Arduino 0022
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 3
|
 |
« Reply #1 on: September 06, 2011, 08:27:45 am » |
I would also like an example of multiple LEDs
I searched the code of tinywebserver blinkled example, but I don't know where to start, Any help or an example would be appreciated
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 3
|
 |
« Reply #2 on: September 06, 2011, 05:54:32 pm » |
After testing and debugging today I managed to get a second LED working, just some problems, the page shows 2 Lamps, but the status of lamp1 and lamp2 remains the same. Lamp 2 goes on when I click it on the page, but it wont turn off. anyone that can help me out ? here is my code (also next post): Arduino: // -*- c++ -*- // // Copyright 2010 Ovidiu Predescu <ovidiu@gmail.com> // Date: December 2010 //
#include <SPI.h> #include <Ethernet.h> #include <Flash.h> #include <SdFat.h> #include <SdFatUtil.h> #include <TinyWebServer.h>
// The LED attached in PIN 13 on an Arduino board. const int ledPin = 7; const int ledPin2 = 8;
// The initial state of the LED int ledState = LOW; int ledState2 = LOW;
void setLedEnabled(boolean state) { ledState = state; digitalWrite(ledPin, ledState); }
void setLed2Enabled(boolean state2) { ledState2 = state2; digitalWrite(ledPin2, ledState2); }
inline boolean getLedState() { return ledState; } inline boolean getLedState2() { return ledState2; }
boolean file_handler(TinyWebServer& web_server); boolean blink_led_handler(TinyWebServer& web_server); boolean blink_led2_handler(TinyWebServer& web_server); boolean led_status_handler(TinyWebServer& web_server); boolean led2_status_handler(TinyWebServer& web_server); boolean index_handler(TinyWebServer& web_server);
TinyWebServer::PathHandler handlers[] = { // Work around Arduino's IDE preprocessor bug in handling /* inside // strings. // // `put_handler' is defined in TinyWebServer {"/", TinyWebServer::GET, &index_handler }, {"/upload/" "*", TinyWebServer::PUT, &TinyWebPutHandler::put_handler }, {"/blinkled", TinyWebServer::POST, &blink_led_handler }, {"/blinkled2", TinyWebServer::POST, &blink_led2_handler }, {"/ledstatus" "*", TinyWebServer::GET, &led_status_handler }, {"/ledstatus2" "*", TinyWebServer::GET, &led2_status_handler }, {"/" "*", TinyWebServer::GET, &file_handler }, {NULL}, };
const char* headers[] = { "Content-Length", NULL };
TinyWebServer web = TinyWebServer(handlers, headers);
boolean has_filesystem = true; Sd2Card card; SdVolume volume; SdFile root; SdFile file;
static uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// Don't forget to modify the IP to an available one on your home network byte ip[] = { 192, 168, 1, 25 };
void send_file_name(TinyWebServer& web_server, const char* filename) { if (!filename) { web_server.send_error_code(404); web_server << F("Could not parse URL"); } else { TinyWebServer::MimeType mime_type = TinyWebServer::get_mime_type_from_filename(filename); web_server.send_error_code(200); web_server.send_content_type(mime_type); web_server.end_headers(); if (file.open(&root, filename, O_READ)) { Serial << F("Read file "); Serial.println(filename); web_server.send_file(file); file.close(); } else { web_server << F("Could not find file: ") << filename << "\n"; } } }
boolean file_handler(TinyWebServer& web_server) { char* filename = TinyWebServer::get_file_from_path(web_server.get_path()); send_file_name(web_server, filename); free(filename); return true; }
boolean blink_led_handler(TinyWebServer& web_server) { web_server.send_error_code(200); web_server.send_content_type("text/plain"); web_server.end_headers(); // Reverse the state of the LED. setLedEnabled(!getLedState()); Client& client = web_server.get_client(); if (client.available()) { char ch = (char)client.read(); if (ch == '0') { setLedEnabled(false); } else if (ch == '1') { setLedEnabled(true); } } }
boolean blink_led2_handler(TinyWebServer& web_server) { web_server.send_error_code(200); web_server.send_content_type("text/plain"); web_server.end_headers(); // Reverse the state of the LED. setLed2Enabled(!getLedState2()); Client& client = web_server.get_client(); if (client.available()) { char ch = (char)client.read(); if (ch == '0') { setLed2Enabled(false); } else if (ch == '1') { setLed2Enabled(true); } } }
boolean led_status_handler(TinyWebServer& web_server) { web_server.send_error_code(200); web_server.send_content_type("text/plain"); web_server.end_headers(); Client& client = web_server.get_client(); client.println(getLedState(), DEC); }
boolean led2_status_handler(TinyWebServer& web_server) { web_server.send_error_code(200); web_server.send_content_type("text/plain"); web_server.end_headers(); Client& client = web_server.get_client(); client.println(getLedState2(), DEC); }
boolean index_handler(TinyWebServer& web_server) { send_file_name(web_server, "INDEX.HTM"); return true; }
void file_uploader_handler(TinyWebServer& web_server, TinyWebPutHandler::PutAction action, char* buffer, int size) { static uint32_t start_time; static uint32_t total_size;
switch (action) { case TinyWebPutHandler::START: start_time = millis(); total_size = 0; if (!file.isOpen()) { // File is not opened, create it. First obtain the desired name // from the request path. char* fname = web_server.get_file_from_path(web_server.get_path()); if (fname) { Serial << F("Creating ") << fname << "\n"; file.open(&root, fname, O_CREAT | O_WRITE | O_TRUNC); free(fname); } } break;
case TinyWebPutHandler::WRITE: if (file.isOpen()) { file.write(buffer, size); total_size += size; } break;
case TinyWebPutHandler::END: file.sync(); Serial << F("Wrote ") << file.fileSize() << F(" bytes in ") << millis() - start_time << F(" millis (received ") << total_size << F(" bytes)\n"); file.close(); } }
void setup() { Serial.begin(115200); Serial << F("Free RAM: ") << FreeRam() << "\n";
pinMode(ledPin, OUTPUT); pinMode(ledPin2, OUTPUT); setLedEnabled(false); setLed2Enabled(false);
// initialize the SD card Serial << F("Setting up SD card...\n"); pinMode(10, OUTPUT); // set the SS pin as an output (necessary!) digitalWrite(10, HIGH); // but turn off the W5100 chip! if (!card.init(SPI_FULL_SPEED, 4)) { Serial << F("card failed\n"); has_filesystem = false; } // initialize a FAT volume if (!volume.init(&card)) { Serial << F("vol.init failed!\n"); has_filesystem = false; } if (!root.openRoot(&volume)) { Serial << F("openRoot failed"); has_filesystem = false; }
if (has_filesystem) { // Assign our function to `upload_handler_fn'. TinyWebPutHandler::put_handler_fn = file_uploader_handler; }
Serial << F("Setting up the Ethernet card...\n"); Ethernet.begin(mac, ip);
// Start the web server. Serial << F("Web server starting...\n"); web.begin();
Serial << F("Ready to accept HTTP requests.\n"); }
void loop() { if (has_filesystem) { web.process(); } }
|
|
|
|
« Last Edit: September 06, 2011, 05:56:10 pm by timminater »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 3
|
 |
« Reply #3 on: September 06, 2011, 05:55:24 pm » |
main.js: /* * Copyright 2010 Ovidiu Predescu < ovidiu@gmail.com> * Date: December 2010 */ String.prototype.trim = function () { return this.replace(/^\s*/, "").replace(/\s*$/, ""); }; function Button(elem) { var btn = this; this.elem = elem; this.setEnabled(false); this.elem.click(function(e) { e.preventDefault(); btn.clickHandler(btn, e); }); }; Button.prototype.setEnabled = function(yesno) { this.enabled = yesno; this.elem.attr('class', function(idx, val) { var newClass = yesno ? "on" : "off"; return newClass; }); }; Button.prototype.isEnabled = function() { return this.enabled; }; Button.prototype.clickHandler = function(btn, e) { var url = btn.elem.attr('href'); $.ajax({type: "POST", data: (!btn.isEnabled() + 0).toString(10), dataType: "text", cache: false, url: url, success: function(r) { }, error: function(s, xhr, status, e) { console.log("POST failed: " + s.responseText); } }); }; function ledStatus(btn, url) { $.ajax({type: "GET", cache: false, url: url, success: function(status) { status = parseInt(status.trim()); btn.setEnabled(status); window.setTimeout(ledStatus, 400, btn, url); }, error: function(s, xhr, status, e) { console.log("Getting status failed: " + s.responseText); } }); }; function ledStatus2(btn, url) { $.ajax({type: "GET", cache: false, url: url, success: function(status2) { status2 = parseInt(status2.trim()); btn.setEnabled(status2); window.setTimeout(ledStatus2, 400, btn, url); }, error: function(s, xhr, status2, e) { console.log("Getting status2 failed: " + s.responseText); } }); }; $(document).ready( function() { var lightBulb = new Button($("#lightbulb")); var lightBulb2 = new Button($("#lightbulb2")); ledStatus(lightBulb, "/ledstatus"); ledStatus2(lightBulb2, "/ledstatus2"); }); index.html: <html> <!-- Copyright 2010 Ovidiu Predescu < ovidiu@gmail.com> Date: December 2010 --> <head> <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" /> <meta name="HandheldFriendly" content="True" /> <link rel="stylesheet" type="text/css" href="style.css" media="screen"/> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="main.js"></script> <title>TinyWebServer LED control</title> </head> <body> <div class="light"> <div class="center"> <a id="lightbulb" class="on" href="/blinkled"></a><br/> <a id="lightbulb2" class="on" href="/blinkled2"></a><br/> </div> </div> </body> </html>
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #4 on: September 07, 2011, 05:46:36 am » |
{"/blinkled", TinyWebServer::POST, &blink_led_handler }, {"/blinkled2", TinyWebServer::POST, &blink_led2_handler }, {"/ledstatus" "*", TinyWebServer::GET, &led_status_handler }, {"/ledstatus2" "*", TinyWebServer::GET, &led2_status_handler }, You've registered two handlers for the same event. It is unlikely that the TinyWebServer class manages this. You should have one handler for each event (POST, GET, PUT, etc.) and, in that handler, deal with all the data passed in. For instance, the GET request should include information about which LED you want the status of, and the led_status_handler method should get the status of that LED (not a hard-coded number).
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 2
Arduino rocks
|
 |
« Reply #5 on: February 06, 2012, 04:56:21 pm » |
Try rearranging the order of the handlers, like this:
{"/blinkled2", TinyWebServer::POST, &blink_led2_handler }, {"/blinkled", TinyWebServer::POST, &blink_led_handler }, {"/ledstatus2" "*", TinyWebServer::GET, &led2_status_handler }, {"/ledstatus" "*", TinyWebServer::GET, &led_status_handler },
The code matches the handlers by doing string comparisons.
TinyWebServer can definitely handle multiple request handlers for the same method type.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 28
|
 |
« Reply #6 on: March 08, 2012, 08:33:48 am » |
I have managed to work on 5 led with more mobile friendly toggle switch 
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 28
|
 |
« Reply #7 on: March 08, 2012, 08:48:34 am » |
Thank you, Ovidiu.  The goal of my project are accomplished. 1. Self sufficient, no external web server or PC is required. 2. Low Power consumption. 3. min. 5 output controlled. 4. Operational from any device be it android, blackberry, pc or ipad so webbased app is only waytogo 5. Password protected access. big thanks to you for tinywebserver.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 2
|
 |
« Reply #8 on: March 09, 2012, 11:13:24 am » |
I have managed to work on 5 led with more mobile friendly toggle switch
Impressive piece of work. Any chance for letting us have a glance at the code???
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 28
|
 |
« Reply #9 on: March 10, 2012, 12:27:18 pm » |
I am stuck at password protection. The pc works fine but android and blackberry fail even if password is right using Javascript in the <body> section after other js load. Solved 
|
|
|
|
« Last Edit: March 15, 2012, 02:29:03 pm by capper »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 28
|
 |
« Reply #10 on: March 11, 2012, 05:48:42 pm » |
"BLINKLED5" Password Page 
|
|
|
|
« Last Edit: March 11, 2012, 05:52:23 pm by capper »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 28
|
 |
« Reply #11 on: March 11, 2012, 05:54:47 pm » |
"BLINKLED5"  Note that password field is masked,COOL!  isn't it ?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 28
|
 |
« Reply #12 on: March 11, 2012, 05:58:38 pm » |
"BLINKLED5" TADA  FIND THE ATTACHED ZIP FILE FOR THE SOURCE CODE AND IMAGES THANKS EVERYBODY.
|
|
|
|
« Last Edit: March 11, 2012, 06:27:59 pm by capper »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 29
|
 |
« Reply #13 on: April 12, 2012, 02:27:38 pm » |
hello, I am testing 'BlinkLed5.pde' code with Arduino 1.0 but I have these errors :
/Applications/Arduino 1.app/Contents/Resources/Java/libraries/TinyWebServer/TinyWebServer.cpp:36:16: error: SD.h: No such file or directory /Applications/Arduino 1.app/Contents/Resources/Java/libraries/TinyWebServer/TinyWebServer.cpp: In member function 'void TinyWebServer::send_file(SdFile&)': /Applications/Arduino 1.app/Contents/Resources/Java/libraries/TinyWebServer/TinyWebServer.cpp:489: error: invalid use of incomplete type 'struct SdFile' /Applications/Arduino 1.app/Contents/Resources/Java/libraries/TinyWebServer/TinyWebServer.h:15: error: forward declaration of 'struct SdFile'
can you help me?... thank you.
|
|
|
|
|
Logged
|
|
|
|
|
Genova
Offline
Edison Member
Karma: 13
Posts: 2437
|
 |
« Reply #14 on: April 12, 2012, 05:15:24 pm » |
perchè questo chè è pubblicato qui non funziona sulla 1.0 va modificato
|
|
|
|
|
Logged
|
Meglio imparare dalle cose inutili piuttosto che non imparare niente. [Arduino Mega R3 + Ethernet shield W5100 + SD card 8Gb FAT32]
|
|
|
|
|