HI,
I have ARduino uno 3r and I have the Ethernet shield. I recently bought them.
what I want to do is this:
to access a simple on and off switch remotely. over the internet.
I have read a lot, learned a lot and also at same time got confused a lot with so much info.
what should I do next?
what should I load to arduino?
which pins should I use?
etc?
is there a step by step on how to do it?
has someone done this before and I can copy and learn?
I really really appreciate you help.
Thank you
Dave
Here is a simple webserver sketch to do that. It needs refining if there is more than one client.
Connect an LED and series resistor (1000 ohms should do for the new
extra bright LEDS) to logic pin 2 (pin 3 on the UNO header) for testing.
Note:
you have to find out what your local IPaddress is and substitute it for the example IP in
this sketch which is around line 37.. Use Google to find your WAN IP (for a remote
client to access your webserver) and DhcpAddressPrinter sketch for your local IP
which you should find in Examples|Ethernet in your IDE
/*--------------------------------------------------------------
Program: CheckBox mods by JEK 9/17/14
Description: Arduino web server that serves up a web page
allowing the user to control an LED
Hardware: - Arduino Uno and official Arduino Ethernet
shield. Should work with other Arduinos and
compatible Ethernet shields.
- LED and resistor on pin 2
Software: Developed using Arduino 1.0.3 software
Should be compatible with Arduino 1.0 +
References: - WebServer example by David A. Mellis and
modified by Tom Igoe
This was derived from the example. It did not
get feedback from the client as to whether the box
was actually checked or not, only that it had
been clicked so the server only toggled the led.
If it was out of sync it would forever stay out
of sync. Now it uses feedback to get it back
in sync if it starts up out of sync, which can
happen in some cases. JEK
- Ethernet library documentation:
http://arduino.cc/en/Reference/Ethernet
--------------------------------------------------------------*/
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// this mac address is in the Banggood description
IPAddress ip(192, 168, 1,111); // *** SUBSTITUTE YOUR OWN IPAddress FOR THE ONE IN THIS LINE ***
EthernetServer server(80); // create a server at port 80
String HTTP_req; // stores the entire HTTP request
boolean LED_status = 0; // state of LED, off by default
void setup()
{
Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin(); // start to listen for clients
Serial.begin(9600); // for diagnostics
pinMode(2, OUTPUT); // LED on pin 2
}
void loop()
{
EthernetClient client = server.available(); //try to get client
if (client) {
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) { // client data available to read
char c = client.read(); // read 1 byte (character) from client
HTTP_req += c; // save the HTTP request 1 char at a time
// last line of client request is blank and ends with \n
// respond to client only after last line received
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
// send web page
client.println("<!DOCTYPE html>");
client.println("<html>");
client.println("<head>");
client.println("<title>Arduino LED Control</title>");
client.println("</head>");
client.println("<body>");
///- *** next 3 lines are a potential problem ***
// It is assuming a change of LED status without knowing
if (LED_status)
client.println("<h1>MY LED IS OFF</h1>");
else
client.println("<h1>MY LED IS ON</h1>");
client.println("<p>Click to switch LED on and off.</p>");
client.println("<form method=\"get\">");
ProcessCheckbox(client);
client.println("</form>");
client.println("</body>");
client.println("</html>");
Serial.print(HTTP_req); // debug ***
HTTP_req = ""; // finished with request, empty string
break;
}
// every line of text received from the client ends with \r\n
if (c == '\n') {
// last character on line of received text
// starting new line with next character read
currentLineIsBlank = true;
}
else if (c != '\r') {
// a text character was received from client
currentLineIsBlank = false;
}
} // end if (client.available())
} // end while (client.connected())
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection
} // if (client)
} // loop()
// switch LED and send back HTML for LED checkbox
void ProcessCheckbox(EthernetClient cl)
{
int index;
index = (HTTP_req.indexOf("LED2=2"));
if ( index > -1) // see if checkbox was clicked
{
// the checkbox was clicked
// see where the substring is so we know if the
// checkbox is on or off.
Serial.println(index); // **Debug **
LED_status = (index < 148);
// index = 6 if on, 296 if off
}
if (LED_status) { // switch LED on if true
digitalWrite(2, HIGH);
cl.println("<input type=\"checkbox\" name=\"LED2\" value=\"2\" \
onclick=\"submit();\" checked>LED2");
}
else {
digitalWrite(2, LOW);
cl.println("<input type=\"checkbox\" name=\"LED2\" value=\"2\" \
onclick=\"submit();\">LED2");
}
} // ProcessCheckbox()
@davejava, I have not seen a response from you. If my post was not detailed enough to get you going, lets get together here in real time and I will give you the details step by step. I want to see you getting this project running. It took me quite a while to get a webserver up and working, but I can help you get yours working in a very little time because of my recent experience.
Let me know what day and time we can work together in real time.
HI,
Thank you for the post.
I will be trying it and will let you know.
got busy with family for a while.
now quiet and will get at it.
thanks so much pegwatcher
HI,
how would I make it password protected from wlan?