Introduction: To Blink LED From Mobile Phone or IPod or Another Computer Via WiFi
Hello Everyone,
I like this site too much, the site really played very important role in awakening my enthusiasm and creativity.
This is my first project and its really for the first timers project.
I got this project from Instructible site , modified to access from web and mobile.
My aim was to control some electronics from mobile.
I wanted to achieve it with existing devices from our daily use ,without buying any special circuit. Also want to keep it simple so that it would be easy for someone to start as his first project.
For the project we need,
Laptop with wifi capability
- Wify Lan
- Mobile phone or ipod with Wify Connectivity
- Visual Studio .net. (You can also develop it into jsp or java but I already have .net)
- Main component Arduino
- Led
- 1 k Registor
- Connecting Wires
Step 1:
Arduino Code
Upload following sketch to arduino . I am connecting LED to pin 8. You can modify it with the number you want to connect to.
//Arduino Control
int ledPin = 8; // LED connected to digital pin 8
int incomingByte = 0; // for incoming serial data
// The setup() method runs once, when the sketch starts
void setup() {
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
// the loop() method runs over and over again,
// as long as the Arduino has power
void loop()
{
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
if(incomingByte == 105){
digitalWrite(ledPin, HIGH);
}
else if(incomingByte == 111){
digitalWrite(ledPin, LOW);
}
}
}
Upload following sketch to arduino . I am connecting LED to pin 8. You can modify it with the number you want to connect to.
//Arduino Control
int ledPin = 8; // LED connected to digital pin 8
int incomingByte = 0; // for incoming serial data
// The setup() method runs once, when the sketch starts
void setup() {
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
// the loop() method runs over and over again,
// as long as the Arduino has power
void loop()
{
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
if(incomingByte == 105){
digitalWrite(ledPin, HIGH);
}
else if(incomingByte == 111){
digitalWrite(ledPin, LOW);
}
}
}
Step 2:
The c# .net code
We are basically developing simple website which will be accessed by mobile or computer.
The website has “On” and “Off” button.
Following is the c# code
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO.Ports;
namespace DotNetControl
{
public partial class _Default : System.Web.UI.Page
{
SerialPort port = new SerialPort("COM8", 9600, Parity.None, 8, StopBits.One);
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (port.IsOpen)
{
port.Close();
}
}
catch (Exception exp)
{
Response.Write(exp.Message);
}
}
protected void btnOn_Click(object sender, EventArgs e)
{
if (port.IsOpen)
{
port.Close();
port.Open();
port.Write("i");
port.Close();
}
else
{
port.Open();
port.Write("i");
port.Close();
}
}
protected void btnOff_Click(object sender, EventArgs e)
{
if (port.IsOpen)
{
port.Close();
port.Open();
port.Write("o");
port.Close();
}
else
{
port.Open();
port.Write("o");
port.Close();
}
}
}
}
As you can see in the code above
protected void btnOn_Click(object sender, EventArgs e)
{
if (port.IsOpen)
{
port.Close();
port.Open();
port.Write("i");
port.Close();
}
else
{
port.Open();
port.Write("i");
port.Close();
}
}
I am making checks before opening the port , if the port is open then close it and again open it for sending .This is because in web application it always gives error that port is open or port can not be accessed and similar error though the port is available .
I tried to avoid these errors.
We are basically developing simple website which will be accessed by mobile or computer.
The website has “On” and “Off” button.
Following is the c# code
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO.Ports;
namespace DotNetControl
{
public partial class _Default : System.Web.UI.Page
{
SerialPort port = new SerialPort("COM8", 9600, Parity.None, 8, StopBits.One);
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (port.IsOpen)
{
port.Close();
}
}
catch (Exception exp)
{
Response.Write(exp.Message);
}
}
protected void btnOn_Click(object sender, EventArgs e)
{
if (port.IsOpen)
{
port.Close();
port.Open();
port.Write("i");
port.Close();
}
else
{
port.Open();
port.Write("i");
port.Close();
}
}
protected void btnOff_Click(object sender, EventArgs e)
{
if (port.IsOpen)
{
port.Close();
port.Open();
port.Write("o");
port.Close();
}
else
{
port.Open();
port.Write("o");
port.Close();
}
}
}
}
As you can see in the code above
protected void btnOn_Click(object sender, EventArgs e)
{
if (port.IsOpen)
{
port.Close();
port.Open();
port.Write("i");
port.Close();
}
else
{
port.Open();
port.Write("i");
port.Close();
}
}
I am making checks before opening the port , if the port is open then close it and again open it for sending .This is because in web application it always gives error that port is open or port can not be accessed and similar error though the port is available .
I tried to avoid these errors.
Step 3:
Other part we need to take care is while deploying the website
1) Bind the website to IP and Port. IP is the IP of the PC where the site is hosted.
2) Open the Port . In Fire wall setting add the port under exception so that you can access the site from mobile devices
1) Bind the website to IP and Port. IP is the IP of the PC where the site is hosted.
2) Open the Port . In Fire wall setting add the port under exception so that you can access the site from mobile devices
Step 4:
3) Access the deployed site by typing IP address
4) Access the same from mobile by typing above and page name
e.g type http://192.168.0.100:12345/default.aspx in mobile browser
The LED can be accessed from LAN means it can also be accessed over internet .
4) Access the same from mobile by typing above and page name
e.g type http://192.168.0.100:12345/default.aspx in mobile browser
The LED can be accessed from LAN means it can also be accessed over internet .