Hi,
I have the ethernet shield and its working with the webclient examples.
I need to send a value to a PHP page. Here, the value will be checked: if the value is correct it will send back to the arduino a 'true' (otherwise a 'false') ack.
I'm a bit lost about how my http request should be and how to send back the response. I read a few tutorials but I couldn't find a clear template to connect to a custom php page and then send back the response..
thanks
L.
I'm using the webclient example but it won't print out anything that's in the page..
#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[] = { 0x90, 0xA2, 0xDA, 0x10, 0x0B, 0x80 };
// 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(74,125,232,128); // numeric IP for Google (no DNS)
char server[] = "http://www.assatena.it/"; // name address for Google (using DNS)
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 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(4,HIGH);
//pinMode(10,HIGH);
delay(5000);
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
}
// give the Ethernet shield a second to initialize:
delay(5000);
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 /http://www.assatena.it/Atena/arduTrial.php HTTP/1.1\r\n");
client.println("Host: http://www.assatena.it");
client.println("Connection: close");
client.println();
} else {
// if 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);
}
}
#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[] = { 0x90, 0xA2, 0xDA, 0x10, 0x0B, 0x80 };
// 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(62,149,150,179); // numeric IP for Google (no DNS)
char server[] = "webpub01.ad.aruba.it"; // name address for Google (using DNS)
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 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(4,HIGH);
//pinMode(10,HIGH);
delay(5000);
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
}
// give the Ethernet shield a second to initialize:
delay(5000);
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 /Atena/arduTrial.php HTTP/1.1\r\n");
client.println("Host: http://www.assatena.it");
client.println("Connection: close");
client.println();
} else {
// if 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);
}else
Serial.println("not AVAILABLE");
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing forevermore:
while (true);
}
}
now..at least I get something which is a "error 400 - bad request, the hostname is invalid"
as the website is on a shared hosting I can't use the ip address of the server..I know the name of the server, which is webpub01.ad.aruba.it
sorry, but I am completely lost when I try to understand your issue.
Are you able to connect ?
I see different strange things in your code. You do a \r\n in the first println, but println is sending a new line. And you don't do it in the next line. I would not do that, I would use print, not println and add the CR LF in the string itself.
I am pretty sure that your HTTP header is misformed, it should be checked with a tool like Wireshark.
When I deal with server issues like that, I validate my HTTP code on a PC or a Mac. Once the code is working, I put it on the Arduino, so I am sure that the HTTP is formed correctly (so a problem will come only from the C code itself, not from the HTTP protocol implemented by hands)
That is probably the server saying that there is something wrong with the below. What is the UL that works in a browser? If the preface is https then the arduino won't work.
Client test code you can try unmodified to see if you can get a response from your server. I tried it and got a response from your server.
//zoomkat 11-04-13
//simple client test
//for use with IDE 1.0.1
//with DNS, DHCP, and Host
//open serial monitor and send an e to test client GET
//for use with W5100 based ethernet shields
//remove SD card if inserted
//data from myIP server captured in readString
#include <SPI.h>
#include <Ethernet.h>
String readString;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
char serverName[] = "www.assatena.it"; // myIP server test web page server
EthernetClient client;
//////////////////////
void setup(){
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
while(true);
}
Serial.begin(9600);
Serial.println("client readString test 11/04/13"); // so I can keep track of what is loaded
Serial.println("Send an e in serial monitor to test"); // what to do to test
Serial.println();
}
void loop(){
// check for serial input
if (Serial.available() > 0) //if something in serial buffer
{
byte inChar; // sets inChar as a byte
inChar = Serial.read(); //gets byte from buffer
if(inChar == 'e') // checks to see byte is an e
{
sendGET(); // call sendGET function below when byte is an e
}
}
}
//////////////////////////
void sendGET() //client function to send/receive GET request data.
{
if (client.connect(serverName, 80)) { //starts client connection, checks for connection
Serial.println("connected");
client.println("GET /Atena/arduTrial.php HTTP/1.1"); //download text
client.println("Host: www.assatena.it");
client.println("Connection: close"); //close 1.1 persistent connection
client.println(); //end of get request
}
else {
Serial.println("connection failed"); //error message if no client connect
Serial.println();
}
while(client.connected() && !client.available()) delay(1); //waits for data
while (client.connected() || client.available()) { //connected or data available
char c = client.read(); //gets byte from ethernet buffer
readString += c; //places captured byte in readString
}
//Serial.println();
client.stop(); //stop client
Serial.println("client disconnected.");
Serial.println("Data from server captured in readString:");
Serial.println();
Serial.print(readString); //prints readString to serial monitor
Serial.println();
Serial.println();
Serial.println("End of readString");
Serial.println("==================");
Serial.println();
readString=""; //clear readString variable
}
Hi, thank you. I tried your code but it won't print out anything other than
Just a check, did you type an e in the serial monitor and send it to the arduino? Below is what I get returned when I run the code I posted.
client readString test 11/04/13
Send an e in serial monitor to test
connected
client disconnected.
Data from server captured in readString:
HTTP/1.1 200 OK
Date: Sun, 21 Feb 2016 16:14:38 GMT
Server: Apache
X-Powered-By: PHP/5.5.31
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html
d9
<html>
<head>
<title>Sensor Data</title>
</head>
<body>
<h1>value</h1>
<table border="1" cellspacing="1" cellpadding="1">
<tr>
<td>30</td>
<td>25</td>
</tr>
</table>
</body>
</html>
0
End of readString
==================