Pages: [1]   Go Down
Author Topic: XPort Direct+ using connection by hostname  (Read 3598 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 84
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I am wondering if the XPort Direct+ can talk to a web server using its name instead of its IP address?
Cwww.google.com/80 instead of Cxxx.yyy.zzz.aaa/80
Did someone tried that?
I am wondering that because nowadays, where every web site is a virtual host, it is often impossible to connect to them using IP directly.
Thanks
« Last Edit: April 06, 2010, 10:05:26 am by strus_fr » Logged

Pullman, WA
Offline Offline
Edison Member
*
Karma: 12
Posts: 1248
Arduino Ninja
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It's still possible to connect to an IP address. That's why you also have the "Host:" line in the request. The correct vhost will be selected based on that parameter.
Logged

Wearable Arduino-compatible LED matrix glasses: https://www.kickstarter.com/projects/macetech/rgb-led-shades

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 84
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok, but before sending the Host: I need to connect to it and it is where the connection fails 99% of the time.
« Last Edit: April 06, 2010, 08:35:18 pm by strus_fr » Logged

Pullman, WA
Offline Offline
Edison Member
*
Karma: 12
Posts: 1248
Arduino Ninja
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Are you going to post some code? Just as a guess, if you try to connect while already connected, things won't work as you hope.
Logged

Wearable Arduino-compatible LED matrix glasses: https://www.kickstarter.com/projects/macetech/rgb-led-shades

Wuppertal/Germany
Offline Offline
God Member
*****
Karma: 1
Posts: 895
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
AFAIK the XPort direct+ does not support DNS, so this is not possible.

The XPort AR seems to be the only one from the product line that supports this, but its expensive too!

But at least here in germany (virtual) server prices with a dedicated IP address have dropped down to 10€ a month.

Eberhard



Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 84
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I will dump my code here tonight, but I think your advice worked.
I used 'Host: xxxxx.com' and the request go through.
But it goes through only when it wants ... the connect() fails for time to time and the issue may be linked to what you suggested. I need to do more testing, and I will give you the results later.

Update: so putting a myserial.disconnected() around the connect() statement helps a lot indeed !! Now I have a connection every time.
I still have some failure on the reset sometimes ... may be I need a flush somewhere.
But now my concern is that when I post a POST request I get as an answer:
*** NodeSet 2.0 ***
And not my "HTTP/1.1 200 OK" answer .... which means I entered into the Monitor Mode at some point ... not sure how !!

If I do the same POST using curl it works fine.

Here is some code pde and php (that may help others). Any help is welcome:

Code:
#include <NewSoftSerial.h>
#include <AF_XPort.h>
#include <avr/io.h>
#include <string.h>

#define HTTPPATH "/test.php"
#define WEB_HOST "secretweb.free.fr"
#define IPADDR "2x2.yx.63.zzz"
#define PORT 80

char linebuffer[256];

#define XPORT_RXPIN 2
#define XPORT_TXPIN 3
#define XPORT_RESETPIN 4
#define XPORT_DTRPIN 0
#define XPORT_CTSPIN 6
#define XPORT_RTSPIN 0

//*** Channel 1
//Baudrate 9600, I/F Mode 4C, Flow 00
//Port 10001
//Connect Mode : D4
//Send '+++' in Modem Mode enabled
//Show IP addr after 'RING' enabled
//Auto increment source port disabled
//Remote IP Adr: --- none ---, Port 00000
//Disconn Mode : 00
//Flush   Mode : 77

AF_XPort xPortSerial = AF_XPort(XPORT_RXPIN, XPORT_TXPIN, XPORT_RESETPIN, XPORT_DTRPIN, XPORT_RTSPIN, XPORT_CTSPIN);

uint8_t errno;

void setup()  
{
  Serial.begin(9600);
  Serial.println("Ready?");

  // set the data rate for the NewSoftSerial port
  xPortSerial.begin(9600);
  int ver = NewSoftSerial::library_version();
  Serial.println(ver);
}

void loop()                     // run over and over again
{
  Serial.println("Saving...");
  save_data();
  Serial.println("Done");
  delay(10000);
}

void save_data() {
  int i=0;
  char json[328];
  sprintf(json,"{\"log\":{\"AC_relays\":[{\"id\":0,\"value\":%d},{\"id\":1,\"value\":%d},{\"id\":2,\"value\":%d},{\"id\":3,\"value\":%d}],\"DC_relays\":[{\"id\":0,\"value\":%d},{\"id\":1,\"value\":%d},{\"id\":2,\"value\":%d},{\"id\":3,\"value\":%d}],\"ds18b20\":[{\"id\":0,\"value\":%2d},{\"id\":1,\"value\":%2d},{\"id\":2,\"value\":%2d}],\"water_level\":[{\"id\":0,\"value\":%4d},{\"id\":1,\"value\":%4d}]}}",0,1,0,1,1,2,1,0,25,22,27,1003,2000);
  
  httpPost(json);
  //httpGet();
}

int httpPost(char* json)
{
  uint8_t ret;
  char *found=0;

  ret = xPortSerial.reset();
  switch (ret) {
   case  ERROR_TIMEDOUT: {
      Serial.println("Timed out on reset!");
      return 0;
   }
   case ERROR_BADRESP:  {
      Serial.println("Bad respons on reset!");
      return 0;
   }
   case ERROR_NONE: {
    Serial.println("Reset OK!");
    break;
   }
   default:
     Serial.println("Unknown error");
     return 0;
  }
  
  // time to connect...
  if (xPortSerial.disconnected()) {
  ret = xPortSerial.connect(IPADDR, PORT);
  switch (ret) {
   case  ERROR_TIMEDOUT: {
      Serial.println("Timed out on connect");
      return 0;
   }
   case ERROR_BADRESP:  {
      Serial.println("Failed to connect");
      return 0;
   }
   case ERROR_NONE: {
     Serial.println("Connected..."); break;
   }
   default:
     Serial.println("Unknown error");
     return 0;
  }
  } else {
   Serial.println("Already connected");
  }
  //xPortSerial.flush(250);
  
  //xPortSerial.print("GET "); xPortSerial.print("/"); xPortSerial.println(" HTTP/1.1");
  //base64encode(USERNAMEPASS, linebuffer);

  xPortSerial.print("POST "); xPortSerial.print(HTTPPATH); xPortSerial.println(" HTTP/1.1");
  Serial.print("POST "); Serial.print(HTTPPATH); Serial.println(" HTTP/1.1");
  // next, the authentication
  xPortSerial.print("Host: "); xPortSerial.println(WEB_HOST);
  Serial.print("Host: "); Serial.println(WEB_HOST);
  //xPortSerial.print("Authorization: Basic ");
  //Serial.print("Authorization: Basic ");
  //xPortSerial.println(linebuffer);
  //Serial.println(linebuffer);
  //xPortSerial.println("User-Agent: arduino");
  //Serial.println("User-Agent: arduino");
  xPortSerial.print("Content-Length: "); xPortSerial.println(1+strlen(json), DEC);
  Serial.print("Content-Length: "); Serial.println(1+strlen(json), DEC);
  xPortSerial.print("Content-Type: application/x-www-form-urlencoded\n");
  Serial.print("Content-Type: application/x-www-form-urlencoded\n");
  //xPortSerial.print("\nsubmit=");
  xPortSerial.println(json);
  //Serial.print("\nsubmit=");
  Serial.println(json);
  
  xPortSerial.println("");
 Serial.println("");

 // while (1) {
    // read one line from the xport at a time
    ret = xPortSerial.readline_timeout(linebuffer, 255, 3000); // 6s timeout
    Serial.println(linebuffer);
    if (strstr(linebuffer, "HTTP/1.1 200 OK") == linebuffer) {
      Serial.println("Done Success...");
      ret = 1;
    } else if (strstr(linebuffer, "HTTP/1.1 400 Bad Request") == linebuffer) {
      Serial.println("Done Bad Request..");
      ret = 0;
    }
    if (((errno == ERROR_TIMEDOUT) && xPortSerial.disconnected()) ||
        ((XPORT_DTRPIN == 0) &&
         (linebuffer[0] == 'D') && (linebuffer[1] == 0)))  {
       Serial.println("Disconnected...");
       return ret;
    }
 // }
}

Here is the test.php file:
Code:
<?php
      
if (!function_exists('json_decode')) &#123;
            
function json_decode($json) &#123;
             
   $comment false;
             
   $out '$x=';
             
   for ($i=0$i<strlen($json); $i++) 
             
   &#123; 
                   
 if (!$comment
                   
 &#123; 
                        
if ($json[$i] == '&#123;'$out .= ' array('
                        else if (
$json[$i] == '&#125;'$out .= ')'
                        else if (
$json[$i] == ':'$out .= '=>';
                   
   else if ($json[$i] == '['$out .= ' array(';                         else if ($json[$i] == ']'$out .= ')'
                        else 
$out .= $json[$i];            
                   
 &#125; 
                   
 else $out .= $json[$i]; 
                   
 if ($json[$i] == '"'$comment = !$comment
             
   &#125;
             
   eval($out ';');
             
   return $x;
            &
#125;
      
&#125;
      
$handle fopen('php://input','r');
      
$jsonInput fgets($handle);
      
$decoded json_decode($jsonInput);
      
var_dump($decoded);
      
fclose($handle);
?>


Here is the result I get:
Code:
Ready?
9
Saving...
Reset OK!
Connected
POST /test.php HTTP/1.1
Host: mysecretweb.com
Content-Length: 22
Content-Application: application/x-www-form-urlencoded

<json string>
*** NodeSet 2.0 ***
Done

Any idea how can I get an html header as expected.
« Last Edit: April 07, 2010, 06:40:13 pm by strus_fr » Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 84
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, after more experiences, I finally succeed in having all working.
So, here is a piece of code for anybody who wants to send some data to a mysql database on a web server using PHP and the ethernet shield with XPort Direct+: (methods names do not match: I was supposed to use POST and JSON, I finally I am using GET and a string...):

PDE code:
Code:
#include <NewSoftSerial.h>
#include <AF_XPort.h>
#include <avr/io.h>
#include <string.h>

#define HTTPPATH "/xPortPost.php?submit="
#define WEB_HOST "mywebsite.com"
#define IPADDR "9.38.zz.1xx"
#define PORT 80

char linebuffer[256];

#define XPORT_RXPIN 2
#define XPORT_TXPIN 3
#define XPORT_RESETPIN 4
#define XPORT_DTRPIN 0
#define XPORT_CTSPIN 6
#define XPORT_RTSPIN 0

//*** Channel 1
//Baudrate 9600, I/F Mode 4C, Flow 00
//Port 10001
//Connect Mode : D4
//Send '+++' in Modem Mode enabled
//Show IP addr after 'RING' enabled
//Auto increment source port disabled
//Remote IP Adr: --- none ---, Port 00000
//Disconn Mode : 00
//Flush   Mode : 77

AF_XPort xPortSerial = AF_XPort(XPORT_RXPIN, XPORT_TXPIN, XPORT_RESETPIN, XPORT_DTRPIN, XPORT_RTSPIN, XPORT_CTSPIN);

uint8_t errno;

void setup()  
{
  Serial.begin(9600);
  Serial.println("Ready?");

  // set the data rate for the NewSoftSerial port
  xPortSerial.begin(9600);
  int ver = NewSoftSerial::library_version();
  Serial.println(ver);
}

void loop()                     // run over and over again
{
  Serial.println("Saving...");
  save_data();
  Serial.println("Done");
  delay(10000);
}

void save_data() {
  int i=0;
  char json[328];
  sprintf(json,"ac0:%d,ac1:%d,ac2:%d,ac3:%d,dc0:%d,dc1:%d,dc2:%d,dc3:%d,temp0:%2d,temp1:%2d,temp2:%2d,ato0:%4d,ato1:%4d,alarm:%d",0,1,0,1,1,2,1,0,25,22,27,1003,2000,3);
  
  httpPost(json);
}

int httpPost(char* json)
{
  uint8_t ret;
  char *found=0;

  ret = xPortSerial.reset();
  switch (ret) {
   case  ERROR_TIMEDOUT: {
      Serial.println("Timed out on reset!");
      return 0;
   }
   case ERROR_BADRESP:  {
      Serial.println("Bad respons on reset!");
      return 0;
   }
   case ERROR_NONE: {
    Serial.println("Reset OK!");
    break;
   }
   default:
     Serial.println("Unknown error");
     return 0;
  }
  
  // time to connect...
  ret = xPortSerial.connect(IPADDR, PORT);
  switch (ret) {
   case  ERROR_TIMEDOUT: {
      Serial.println("Timed out on connect");
      return 0;
   }
   case ERROR_BADRESP:  {
      Serial.println("Failed to connect");
      return 0;
   }
   case ERROR_NONE: {
     Serial.println("Connected..."); break;
   }
   default:
     Serial.println("Unknown error");
     return 0;
  }
  
  xPortSerial.print("GET "); xPortSerial.print(HTTPPATH);   xPortSerial.print(json); xPortSerial.println(" HTTP/1.1");
  Serial.print("GET "); Serial.print(HTTPPATH); Serial.print(json); Serial.println(" HTTP/1.1");
  xPortSerial.print("Host: "); xPortSerial.println(WEB_HOST);
  Serial.print("Host: "); Serial.println(WEB_HOST);
  
  xPortSerial.println("");
  Serial.println("");
 
  while (1) {
    // read one line from the xport at a time
    ret = xPortSerial.readline_timeout(linebuffer, 255, 3000); // 6s timeout
    Serial.println(linebuffer);
    if (strstr(linebuffer, "HTTP/1.1 200 OK") == linebuffer) {
      Serial.println("Done Success...");
      ret = 1;
    } else if (strstr(linebuffer, "HTTP/1.1 400 Bad Request") == linebuffer) {
      Serial.println("Done Bad Request..");
      ret = 0;
    }
    if (((errno == ERROR_TIMEDOUT) && xPortSerial.disconnected()) ||
        ((XPORT_DTRPIN == 0) &&
         (linebuffer[0] == 'D') && (linebuffer[1] == 0)))  {
       Serial.println("Disconnected...");
       return ret;
    }
  }
}

le code php de xPortPost.php
Code:
<?php
      $DBhost 
"myhost";
      
$DBuser "myself";
      
$DBpass "secretpasswd";
      
$DBName "mysuperdb";
      
$table "my_data";
      
      if (isset(
$_GET['submit'])) &#123;
            
$con mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");

            @
mysql_select_db("$DBName") or die("Unable to select database $DBName");
            
            
$decoded $_GET['submit'];
            
$tmp split(","$decoded);
            for(
$i 0$i sizeof($tmp); $i=$i+1) &#123;
            
                  
$bou split (":"$tmp[$i]);
                  
$name $bou[0];
                  
$value $bou[1];
                  
$sqlquery "INSERT INTO $table (name, value) VALUES('$name','$value')";
                  if (!
mysql_query($sqlquery,$con))
                   
 &#123;
                        
die('Error: ' mysql_error());
                  &
#125;
            
&#125;
            
mysql_close();
      &
#125;
?>

« Last Edit: April 10, 2010, 04:54:07 pm by strus_fr » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have been spending a lot of time with this Strus' above program however, is seems that unless the server times out I fail to get a connection, maybe the server won't allow multiple connections from the same IP?  

I am wondering of there is a "disconnect" command for the Xport and how it would work


Tyler
Logged

Pullman, WA
Offline Offline
Edison Member
*
Karma: 12
Posts: 1248
Arduino Ninja
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Disconnect settings are controlled in the XPort configuration. You can set a local timeout, disconnect on a character, etc.
Logged

Wearable Arduino-compatible LED matrix glasses: https://www.kickstarter.com/projects/macetech/rgb-led-shades

0
Offline Offline
Newbie
*
Karma: 0
Posts: 1
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can use hostname and IP address. xPort can Support DNS. Just update it to the lasted firmware version from Grid Connect.
Logged

Pages: [1]   Go Up
Jump to: