Go Down

Topic: Arduino ethernet webclient php script and mysql (Read 61 times) previous topic - next topic

zoomkat

Quote
So would I use the Post method from a html Form or is there a different method?

Post and get request are two different animals. Get request are appended to the url using the ?. Post are sent as seperate attachments with different requirements. Due to various issues on the server end, I've always use the get method because it is simple and met my needs. I think you can use either the post or get methods in an html form.
Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

xsilvergs

I'm afraid this is just not working out for me  :'(
The code in the Arduino is this:
Code: [Select]
//zoomkat 12-21-10
//simple ethernet client test code
//for use with IDE 0021 and W5100 ethernet shield
//modify the arduino lan ip address as needed
//open serial monitor to see what the arduino receives
//push the shield reset button to run client again

#include <SPI.h>
#include <Ethernet.h>
String readString, readString1;

byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x11, 0xAC };
byte ip[] = { 192, 168, 1, 19 };
byte server[] = { 192,168,1,20 };

Client client(server, 80);

void setup()
{
 Ethernet.begin(mac, ip);
 Serial.begin(9600);
//pinMode;
pinMode(7,OUTPUT); digitalWrite(7,LOW);  // I use this pin as GND for the LED.
pinMode(8,OUTPUT); // Sample output, unable to use built-in LED at pin 13 because Ethernet Shield uses pins 10,11,12,13.
 
 Serial.println("starting simple arduino client test");
 Serial.println();
 Serial.println("connecting...");

 if (client.connect()) {
   Serial.println("connected");
   client.println("GET /update_db.php?celsius=45 HTTP/1.0");
   client.println();
 } else {
   Serial.println("connection failed");
 }
}

void loop()
{
 if (client.available()) {
   char c = client.read();
   readString += c;
 }
       
 if (!client.connected()) {
    client.stop();
   
   Serial.print(readString);
   Serial.println();
   Serial.println();
   
   int d1 = readString.indexOf('<');
   int d2 = readString.indexOf('>');
   readString1 = (readString.substring(d1+1,d2));
   Serial.println(readString1);

   if(readString1 == ("heater1=on")) {
     digitalWrite(7, HIGH);    // set the LED on
   
   }
   
   Serial.println("==================================");
   Serial.println("data from server:");
   Serial.println();
   Serial.println(readString1);
   Serial.println();
   Serial.println("==================================");
   Serial.println();
     
   for(;;);
   
   }
}



First Question.
Can you explain "if (!client.connected()) {
    client.stop();" as used in the above code please. Won't it miss out the next part
Code: [Select]
Serial.print(readString);
   Serial.println();
   Serial.println();
   int d1 = readString.indexOf('<');
   int d2 = readString.indexOf('>');
   readString1 = (readString.substring(d1+1,d2));
   Serial.println(readString1);

   if(readString1 == ("heater1=on")) {
     digitalWrite(7, HIGH);    // set the LED on


Second Question.
Shouldn't I be able to type
Code: [Select]
http://192.168.1.19/<heater1=on> and hit Enter to turn the LED on?

zoomkat

Quote
First Question. Can you explain "if (!client.connected()) {
   client.stop();" as used in the above code please. Won't it miss out the next part


All I can deduce from the code is that the ethernet library has a function, client.connected(), which will respond true/not true if a connection is made to a server. Putting in the ! turns it into NOT connected. If there is no connection the server, then the code starts doing other things.  

Quote
Shouldn't I be able to type Code:http://192.168.1.19/<heater1=on>
and hit Enter to turn the LED on?


No for a couple of reasons. First is that the url is not made to http get standards. There is no ? to indicate an attached query_string, and the <> characters may be reserved and probably can't be used in a query_string. Second, it appears that you are trying to send a client request to an arduino client. Clients do not receive request, they send request to servers. Servers receive request from clients and return info to the client. Servers do not send request. Using google, search for things like "http tutorial", "query_string tutorial",  "http get" and "http post", and similar to get familiar with what you are working with.

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

xsilvergs

#38
Dec 22, 2010, 11:23 am Last Edit: Dec 22, 2010, 11:30 am by xsilvergs Reason: 1
zoomkat, thanks for info, I've never understood how ethernet works, this is all very interesting but I do have a better understanding.

I'm still not sure about this bit of code:
Code: [Select]

if (!client.connected()) {
    client.stop();
   
   Serial.print(readString);
   Serial.println();
   Serial.println();
   
   int d1 = readString.indexOf('<');
   int d2 = readString.indexOf('>');
   readString1 = (readString.substring(d1+1,d2));
   Serial.println(readString1);

   if(readString1 == ("heater1=on")) {
     digitalWrite(7, HIGH);    // set the LED on
   
   }
   

Does it not say:- If the client is not connected stop? But how does it ever manipulate the next lines of code
Code: [Select]

Serial.print(readString);
   Serial.println();
   Serial.println();
   
   int d1 = readString.indexOf('<');
   int d2 = readString.indexOf('>');
   readString1 = (readString.substr
etc, etc....

as they are all within the same {  }.

If the client is connected will it not jump straight to here?
Code: [Select]

for(;;);
   
   [glow]}[/glow]
}




PaulS

Look back at the code in Reply #24. You extended that code, adding code in the wrong place.

Go Up