我刚刚在 Ebay 上购买了新的 ENC28J60 以太网 LAN 网络模块,我想用这个模块将 POST 数据发送到指定的网址,例如。www.mydomain.com/example.php
我向谷歌搜索了一些示例,但我所看到的只是 arduino shield 的示例,而不是我拥有的模块。有了这个模块,我正在使用以下库:
"etherShield.h"
"ETHER_28J60.h"
我想将一/两个指定的 POSTS(变量)发送到 php 公式,但我不知道该怎么做。
首先你需要安装以下库: https ://github.com/jcw/ethercard
使用 6 个引脚将您的模块连接到 arduino:
然后使用以下代码:
#include <EtherCard.h>
// your variable
#define PATH "example.php"
#define VARIABLE "test"
// ethernet interface mac address, must be unique on the LAN
byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
char website[] PROGMEM = "www.mydomain.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.print("&action=Submit");
stash.save();
// generate the header with payload - note that the stash size is used,
// and that a "stash descriptor" is passed in as argument using "$H"
Stash::prepare(PSTR("POST http://$F/$F.csv HTTP/1.0" "\r\n"
"Host: $F" "\r\n"
"Content-Length: $D" "\r\n"
"Content-Type: application/x-www-form-urlencoded" "\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();
}
}
Arduino v1.6.12
#include <EtherCard.h>
// your variable
#define PATH "example.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 = "www.google.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.print("&action=Submit");
stash.save();
// generate the header with payload - note that the stash size is used,
// and that a "stash descriptor" is passed in as argument using "$H"
Stash::prepare(PSTR("POST http://$F/$F.csv HTTP/1.0" "\r\n"
"Host: $F" "\r\n"
"Content-Length: $D" "\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();
}
}
如果有人要调用 API,您需要在请求中删除 .csv,如下所示:
byte sd = stash.create();
stash.print("variable="); //change this to your post variable
stash.print(VARIABLE);
stash.print("&action=Submit");
stash.save();
// generate the header with payload - note that the stash size is used,
// and that a "stash descriptor" is passed in as argument using "$H"
//remove the .csv
Stash::prepare(PSTR("POST http://$F/$F HTTP/1.0" "\r\n"
"Host: $F" "\r\n"
"Content-Length: $D" "\r\n"
"Content-Type: application/x-www-form-urlencoded" "\r\n"
"\r\n"
"$H"),
website, PSTR(PATH), website, stash.size(), sd);
附加信息,如果您使用本地服务器进行测试,您可以查看 Wampp 或 Xampp Server 的访问日志以了解您的请求是否已到达服务器的天气。
并在这一行中使用 const :
char const website[] PROGMEM = "www.mydomain.com";
您可能想尝试这个与现有以太网库完全兼容的库:
https://github.com/ntruchsess/arduino_uip
Arduino-IDE 以太网的示例通过在 [arduino-dir]/libraries/UIPEthernet 中安装此库来工作,打开标准以太网示例(例如:examples->Ethernet->WebServer)并将包含“Ethenet.h”替换为“ UIPEthernet.h”。
关于 netheads 的回答,我无法在服务器端接收 POST 数据。它正在接收标题信息,我只是无法使用 $_POST 或任何其他方式访问帖子数据。我发现指定内容类型解决了这个问题。只需将 HTTP 标头部分更改如下:
Stash::prepare(PSTR("POST http://$F/$F.csv HTTP/1.0" "\r\n"
"Host: $F" "\r\n"
"Content-Length: $D" "\r\n"
"Content-Type: application/x-www-form-urlencoded" "\r\n"
"\r\n"
"$H"),
website, PSTR(PATH), website, stash.size(), sd);