我有一个问题,我执行这个简单的代码 - 发送一个 http 请求:
include <ESP8266WiFi.h>
//Router 1 - Livebox
const char* ssid = "Livebox-9e38";
const char* password = "xxxxxxxxxxx";
//Router 2 - Android phone
//const char* ssid = "DARKSIDE";
//const char* password = "xxxxxxxxx";
// server address:
char server[] = "www.arduino.cc";
//IPAddress server(64,131,82,241);
WiFiClient client;
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
void setup() {
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void httpRequest() {
// close any connection before send a new request.
// This will free the socket on the WiFi shield
client.stop();
// if there's a successful connection:
if (client.connect(server, 80)) {
Serial.println("connecting...");
// send the HTTP PUT request:
client.println("GET /latest.txt HTTP/1.1");
client.println("Host: www.arduino.cc");
client.println("User-Agent: ArduinoWiFi/1.1");
client.println("Connection: close");
client.println();
// note the time that the connection was made:
lastConnectionTime = millis();
} else {
// if you couldn't make a connection:
Serial.println("connection failed");
}
}
void loop() {
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
while (client.available()) {
char c = client.read();
Serial.write(c);
}
// if ten seconds have passed since your last connection,
// then connect again and send data:
if (millis() - lastConnectionTime > postingInterval) {
httpRequest();
}
}
当我在路由器 2(Android 手机上的简单热点路由器)上时,代码有效。
当我在路由器 1(我的互联网提供商提供的 livebox)上时,我有一个 IP 地址,我可以在路由器中看到 ESP(它是一个 Web 界面),但我无法从请求中获得任何结果(并且我无法 ping 模块)。路由器不路由任何东西。
当然,路由器可以访问互联网,并使用 Curl 命令从计算机连接到 wifi 中的路由器,请求工作。
你有什么想法吗?