使用ENC28J60的Arduino Uno POST数据

时间:2018-03-12 21:17:39

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

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

<th class="col-xs-1" data-class='hidden' data-field="stargazers_count">Stars</th>

.hidden{
  display:none;
  visibility:hidden;
}

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

onPostBody()

我正在使用:

 :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 "-" "-"

我的httpPost请求有什么问题?

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

完整代码:

192.168.1.15 - - [12/Mar/2018:11:26:23 -0300] "GET /SafeHome/index.php HTTP/1.0" 200 8 "-" "-"
  • 注意:服务端的所有服务都可以正常工作,可以使用POST,由PostMan GET。

Arduino GET请求代码:

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

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

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

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