我正在尝试使用ESP8266设备控制一些继电器到目前为止我已经成功创建了一个本地网络(Android App <==> Rooter <==> ESP8622 <==> Relay)。
但是,如果我想从外部连接到 ESP8266,我需要在 rooter 中配置端口转发,这不是问题并且可以完美运行,但是一旦 rooter 重新启动,它将获得不同的公共 IP 地址。
我知道如何使用 ESP8266 收集公共 IP:
?f=6&t=12782 在我的设置中,如果 rooter 重新启动,网络中的所有 ESP 也将重新启动并保持重新启动直到 rooter 再次可用。
稍后由我的android APP从外部使用。
我知道即使 rooter 启动了互联网也可能还不可用,我可以稍后处理。
2024-5-20 18:19:30
您可以使用ESP8266连接到一个公共IP地址查询API(例如ipinfo.io),然后从响应中提取公共IP地址并将其发送到外部服务器。以下是一个可能的示例代码:
#include
#include
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
const char* server = "ipinfo.io";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi!");
}
void loop() {
if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi connection lost, attempting to reconnect...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi!");
}
WiFiClient client;
if (client.connect(server, 80)) {
Serial.println("Connected to IP query API!");
client.println("GET /ip HTTP/1.1");
client.println("Host: ipinfo.io");
client.println("Connection: close");
client.println();
String response;
while (client.connected()) {
if (client.available()) {
char c = client.read();
response += c;
}
}
Serial.println(response);
String publicIP = response.substring(response.indexOf(": "") + 3, response.indexOf("","));
Serial.println("Public IP: " + publicIP);
// Send public IP to external server (To be implemented)
} else {
Serial.println("Unable to connect to IP query API!");
}
delay(60000); // Query API every minute
}
请注意,您需要将此代码中的“your_SSID”和“your_PASSWORD”替换为您的WiFi网络的凭据,并将发送代码添加到收集到的公共IP地址传递到您的外部服务器。
您可以使用ESP8266连接到一个公共IP地址查询API(例如ipinfo.io),然后从响应中提取公共IP地址并将其发送到外部服务器。以下是一个可能的示例代码:
#include
#include
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
const char* server = "ipinfo.io";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi!");
}
void loop() {
if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi connection lost, attempting to reconnect...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi!");
}
WiFiClient client;
if (client.connect(server, 80)) {
Serial.println("Connected to IP query API!");
client.println("GET /ip HTTP/1.1");
client.println("Host: ipinfo.io");
client.println("Connection: close");
client.println();
String response;
while (client.connected()) {
if (client.available()) {
char c = client.read();
response += c;
}
}
Serial.println(response);
String publicIP = response.substring(response.indexOf(": "") + 3, response.indexOf("","));
Serial.println("Public IP: " + publicIP);
// Send public IP to external server (To be implemented)
} else {
Serial.println("Unable to connect to IP query API!");
}
delay(60000); // Query API every minute
}
请注意,您需要将此代码中的“your_SSID”和“your_PASSWORD”替换为您的WiFi网络的凭据,并将发送代码添加到收集到的公共IP地址传递到您的外部服务器。
举报