This example for the Arduino Yún shows how to use the Bridge library to access the digital and analog pins on the board through REST calls. It demonstrates how you can create your own API when using REST style calls through the browser.
When running this example, make sure your computer is on the same network as the Yún. When you have have programmed the board, you can request the value on a pin, write a value to a pin, and configure a pin as an input or output.
When the REST password is turned off, you can use a browser with the following URL structure :
You can use the CURL command from the command line instead of a browser if you prefer.
There is no circuit for this example.
The example code shows how it is possible to make REST requests to the Yún to read from and write information to the board's pins.
You need to include the Bridge, YunServer, and YunClient libraries :
Instantiate a server enabling the the Yun to listen for connected clients.
setup(), start serial communication for debugging purposes, and turn the built-in LED on pin 13 high while Bridge begins.
Bridge.begin() is blocking, and should take about 2 seconds to complete. Once Bridge starts up, turn the LED off.
In the second part of
setup(), tell the instance of YunServer to listen for incoming connections only coming from localhost. Connections made to Linux will be passed to the 32U4 processor for parsing and controlling the pins. This happens on port 5555. Start the server with
loop(), you'll create an instance of the YunClient for managing the connection. If the client connects, process the requests in a custom function (described below) and close the connection when finished.
Putting a delay at the end of
loop() will be helpful in keeping the processor from doing too much work.
Create a function named
process that accepts the YunClient as its argument. Read the command by creating a string to hold the incoming information. Parse the REST commands by their functionality (digital, analog, and mode) and pass the information to the appropriately named function.
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.
Parse the client's request for the pin to work with using
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.
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.
Set up a function to handle analog calls in the same fashion, except setting the key to A instead of D when working with the analog input pins :
Create one more function to handle pin mode changes. Accept the YunClient as the argument, and create a local variable to hold the pin number. Read the pin value just as you did in the digital and analog functions.
Check to make sure the URL is valid
If it's a valid URL, store the URL as a string. If the mode is an
output, configure the pin and report it to client. If the string doesn't match those values, return an error.
The complete sketch is below :
Last revision 2015/08/11 by SM