2017-10-16 115 views
1

我正在構建一個簡單的應用程序,我想從我的Arduino使用enc28j60發送GET請求到位於我的在線主機中的php腳本。除了從數據庫中看不到任何東西外,一切看起來都很完美。使用enc28j60無法發送GET請求arduino使用enc28j60

引腳:

ENC SO - > Arduino的銷12

ENC SI - > Arduino的銷11

ENC SCK - > Arduino的銷13

ENC CS - > Arduino的銷10

ENC VCC - >的Arduino 3V3銷

ENC GND - >甲rduino GND引腳

這裏是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)。 我改變了發佈,但我不能看到任何改變。

我無法弄清楚問題出在哪裏,如果有人能幫助我,我會在這裏發佈。

非常感謝。

+0

GET HTTP請求應該沒有消息體。 –

+0

你可以更特別嗎?我認爲我必須從Stash :: prepare中刪除一些東西? – kakavia

+0

https://www.w3schools.com/tags/ref_httpmethods.asp –

回答

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請求,你用應用程序/ x-WWW窗體-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

相關問題