Yún – Toggle a LED via web

by luca

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:


Related Posts


marco Saturday July 8th, 2017 - 03:37 PM

Ciao, grazie mille del tutorial. Ho seguito tutto alla lettera ma quando digito myip/arduino/test appare questo messaggio: Could not connect to YunServer 146 Connection refused
Potresti aiutarmi?

luca Monday July 10th, 2017 - 07:32 AM

ciao Marco, sembra che lo Yun non accetti le connessioni… prova a fare un PING per verificare se almeno è correttamente in rete

marco Monday July 10th, 2017 - 02:50 PM

Ciao Luca,
grazie mille per la risposta. Ho effettuato il ping di yun e lo vede correttamente. Non è che ho sbagliato qualcosa nella procedura? Io ho messo il file php nella cartella /arduino/www/
Oltre a ciò ho solo reso statico l’indirizzo ip di yun perchè mi entrava sempre in confitto con la stampante..
Qualche idea?

luca Tuesday July 11th, 2017 - 10:47 AM

ciao, ma se provi da browser web (senza php) funziona?

marco Thursday July 20th, 2017 - 06:43 AM

Ciao Luca, in realtà ho sempre provato da browser web.. Mi da quell’errore quando provo a connettermi da internet..

luca Thursday July 20th, 2017 - 02:41 PM

Marco per raggiungere lo Yun via internet devi configurare il tuo router per effettuare il NAT e dovrai puntare all’indirizzo IP pubblico, non al nome “yunserver” o simile…

marco Wednesday August 2nd, 2017 - 09:18 AM

Ciao Luca, per quanto riguarda la configurazione del router ho impostato le porte puntandole allo YUN con codifica HTTP (http://www.mauroalfieri.it/informatica/router-port-mapping.html) . per quanto riguarda l’indirizzo, mi da errore anche puntandolo dall’indirizzo pubblico come descritto in tua guida (http://www.lucadentella.it/2012/04/28/enc28j60-e-arduino-6/2/).
Qualche idea?


Leave a Reply to marco Cancel Reply

five × one =