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:

(36059)

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

    1. Hello! There is no HTML in the tutorial 🙂 just an Arduino sketch. You need to upload it to the Arduino, and it will generate the HTML for you. It’s in the process() loop above.

  1. Worked on it for hours without any luck…. I’m using Chrome then added command.trim(); before the if statements and presto!! Not sure why but .. hey it works now Thanks for posting

  2. Hello Lloyd, The sketch would not work until I added the below line. My browser is Google Chrome. // Turn on command.trim(); // This line Added if (command == “on”) { digitalWrite(3,HIGH); } // Turn off command.trim(); // This line Added if (command == “off”) { digitalWrite(3,LOW);

  3. Hey guys, when I uploaded the sketch and opened the yun.local/arduino/test it asked me to give a username and password, i tried the comination of my arduino’s name+ password that i used to configure the arduino, but that didn’t work. Now i’m locked out of the page. when i unpugged and replugged the battery it still refused to let me into the test page. help??

    1. I found somewhere that you are supposed to write ‘root’ as username. Now the browser doesn’t tell me that i’m blocked, but the site itself only shows this line: Could not connect to YunServer 146 Connection refused, any idea what the issue could be?

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.