sausim:
Con tinywebserver è possibile prendere l'url e poi lavorarlo????
si, ma non lo fa solo quello
eccoti un esempio base presente anche nele cartelle dell'ide, ti ho aggiunto 2 pulsanti giusto per farti vedere il risultato dell'url sul serial.print
/*
Web Server
A simple web server that shows the value of the analog input pins.
using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
* Analog inputs attached to pins A0 through A5 (optional)
created 18 Dec 2009
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe
*/
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,2, 177);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
String pacchetto;
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
pacchetto += (char)c;
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// add a meta refresh tag, so the browser pulls again every 5 seconds:
//client.println("<meta http-equiv=\"refresh\" content=\"5\">");
// output the value of each analog input pin
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
int sensorReading = analogRead(analogChannel);
client.print("analog input ");
client.print(analogChannel);
client.print(" is ");
client.print(sensorReading);
client.println("
");
}
client.println("</html>");
client.print("<input type=\"button\" style=\"width:120px; height:80px\" value=\"On\" onclick =\" location.href='/?on_2'\">");
client.print("<input type=\"button\" style=\"width:120px; height:80px\" value=\"Off\" onclick =\" location.href='/?off_2'\">");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
pacchetto="";
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}
if (client.available()) {
char c = client.read();
non si limita a leggere solo off_2 ma è un pacchtto con molte informazioni
- Tipo di richiesta GET,POST,PUT, ecc
- user-agent descrive il client che fa la richiesta, come vedi mi sono connesso con un windows nt6.1, windows a 64bit e il browser è Firefox
- accept ti descrive il content-type text/html
- language decodifiche ecc ecc
- se aggiungessimo i popup di autenticazione, qui dentro ci sarebbero anche le chiavi a base64 dell' username and passw
risultato nel serial dopo la pressione del tasto da browser
server is at 192.168.2.177
new client
GET /?on_2 HTTP/1.1 >>>>>> GET attuale
Host: 192.168.2.177
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
Accept-Language: it-IT,it;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Referer: http://192.168.2.177/?off_2 >>> GET precedente
Connection: keep-alive
client disconnected
sausim:
scusa query.indexof cosa fa???? ....mi daresti un po' più di codice che non riesco a capire??? grazie mille
La richiesta arriva sottoforma di caratteri seriali, uno alla volta e la riga "pacchetto += (char)c;" ha il compito di salvare tutti i char in un unica stringa. Questa stringa "pacchetto" come vedi è lunga e occupa molti byte mettendo a rischio la ram, andrebbe lavorata un po meglio e salvare solo il necessario, un altro accorgimento è quella di non dichiararla pubblica
"indexof" cerca in tutta la stringa il pezzo che tu vuoi vedere se esiste....
Se è contenuto all'interno della stringa ti restutisce la posizione inziale
Se non esiste la sequenza esatta ti restituisce -1
stringa = "ciao mondo e ciao universo";
int posizione = stringa.indexOf("mondo"); // cerchiamo mondo
il risultato sarà
5 quinto carattere dalla pos iniziale
se cerchi "ciao" essendocene 2 ti restituisce la posiz solo del primo
vedi:
indexOf
Substring
string.length()
LastIndexOf
Ultima cosa: se non vuoi vedere sulla barra degli indirizzi "http://192.168.2.177/?off_2" nell' html costruisci un form con metodo POST