完整如下
#include <ESP8266.h>
#define SSID "111"
#define PASSWORD "yds111000"
#define HOST_NAME "192.168.137.1"
#define HOST_PORT 80
ESP8266 wifi(&Serial1);
int LED = 6;
const char *hello = "GET /get HTTP/1.0\r\nHost: 192.168.137.1:80\r\nConnection: close\r\n\r\n";// HTTP 璇锋眰鏍煎紡锛岃缁嗚鏌ョ湅HTTP鍗忚
void setup(void)
{
// put your setup code here, to run once:
pinMode(LED,OUTPUT);
WifiInit(Serial1, 115200);
Serial.begin(115200);
Serial.print(F("FW Version:"));
Serial.println(wifi.getVersion().c_str());
if (wifi.setOprToStationSoftAP()) {
Serial.print(F("to station + softap ok\r\n"));
} else {
Serial.print(F("to station + softap err\r\n"));
}
if (wifi.joinAP(SSID, PASSWORD)) {
Serial.print(F("Join AP success\r\n"));
Serial.print(F("IP:"));
Serial.println( wifi.getLocalIP().c_str());
} else {
Serial.print(F("Join AP failure\r\n"));
}
if (wifi.disableMUX()) {
Serial.print(F("single ok\r\n"));
} else {
Serial.print(F("single err\r\n"));
}
}
void loop(void)
{
// put your main code here, to run repeatedly:
if (wifi.createTCP(HOST_NAME, HOST_PORT)) {
Serial.print(F("create tcp ok\r\n"));
} else {
Serial.print(F("create tcp err\r\n"));
}
wifi.send((const uint8_t *)hello, strlen(hello) * sizeof(char));
Serial.print("发送:");
Serial.print(hello);
uint8_t buffer[512] = {0};
uint32_t len = wifi.recv(buffer, sizeof(buffer), 20000);
// Serial.print(F("Received:["));
// for (uint32_t i = 0; i < len; i++) {
// Serial.print((char)buffer[i]);
// }
char response = (char)buffer[len-3];
Serial.print("服务器返回:");
Serial.print(response);
if(response == '0'){
digitalWrite(LED,LOW);
} else {
digitalWrite(LED,HIGH);
}
delay(1000);
}