Go Down

Topic: Arduino Ethernet send to php (Read 1 time) previous topic - next topic

thanasisloi7

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:
Code: [Select]

#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.

PaulS

Code: [Select]
    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?

SurferTim

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

thanasisloi7


Code: [Select]
    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
Code: [Select]
GET /search?q=arduino HTTP/1.1
with this
Code: [Select]
GET http://47.226.195.1/controlpanel/getalarm.php?alarmstate=");
    client.print(alarmstate);
    client.println(" HTTP/1.1");

it doesn't work.

thanasisloi7


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

PaulS

Quote
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?

Quick5pnt0



Code: [Select]
    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
Code: [Select]
GET /search?q=arduino HTTP/1.1
with this
Code: [Select]
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?
Code: [Select]
GET /controlpanel/getalarm.php?alarmstate=");
    client.print(alarmstate);
    client.println(" HTTP/1.1");

thanasisloi7

I tried and this but nothing. Also i try this but again nothing:
Code: [Select]
#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();
}

PaulS

Quote
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.

PeterH

#9
Feb 05, 2013, 11:48 pm Last Edit: Feb 05, 2013, 11:51 pm by PeterH Reason: 1

Code: [Select]

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:

Code: [Select]

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.
I only provide help via the forum - please do not contact me for private consultancy.

thanasisloi7

#10
Feb 06, 2013, 05:32 pm Last Edit: Feb 06, 2013, 05:35 pm by thanasisloi7 Reason: 1

Quote
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...
Code: [Select]
#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(); 
}


PaulS

Quote
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?

thanasisloi7

How can i fix that?

I try also this code:
Code: [Select]
#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 "-" "-"

PaulS

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

Get rid of alarmstatebuffer[].

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

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:

Code: [Select]
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.
Quote
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.
I only provide help via the forum - please do not contact me for private consultancy.

Go Up