Show Posts
Pages: [1]
1  Using Arduino / Programming Questions / Re: problems about Reading website and Getting information on: January 13, 2013, 08:48:42 pm
"I have some code. I'm not going to tell you what it actually does, or what output it produces. Help me fix it." Did I paraphrase your post correctly?
oh sry, i was not clear enough.i m going to make it more clear...but as i said. the first know works as a webclient and it can alright reads the code of the website http://www.weather.com/weather/right-now/Enschede+Netherlands+NLXX0183.
and the second one is one part of the code which enables the serial to return whatever that is between < and >. for example..it will return !DOCTYPE HTML mostly because that is the first line of code in most website.
the original code that tells wind speed in the weather website is <span class"wx-temp"> 5</span>
so my idea is to change the case"<" and ">"  into "temp">" and"<" so that it can read  5 the wind speed.
however i failed to combine both codes and make it work.
i thought it would be ok if i simply copy paste the second code after the first now and organize them a little bit.
but it failed and i feel desperate after  finding any possibilities to fix it for 3hours.
by the way you relied all of my post and offered me lots of help already.big thanks! smiley-kiss
2  Using Arduino / Programming Questions / problems about Reading website and Getting information on: January 13, 2013, 08:26:07 pm
i have got 2 code from forum,  and i wanna combine them so that i can not only read the website's html code but also select the information i need out of it.
the first one is
Code:
//zoomkat 9-22-12
//simple client test
//for use with IDE 1.0.1
//with DNS, DHCP, and Host
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0x00, 0x03, 0x0D, 0xE9, 0xAD, 0x27 }; //physical mac address

char serverName[] = "www.weather.com"; // zoomkat's test web page server
EthernetClient client;
char inString[32]; // string for incoming serial data
int stringPos = 0; // string index counter
boolean startRead = false; // is reading?

//////////////////////

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("Better client test 9/22/12"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
}

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 /weather/right-now/Enschede+Netherlands+NLXX0183 HTTP/1.0"); //download text
    client.println("Host: www.weather.com");
    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
    Serial.print(c); //prints byte to serial monitor
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}

////////////////
this one allows me to connect to the website i wanna visit.

then i have something like this.
Code:
void loop(){
  String pageValue = connectAndRead(); //connect to the server and read the output

  Serial.println(pageValue); //print out the findings.

  delay(5000); //wait 5 seconds before connecting again
}

String connectAndRead(){
  //connect to the server

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

  //port 80 is typical of a www page
  if (client.connect(serverName, 80)) {
    Serial.println("connected");
    client.print("GET ");
    client.println(location);
    client.println("Host: www.weather.com");
    client.println();

    //Connected - Read the page
    return readPage(); //go and read the output

  }else{
    return "connection failed";
  }

}

String readPage() {
  //read the page, and capture & return everything between '<' and '>'

  stringPos = 0;
  memset( &inString, 0, 32 ); //clear inString memory

  while(true){

    if (client.available()) {
      char c = client.read();

      switch (c) {
       
      case '<':
        stringPos = 0;
        startRead = true; //Ready to start reading the part
        break;

      case '>':
        if (startRead) {
          inString[stringPos++] = '\0'; // Add null terminator
          client.stop();
          client.flush();
          Serial.println("disconnecting.");
          return inString;
        }

      default:
        if (startRead && stringPos < (sizeof inString - 1)) {
          inString[stringPos++] = c;
        }
      }
    }
  }
}
this one enable me to get the information between "<",">".and im going to change it to "temp">" and"<"so that i can get the wind speed in the website http://www.weather.com/weather/right-now/Enschede+Netherlands+NLXX0183.

however, i have spend hours combing them and it always returns something useless.
at this step my code is like
Code:
/*
  Web client
 
 This sketch connects to a website (http://www.google.com)
 using an Arduino Wiznet Ethernet shield.
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 
 created 18 Dec 2009
 modified 9 Apr 2012
 by David A. Mellis
 
 */

#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[] = { 0x00, 0x03, 0x0D, 0xE9, 0xAD, 0x27};
IPAddress server(173,194,33,104); // Google
char serverName[] = "www.weather.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;
char inString[32]; // string for incoming serial data
int stringPos = 0; // string index counter
boolean startRead = false; // is reading?

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:
    for(;;)
      ;
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  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 /weather/right-now/Enschede+Netherlands+NLXX0183 HTTP/1.0");
    client.println("Host: www.weather.com");
    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()) {
    String pageValue = readPage();
    Serial.print(pageValue);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    for(;;)
      ;
  }
}
/////////////////////////
String readPage() {
  //read the page, and capture & return everything between '<' and '>'

  stringPos = 0;
  memset( &inString, 0, 32 ); //clear inString memory

  while(true){

    if (client.available()) {
      char c = client.read();

      switch (c) {
       
      case '<':
        stringPos = 0;
        startRead = true; //Ready to start reading the part
        break;

      case '>':
        if (startRead) {
          inString[stringPos++] = '\0'; // Add null terminator
          client.stop();
          client.flush();
          Serial.println("disconnecting.");
          return inString;
        }

      default:
        if (startRead && stringPos < (sizeof inString - 1)) {
          inString[stringPos++] = c;
        }
      }
    }
  }
}

can somebody kindly help me combing them or teach me how to do that?
3  Using Arduino / Project Guidance / Re: Reading a website and selecting imformation among the code return on: January 12, 2013, 10:14:32 am

Why can't you read from the second site? When you try, something is returned to the Arduino. What is it?

Using the String class at all is a really lousy idea.
Code:
/*
  DNS and DHCP-based Web client
 
This sketch connects to a website (http://www.google.com)
using an Arduino Wiznet Ethernet shield.
 
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
 
created 18 Dec 2009
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe, based on work by Adrian McEwen
 
*/

#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[] = {  0x00, 0x03, 0x0D, 0xE9, 0xAD, 0x27 }; //请自行替换成自己的mac地址
char serverName[] = "www.weather.com/weather/right-now/Enschede+Netherlands+NLXX0183";

// 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(1000);
  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 /s?wd=arduino HTTP/1.0");
    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);
  }
}
here is the code i got and it only returns:
connecting…
connected

disconnecting.
so it actually connected but could read nothing.
do u know why?
4  Using Arduino / Project Guidance / Re: Reading a website and selecting imformation among the code return on: January 12, 2013, 10:08:14 am

Why can't you read from the second site? When you try, something is returned to the Arduino. What is it?

Using the String class at all is a really lousy idea.

when i read the first one...it returns Html codes...but when i read the second one...there is nothing but "connecting...."
5  Using Arduino / Project Guidance / Reading a website and selecting imformation among the code return on: January 12, 2013, 08:20:42 am
Im making a smart clock project now and i wanna get information from website posting weather information by using the Web client code.However, i can only read "www.google.com" but not "www.weather.com/weather/right-now/Enschede+Netherlands+NLXX0183" the page which i wanna get information from.
Besides. by looking at the example given in "http://bildr.org/page/11/"
code:
Code:
//ARDUINO 1.0+ ONLY
//ARDUINO 1.0+ ONLY
#include <Ethernet.h>
#include <SPI.h>

////////////////////////////////////////////////////////////////////////
//CONFIGURE
////////////////////////////////////////////////////////////////////////
byte server[] = { 174,123,231,247 }; //ip Address of the server you will connect to

//The location to go to on the server
//make sure to keep HTTP/1.0 at the end, this is telling it what type of file it is
String location = "/~bildr/examples/ethernet/ HTTP/1.0";

// if need to change the MAC address (Very Rare)
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
////////////////////////////////////////////////////////////////////////

EthernetClient client;

char inString[32]; // string for incoming serial data
int stringPos = 0; // string index counter
boolean startRead = false; // is reading?

void setup(){
  Ethernet.begin(mac);
  Serial.begin(9600);
}

void loop(){
  String pageValue = connectAndRead(); //connect to the server and read the output

  Serial.println(pageValue); //print out the findings.

  delay(5000); //wait 5 seconds before connecting again
}

String connectAndRead(){
  //connect to the server

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

  //port 80 is typical of a www page
  if (client.connect(server, 80)) {
    Serial.println("connected");
    client.print("GET ");
    client.println(location);
    client.println();

    //Connected - Read the page
    return readPage(); //go and read the output

  }else{
    return "connection failed";
  }

}

String readPage(){
  //read the page, and capture & return everything between '<' and '>'

  stringPos = 0;
  memset( &inString, 0, 32 ); //clear inString memory

  while(true){

    if (client.available()) {
      char c = client.read();

      if (c == '<' ) { //'<' is our begining character
        startRead = true; //Ready to start reading the part
      }else if(startRead){

        if(c != '>'){ //'>' is our ending character
          inString[stringPos] = c;
          stringPos ++;
        }else{
          //got what we need here! We can disconnect now
          startRead = false;
          client.stop();
          client.flush();
          Serial.println("disconnecting.");
          return inString;

        }

      }
    }

  }

}
, i find it possible to select information by recognizing "<" and ">", so my idea is to recognize something like<span class="wx-temp"> and<span> instead of simply recognizing "<"">". But when i tried to implement this method, it returns nothing in serial monitor, i think it is because client.read reads one character at a time.so how shall i change the code? or can anyone of u bring me any better solutions for this problem?
6  Using Arduino / Project Guidance / Re: How to host a website that can control the Arudino over Internet on: January 09, 2013, 03:32:08 am


Go to walmart and get a netgear 614 router for $25 so you can do your testing with/without the university network.

BTW,as i see in the sketch, am i using the server offered by u?is it possible for me to use my Arduino as the server as well?
7  Using Arduino / Project Guidance / Re: How to host a website that can control the Arudino over Internet on: January 04, 2013, 03:15:19 pm
thanks all of you from explanation and examples...i think i have got some clues now....time to work harder and understand the codes!
8  Using Arduino / Project Guidance / Re: How to host a website that can control the Arudino over Internet on: January 04, 2013, 01:00:38 pm
Client/server test code with buttons.

Code:
//zoomkat 7-03-12, combined client and server
//simple button GET with iframe code
//for use with IDE 1.0
//open serial monitor and send an g to test client and
//see what the arduino client/server receives
//web page buttons make pin 5 high/low
//use the ' in html instead of " to prevent having to escape the "
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields
//note that the below bug fix may be required
// http://code.google.com/p/arduino/issues/detail?id=605

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //assign arduino mac address
byte ip[] = {192, 168, 1, 102 }; // ip in lan assigned to arduino
byte gateway[] = {192, 168, 1, 1 }; // internet access via router
byte subnet[] = {255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port arduino server will use
EthernetClient client;
char serverName[] = "web.comporium.net"; // (DNS) zoomkat's test web page server
//byte serverName[] = { 208, 104, 2, 86 }; // (IP) zoomkat web page server IP address

String readString; //used by server to capture GET request

//////////////////////

void setup(){

  pinMode(5, OUTPUT); //pin selected to control
  pinMode(6, OUTPUT); //pin selected to control
  pinMode(7, OUTPUT); //pin selected to control
  pinMode(8, OUTPUT); //pin selected to control

  //pinMode(5, OUTPUT); //pin 5 selected to control
  Ethernet.begin(mac,ip,gateway,gateway,subnet);
  server.begin();
  Serial.begin(9600);
  Serial.println("server/client 1.0 test 7/03/12"); // keep track of what is loaded
  Serial.println("Send an g in serial monitor to test client"); // what to do to test client
}

void loop(){
  // check for serial input
  if (Serial.available() > 0)
  {
    byte inChar;
    inChar = Serial.read();
    if(inChar == 'g')
    {
      sendGET(); // call client sendGET function
    }
  } 

  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string
          readString += c;
          //Serial.print(c);
        }

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.print(readString); //print to serial monitor for debuging

            //now output HTML data header
          if(readString.indexOf('?') >=0) { //don't send new page
            client.println(F("HTTP/1.1 204 Zoomkat"));
            client.println();
            client.println(); 
          }
          else {   
            client.println(F("HTTP/1.1 200 OK")); //send new page on browser request
            client.println(F("Content-Type: text/html"));
            client.println();

            client.println(F("<HTML>"));
            client.println(F("<HEAD>"));
            client.println(F("<TITLE>Arduino GET test page</TITLE>"));
            client.println(F("</HEAD>"));
            client.println(F("<BODY>"));

            client.println(F("<H1>Zoomkat's simple Arduino 1.0 button</H1>"));

            // DIY buttons
            client.println(F("Pin5"));
            client.println(F("<a href=/?on2 target=inlineframe>ON</a>"));
            client.println(F("<a href=/?off3 target=inlineframe>OFF</a><br><br>"));

            client.println(F("Pin6"));
            client.println(F("<a href=/?on4 target=inlineframe>ON</a>"));
            client.println(F("<a href=/?off5 target=inlineframe>OFF</a><br><br>"));

            client.println(F("Pin7"));
            client.println(F("<a href=/?on6 target=inlineframe>ON</a>"));
            client.println(F("<a href=/?off7 target=inlineframe>OFF</a><br><br>"));

            client.println(F("Pin8"));
            client.println(F("<a href=/?on8 target=inlineframe>ON</a>"));
            client.println(F("<a href=/?off9 target=inlineframe>OFF</a><br><br>"));

            client.println(F("Pins"));
            client.println(F("&nbsp;<a href=/?off2468 target=inlineframe>ALL ON</a>"));
            client.println(F("&nbsp;<a href=/?off3579 target=inlineframe>ALL OFF</a><br><br>"));

           
           
                      // mousedown buttons
          client.println(F("<input type=button value=ON onmousedown=location.href='/?on4;' target=inlineframe>"));
          client.println(F("<input type=button value=OFF onmousedown=location.href='/?off5;' target=inlineframe>"));       
          client.println(F("&nbsp;<input type=button value='ALL OFF' onmousedown=location.href='/?off3579;' target=inlineframe><br><br>"));       
                   
          // mousedown radio buttons
          client.println(F("<input type=radio onmousedown=location.href='/?on6;' target=inlineframe>ON</>"));
          client.println(F("<input type=radio onmousedown=location.href='/?off7; target=inlineframe'>OFF</>"));
          client.println(F("&nbsp;<input type=radio onmousedown=location.href='/?off3579;' target=inlineframe>ALL OFF</><br><br>"));   
   
         
          // custom buttons
          client.print(F("<input type=submit value=ON target=inlineframe style=width:100px;height:45px onClick=location.href='/?on8;'>"));
          client.print(F("<input type=submit value=OFF target=inlineframe style=width:100px;height:45px onClick=location.href='/?off9;' target=inlineframe>"));
          client.print(F("&nbsp;<input type=submit value='ALL OFF' target=inlineframe style=width:100px;height:45px onClick=location.href='/?off3579;' target=inlineframe>"));

           
            client.println(F("<IFRAME name=inlineframe style='display:none'>"));         
            client.println(F("</IFRAME>"));

            client.println(F("</BODY>"));
            client.println(F("</HTML>"));
          }

          delay(1);
          //stopping client
          client.stop();

          ///////////////////// control arduino pin
          if(readString.indexOf('2') >0)//checks for 2
          {
            digitalWrite(5, HIGH);    // set pin 5 high
            Serial.println("Led 5 On");
            Serial.println();
          }
          if(readString.indexOf('3') >0)//checks for 3
          {
            digitalWrite(5, LOW);    // set pin 5 low
            Serial.println("Led 5 Off");
            Serial.println();
          }
          if(readString.indexOf('4') >0)//checks for 4
          {
            digitalWrite(6, HIGH);    // set pin 6 high
            Serial.println("Led 6 On");
            Serial.println();
          }
          if(readString.indexOf('5') >0)//checks for 5
          {
            digitalWrite(6, LOW);    // set pin 6 low
            Serial.println("Led 6 Off");
            Serial.println();
          }
          if(readString.indexOf('6') >0)//checks for 6
          {
            digitalWrite(7, HIGH);    // set pin 7 high
            Serial.println("Led 7 On");
            Serial.println();
          }
          if(readString.indexOf('7') >0)//checks for 7
          {
            digitalWrite(7, LOW);    // set pin 7 low
            Serial.println("Led 7 Off");
            Serial.println();
          }     
          if(readString.indexOf('8') >0)//checks for 8
          {
            digitalWrite(8, HIGH);    // set pin 8 high
            Serial.println("Led 8 On");
            Serial.println();
          }
          if(readString.indexOf('9') >0)//checks for 9
          {
            digitalWrite(8, LOW);    // set pin 8 low
            Serial.println("Led 8 Off");
            Serial.println();
          }         

          //clearing string for next read
          readString="";

        }
      }
    }
  }
}

//////////////////////////
void sendGET() //client function to send and receive GET data from external server.
{
  if (client.connect(serverName, 80)) {
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.0");
    client.println();
  }
  else {
    Serial.println("connection failed");
    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();
    Serial.print(c);
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop();

}



for me,the Eternet Shield im using not is missing the sticker with Mac address on it, What's worse,im living on Campus so i dont have my router but use the internet provided by Uni. In this situation, can i just use the Mac address and IP address .etc. given in your Sketch? or is there any way that i can figure out my own Mac address and IP address?
9  Using Arduino / Project Guidance / Re: How to host a website that can control the Arudino over Internet on: January 04, 2013, 12:52:38 pm
Have you looked any of the examples that came with the IDE?

you mean the examples in the Arduino? ya, i did have a look at some of them, but now i wanna have tutorials that can help me understand how everything works so that i can fully understand it and make my own Web interface
10  Using Arduino / Project Guidance / How to host a website that can control the Arudino over Internet on: January 04, 2013, 09:53:22 am
Im doing a Smart Clock Project which requires a Web user interface.
So i wanna ask the following questions:
1.Do i need to have a fixed IP address for the Eternet shield?
2.I have known that Arudino can be controlled by sending orders through internet,
but how can i make a website-looking thingy which has buttons to control the Arduino?

I will really appreciate it if anyone can help me out here,if it is possible, could u also provide some links to the tutorials about that?
Pages: [1]