Hello everybody
I have a problem that i hope someone can help me solve. I´m using an Arduino Uno with a Ethernet shield and would like to control outputs using a webpage on a different server on my LAN i have tried to google for eksamples but i can´t find any.
So if someone could help me with an eksample or a link to a tutorial i would be very grateful. The only tutorials i have been able to find uses the arduino as a server and i can´t use that.
Thank you very much
I´m using an Arduino Uno with a Ethernet shield and would like to control outputs using a webpage on a different server on my LAN i have tried to google for eksamples but i can´t find any.
The below server test code generates a web page with buttons that send the GET request to client/server connection IP address, to a specific LAN IP address, and thru a dynamic IP address server.
//zoomkat 04-10-15
//simple button GET with iframe code
//open serial monitor to see what the arduino receives
//use the ' instead of " in html ilnes
//address will look like http://192.168.1.102:84/ when submited
//for use with W5100 based ethernet shields
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //ethernet shield mac address
byte ip[] = {
192, 168, 1, 102 }; // arduino IP in lan
byte gateway[] = {
192, 168, 1, 1 }; // internet access via router
byte subnet[] = {
255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port
String readString;
//////////////////////
void setup(){
pinMode(4, OUTPUT); //pin selected to control
//start Ethernet
Ethernet.begin(mac, ip, gateway, gateway, subnet);
server.begin();
//enable serial data print
Serial.begin(9600);
Serial.println("server test no-ip 04-10-15"); // so I can keep track of what is loaded
}
void loop(){
// Create a client connection
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
//Serial.print(c);
}
//if HTTP request has ended
if (c == '\n') {
///////////////
Serial.println(readString); //print to serial monitor for debuging
//now output HTML data header
if(readString.indexOf('?') >=0) { //don't send new page
client.println("HTTP/1.1 204 Zoomkat\r\n\r\n");
}
else {
client.println("HTTP/1.1 200 OK"); //send new page
client.println("Content-Type: text/html");
client.println();
client.println("<HTML>");
client.println("<HEAD>");
client.println("<TITLE>Arduino GET test page</TITLE>");
client.println("</HEAD>");
client.println("<BODY>");
client.println("<H1>Zoomkat's simple Arduino button</H1>");
client.println("Arduino served LAN: <a href='/?on1' target='inlineframe'>ON</a>");
client.println("<a href='/?off' target='inlineframe'>OFF</a>
");
client.println("Remote served LAN: <a href='http://192.168.1.102:84/?on1' target='inlineframe'>ON</a>");
client.println("<a href='http://192.168.1.102:84/?off' target='inlineframe'>OFF</a>
");
client.println("Remote served no-ip: <a href='http://zoomkat.no-ip.org:84/?on1' target='inlineframe'>ON</a>");
client.println("<a href='http://zoomkat.no-ip.org:84/?off' target='inlineframe'>OFF</a>");
client.println("<IFRAME name=inlineframe style='display:none'>");
client.println("</IFRAME>");
client.println("</BODY>");
client.println("</HTML>");
}
delay(1);
//stopping client
client.stop();
///////////////////// control arduino pin
if(readString.indexOf("on1") >0)//checks for on
{
digitalWrite(4, HIGH); // set pin 4 high
Serial.println("Led On");
}
if(readString.indexOf("off") >0)//checks for off
{
digitalWrite(4, LOW); // set pin 4 low
Serial.println("Led Off");
}
//clearing string for next read
readString="";
}
}
}
}
}
The arduino webclient example https://www.arduino.cc/en/Tutorial/WebClient is a good place to start if all you want to do is have your arduino load a webpage and act on the results.
The trick is to modify the section shown below to look for specific strings and perform related actions.
if (client.available()) {
char c = client.read();
Serial.print(c);
}
See this thread http://forum.arduino.cc/index.php/topic,45629.0.html for ways to turn a bunch of individual chars into a nice String.