Go Down

Topic: Cannot connect to Apache through arduino Ethernet Shield (Read 968 times) previous topic - next topic

manos437

Oct 12, 2013, 04:47 pm Last Edit: Oct 12, 2013, 05:03 pm by manos437 Reason: 1
i am having a problem with my arduino when trying to connect to a apache web server.

I tried to use both public ip and local ip to connect but none works, i still get connection failed message..

Here is the arduino sketch:

Code: [Select]
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {
 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {
 192, 168, 1, 6 }; //assigned arduino LAN IP address
byte server[] = {
 46,198,212,78 };
EthernetClient client; //apache web server running on port 80

void setup()
{
 Ethernet.begin(mac, ip);
 Serial.begin(9600);
 Serial.println("starting simple arduino client test");
 Serial.println();

 delay(1000);

 Serial.println("connecting...");

 if (client.connect(server, 80 > 0)) {
   Serial.println("connected");
   client.println("GET /write.php?value0=99 HTTP/1.0"); //php page invoking my web service
   client.println();
 }
 else {
   Serial.println("connection failed");
 }
}

void loop()
{
 if (client.available()) {
   char c = client.read();
   Serial.print(c);
 }

 if (!client.connected()) {
   Serial.println();
   Serial.println("disconnecting.");
   Serial.println("==================================");
   Serial.println("");
   client.stop();
   for(;;);
 }
}


And here is the php script:

Code: [Select]
<?php

echo "connection receiving";
$value0=$_GET['value0'];


$opendb=mysql_connect("localhost","root","") or die(mysql_error());
$db mysql_select_db('arduino',$opendb);

if (
$opendb){
  echo 
" database open.";
  if (!
$db) {
echo mysql_error();
}
  
$query "INSERT INTO analoog0 VALUES(curdate(), curtime(), '$value0');";
  
/* Run the query */
  
$resultmysql_query($query)or die(mysql_error());
  
mysql_close($opendb);
  echo 
"values written = $value0";
}


?>



Thanks in advance..!!

SurferTim

Is this server your Apache server? Or is it a hosting company's server?

manos437


Is this server your Apache server? Or is it a hosting company's server?



The server is mine.. It is hosted on my machine

SurferTim

#3
Oct 12, 2013, 04:55 pm Last Edit: Oct 12, 2013, 04:58 pm by SurferTim Reason: 1
Quote
Client and server is the same machine.

What do you mean same? Not both on the Arduino, right?

edit: If the Apache server has a 192.168.1.x ip, use that ip as the server in the Arduino.

manos437


Quote
Client and server is the same machine.

What do you mean same? Not both on the Arduino, right?

edit: If the Apache server has a 192.168.1.x ip, use that ip as the server in the Arduino.



Bad syntax, the web server is running on the computer and the client is the arduino. The apache server doesnt have such an ip adress. Only 127.0.0.1 or "localhost"..

SurferTim

Quote
The apache server doesnt have such an ip adress. Only 127.0.0.1 or "localhost"..

Then you will not be able to connect from any other device, not just the Arduino. It will work fine from a web browser on the server, but nowhere else. You must assign it an ip in the localnet range (192.168.1.x), and allow the server to be accessed from the internet. I suggest trying the web server from a separate computer on the localnet. Get it working with the computer first.

Insure the server firewall allows port 80 requests through from the internet.

manos437


Quote
The apache server doesnt have such an ip adress. Only 127.0.0.1 or "localhost"..

Then you will not be able to connect from any other device, not just the Arduino. It will work fine from a web browser on the server, but nowhere else. You must assign it an ip in the localnet range (192.168.1.x), and allow the server to be accessed from the internet. I suggest trying the web server from a separate computer on the localnet. Get it working with the computer first.

Insure the server firewall allows port 80 requests through from the internet.



I tried with 192.168.1.7 ip adress. I am able to access the server from another local network computer but arduino still gets the error.....

SurferTim

Your parenthesis are not correct:
Code: [Select]
// this is incorrect
if (client.connect(server, 80 > 0)) {
// it should be this:
if (client.connect(server, 80) > 0) {


manos437

#8
Oct 12, 2013, 10:19 pm Last Edit: Oct 12, 2013, 10:23 pm by manos437 Reason: 1

Your parenthesis are not correct:
Code: [Select]
// this is incorrect
if (client.connect(server, 80 > 0)) {
// it should be this:
if (client.connect(server, 80) > 0) {




Ye that was the mistake...baaah...

Now i came up with a new problem. I have a sketch that reads Temperature every one minuite and stores it into a database, but the sketch stops running after some minuites...

Here is the sketch... It always stucks at "Connected" line

Code: [Select]
#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:

//EthernetServer server(80);  // create a server at port 80

IPAddress myDns(46, 198, 212, 78);
byte gw[] = { 192, 168, 1, 1 };
byte subnet[] = { 255, 255, 255, 0 };


byte mac[] = {
 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {
 192, 168, 1, 6 }; //assigned arduino LAN IP address
byte server[] = {
 192, 168, 1, 7 };
EthernetClient client; //apache web server running on port 80


int value0 = 13;                //measurement value analog 0
int value1 = 0;                //measurement value analog 1
int ledPin3 = 12;               //led indicating value analog 0 (pwd-pin)
int ledPin5 = 5;               //led indicating value analog 1 (pwd-pin)
unsigned long time;
long previoustime = 0;         // will store last time loop run
long interval = 60000;        // time between measurements in msec.

void setup(){
 // open the serial port
 Serial.begin(9600);
 //setup ledpins
 pinMode(ledPin3, OUTPUT);
 pinMode(ledPin5, OUTPUT);
 Ethernet.begin(mac, ip);
 //server.begin();
}

void loop(){
 delay(100);
 time = millis();
 Serial.println();
 Serial.println(time);

 int ledpin3value = 0;
 int ledpin5value =0;

 value0 = analogRead(13);
 value0 = (5.0 * value0 * 100.0)/1024.0;
 
// value1 = analogRead(1);
 ledpin3value = map(value0,0,1023,0,255);         //map read values to pwd values
// ledpin5value = map(value1,0,1023,0,255);
 analogWrite(ledPin3,ledpin3value);
 analogWrite(ledPin5,ledpin5value);  
 //Serial.print(value0);            //debug
 //Serial.println(value1);          //debug
 if (time - previoustime > interval) {    //true, send data to server
   previoustime = time;
   Serial.println(previoustime);
   Serial.println(time);            //debug
   senddata();                      //call function to send data to server
 }
 delay(2000);
}

void senddata(){
 // start the Ethernet connection and the server:
 Serial.println("setting up ip connection");
 
 // Initialize the Ethernet server library with the IP address and port you want to use
 // (port 80 is default for HTTP):
 //EthernetClient client = server.available();
 delay(1000);
 if (client.connect(server, 80)){
   delay(1000);
   Serial.println("connected");
   //Serial.println();
  // Serial.println(value0);
   //Serial.println();

   client.print("GET /write.php?value0=");  //GET to php file which handle data (value0 and value1) into a database
   client.print(value0);
   client.println(" HTTP/1.1");
   client.println("Host: 46.198.212.78");  //webserver site with php file (www.asrdff.com/write.php)
   client.println();
   // if there are incoming bytes available
   // from the server, read them and print them:
   delay(100);      //wait some time for server to answer
   while (client.available()) {
     //Serial.println("inside while:");
     char c = client.read();
     Serial.print(c);
   }
 Serial.println();
 Serial.println("End SendData");
 }
 else{
   Serial.println("connection failed");
 }
 client.stop();
}

SurferTim

Here is the "perfect world" version of this code. There should be a timeout in this, but it should be good enough to get you a couple hours of running without a fail. I deleted the delay(100) call. You don't need that with this.
Code: [Select]
    // if there are incoming bytes available
    // from the server, read them and print them:
    while(client.connected()) {   
        while (client.available()) {
          char c = client.read();
          Serial.print(c);
        }
    }
    client.stop();
    Serial.println();
    Serial.println("End SendData");

The server will close the connection when it is finished sending packets, then you close your end. Otherwise, you could be leaving sockets open, and you only have 4.

manos437


Here is the "perfect world" version of this code. There should be a timeout in this, but it should be good enough to get you a couple hours of running without a fail. I deleted the delay(100) call. You don't need that with this.
Code: [Select]
    // if there are incoming bytes available
    // from the server, read them and print them:
    while(client.connected()) {   
        while (client.available()) {
          char c = client.read();
          Serial.print(c);
        }
    }
    client.stop();
    Serial.println();
    Serial.println("End SendData");

The server will close the connection when it is finished sending packets, then you close your end. Otherwise, you could be leaving sockets open, and you only have 4.



Unfortunately it still doesnt works, it stucks.... :(

SurferTim

#11
Oct 13, 2013, 04:33 am Last Edit: Oct 13, 2013, 04:36 am by SurferTim Reason: 1
What is the last thing displayed on the serial monitor? Does it read part of the page or freeze at "connected"?

edit: Here is the client code I use. It has a timeout feature that keeps it from locking up in the "while(client.connected())" loop.
http://playground.arduino.cc/Code/WebClient

manos437


What is the last thing displayed on the serial monitor? Does it read part of the page or freeze at "connected"?

edit: Here is the client code I use. It has a timeout feature that keeps it from locking up in the "while(client.connected())" loop.
http://playground.arduino.cc/Code/WebClient



It always freezes on "connected".

I will try to modify my code from your link and see if it works.

SurferTim

Sounds like the server stalls. Your code must compensate for that. Take a look at the example code in the link I posted above. The connectLoop variable controls the timeout feature.

I can create that type of fail (freeze/lockup) by downloading a large page like Google's home page, then pulling the CAT5 out of the Arduino as soon as the page starts downloading. Without the timeout, it will lock up. With the timeout, it stalls for only 10 seconds, then goes right back to it.

manos437


Sounds like the server stalls. Your code must compensate for that. Take a look at the example code in the link I posted above. The connectLoop variable controls the timeout feature.

I can create that type of fail (freeze/lockup) by downloading a large page like Google's home page, then pulling the CAT5 out of the Arduino as soon as the page starts downloading. Without the timeout, it will lock up. With the timeout, it stalls for only 10 seconds, then goes right back to it.



I modified the code from your link above for my needs and it runs for over one hour without problem. :)

Go Up