![Arduino Yun Скачать руководство пользователя страница 19](http://html1.mh-extra.com/html/arduino/yun/yun_manual_2966336019.webp)
void process(YunClient client) {
String command = client.readStringUntil('/');
if (command == "digital") {
digitalCommand(client);
}
if (command == "analog") {
analogCommand(client);
}
if (command == "mode") {
modeCommand(client);
}
}
[Get Code]
Create a function to deal with
digital
commands. Accept the client as the argument. Create some
local variables to hold the pin and value of the command.
void digitalCommand(YunClient client) {
int pin, value;
[Get Code]
Parse the client's request for the pin to work with using
client.parseInt()
.
If the character after the pin is a "/", it means the URL is going to have a value of 1 or 0
following. This value will assign a value to the pin, turning it HIGH or LOW. If there is no
trailing "/", read the value from the specified pin.
pin = client.parseInt();
if (client.read() == '/') {
value = client.parseInt();
digitalWrite(pin, value);
}
else {
value = digitalRead(pin);
}
[Get Code]
Print the value to the client and update the datastore key with the current pin value.
By wrapping the value to the client in
F()
, you'll be printing form the flash memory. This helps
conserve space in SRAM, which is useful when dealing with long strings like URLs.
The key will be the pin, and type. For example
D2
will be saved for for digital pin 2. The value
will be whatever value the pin is currently set to, or was read from the pin.
client.print(F("Pin D"));
client.print(pin);
client.print(F(" set to "));
client.println(value);