trochu programovania (Ar&^#$@)
balu
balu na k-net.fr
Pondělí Prosinec 31 18:22:29 CET 2018
Zdravim osadenstvo,
programovanim nepoľúbený, mal by som velmi primitivnu otazku. Hram sa s
ethernet modulom na Arduine (ano...), a neviem ako odovzdat parameter.
Vychadzal som z prikladu web servera. Vytvori sa server na porte 80 a
ked mu pride poziadavka, tak vypluje text s primitivnou strankou.
V priklade je to napisane tak, ze testovanie ci prisla poziadavka aj
odozva je napisana v hlavnej slucke. Chcel by som hlavnu slucku trochu
upratat a odozvu odlozit do funkcie mimo hlavnu slucku. Urcite by to
islo urobit aj cez prerusenie, ale programovat neviem, takze to je mimo
moj dosah.
Takze som rozmyslal nad ponechanim testovania podmienky v hlavnej slucke
a presunom zbytku do funkcie, vid kod:
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
TreatConnection(); // if http request available, treat it
}
}
// ------------------ various functions ----------------------
void TreatConnection() {
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();
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"); // the connection will
be closed after completion of the response
client.println("Refresh: 5"); // refresh the page
automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<b>Tu nieco vypis....</b><br>");
client.println("Sensor readings:<br>");
client.println("</html>");
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;
}
}
}
// give the web browser time to receive the data
delay(1);
client.stop(); // close the connection:
Serial.println("client disconnected");
}
a vobec ma neprekvapuje, ze to nefunguje, vadi mu, ze vo funkcii nepozna
'client':
/Control.ino: In function 'void TreatConnection()':
Control:185:12: error: 'client' was not declared in this scope
while (client.connected()) {
^
Control:228:5: error: 'client' was not declared in this scope
client.stop(); // close the connection:
^
Using library SPI at version 1.0 in folder:
/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/libraries/SPI
Using library Ethernet at version 2.0.0 in folder:
/Applications/Arduino.app/Contents/Java/libraries/Ethernet
exit status 1
'client' was not declared in this scope
Otazka je ako mu v riadku TreatConnection() posunut informaciu co/kto je
client?
Dakujem za nasmerovanie...
b.
Další informace o konferenci Hw-list