零知WIFI教程-http client客户端示例

Http,超文本传输协议,用于服务器与浏览器通信,在网络中应用中非常常用,下面在零知开源平台上用零知-ESP8266演示HTTP的一个简单示例。

一、软件和硬件

硬件我们本次使用零知-ESP8266;

软件使用零知开发工具,自带示例:

二、方法步骤

(1)先在零知开发工具中打开BasicHttpClient示例,或者复制下面的代码到零知开发工具中:


							
	/**********************************************************
	*    文件: x.ino      by 零知实验室([url]www.lingzhilab.com[/url])
	*    -^^- 零知开源,让电子制作变得更简单! -^^-
	*    时间: 2019/05/28 12:22
	*    说明: 
	************************************************************/
	  
	#include <Arduino.h>
	  
	#include <ESP8266WiFi.h>
	#include <ESP8266WiFiMulti.h>
	  
	#include <ESP8266HTTPClient.h>
	  
	#include <WiFiClient.h>
	  
	ESP8266WiFiMulti WiFiMulti;
	  
	void setup() {
	  
	  Serial.begin(115200);
	  // Serial.setDebugOutput(true);
	  
	  Serial.println();
	  Serial.println();
	  Serial.println();
	  
	  for (uint8_t t = 4; t > 0; t--) {
	    Serial.printf("[SETUP] WAIT %d...\n", t);
	    Serial.flush();
	    delay(1000);
	  }
	  
	  WiFi.mode(WIFI_STA);
	  WiFiMulti.addAP("wifi-ssid", "passwd");
	  
	}
	  
	void loop() {
	  // wait for WiFi connection
	  if ((WiFiMulti.run() == WL_CONNECTED)) {
	  
	    WiFiClient client;
	  
	    HTTPClient http;
	  
	    Serial.print("[HTTP] begin...\n");
	    if (http.begin(client, "http://www.lingzhilab.com/bbs/plate/plate?fid=3")) {  // HTTP
	  
	  
	      Serial.print("[HTTP] GET...\n");
	      // start connection and send HTTP header
	      int httpCode = http.GET();
	  
	      // httpCode will be negative on error
	      if (httpCode > 0) {
	        // HTTP header has been send and Server response header has been handled
	        Serial.printf("[HTTP] GET... code: %d\n", httpCode);
	  
	        // file found at server
	        if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
	          String payload = http.getString();
	          Serial.println(payload);
	        }
	      } else {
	        Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
	      }
	  
	      http.end();
	    } else {
	      Serial.printf("[HTTP} Unable to connect\n");
	    }
	  }
	  
	  delay(10000);
	}
							
						

(2)验证上传代码到零知-esp8266开发板;

(3)打开串口调试窗口,就可以看到网址:

http://www.lingzhilab.com/bbs/plate/plate?fid=3 这个站点内容