esp8266 + http
使用esp8266发起http请求
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#define led_1 2
const char *SSID = "优美屋205";
const char *PASSWORD = "18111549";
const char *URL = "http://192.168.124.17:3000/users";
void init_wifi();
void esp8266Http(); // http
HTTPClient httpClient;
WiFiClient client;
void setup() {
Serial.begin(115200);
pinMode(led_1, OUTPUT);
init_wifi();
}
void loop() {
digitalWrite(led_1, LOW);
delay(500);
digitalWrite(led_1, HIGH);
delay(500);
esp8266Http();
}
void init_wifi(){
Serial.println("连接中...");
Serial.println(SSID);
WiFi.begin(SSID, PASSWORD);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.print("IP:");
Serial.println(WiFi.localIP());
}
void esp8266Http(){
httpClient.begin(client, URL);
int httpResponseCode = httpClient.GET();
if(httpResponseCode > 0){
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
String payload = httpClient.getString();
Serial.println(payload);
}else{
Serial.print("Error code:");
Serial.println(httpResponseCode);
}
// 结束
httpClient.end();
}