2019年8月3日 星期六

ESP8266 HTTP GET/POST





開始進入HTTP Body,  HTTP Header 結尾必定是連續2組\r\n \r\n , 接著就是準備好實際HTTP要傳送的內容,


ESP8266 寫法1:
String cmd = "GET /test.html HTTP/1.1"; 
ESP8266.println("AT+CIPSEND=4," + String(cmd.length() + 4)); 
delay(1000);
ESP8266.println(cmd);
 delay(1000); 
ESP8266.println(""); 

ESP8266 寫法2:
  sendData("AT+CWMODE=1\r\n", 1000, DEBUG); //  access point
  sendData("AT+CIFSR\r\n", 1000, DEBUG); // get ip address
  sendData("AT+CIPMUX=1\r\n", 1000, DEBUG); // configure for multiple connections
  sendData("AT+CIPSTART=4,\"TCP\",\"api.thingspeak.com\",80\r\n", 1000, DEBUG); // turn on server on port 80
  key = 23;   //random number for testing
  String web = "GET http://api.thingspeak.com/update?api_key=KTQXXXXXXXXXXXXX&field1="";
  web += key;
  web += "HTTP / 1.0";
  web += "\r\n";
  web += "\r\n";

//HTTP Body 開始
  String cipsend = "AT + CIPSEND = ";
  cipsend += 4;
  cipsend += ", ";
  cipsend += String(web.length());

ReadMore:


  1. HTTP 的訊息格式處理
  2. 可以透過wireshark 抓封包軟體, 解析出HTTP 通訊協定https://www.wireshark.org/download.html



沒有留言 :

張貼留言