使用enc28j60无法发送GET请求arduino使用enc28j60
问题描述:
我正在构建一个简单的应用程序,我想从我的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)。 我改变了发布,但我不能看到任何改变。
我无法弄清楚问题出在哪里,如果有人能帮助我,我会在这里发布。
非常感谢。
答
你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。
GET HTTP请求应该没有消息体。 –
你可以更特别吗?我认为我必须从Stash :: prepare中删除一些东西? – kakavia
https://www.w3schools.com/tags/ref_httpmethods.asp –