[vc_row][vc_column][vc_column_text]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.[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column][vc_tta_tour][vc_tta_section title=”YunServer & YunClient” tab_id=”1463101575546-4a53ab33-965c”][vc_column_text]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:///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).
You can restrict the use of the REST interface configuring a password (the default is arduino) through the Yun’s web interface:
[/vc_column_text][/vc_tta_section][vc_tta_section title=”YunLedToggle” tab_id=”1463101575594-d4425f00-0c65″][vc_column_text]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:
[/vc_column_text][/vc_tta_section][vc_tta_section title=”callArduino” tab_id=”1463101578332-57b133c6-7b34″][vc_column_text]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:
[/vc_column_text][/vc_tta_section][/vc_tta_tour][/vc_column][/vc_row][vc_row][vc_column][vc_column_text]For more details,please refer to original post
http://www.lucadentella.it/en/2015/03/13/yun-gestire-led-via-web/[/vc_column_text][/vc_column][/vc_row]
Leave a Reply
You must be logged in to post a comment.