Arduino Ethernet send to php

Hi,

I try to send data to php using Arduino Ethernet. Please can someone post here a very simple code how to make it. I already have the php and database ready so i need only the arduino code.
This is what i made but i thing is something wrong:

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

int incomingalarmstate = 0;

byte mac[] = { 
  0x90, 0xa2, 0xda, 0x0d, 0x69, 0xa4 };
IPAddress ip(192,168,1,240);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress server(47, 226, 195, 1);
EthernetClient client;

void setup(void) {
  Serial.begin(9600);
  Ethernet.begin(mac, ip);
  delay(1000);
  Serial.println("connecting...");

}

void sendalarm() {
  float alarmstate = incomingalarmstate;
  Serial.println(alarmstate);
  delay(1000); 
  if (client.connect(server, 80)) {
    Serial.println("connected");
    client.println("GET /search?q=arduino HTTP/1.1");
    client.println();
  } 
  else {
    Serial.println("connection failed");
  }
  if (client.available()) {
    client.print("GET http://47.226.195.1/controlpanel/getalarm.php?alarmstate=");
    client.print(alarmstate);
    client.println(" HTTP/1.1");
    client.println("Host: http://controlpanel.com");
    client.println();
  }
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;)
      ;
  }
}

void loop(void) {
  sendalarm();
}

I want to send the value alarmstate (0 or 1). I mixed the arduino example with my code.

    client.println("GET /search?q=arduino HTTP/1.1");

search is the script to execute. q=arduino is the name=value data to send.

What script are you going to actually execute?

What name does that script expect to define the value?

Here is a thread that has the stuff to do it.
http://arduino.cc/forum/index.php/topic,124289.0.html

PaulS:

    client.println("GET /search?q=arduino HTTP/1.1");

search is the script to execute. q=arduino is the name=value data to send.

What script are you going to actually execute?

What name does that script expect to define the value?

My php script is getalarm.php. In my code above when i change the line

GET /search?q=arduino HTTP/1.1

with this

GET http://47.226.195.1/controlpanel/getalarm.php?alarmstate=");
    client.print(alarmstate);
    client.println(" HTTP/1.1");

it doesn't work.

SurferTim:
Here is a thread that has the stuff to do it.
http://arduino.cc/forum/index.php/topic,124289.0.html

Thanks for this, i have already read this post but i want something very simple, only to send the value alarmstate

My php script is getalarm.php. In my code above when i change the line

You script name is getalarm.php, not http://47.226.195.1/controlpanel/getalarm.php.
So, why are you using that bogus name in the call?

thanasisloi7:

PaulS:

    client.println("GET /search?q=arduino HTTP/1.1");

search is the script to execute. q=arduino is the name=value data to send.

What script are you going to actually execute?

What name does that script expect to define the value?

My php script is getalarm.php. In my code above when i change the line

GET /search?q=arduino HTTP/1.1

with this

GET http://47.226.195.1/controlpanel/getalarm.php?alarmstate=");

client.print(alarmstate);
    client.println(" HTTP/1.1");



it doesn't work.

Have you tried this?

GET /controlpanel/getalarm.php?alarmstate=");
    client.print(alarmstate);
    client.println(" HTTP/1.1");

I tried and this but nothing. Also i try this but again nothing:

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

int incomingalarmstate = 1;
char pageAdd[32];

byte mac[] = {  0x90, 0xa2, 0xda, 0x0d, 0x69, 0xa4 };
IPAddress ip(192,168,1,240);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress server(43, 222, 192, 2);
EthernetClient client;

void setup()
{
  Serial.begin(9600);
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  Serial.println("Initializing...");
  Serial.println();
  delay(5000);
}

void loop()
{
  Serial.println(incomingalarmstate);
sprintf(pageAdd,"/controlpanel/getalarm.php?alarmstate=%s",incomingalarmstate);
if(!getPage(server,pageAdd));
}

byte getPage(IPAddress ipBuf,char *page)
{
  int inChar;
  char outBuf[128];


  Serial.print("connecting...");

  if(client.connect(ipBuf,80))
  {
    Serial.println("connected");

    sprintf(outBuf,"GET %s HTTP/1.1\r\nHost: www.fdsfasdfads.com\r\n\r\n",page);
    client.write(outBuf);
    delay(5000);
  } 
  else
  {
    Serial.println("failed");
  }
  
  while(client.connected())
  {
    while(client.available())
    {
      inChar = client.read();
      Serial.write(inChar);
    }

    delay(10);
 
  }
  
  Serial.println("disconnecting.");
  client.stop();
}

I tried and this but nothing.

Wrong. Something happened. Look at the logs on the server and find out.

Look in the Serial Monitor, and see what it shows.

Show all the information to us, if you don't understand it.

thanasisloi7:

char pageAdd[32];

sprintf(pageAdd,"/controlpanel/getalarm.php?alarmstate=%s",incomingalarmstate);

Isn't that string longer than the buffer you're storing it in?

Also:

int incomingalarmstate = 1;

You're trying to format a int using a %s format specifier - that's going to produce garbage.

Suggest you use a plain old browser to work out which URL string you need, and then hard-code the path from that URL in your code, and if the client is working it should produce the same result as your browser.

PaulS:

I tried and this but nothing.

Wrong. Something happened. Look at the logs on the server and find out.

Look in the Serial Monitor, and see what it shows.

Show all the information to us, if you don't understand it.

In my Logs files i found this:
[06/Feb/2013:18:31:10 +0200] "GET /controlpanel/getalarm.php?alarmstate= HTTP/1.1" 200 180 "-" "-"
[06/Feb/2013:18:31:15 +0200] "GET /controlpanel/getalarm.php?alarmstate= HTTP/1.1" 200 180 "-" "-"

I used this arduino Code, any help...

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

char pageAdd[32];
int alarmstatebuffer;
int alarmstate = 1;

byte mac[] = {  
  0x90, 0xa2, 0xda, 0x0d, 0x69, 0xa4 };
IPAddress ip(192,168,1,240);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress server(0, 0, 0, 0); //Put here you IP Server
EthernetClient client;

void setup(void) {
  Serial.begin(9600);
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  Serial.println("Initializing...");
}

void loop(void) {
  int alarmstatebuffer = alarmstate;
  Serial.print("Alarm State Buffer: ");
  Serial.println(alarmstatebuffer);
  sprintf(pageAdd,"/controlpanel/getalarm.php?alarmstate=%s",alarmstatebuffer);
  if(!getPage(server,pageAdd));
}

byte getPage(IPAddress ipBuf,char *page)
{
  int inChar;
  char outBuf[128];


  Serial.print("connecting...");

  if(client.connect(ipBuf,80))
  {
    Serial.println("connected");

    sprintf(outBuf,"GET %s HTTP/1.1\r\nHost: www.gggggggg.com\r\n\r\n",page);
    client.write(outBuf);
    Serial.println("Temperatures saved succesfull in your database!!!");
    delay(5000);
  } 
  else
  {
    Serial.println("failed");
  }

  while(client.connected())
  {
    while(client.available())
    {
      inChar = client.read();
      //Serial.write(inChar);
    }

    delay(10);

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

I used this arduino Code, any help...

It's already been pointed out that pageAdd[] is too small for what you are trying to write into it. Why haven't you fixed that?

It's already been pointed out that %s is not the correct format specifier for an int. Why haven't you fixed that?

How can i fix that?

I try also this code:

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

char pageAdd[64];
char alarmstatebuffer[8];
int alarmstate = 0;

byte mac[] = {  
  0x90, 0xa2, 0xda, 0x0d, 0x69, 0xa4 };
IPAddress ip(192,168,1,240);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress server(4, 4, 4, 4); //Put here you IP Server
EthernetClient client;

void setup(void) {
  Serial.begin(9600);
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  Serial.println("Initializing...");
}

void loop(void) {
  dtostrf(alarmstate,3,1,alarmstatebuffer);
  Serial.print("Alarm State Buffer: ");
  Serial.println(alarmstatebuffer);
  sprintf(pageAdd,"/controlpanel/getalarm.php?alarmstate=%s",alarmstatebuffer);
  if(!getPage(server,pageAdd));
}

byte getPage(IPAddress ipBuf,char *page)
{
  int inChar;
  char outBuf[128];


  Serial.print("connecting...");

  if(client.connect(ipBuf,80))
  {
    Serial.println("connected");

    sprintf(outBuf,"GET %s HTTP/1.1\r\nHost: www.dddddddd.com\r\n\r\n",page);
    client.write(outBuf);
    Serial.println("Temperatures saved succesfull in your database!!!");
    delay(5000);
  } 
  else
  {
    Serial.println("failed");
  }

  while(client.connected())
  {
    while(client.available())
    {
      inChar = client.read();
      Serial.write(inChar);
    }

    delay(10);

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

but now in the logs i take this errors:
[06/Feb/2013:18:52:14 +0200] "GET /controlpanel/getalarm.php?alarmstate=state=s HTTP/1.1" 200 180 "-" "-"

dtostrf() expects a double or a float, not an int.

Get rid of alarmstatebuffer[].

sprintf(pageAdd,"/controlpanel/getalarm.php?alarmstate=%i",alarmstate);

I suggest you stop mucking about with string formatting that you evidently don't understand yet, and just type the URL into your sketch:

char *pageAdd = "/controlpanel/getalarm.php?alarmstate=0";

Or whatever values you want to use for testing. You can confirm that the path is correct by typing the corresponding URL into the address field of your browser.

http://4.4.4.4/controlpanel/getalarm.php?alarmstate=0

Does the browser show the response you expected? The Arduino should receive the same response, if the network access is succeeding.

PeterH:
I suggest you stop mucking about with string formatting that you evidently don't understand yet, and just type the URL into your sketch:

char *pageAdd = "/controlpanel/getalarm.php?alarmstate=0";

Or whatever values you want to use for testing. You can confirm that the path is correct by typing the corresponding URL into the address field of your browser.

http://4.4.4.4/controlpanel/getalarm.php?alarmstate=0

Does the browser show the response you expected? The Arduino should receive the same response, if the network access is succeeding.

Yes in this way i get the right response....How can i i change 0 with the value?

thanasisloi7:
Yes in this way i get the right response....How can i i change 0 with the value?

That's great news. Now we know the only problem is the format of the request string.

Your code to generate it using sprintf was along the right lines. You would need something like this:

char pageAdd[64];
int alarmstate=0;
sprintf(pageAdd, "/controlpanel/getalarm.php?alarmstate=%d", alarmstate);

That should produce the same behaviour as before. If it doesn't, print the value of pageAdd before you make the page request, and compare it carefully with the fixed string that you know works correctly.

This works correctly but what %d define? I ask because i want to understand how it works so i can send to the web and other values...

Here is a list of things sprintf does and how to use them. The float stuff doesn't work last time I checked.
http://www.cplusplus.com/reference/cstdio/printf/