从ESP8266 Arduino连接到我的服务器

Connect to my server from ESP8266 Arduino

本文关键字:我的 服务器 连接 ESP8266 Arduino      更新时间:2023-10-16

我有一个Arduino Uno和一个用C++编写的服务器。我使用以下代码成功地将ESP8266连接到我的路由器:

#include <SoftwareSerial.h>
SoftwareSerial esp8266(3, 2);
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
  }
  Serial.println("Started");
  // set the data rate for the SoftwareSerial port
  esp8266.begin(115200);
  esp8266.write("ATrn");
}
void loop() {
  if (esp8266.available()) {
    Serial.write(esp8266.read());
  }
  if (Serial.available()) {
    esp8266.write(Serial.read());
  }
}

现在,我希望ESP8266作为同一局域网中的客户端连接到我的服务器(我有服务器IP)。如何使用SoftwareSerial?还有别的办法吗?

您必须向它发送AT命令才能创建HTTP请求。这将连接到端口80 上192.168.88.35的服务器

// Connect to the server
esp8266.write("AT+CIPSTART="TCP","192.168.88.35",80rn"); //make this command: AT+CPISTART="TCP","192.168.88.35",80
//wait a little while for 'Linked'
delay(300);
//This is our HTTP GET Request change to the page and server you want to load.
String cmd = "GET /status.html HTTP/1.0rn";
cmd += "Host: 192.168.88.35rnrn";
//The ESP8266 needs to know the size of the GET request
esp8266.write("AT+CIPSEND=");
esp8266.write(cmd.length());
esp8266.write("rn");
esp8266.write(cmd);
esp8266.write("AT+CIPCLOSErn");

如果您需要更多详细信息,此链接将有所帮助:http://blog.huntgang.com/2015/01/20/arduino-esp8266-tutorial-web-server-monitor-example/