Ethernet enc28j60 + php

Hi everyone, sorry my ingles.

Im creating a project that will be able to control some lights remotely. at the momente i was able to send from php server to arduino and turn on/off the lights.

Im having a problems to sent data from arduino to php, how i can read a data from arduino.

Arduino

#include <EtherCard.h>

static byte mymac[] = {0x00,0x01,0x02,0x03,0x04,0x05};
byte Ethernet::buffer[500];

unsigned long timer;

const char website[] PROGMEM = "casacampinas.hol.es";

void setup () 
{
  Serial.begin(9600);
  Serial.println("03 - Basic Web Client");

  if (!ether.begin(sizeof Ethernet::buffer, mymac, 10)){
    Serial.println( "Failed to access Ethernet controller");
  }else{
    Serial.println("Ethernet controller initialized");
  }
 
  if (!ether.dhcpSetup()){
    Serial.println("Failed to get configuration from DHCP");
  }else{
    Serial.println("DHCP configuration done");
  }
  ether.printIp("IP Address:\t", ether.myip);
  ether.printIp("Netmask:\t", ether.netmask);
  ether.printIp("Gateway:\t", ether.gwip);
  Serial.println();

  // DNS Setup
  if (!ether.dnsLookup(website))
    Serial.println("DNS failed");
    
  ether.printIp("SRV: ", ether.hisip);
}

void loop () {
  ether.packetLoop(ether.packetReceive());
  
  if ((millis()-timer)>5000) {
    timer = millis();
    Serial.println("Request sent");
    
    // Send some test data to the server:
    ether.browseUrl(PSTR("arduino2.php?apikey=asd"), "{power:252.4,temperature:15.4}", website, 0);
  }
}

I dont know how php read this value:

ether.browseUrl(PSTR("arduino2.php?apikey=asd"), "{power:252.4,temperature:15.4}", website, 0);

php code, just to test:

<?php

echo $_POST["apikey"];

echo $_GET["apikey"];
?>

I dont know how php read this value:

You don't know how it reads what value? The value for apikey?

PaulS: You don't know how it reads what value? The value for apikey?

yes, i think that apikey is the value sent from arduio to php when request is completed.

You'll need to look at the browseURL() method, to see whether it makes a GET request or a POST request.

When the php script is executed, where do you expect the see the data echoed? What you are doing in the PHP script does not send anything back to the client.

This is the PHP code that i’m using to turn on/off one led, when the arduino receive fopen command from php i need him send a message to php for validation.

I would like to receive this validation like $_post but i dont know if this is possible.
i just need help to implement this function, if is necessary changes on php or arduino code it is not a problem.

<!-- Start of the HTML -->
<html>
<head>
<title>Click to Turn on or OFF the LED in the background</title>
</head>
<body bgcolor="#FF9933">
<?php

// Check of LED2 is set.  If it is use it
if (isset($_POST["LED"])){
	$LED= $_POST["LED"];
	
	if ($LED == "ON"){
	
	ini_set("allow_url_fopen On", true);
	$h = @fopen("http://192.168.0.23/?LED2=ON", "rb");
}else if ($LED == "OFF"){
	
	ini_set("allow_url_fopen On", true);
	$h= @fopen("http://192.168.0.23/?LED2=OFF", "rb");
}
}else{
	$LED ="";
}


?>
<!-- LED FORM -->
<table>
<tr><td colspan="2"><font size="4" color="yellow">Turn on and off the LED2</font></H4></td></tr>
<tr><td> 
<form action="arduino.php" method="post">
<input type="hidden" name="LED" value="ON">
<input type="submit" name="submit" value="ON">
</form>
</td><td>
<form action="arduino.php" method="post">
<input type="hidden" name="LED" value="OFF">
<input type="submit" name="submit" value="OFF">
</form>
</td></tr>
</table>


</body>
</html>

when the arduino receive fopen command from php i need him send a message to php for validation.

The Arduino doesn't receive an fopen command. Executing the fopen command in PHP normally opens a file. It's been a while since I looked at the documentation for the fopen command, but that seems a strange way of making a GET request.

But, if it works...

I don't see how that is going to work with the Arduino, since the Arduino seems to be acting as a client, not a server.

after changes i could send tcp data to arduino and send reply to the php.

PHP code:

<?php
$fp = fsockopen("192.168.0.20", 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)
\n";
} else {
    fwrite($fp, "You message");
    while (!feof($fp)) {
        echo fgets($fp, 128);
    }
    fclose($fp);
}
?>

Arduino code:

#include <EtherCard.h>


// ethernet mac address - must be unique on your network
static byte mymac[] = { 
  0x00,0x01,0x02,0x03,0x04,0x05 };

byte Ethernet::buffer[500]; // tcp/ip send and receive buffer

// char page[] is the HTML page you are uploading ..
const char page[] PROGMEM = "done";

void setup(){
   Serial.begin(9600);
  Serial.println("BasicServer Demo");
 
  if (!ether.begin(sizeof Ethernet::buffer, mymac, 10)){
    Serial.println( "Failed to access Ethernet controller");
  }else{
    Serial.println("Ethernet controller initialized");
  }
 
  if (!ether.dhcpSetup()){
    Serial.println("Failed to get configuration from DHCP");
  }else{
    Serial.println("DHCP configuration done");
  }
  
  ether.printIp("IP Address:\t", ether.myip);
  ether.printIp("Netmask:\t", ether.netmask);
  ether.printIp("Gateway:\t", ether.gwip);
  Serial.println();
}
void loop(){
  // wait for an incoming TCP packet, but ignore its contents
  if (ether.packetLoop(ether.packetReceive())) {
    word len = ether.packetReceive();
    word pos = ether.packetLoop(len);
    Serial.print("Data>> ");
    Serial.println(len);
    //Send a reply.
    memcpy_P(ether.tcpOffset(), page, sizeof page);
    ether.httpServerReply(sizeof page - 1);
  }
}

Now i dont able to read the tcp data in arduino and print it in serial, how i can read it?
i think it is in this code ether.packetReceive() but when print it is blank.

Finally.

I managed to create.

see the codes.
Arduino:

#include <EtherCard.h>

const char pageA[] PROGMEM =
"HTTP/1.0 200 OK\r\n"
"Content-Type: text/html\r\n"
"\r\n"
"<html>"
  "<head><title>"
    "multipackets Test"
  "</title></head>"
  "<body>"
    "<a href='/'>Start here</a>
"
    "<h3>packet 1</h3>"
    "<p><em>"
      "the first packet send "
    "</em></p>"
;
const char pageB[] PROGMEM =
    "<h3>packet 2</h3>"
    "<p><em>"
      "if you read this it mean it works"
    "</em></p>"
;
const char pageC[] PROGMEM =
     "<h3>packet 3</h3>"
    "<p><em>"
      "if you read this it mean it works"
    "</em></p>"
;
const char pageD[] PROGMEM =
      "<h3>packet 4</h3>"
    "<p><em>"
      "if you read this it mean it works"
    "</em></p>"
;
const char pageE[] PROGMEM =
      "<h3>packet 5</h3>"
    "<p><em>"
      "this is the last packet"
    "</em></p>"
;


// ethernet mac address - must be unique on your network
static byte mymac[] = { 
  0x00,0x01,0x02,0x03,0x04,0x05 };

byte Ethernet::buffer[500]; // tcp/ip send and receive buffer

// char page[] is the HTML page you are uploading ..
const char page[] PROGMEM = "done";

void setup(){
   Serial.begin(9600);
  Serial.println("BasicServer Demo");
 
  if (!ether.begin(sizeof Ethernet::buffer, mymac, 10)){
    Serial.println( "Failed to access Ethernet controller");
  }else{
    Serial.println("Ethernet controller initialized");
  }
 
  if (!ether.dhcpSetup()){
    Serial.println("Failed to get configuration from DHCP");
  }else{
    Serial.println("DHCP configuration done");
  }
  
  ether.printIp("IP Address:\t", ether.myip);
  ether.printIp("Netmask:\t", ether.netmask);
  ether.printIp("Gateway:\t", ether.gwip);
  Serial.println();
}
void loop(){
  word pos = ether.packetLoop(ether.packetReceive());
    // check if valid tcp data is received
    if (pos) {
        char* data = (char *) Ethernet::buffer + pos;
        if (strncmp("GET / ", data, 6) == 0) {
            ether.httpServerReplyAck(); // send ack to the request
            memcpy_P(ether.tcpOffset(), pageA, sizeof pageA); // send first packet and not send the terminate flag
            ether.httpServerReply_with_flags(sizeof pageA - 1,TCP_FLAGS_ACK_V);
            memcpy_P(ether.tcpOffset(), pageB, sizeof pageB); // send second packet and not send the terminate flag
            ether.httpServerReply_with_flags(sizeof pageB - 1,TCP_FLAGS_ACK_V);
            memcpy_P(ether.tcpOffset(), pageC, sizeof pageC); // send thirdt packet and not send the terminate flag
            ether.httpServerReply_with_flags(sizeof pageC - 1,TCP_FLAGS_ACK_V);
            memcpy_P(ether.tcpOffset(), pageD, sizeof pageD); // send fourth packet and not send the terminate flag
            ether.httpServerReply_with_flags(sizeof pageD - 1,TCP_FLAGS_ACK_V);
            memcpy_P(ether.tcpOffset(), pageE, sizeof pageE); // send fiveth packet and send the terminate flag
            ether.httpServerReply_with_flags(sizeof pageE - 1,TCP_FLAGS_ACK_V|TCP_FLAGS_FIN_V); }
        else
        {
            Serial.println(data);
            ether.httpServerReplyAck(); // send ack to the request
            memcpy_P(ether.tcpOffset(), pageA, sizeof pageA);//only the first part will sended 
            ether.httpServerReply_with_flags(sizeof pageA - 1,TCP_FLAGS_ACK_V|TCP_FLAGS_FIN_V);
        }
  }
  
}

PHP(just to send and print the reply):

<?php
$mensagem = "Glauco de Oliveira Ribeiro";
$fp = fsockopen("192.168.0.20", 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)
\n";
} else {
    fwrite($fp, $mensagem);
    while (!feof($fp)) {
        echo fgets($fp, 128);
    }
    fclose($fp);
}
?>