|
POST请求怎么改成GET请求?新手小白看不懂官档!
#include <SoftwareSerial.h>
SoftwareSerial mySerial(13, 12); // RX, TX 通过软串口连接esp8266
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
mySerial.begin(115200);
mySerial.println("AT+RST"); // 初始化重启一次esp8266
delay(1500);
echo();
mySerial.println("AT");
echo();
delay(500);
mySerial.println("AT+CWMODE=3"); // 设置Wi-Fi模式
echo();
mySerial.println("AT+CWJAP=\"WiFiSSID\",\"password\""); // 连接Wi-Fi
echo();
delay(10000);
}
void loop() {
if (mySerial.available()) {
Serial.write(mySerial.read());
}
if (Serial.available()) {
mySerial.write(Serial.read());
}
post();
}
void echo(){
delay(50);
while (mySerial.available()) {
Serial.write(mySerial.read());
}
}
void post(){
String temp = "POST data";
mySerial.println("AT+CIPMODE=1");
echo();
mySerial.println("AT+CIPSTART=\"TCP\",\"192.168.10.204\",80"); // 连接服务器的80端口
delay(1000);
echo();
mySerial.println("AT+CIPSEND"); // 进入TCP透传模式,接下来发送的所有消息都会发送给服务器
echo();
mySerial.print("POST /update.php?params=sth"); // 开始发送post请求
mySerial.print(" HTTP/1.1\r\nHost: 192.168.10.204\r\nUser-Agent: arduino-ethernet\r\nConnection:close\r\nContent-Length:"); // post请求的报文格式
mySerial.print(temp.length()); // 需要计算post请求的数据长度
mySerial.print("\r\n\r\n");
mySerial.println(temp); // 结束post请求
delay(3000);
echo();
mySerial.print("+++"); // 退出tcp透传模式,用println会出错
delay(2000);
}
|
|