Hi all:
I am new to Arduino. Needed some help on connecting to the Webserver and being able to e-mail based on a PHP script. I am using a standard script borrowed from the internet.
Here is my setup:
Arduino Uno R3
Ethernet Shield R3
Mac OS X 10.7.2
Arduino IDE 1.0.1
I have posted the code I am using, the corresponding output on the serial monitor and the PHP 'test.php' file content.
Appreciate your help in fixing this or some light on what is that I am missing here fundamentally.
Hi SurferTim:
Thank you for a quick response. I did try your suggestion and here is the output. Please validate and let me know if there is anything still missing. Appreciate that.
connecting...
connected
Sent the http request
HTTP/1.1 302 Found
Date: Sun, 26 Aug 2012 14:09:07 GMT
Server: Apache
Location: http://error404.000webhost.com/?
Content-Length: 216
Connection: close
Content-Type: text/html; charset=iso-8859-1
I will guess that is a virtual hosting web server. It is redirecting you to a error 404 (file not found) page. If that is the case, then you will need to send the Host parameter also. Like this:
Hi dxw00d:
Thank you for your observation. I did try SurferTim's suggestion and below is the outcome
Using this line in the code, I got the following compilation error (in red), hence had to include it within the quotes which works fine. Please let me know if that is correct or am I missing something.
sketch_aug24c:25: error: stray '' in program
sketch_aug24c:25: error: stray '' in program
sketch_aug24c.cpp: In function 'void setup()':
sketch_aug24c:25: error: expected `)' before 'r'
I tried both the following and obtained same results
What you said is correct, i.e the hostname is resolving to a different IP {31, 170, 161, 36} than what I am currently using which is {31, 170, 160, 99}. However, I did try that earlier and got the same response from both the IPs.
Not sure what the problem was so far. Did you do some magic? :). It will be nice if you could share what could've gone wrong and how did it get addressed/fixed finally. I guess one of the factors being the IP address set to what it actually resolved to. The one I've used {31, 170, 160, 99} was given to me by the hosting provider.
Appreciate all the help and persistence all through till it worked.
Thanks and regards.
Here is the output at my end
connecting...
connected
Sent the http request
HTTP/1.1 200 OK
Date: Sun, 26 Aug 2012 16:14:42 GMT
Server: Apache
X-Powered-By: PHP/5.2.17
Content-Length: 193
Connection: close
Content-Type: text/html
No magic here. I am not the Wizard of Oz. I just used what I know about the internet and tcp/ip protocols.
You may want to change that to a dns type server. You can use a domain name for a server now, instead of an ip address. I have not tried it yet, but that would eliminate the possibility of your web host changing the ip of the server. They do that occasionally to do maintenance on the servers.
I want control device over internet.
I have server host at 000webhost.com . server name: http://tiendt343.comli.com
Ardui connect sheid ethenet is client.
i want ask? i problemed as:
i connected to server.
But when i get request , Server reply as is Form html.
connecting...
connected
HTTP/1.1 200 OK
Date: Fri, 03 May 2013 00:51:01 GMT
Server: Apache
X-Powered-By: PHP/5.2.17
Content-Length: 279
Connection: close
Content-Type: text/html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
@01$</html>
<!-- Hosting24 Analytics Code -->
<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
<!-- End Of Analytics Code -->
disconnecting.
Data then i receive is " Form web html" as top.
But i want send 1 request to server and receive 1 value when i request from server. How do ?
this is code arduino
// Chuong trinh Client ket noi webserver
// Ngay bat dau: 23/4/2013
// Arthor : Bui Duy Tien
#include <SPI.h>
#include <Ethernet.h>
// Khai bao dia chi MAC cua Ethernet Shield.
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
// Khai bao server name k?t n?i
char serverName[] = "http://tiendt343.comli.com";
// 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);
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:
while(true);
}
// give the Ethernet shield a second to initialize:
delay(100);
Serial.println("connecting...");
// if you get a connection, report back via serial:
if (client.connect(serverName, 80)) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /vlir/index.php?device=hardware&temp=30&as=200 HTTP/1.0");
client.println("Host: tiendt343.comli.com");
client.println("Connection: close\r\n");
client.println();
}
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);
}
}
and this code php .
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<?php
/**
* @author tiendientu343
* @copyright 2013
*/
mysql_connect('localhost', 'root', '');//K?t n?i CSDL
mysql_select_db('tien');//Ch?n CSDL
/**
* ?i?u khi?n thi?t b?, t? web l?u vào CSDL
*/
if (isset($_GET['sub'])){//?ã submit d? li?u trong form
mysql_query("UPDATE thietbi SET dieukhien='".$_GET['rd1']."' WHERE id='1'");
mysql_query("UPDATE thietbi SET dieukhien='".$_GET['rd2']."' WHERE id='2'");
//header("location:?device=webservice");
}
/**
* L?y d? li?u t? CSDL
*/
$tttb1 = "";
$tttb2 = "";
$txttemp = "";
$txtas = "";
$sql = mysql_query("SELECT * FROM thietbi"); //??c t? CSDL
if ($row = mysql_fetch_object($sql)){
$tttb1 = $row->dieukhien;
$txttemp = $row->giatri;
}
if ($row = mysql_fetch_object($sql)){
$tttb2 = $row->dieukhien;
$txtas = $row->giatri;
}
/**
*
*/
if (isset($_GET['device'])){//Ki?m tra thi?t b? truy c?p
if ($_GET['device']=="hardware"){
/**
* Ph?n c?ng truy c?p
* http://localhost/tien/?device=hardware&temp=30&as=400
*/
if (isset($_GET['temp'])){
mysql_query("UPDATE thietbi SET giatri='".$_GET['temp']."' WHERE id='1'");
}
if (isset($_GET['as'])){
mysql_query("UPDATE thietbi SET giatri='".$_GET['as']."' WHERE id='2'");
}
echo '@'.$tttb1.'&tttb2='.$tttb2.'
Your shield is connecting and getting this as a return from the php server:
echo '@'.$tttb1.'&tttb2='.$tttb2.'
It apparently is printing the "@" and the two values ("01") with the "$", but I don't know why it is not printing the "&ttb2=" part in the middle though.
You should start another topic in networking.
;//response
It apparently is printing the "@" and the two values ("01") with the "$", but I don't know why it is not printing the "&ttb2=" part in the middle though.
You should start another topic in networking.