Go Down

Topic: open URL (Read 9737 times) previous topic - next topic

Mike505

I'm looking for a very simple way to open a URL from the Arduino when it it NOT attached to a PC via serial connection but is using an ethernet shield.

The URL contains keywords that will tell the server what it needs to do.  Specifically, it's telling the server to relay an SMS text. The phone#, message, etc is embedded in the URL as keywords.

Everything that I find that connects with a remote server uses a port number that the server wants to communicate over.

If I type the URL and its keywords into a browser window, everything works as it should and I'm not specifying a port number so how do I make the Arduino do what I can do manually by typing into a browser window.

As always, sample code would really be appreciated.

johnwasser

If you don't tell the browser otherwise it will use Port 80, the default port for HTTP.  Your Arduino can do the same.  Look at Web Client examples like File->Examples->Ethernet->WebClient
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp
See who has no social life: https://forum.arduino.cc/index.php?action=stats :)

Mike505

Thanks but I am using port 80 and it gets stuck on the following check:

      if (client.connect(REMOTE_SERVER, SERVER_PORT) <= 0)
          {
          Serial.println(" connection failed.");
          }

It works if paste what's in REMOTE_SERVER into a browser window but fails during this call from the Arduino.  Not sure why.

PaulS

Quote
It works if paste what's in REMOTE_SERVER into a browser window
Fell free to share that IS in the variable. It is, however, quite likely wrong. What belongs in the variable is JUST the name of the server (or it's IP address).

Mike505

#4
Nov 26, 2014, 02:00 pm Last Edit: Nov 26, 2014, 02:12 pm by Mike505
You're right, the variable contains more than the server name. It also contains a series of slashes followed by information for the server (ex: /message= xyz).

So the question is how to send the other information to the server so that it knows what to do. Can anyone answer that. Is there a simple universal way to for the arduinio to pass that information AFTER the connection is established by including ONLY the server name in the connect statement.

Maybe a new ethernet library routine for someone to write someday - code that not only opens the url but can truly simulate the entry of a string of information into a browser window.

SurferTim

If the URL is http://www.mydomain.com/mypage.php?value=123, then
Code: [Select]
if(client.connect("www.mydomain.com",80) == 1)
// and after connecting
client.println("GET /mypage.php?value=123 HTTP/1.1");

Mike505

Thanks, that's a help.
It's still not working but it seems to be getting closer.
I think the server that it's contacting is refusing to be contacted thru an API
I'll keep working on it but thanks for the help and thanks for the sample code.

zoomkat

Simple client test code you can try to see if your arduino can connect to an outside server.

Code: [Select]

//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[] = "checkip.dyndns.com"; // 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 / HTTP/1.1"); //download text
    client.println("Host: checkip.dyndns.com");
    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

}

Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

Mike505

This link contained an example that worked for me ((I'm using an uno board). Similar to surfer tim's suggestion but with more detail.

However, it only works for a non-secure web site (http) and not a secure site (https). At least that's my assumption because get a 301 error when trying to access the https site but it works perfectly when going to an http site.

http://arduino.cc/en/Tutorial/WebClient

zoomkat

Quote
and not a secure site (https).
The w5100 ethernet shield does not support https.
Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

Go Up