Yún – Toggle a LED via web

luca 13/03/2015 0

In my previous tutorial you learned how a sketch, running on the Arduino Yun’s ATMega chip, can execute processes (commands, scripts…) on Linino thanks to the Bridge library.

In this tutorial you’ll learn how to do the opposite: send commands from Linino to the sketch that runs on the microcontroller.

YunServer & YunClient

In my sketch I’ll use two objects from the Bridge library:

  • YunServer, to listen for new incoming connections;
  • YunClient, to send/receive data from the client that has connected.

Yun offers an easy REST interface to directly interact with the YunServer object. Each web connection to an address like the following:


is passed to the sketch and, using the readString() method of the YunClient object, you can get the part of the URL that follows /arduino/ (in the example, the bold text).


You can restrict the use of the REST interface configuring a password (the default is arduino) through the Yun’s web interface:



The sketch I developed for this example is available in my Github’s repository. Its goal is to change the status of a LED (to keep it simple I chose the onboard one, connected to PIN 13) when it receives the command toggle.

Within the setup() you need to initialize the Bridge library and make the YunServer listen for new connections (to increase the security, I configured it to accept connections only from localhost):


In the loop() the method server.accept() blocks the sketch waiting for a new connection. When a client connects, a YunClient object is created and it will be used to transfer the data:

YunClient client = server.accept();

The command sent by the client is read and cleaned (trim) from any spaces:

String command = client.readString();

If the command is “toggle”, the LED status is changed:

if(command == "toggle") {
  digitalWrite(LED_PIN, !digitalRead(LED_PIN));

At the end, the client connection is closed:


You can test the sketch using a web browser:

yun-led1 yun-led2


What I described above can also be used by PHP pages running on Linino to interact with a sketch.

I prepared a simple example (saved in my repository on Github) that explains the use of cURL, even if the REST interface is password protected:


Leave A Response »

This website uses cookies to ensure you get the best experience on our website maggiori informazioni

Questo sito utilizza i cookie per fonire la migliore esperienza di navigazione possibile. Continuando a utilizzare questo sito senza modificare le impostazioni dei cookie o clicchi su "Accetta" permetti al loro utilizzo.