我有问题,我执行这个简单的代码 - 发送一个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命令连接到路由器,请求工作。
你有什么想法吗?
答案 0 :(得分:-1)
尝试这些更改:
client.println("GET /latest.txt")
client.println( HTTP/1.1");
client.println("Host: www.arduino.cc");
client.println("User-Agent: ArduinoWiFi/1.1");
client.println("Connection: close");
client.println();
client.println("{\"on\":true}");