Reputation: 11
I need help. Now the mo Arduino connects to the web server . On the webserver I would there were a .php page that switches on and off an LED .
Unfortunately I do not know how to write a .php page and do not know how to interface with the Arduino . I searched a lot on the internet , but I find no guide that helped me .
I already did the same thing but with the Arduino web server and it works perfectly . I wish now that the Arduino is the client , and another server sends them parameters.
Anyone have any ideas ?
Here is my sketch ( Connect to the server successfully ) :
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
IPAddress server(xxx,xxx,xxx,xxx); //
// char server[] = "www.google.com"; // name address for Google (using DNS)
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192,168,1,177);
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
pinMode(2, OUTPUT);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected");
// Make a HTTP request:
// client.println("GET /search?q=arduino HTTP/1.1");
// client.println("Host: www.google.com");
// client.println("Connection: close");
client.println();
digitalWrite(2, HIGH);
}
else {
// kf you didn't get a connection to the server:
Serial.println("connection failed");
}
}
void loop()
{
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing forevermore:
while(true);
}
}
Thanks!
Upvotes: 1
Views: 4822
Reputation: 136
I am sharing a link of code for my final year btech project "Controlling devices using internet". (ofcourse this can be done easily by using arduino+ethernet as server but the problem with this is you need port forward the router in order to access server from outside the local network Port forwarding is little risk as per security aspects.)
I used apache server (for testing I installed in my laptop, later I used hosting sites) and Arduino+Ethernet Shield as client. Arduino sends HTTP request to server for XML file after getting, it parses the XMl and control the devices. I used PHP for creating UI and updating XML file....
As you asked "I wish now that the Arduino is the client , and another server sends them parameters"
Put a xml or text file in the server.( now i am considering test.xml, if you want to use text file, put <1,0,1,0,1> in the text file )
<?xml version="1.0" encoding="utf-8"?>
<devices>
<device name="1">
<state>OFF</state>
</device>
<device name="2">
<state>ON</state>
</device>
</devices>
Make your arduino to send HTTP request to server for the xml file every second.
//sending HTTP request for xml file
client.println("GET /test.xml HTTP/1.1");
client.println( "Host: localhost");
client.println();
After getting xml file you need to parse the xml file for reading the value(ON or OFF) in state tag ( check xmlread() function in the arduino code to parse xml )
The following link contain 4 files class.php, xmlupdate.php, test.xml, withxmldevicecontrol.ino (arduino code)
class.php is used for creating UI (Buttons etc.,) and updating XMLfile via xmlupdate.php
xmlupdate.php will update the XML file( test.xml)
withxmldevicecontrol.ino is arduino code, Arduino gets the text.xml file from server, parses the xml and control the devices.
using this arduino can control 10 devices(It is possible to control many devices using port expander IC)
link for code:
https://drive.google.com/folderview?id=0BxWdBbr_6RYkSXVwcGxOa3pxTDA&usp=sharing
Upvotes: 1