Arduino

Arduino – Controlling LEDs Remotely (Wi-Fi) Using Arduino Yun

What if you were away from home for a few weeks and wanted to control your lights?  Or you wanted to be able to remotely access a temperature sensor at home?  There are a couple of ways to do this including Wi-Fi and GSM modules.  Ethernet or Bluetooth shields,  Or all in one units such as an Arduino Yun.

Where to Get One

There are a few places you can grab an Arduino Yun.  But the parts for this particular build were purchased at the following links:

What Makes a Yun Unique

A Yun does several cool things.  It can connect to a Wi-Fi network and run as a web server.  It can host its own wireless network.  It can do both at the same time.  It can run and manage web sessions.  It has both a Linux and ATMega backbone split.  It works on Ethernet as well.  It takes an SD card which can be used to store websites.  And it can act as a USB controller, too cool.

Setting up a Yun

Once you have a Yun:

Yun

  • Connect it to power
  • Connect the long LED arm to a 330ohm resistor
  • Connect resistor to pin ~3
  • Connect second smaller LED to GND
    • Note:  You should see an ArduinYun{code} Wi-Fi zone
  • Join the Wi-Fi zone and browse to 192.168.1.240.

Yun1

  • When prompted for credentials, use Arduino as the password.

Yun2

  • Press configure when prompted.

Yun3

  • For this build, I called it ‘Yun’

Yun4

  • I also disabled passwords for now.

Grab the Code from Github

Once you are ready, download the code from my Github and extract the zip to your Arduino sketches folder.  Or copy/paste from below.

#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>

int LEDPIN = 3; // your LED PIN
YunServer server;

void setup() {
  // Start our connection
  Serial.begin(9600);
  pinMode(LEDPIN,OUTPUT);
  digitalWrite(LEDPIN,HIGH); // turn on Led while connecting
  Bridge.begin();  

  // Show a fancy flash pattern once connected
  digitalWrite(LEDPIN,LOW); 
  delay(150);
  digitalWrite(LEDPIN,HIGH); 
  delay(150);
  digitalWrite(LEDPIN,LOW); 
  delay(150);
  digitalWrite(LEDPIN,HIGH); 
  delay(150);
  digitalWrite(LEDPIN,LOW); 
  delay(150);
  
  // Disable for some connections:
  // Start listening for connections  
  
  // server.listenOnLocalhost();
  server.begin();
 
}

void loop() {
  // Listen for clients
  YunClient client = server.accept();
  // Client exists?
  if (client) {
    // Lets process the request!
    process(client);
    client.stop();
  }
  delay(50);
}

void process(YunClient client) {
  // Collect user commands
  String command = client.readStringUntil('\\'); // load whole string
  
  // Enable HTML
  client.println("Status: 200");
  client.println("Content-type: text/html");
  client.println();
  
  // Show UI
  client.println("<B><Center>");
  client.println("<a href='http://yun.local/arduino/on\\'>Turn ON LED</a><br>");
  client.println("<a href='http://yun.local/arduino/off\\'>Turn OFF LED</a><br>");
  client.print("Command: ");
  client.println(command);
  client.println("</B></Center>");
  
  // Check what the user entered ...
  
  // Turn on
  if (command == "on") {
    digitalWrite(3,HIGH);
  }
  
  // Turn off
  if (command == "off") {
    digitalWrite(3,LOW);
  }
}

On Windows, this is usually C:\users\%username%\documents\Arduino

Once it is downloaded; open the sketch.

Modify the Sketch

In the sketch, double check the PIN that you used.  Ensure the ‘yun.local’ references are changed to ‘(name_of_yun).local’.

...
    int LEDPIN = 3; // your LED PIN
...
    client.println("<a href='http://yun.local/arduino/on\\'>Turn ON LED</a><br>");
    client.println("<a href='http://yun.local/arduino/off\\'>Turn OFF LED</a><br>");
 ...
    digitalWrite(3,HIGH);
 ...
    digitalWrite(3,LOW);

Once you’ve mastered the brief code, improve on it.  Add both analog and digital.

Upload the Sketch

UploadCode

Once you have completed your configuration, go ahead and upload the sketch to your Yun!  Join the same network.  And then run ‘yun.local/arduino/test’.where yun.local needs the correct hostname if changed from the defaults.

Get it on Github

Related Articles:

(40767)

26 thoughts on “Arduino – Controlling LEDs Remotely (Wi-Fi) Using Arduino Yun

    1. Yes, in a way, but now it gets a bit more complicated. You know have an internal IP and port. However your phone – unless your home – is on an external network. So you need to allow it through. Usually you need to access your router, and open a port to allow the website to reach your phone and vise versa. However there are security concerns here too – you don’t want your neighbor turning on and off your device. So I haven’t included that *yet*. If your interested I can do a follow up article on remote access 🙂

        1. Great question! I took a break from Articles for halloween, but maybe I should do a follow up article. Connecting from a mobile phone is super easy – because you just need to use the public IP + port of your Arduino. So in Cascades (BlackBerry) you sod just use a Web view with a locked down URL. And on Android, you could do the same. However you need to grant the android application Internet permissions for it to work.

  1. Hi, On lines 68 a 73 you use digitalWrite with a fixed led pin (3) instead of using the LEDPIN var (I wanted to avoid adding a led, using the on board L13 instead) .

  2. Here a nice article to easily access your light form any mobile platform using the Arduino Yun. lelylan.github.io/lab-projects/arduino-yun-light/

  3. having issues. I got it to load and I know that the wiring is right because I varied the pattern of lights in the setup and it matches up. But the light does not turn on when I click on the ON link. Any ideas? This is only my 2nd project so Im pretty new. Thanks

  4. I had the same problem myself. I’m on a Mac using Safari and after trying to enter the command by typing out the url myself I noticed an issue. After hitting enter Safari would change the two forward slashes (\\) to back slashes (//). This was a problem because the forward slashes were used to tell the Yun when the string was finished. Here is the line below. … String command = client.readStringUntil(‘\\’); … I just changed the “\\” to a “~” in that line as well as in the url later on in the code (for both the on and off link)

  5. I got it to load and I know that the wiring is right because shows a fancy flash pattern once connected. But the light does not turn on when I click on the ON link. This is my first project. Help please. Thanks

    1. It shouldn’t show anything when connected on pin 3 unless you’ve programmed something unique for it. The first two pins (0 and 1) are serial, they will blink a light if a light is connected to them in error at power up (it can also interfere with sketch uploading too) if your on a Mac, watch for how safari handles the url in the other comment on here 🙂

  6. IS there any way I could make it remain on, without using delay because it just flashes in a split second, even though i press off, it flashes on than off

    1. I can’t help with Apple – I shutdown my Apple dev account a while ago and never really had any apple products in 5+ years. But any browser should be perfect. What errors are you getting?

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.