Monday, January 6, 2014

Arduino Yun - Introduction + Controlling GPIO via a web server

My Arduino Yun arrived over the weekend and I was surprised by how small it was compared to my Pi or Galileo. It is roughly the size of a credit card (see below).



Some of the things I really like about this:

  • Built in Wifi, micro SD card reader and  Linux OS (Linino)
  • Small form factor - ideal for applications that I am interested in like robotics and home automation
  • Easy Wifi setup in any new location
  • Ability to program sketches over Wifi and use the console terminal wirelessly to see events happening in the sketches.

This is a great guide to get started: http://arduino.cc/en/Guide/ArduinoYun. I used it to setup WiFi and allow open REST API access.

My first goal was to set up a USB webcam and install motion on my Yun. I achieved this by using the package management to install the necessary driver and packages
opkg install kmod-video-uvc motion

I plugged in my webcam via a USB hub and I was happy to see that it showed up after using dmesg and lsusb.

I started up motion and was able to see that it streams to a Firefox browser seamlessly. Goal #1 accomplished.

Next, I wanted to use a web browser to control GPIO to enable my next project which is a web controlled robot. So I wrote a simple sketch that uses browser buttons to turn the on board LED on and off. The sketch can be downloaded  here.

To get this to work I had to do one step that was not clearly documented anywhere. I had to manually create a soft link in my /www/ folder for sd:
ln -s /mnt/sda1 /www/sd

Another key thing to note here, is that your sketch folder needs to have a www sub folder with the index.html and zepto.min.js file in it. When your sketch is uploaded via WiFi, the contents of the www folder and automatically copied to /www/sd/<sketchname> folder

The REST calls to /arduino are made from the index.html file. To undestand what the REST API is, I would encourage you to read up on it in the getting started URL mentioned above.

If you view the source of the html, you will see this:


<script type="text/javascript">

    function ledon()
   {
       $('#content').load('/arduino/ledon');
   }
   function ledoff()
  {
      $('#content').load('/arduino/ledoff');
  }

</script>


It basically passes the ledon/ledoff commands to the Arduino once the buttons are pressed. The sketch simply looks for this and manipulates the GPIO:


if (command == "ledon") {
       digitalWrite(13, HIGH);
 }

else if (command == "ledoff") {
       digitalWrite(13, LOW);
}

After you have uploaded the sketch over to your Yun, you can use any browser to go to http://your_yun_name.local/sd/webled

You should see these buttons which will allow you to control the on board LED.


Hope this helps others who are trying to attempt something similar.

9 comments:

  1. Thanks man, that's exactly what I was looking for!

    toni

    ReplyDelete
    Replies
    1. Glad to hear that it helped you. Good luck!

      Delete
    2. This comment has been removed by the author.

      Delete
    3. Thanks very much for this blog post. It's very clear.
      It isn't necessary to create a soft link if you put the folder "www" in "/mnt/sda1/arduino". In this case, the link to www/sd is automatically created. (http://forum.snootlab.com/viewtopic.php?t=519&p=4788)

      Delete
    4. Thanks for this tutorial, excelent.
      Yes, its true, www/sd is automatically created.
      I dont need create the "www" in "/mnt/sda1/arduino".

      Regards

      Delete
  2. Many many thanks for the nice tutorial and the very clear explanations.
    I hope to see more from you on Arduino Yún.

    ReplyDelete
  3. Hello, I write "http: // arduinoIP / sd / webled" into the "crome" search engine and then I get "No such file or directory". What can I do.

    ReplyDelete
    Replies
    1. Have you followed the steps in setting up WiFi on this guide https://www.arduino.cc/en/Guide/ArduinoYun#toc11

      After the setup, you should be able to access the yun by name in any web browser. That is what I used in my example as well

      Delete
    2. Hi, on the page you suggested to me, I do not understand exactly what I have to do and how to proceed. Can you explain to me in more detail what I have to do. I'm just starting with this, and I would like to learn how to use it.

      Delete