Yun_thumb.jpg

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


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:


Leave a comment

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


*

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