从 ESP8266 Arduino 连接到我的服务器
Connect to my server from ESP8266 Arduino
我有一个 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("AT\r\n");
}
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\",80\r\n"); //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.0\r\n";
cmd += "Host: 192.168.88.35\r\n\r\n";
//The ESP8266 needs to know the size of the GET request
esp8266.write("AT+CIPSEND=");
esp8266.write(cmd.length());
esp8266.write("\r\n");
esp8266.write(cmd);
esp8266.write("AT+CIPCLOSE\r\n");
如果您需要更多详细信息,link 应该会对您有所帮助:
http://blog.huntgang.com/2015/01/20/arduino-esp8266-tutorial-web-server-monitor-example/
我有一个 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("AT\r\n");
}
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\",80\r\n"); //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.0\r\n";
cmd += "Host: 192.168.88.35\r\n\r\n";
//The ESP8266 needs to know the size of the GET request
esp8266.write("AT+CIPSEND=");
esp8266.write(cmd.length());
esp8266.write("\r\n");
esp8266.write(cmd);
esp8266.write("AT+CIPCLOSE\r\n");
如果您需要更多详细信息,link 应该会对您有所帮助: http://blog.huntgang.com/2015/01/20/arduino-esp8266-tutorial-web-server-monitor-example/