Yún – Toggle a LED via web

luca 13/03/2015 6

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:

http://<yun_ip>/arduino/something

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).

yun-rest

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

rest-api-pwd

YunLedToggle

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):

Bridge.begin();
[...]
server.listenOnLocalhost();
server.begin();

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();
command.trim();

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:

client.stop();

You can test the sketch using a web browser:

yun-led1 yun-led2

callArduino

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:

yun-led3

6 Comments »

  1. marco 08/07/2017 at 15:37 - Reply

    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 10/07/2017 at 07:32 - Reply

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

  2. marco 10/07/2017 at 14:50 - Reply

    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 11/07/2017 at 10:47 - Reply

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

  3. marco 20/07/2017 at 06:43 - Reply

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

    • luca 20/07/2017 at 14:41 - Reply

      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…

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.

Chiudi