1
votes

I am building a simple application where i want to send a GET request from my Arduino using the enc28j60 to a php script located in my online hosting. Everything seems to working perfect except from not seeing anything to my database.

pins:

ENC SO -> Arduino pin 12

ENC SI -> Arduino pin 11

ENC SCK -> Arduino pin 13

ENC CS -> Arduino pin 10

ENC VCC -> Arduino 3V3 pin

ENC GND -> Arduino Gnd pin

here is arduino code:

#include <EtherCard.h>



#define PATH    "index.php"
#define VARIABLE    "test"

// ethernet interface mac address, must be unique on the LAN
byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };

const char website[] PROGMEM = "billyval-com.stackstaging.com";

byte Ethernet::buffer[700];
uint32_t timer;
Stash stash;

void setup () {
  Serial.begin(57600);
  Serial.println("\n[webClient]");

  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) 
    Serial.println( "Failed to access Ethernet controller");
  if (!ether.dhcpSetup())
    Serial.println("DHCP failed");

  ether.printIp("IP:  ", ether.myip);
  ether.printIp("GW:  ", ether.gwip);  
  ether.printIp("DNS: ", ether.dnsip);  

  if (!ether.dnsLookup(website))
    Serial.println("DNS failed");

  ether.printIp("SRV: ", ether.hisip);
}

void loop () {
  ether.packetLoop(ether.packetReceive());

  if (millis() > timer) {
    timer = millis() + 10000;

    byte sd = stash.create();
    stash.print("variable=");
    stash.print(VARIABLE);
    stash.save();


            Stash::prepare(PSTR("GET http://$F/$F HTTP/1.1" "\r\n"
                    "Host: $F" "\r\n"
                    "Content-Length: $D" "\r\n"
                    "Content-Type: application/x-www-form-urlencoded; charset=UTF-8" "\r\n"
                    "\r\n"
                    "$H"),
        website, PSTR(PATH), website, stash.size(), sd);

    // send the packet - this also releases all stash buffers once done
    ether.tcpSend();

  }
}

and here is the basic php script:

<?php 

$link = mysqli_connect("xxxx", "xxx", "xxx", "xxx");
if(mysqli_connect_error()){
    die("database failed");
}

if(isset($_GET['variable'])){

    $query = "INSERT INTO variables (variable) VALUES ('". $_GET['variable'] ."') ";
    mysqli_query($link, $query);

}

 ?>

here is what i take from the serial monitor:

[webClient]

IP: 192.168.10.10

GW: xxx

DNS: xxx

SRV: xxx

however when i check the base i cant see anything inserted. But if i do it manually in a browser like: billyval-com.stackstaging.com/index.php?variable=x i can see the inserted value (x) in database. I changed get to post but i cant see any change.

I cant figure it out where is the problem and i post it here if someone can help me.

Thanks a lot.

1
GET HTTP request should have no message body.gre_gor
Can u please be more spesific? I assume that i have to delete something from Stash::prepare?billyVal

1 Answers

0
votes

Your GET request is not ok:

Stash::prepare(PSTR("GET http://$F/$F HTTP/1.1" "\r\n"
    "Host: $F" "\r\n"
    "Content-Length: $D" "\r\n"
    "Content-Type: application/x-www-form-urlencoded; charset=UTF-8" "\r\n"
    "\r\n"
    "$H"),
website, PSTR(PATH), website, stash.size(), sd);

It should be POST request, as you use application/x-www-form-urlencoded. POST are more suited for sending data.

A POST request looks like this:

POST /index.php HTTP/1.1
Host: billyval-com.stackstaging.com
Connection: close
Content-Length: 200
Content-Type: application/x-www-form-urlencoded

variable1=1&variable2=2

Applying this to your code:

Stash::prepare(PSTR("POST /$F HTTP/1.1" "\r\n"
    "Host: $F" "\r\n"
    "Content-Length: $D" "\r\n"
    "Content-Type: application/x-www-form-urlencoded" "\r\n"
    "\r\n"
    "$H"),
PSTR(PATH), website, stash.size(), sd);

Also, the php code needs changing:

if(isset($_POST['variable'])){

    $query = "INSERT INTO variables (variable) VALUES ('". $_POST['variable'] ."') ";
    mysqli_query($link, $query);

}

Also, read this.