Offline
Newbie
Karma: 0
Posts: 25
|
 |
« Reply #15 on: October 23, 2012, 04:14:50 pm » |
Ho provato a ragionarci di più e questo è il mio sketch ma NON mi funziona ugualmente. Come dicevo prima avrei intenzione di dare un comando tipo http://Http://192.168.1.100/11#1 dove 11 è l'indirizzo (da 00 a ipotetico 99) 1 è il comando (0 spento e 1 acceso) Qualcuno sa dove sto sbagliando?#include <Shifter.h> #include <SPI.h> #include <Ethernet.h> byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x3A, 0x06 }; byte ip[] = { 192,168,1,100 }; EthernetServer server(80); String query; //Pin connected to latch pin (ST_CP) of 74HC595 const int latchPin = 8; //Pin connected to clock pin (SH_CP) of 74HC595 const int clockPin = 6; ////Pin connected to Data in (DS) of 74HC595 const int dataPin = 7;
#define NUM_REGISTERS 2
Shifter shifter(dataPin, latchPin, clockPin, NUM_REGISTERS);
void setup() { query = String(""); Ethernet.begin(mac, ip); server.begin(); pinMode(latchPin, OUTPUT); pinMode(dataPin, OUTPUT); pinMode(clockPin, OUTPUT); } void loop() {
EthernetClient client = server.available(); if (client) { while (client.connected()) { if (client.available()) { char c = client.read(); query.concat(c); if (c == '\n') { client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println();
byte I = query.indexOf("#"); int Indirizzo = query.substring(I - 2).toInt(); int Comando = query.substring(I + 1).toInt();
if (query.indexOf("#") > 0) { shifter.setPin(Indirizzo, Comando); shifter.write(); delay(3000); shifter.clear(); shifter.write(); }
client.print("<pre>");//DEBUG client.print(query);// DEBUG client.print("</pre>");//DEBUG break;//fermo il ciclo } } } delay(1); client.stop(); query = String(""); } }
|
|
|
|
|
Logged
|
|
|
|
|
Genova
Offline
Faraday Member
Karma: 14
Posts: 2529
|
 |
« Reply #16 on: October 24, 2012, 03:02:08 am » |
Però sei testina ehh ... ti avevo postato qui lo sketch che ti serviva http://arduino.cc/forum/index.php/topic,102505.15.html, ma evidentemente non hai guardato.
|
|
|
|
|
Logged
|
Meglio imparare dalle cose inutili piuttosto che non imparare niente. [Arduino Mega R3 + Ethernet shield W5100 + SD card 8Gb FAT32]
|
|
|
|
Genova
Offline
Faraday Member
Karma: 14
Posts: 2529
|
 |
« Reply #17 on: October 24, 2012, 04:37:47 am » |
Esempio porte da 10 a 99 http://192.168.2.177/110 >> on porta 10 http://192.168.2.177/010 >> off porta 10 ... http://192.168.2.177/199 >> on porta 99 http://192.168.2.177/099 >> off porta 99 porte da 0 a 9 http://192.168.2.177/100 >> on porta 0 http://192.168.2.177/000 >> off porta 0 ... http://192.168.2.177/109 >> on porta 9 http://192.168.2.177/009 >> off porta 9 Non ho lo shifter quindi dovrai sostituire i digitalWrite(port,0); con le tue righe, l'ip e company, ma il principio è lo stesso shifter.setPin(Indirizzo, Comando); shifter.write(); delay(3000); shifter.clear(); shifter.write(); #include <SPI.h> #include <Ethernet.h> byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; byte ip[] = { 192, 168, 2, 177 }; EthernetServer server(80); String query; byte out_1 = 40; byte out_2 = 41; byte out_3 = 42; byte out_4 = 43; void setup(){ Ethernet.begin(mac, ip); pinMode(out_1, OUTPUT); pinMode(out_2, OUTPUT); pinMode(out_3, OUTPUT); pinMode(out_4, OUTPUT); } void loop() { EthernetClient client = server.available(); if (client) { boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); query.concat(c); if (c == '\n' && currentLineIsBlank) { String Digi_port = query.substring(6,8) + "0"; char thisChar[Digi_port.length()]; Digi_port.toCharArray(thisChar, Digi_port.length()); int port = atoi(thisChar); String on_off = query.substring(5,6); client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println(); client.print("<html><head><title>ARDUINO WEB</title></head><body>"); if (on_off != "0" && on_off != "1") { client.println("ERRORE IMMISSIONE DATO !! <BR>");// DEBUG } else { client.println("Stringa inviata ad Arduino >> " + query + "<BR>");// DEBUG client.println("Stringa ricevuta da Arduino parse >> " + (String)port + "<BR>");// DEBUG client.println("Comando da eseguire sulla porta " + (String)port + " >> "+ on_off + "<BR>");// DEBUG if(on_off == "0") digitalWrite(port,0); if(on_off == "1") digitalWrite(port,1); } client.println("</body></html>");
query=""; delay(1); client.flush(); client.stop(); } } } } }
L'ho fatto sul mio quindi deve andare anche sul tuo, fammi sapere ciao
|
|
|
|
« Last Edit: October 24, 2012, 05:02:13 am by pablos »
|
Logged
|
Meglio imparare dalle cose inutili piuttosto che non imparare niente. [Arduino Mega R3 + Ethernet shield W5100 + SD card 8Gb FAT32]
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 25
|
 |
« Reply #18 on: October 24, 2012, 05:34:51 am » |
Ciao Pablos, grazie delle info...stasera guarderò...comunque non vedo più il tuo post dall'altra parte... Cmq grazie ancora poi ci guardo e studio bene :-) Ciao
|
|
|
|
|
Logged
|
|
|
|
|
Genova
Offline
Faraday Member
Karma: 14
Posts: 2529
|
 |
« Reply #19 on: October 24, 2012, 02:42:09 pm » |
visto che qualcuno giustamente fa notare l'uso del toInt() l'ho applicato modificando il precedente programma con lo stesso risultato #include <SPI.h> #include <Ethernet.h> byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; byte ip[] = { 192, 168, 2, 177 }; EthernetServer server(80); String query; byte out_1 = 40; byte out_2 = 41; byte out_3 = 42; byte out_4 = 43; void setup(){ Ethernet.begin(mac, ip); pinMode(out_1, OUTPUT); pinMode(out_2, OUTPUT); pinMode(out_3, OUTPUT); pinMode(out_4, OUTPUT); } void loop(){ EthernetClient client = server.available(); if (client) { boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); query.concat(c); if (c == '\n' && currentLineIsBlank) { digitalWrite(byte(query.substring(6,8).toInt()), byte(query.substring(5,6).toInt())); client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println(); client.print("<html><head><title>ARDUINO WEB</title></head><body>"); client.println("Stringa inviata ad Arduino >> " + query + "<BR>");// DEBUG client.println("</body></html>");
query=""; delay(1); client.flush(); client.stop(); } } } } }
|
|
|
|
« Last Edit: October 24, 2012, 02:45:50 pm by pablos »
|
Logged
|
Meglio imparare dalle cose inutili piuttosto che non imparare niente. [Arduino Mega R3 + Ethernet shield W5100 + SD card 8Gb FAT32]
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 25
|
 |
« Reply #20 on: October 25, 2012, 04:25:15 pm » |
Grazie mille Pablos, funzionano entrambi. Unica cosa che non riesco a capire (e che ammetto non è poco  ) è come hai deciso che la posizione del substring è (6,8) e (5,6). Forse per questo che non riuscivo pirma a ragionare. La stringa totale quindi quale sarebbe? Grazie ancora Ciao
|
|
|
|
|
Logged
|
|
|
|
|
Genova
Offline
Faraday Member
Karma: 14
Posts: 2529
|
 |
« Reply #21 on: October 25, 2012, 04:50:43 pm » |
stampa la stringa query che arriva ad arduino sarà per esempio: GET /140 HTTP/1.1 a noi serve il 140 ... quindi poi suddiviso 1 e 40 conta a partire dalla G fino ad arrivare al carattere 1 ...posizione 5 per il 40 le posizioni 6-8 ... per verificare se prendi i caratteri giusti ti fai dei serial.print e stampi le variabili e controlli se fai errori
la tua idea era di mettere il # che va bene lo stesso come separatore, in tal caso una volta prelevata la posizione del separatore con indexof prendi i caratteri con posiz.#-2 e posiz. #+1 ecc ecc dipende da come costruisci la stringa
ciao
|
|
|
|
« Last Edit: October 25, 2012, 05:02:11 pm by pablos »
|
Logged
|
Meglio imparare dalle cose inutili piuttosto che non imparare niente. [Arduino Mega R3 + Ethernet shield W5100 + SD card 8Gb FAT32]
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 25
|
 |
« Reply #22 on: October 25, 2012, 05:08:00 pm » |
Chiarissimo grazie mille...potevo infatti come dicevi tu stamparmi la stringa così capivo :-) Thanks ancora Ciao
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 25
|
 |
« Reply #23 on: November 04, 2012, 06:22:11 am » |
Mi potete spiegare il perchè utilizzando questo sketch una volta che digito il comando sulla barra indirizzi ci mette circa 5 sec ad eseguire il comando? Mentre se facevo con una serie di "if" mi sembrava fosse più veloce e abbastanza immediato. E sapete dirmi perchè all'interno dell'array se inserisco 08 e 09 mi da errore? #include <Shifter.h> #include <SPI.h> #include <Ethernet.h> byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x3A, 0x06 }; byte ip[] = { 192,168,1,100 }; EthernetServer server(80); String query; //Pin connected to latch pin (ST_CP) of 74HC595 const int latchPin = 8; //Pin connected to clock pin (SH_CP) of 74HC595 const int clockPin = 6; ////Pin connected to Data in (DS) of 74HC595 const int dataPin = 7;
#define NUM_REGISTERS 2
Shifter shifter(dataPin, latchPin, clockPin, NUM_REGISTERS);
void setup() { query = String(""); Ethernet.begin(mac, ip); server.begin(); pinMode(latchPin, OUTPUT); pinMode(dataPin, OUTPUT); pinMode(clockPin, OUTPUT); }
void loop() { EthernetClient client = server.available(); if (client) { boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); query.concat(c); if (c == '\n' && currentLineIsBlank) {
client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println(); client.print("<html><head><title>ARDUINO WEB</title></head><body>"); String Digi_port = query.substring(6,8) + "0"; char thisChar[Digi_port.length()]; Digi_port.toCharArray(thisChar, Digi_port.length()); int port = atoi(thisChar); String on_off = query.substring(5,6); int myPins[] = {00, 01, 02, 03, 04, 05, 06, 07, 10, 11, 12, 13, 14, 15}; int i; if(on_off == "1") shifter.setPin(port, HIGH); shifter.write(); delay(3000); shifter.clear(); shifter.write(); if(on_off == "0") shifter.setPin(port, LOW); shifter.write(); delay(3000); shifter.clear(); shifter.write(); if (query.indexOf("all") > 0){ for (i = 0; i < 15; i = i++){ shifter.setPin(myPins,HIGH); shifter.write(); delay(5); } delay(3000); shifter.clear(); shifter.write(); } client.println("</body></html>"); break;//fermo il ciclo } } } delay(1);
client.stop(); query = String("");
} }
Grazie mille. Ciao
|
|
|
|
|
Logged
|
|
|
|
|
|