Pages: [1] 2   Go Down
Author Topic: Arduino Ethernet send to php  (Read 1243 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46107
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 115
Posts: 5380
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

My php script is getalarm.php. In my code above when i change the line
Code:
GET /search?q=arduino HTTP/1.1
with this
Code:
GET http://47.226.195.1/controlpanel/getalarm.php?alarmstate=");
    client.print(alarmstate);
    client.println(" HTTP/1.1");
it doesn't work.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46107
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

NE PA
Offline Offline
Full Member
***
Karma: 5
Posts: 156
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

My php script is getalarm.php. In my code above when i change the line
Code:
GET /search?q=arduino HTTP/1.1
with this
Code:
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:
GET /controlpanel/getalarm.php?alarmstate=");
    client.print(alarmstate);
    client.println(" HTTP/1.1");
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46107
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11173
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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:
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.
« Last Edit: February 05, 2013, 05:51:17 pm by PeterH » Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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(); 
}

« Last Edit: February 06, 2013, 11:35:07 am by thanasisloi7 » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46107
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How can i fix that?

I try also this code:
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 "-" "-"
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46107
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Get rid of alarmstatebuffer[].

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

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11173
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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

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

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1] 2   Go Up
Jump to: