【问题标题】:Arduino Uno POST Data using ENC28J60使用 ENC28J60 的 Arduino Uno POST 数据
【发布时间】:2018-03-12 21:17:39
【问题描述】:

我尝试对我的 php 服务器进行 HTTP POST,但我在服务器日志中收到错误请求:

 :192.168.1.15 - - [12/Mar/2018:17:46:16 -0300] "POST \xa9\x020\x91\xa7\x020\x93\xa9\x02 \x93\xa7\x02.\xea2\xe0\xa2\xeb\xb2\xe0\xf9\x01B\x91\x84\x0f\x91\x1dN\x91\x8c\x93f#\x11\xf0@\x83\x01\xc0\x10\x82\x89/\x99'D\xe0N\x0fB\x13\xf0\xcf\x10\x92\xb6\x02\x10\x92\xb7\x02\x80\xe5\x80\x93\xb2\x02\b\x95\x0f\x93\x1f\x93\xcf\x93\xdf\x93p\xe0lW}O!0!\xf4\x8a\x01\x07_\x1fO\x0c\xc0\"09\xf0 HTTP/1.0" 400 471 "-" "-"

如果尝试使用 HTTP GET,则会成功并在服务器日志中获得预期的请求:

192.168.1.15 - - [12/Mar/2018:11:26:23 -0300] "GET /SafeHome/index.php HTTP/1.0" 200 8 "-" "-"

我正在使用:

Arduino Uno;
Ethernet Modulo ENC28J60;
Library: <EtherCard.h>;
Class: ether.httpPost;

我的 httpPost 请求有什么问题?

为什么日志请求有这种奇怪的格式“POST \xa9\x020\x91\xa7\x020\x93\xa9\x02...”?

以下完整代码:

#include <EtherCard.h>

#define REQUEST_RATE 5000 // milliseconds

// ethernet interface mac address
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x33 };
static byte myip[] = { 192,168,1,15 };
static byte gwip[] = { 192,168,1,1 };
static byte netmask[] = { 255,255,255,255 };//
static byte dnsip[] = { 8,8,8,8 };
static byte hisip[] = { 192,168,1,1 };//

const char website[] PROGMEM = "192.168.1.1";


byte Ethernet::buffer[300];
static long timer;



void setup () {

Serial.begin(9600);
Serial.println("\n[getStaticIP]");

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

ether.staticSetup(myip, gwip);



if(ether.dnsLookup (website, false)){
Serial.println( "dnsLookup ok");
}else{
Serial.println( "dnsLookup faild");
 ether.parseIp(ether.hisip, "192.168.1.1"); //IP do servidor manual
}


while (ether.clientWaitingGw())
ether.packetLoop(ether.packetReceive());
Serial.println("Gateway found");

ether.printIp("My IP: ", ether.myip);
ether.printIp("Netmask: ", ether.netmask);
ether.printIp("GW IP: ", ether.gwip);
ether.printIp("DNS IP: ", ether.dnsip);
ether.printIp("SRV: ", ether.hisip);

timer = - REQUEST_RATE; // start timing out right away
}

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

if (millis() > timer + REQUEST_RATE) {
timer = millis();
Serial.println("\n>>> REQ");
ether.hisport = 90;//to access  local host

char *PATH="/SafeHome/addevent";
char *contentType = "Content-Type: application/json";
String postDataStr =  "{\"INSTALACAO_ID\": \"1\", \"TPEVENTO_ID\": \"1\",\"ZONA_ID\": \"1\"}";
char *postDataChar = postDataStr.c_str();
ether.httpPost(PATH, website, contentType, postDataChar, my_result_cb);

}
}

// called when the client request is complete
static void my_result_cb (byte status, word off, word len) {
Serial.print("<<< reply ");
Serial.print(millis() - timer);
Serial.println(" ms");
Serial.println((const char*) Ethernet::buffer + off);
}
  • 注意:服务端的所有服务都可以正常工作,可以通过 PostMan 使用 POST 和 GET。

GET 请求的 Arduino 代码:

void loop () {
ether.packetLoop(ether.packetReceive());
if (millis() > timer + REQUEST_RATE) {
timer = millis();
Serial.println("\n>>> REQ");
ether.hisport = 90;//to access  local host
ether.browseUrl(PSTR("/SafeHome/index.php"),"",website, my_result_cb);
}
}

为 GET 请求打印的此服务器日志:

192.168.1.15 - - [12/Mar/2018:11:20:55 -0300] "GET /SafeHome/index.php HTTP/1.0" 200 8 "-" "-"

【问题讨论】:

  • 那个 stranger format 是你的字符串的正确十六进制格式,因为我无法测试为什么现在在 Arduino 上会发生这种情况,你能发布工作代码吗GET 请求,以便我们查看差异?
  • 嗨,GrowingBrick,我用 GET 请求的代码和服务器日志编辑了我的帖子。

标签: c++ json http post arduino-uno


【解决方案1】:

这可能是如何处理字符串的问题,实际上在 GET 请求中,这很好,您将字符串传递给PSTR,而不是在 POST 请求中它是一个指针char *PATH。 ContentType 和 POST 正文消息也是如此。处理 char* 的问题可能出在您正在使用的库中。尝试传递使用PSTR 声明的字符串并告诉我

【讨论】:

  • 请告诉我是否理解您的建议: 1. 将 char 指针更改为字符串; 2. 在我的 httpPost 方法中包含 PSTR。 const char website[] PROGMEM = "192.168.1.1";字符串 PATH="/SafeHome/addevent"; String contentType = "内容类型:应用程序/json";字符串 postDataStr = "{\"INSTALACAO_ID\": \"1\", \"TPEVENTO_ID\": \"1\",\"ZONA_ID\": \"1\"}"; ether.httpPost(PSTR(PATH), 网站, contentType, postDataChar, my_result_cb); }
  • 我不认为 PSTR 返回一个 String 而是这与 PROGMEM 有关,因为您的网站变量适用于两者案例
【解决方案2】:

我使用以下代码解决了我的问题:

#include <EtherCard.h>
#define REQUEST_RATE 5000 // milliseconds

// ethernet interface mac address
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x33 };
static byte myip[] = { 192,168,1,15 };
static byte gwip[] = { 192,168,1,1 };
static byte netmask[] = { 255,255,255,255 };//
static byte dnsip[] = { 8,8,8,8 };
static byte hisip[] = { 192,168,1,1 };//

const char website[] PROGMEM = "192.168.1.1";


byte Ethernet::buffer[500];
static long timer;

void setup () {
Serial.begin(9600);
Serial.println("\n[getStaticIP]");

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

ether.staticSetup(myip, gwip);

if(ether.dnsLookup (website, false)){
Serial.println( "dnsLookup ok");
}else{
Serial.println( "dnsLookup faild");
 ether.parseIp(ether.hisip, "192.168.1.1"); //IP do servidor manual
}

while (ether.clientWaitingGw())
ether.packetLoop(ether.packetReceive());
Serial.println("Gateway found");

ether.printIp("My IP: ", ether.myip);
ether.printIp("Netmask: ", ether.netmask);
ether.printIp("GW IP: ", ether.gwip);
ether.printIp("DNS IP: ", ether.dnsip);
ether.printIp("SRV: ", ether.hisip);

timer = - REQUEST_RATE; // start timing out right away
}

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

if (millis() > timer + REQUEST_RATE) {
timer = millis();
Serial.println("\n>>> REQ");
ether.hisport = 90;//to access  local host

/*char *PATH="/SafeHome/addevent";
char *contentType = "Content-Type: application/json";
String postDataStr =  "";
char *postDataChar = postDataStr.c_str();*/

ether.httpPost(PSTR("/SafeHome/addcommand"), website,PSTR("Content-Type: application/json"), PSTR(""), my_result_cb);


}
}

// called when the client request is complete
static void my_result_cb (byte status, word off, word len) {

// JSON Body of my post request
Serial.print("<<<< {\"EXECUTADO\": \"0\", \"INSTALACAO_ID\": \"1\",\"TPCOMANDO_ID\": \"1\"}");
//Serial.print(millis() - timer);
Serial.println(" ms");
Serial.println((const char*) Ethernet::buffer + off);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-20
    • 1970-01-01
    • 2012-07-03
    • 1970-01-01
    • 1970-01-01
    • 2017-12-13
    • 1970-01-01
    相关资源
    最近更新 更多