无法使用enc28j60从{arduino发送GET请求

时间:2017-10-16 17:01:27

标签: arduino

我正在构建一个简单的应用程序,我想使用enc28j60从我的Arduino发送一个GET请求到我的在线托管中的php脚本。除了没有看到任何东西到我的数据库外,一切似乎都很完美。

引脚:

ENC SO - > Arduino pin 12

ENC SI - > Arduino pin 11

ENC SCK - > Arduino pin 13

ENC CS - > Arduino pin 10

ENC VCC - > Arduino 3V3引脚

ENC GND - > Arduino Gnd pin

这里是arduino代码:

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

  }
}

这是基本的PHP脚本:

<?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);

}

 ?>

这是我从串行监视器中获取的内容:

[Web客户端]

IP:192.168.10.10

GW:xxx

DNS:xxx

SRV:xxx

然而,当我检查基地时,我看不到任何插入的东西。但如果我在浏览器中手动执行此操作,例如: billyval-com.stackstaging.com/index.php?variable=x我可以在数据库中看到插入的值(x)。 我改变了发布但我看不到任何改变。

我无法弄清楚问题在哪里,如果有人可以帮助我,我会在这里发布。

非常感谢。

1 个答案:

答案 0 :(得分:0)

您的 GET 请求不正常:

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);

它应该是 POST 请求,因为您使用 application / x-www-form-urlencoded 。 POST更适合发送数据。

POST 请求如下所示:

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

将此应用于您的代码:

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);

此外,php代码需要更改:

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

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

}

另外,read this