乐鑫技术交流
直播中

李娜

8年用户 1723经验值
私信 关注
[问答]

无法使WiFiClient示例正常工作怎么解决?

这是我的代码版本。它不喜欢访问示例中的站点,Firefox 也不喜欢,所以我编写了一个简单的 php 脚本。它在 wget、curl 和浏览器上工作得很好,但在 esp8266 上不行。我有三个 ESP8266-01 部件,但没有一个工作,所以我认为排除了硬件问题。  
这是输出:
代码:全选Connecting to h*****
........
WiFi connected
IP address:
192.168.1.102
connecting to 192.168.1.100/php1.php:80
connection failed
connecting to 192.168.1.100/php1.php:80
connection failed
connecting to 192.168.1.100/php1.php:80
connection failed


代码:全选/*
    This sketch establishes a TCP connection to a "quote of the day" service.
    It sends a "hello" message, and then prints received data.
*/

#include

#ifndef STASSID
#define STASSID "hxxxxxx"
#define STAPSK  "2xxxxxxx"
#endif

const char* ssid     = STASSID;
const char* password = STAPSK;

const char* host = "192.168.1.100/php1.php";
/* rustburg.us/php1.php */
const uint16_t port = 80;

void setup() {
  Serial.begin(115200);

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  /* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
     would try to act as both a client and an access-point and could cause
     network-issues with your other WiFi-devices on your WiFi-network. */
  WiFi.mode(WIFI_STA);
  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 loop() {
  static bool wait = false;

  Serial.print("connecting to ");
  Serial.print(host);
  Serial.print(':');
  Serial.println(port);

  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  if (!client.connect(host, port)) {
    Serial.println("connection failed");
    delay(30000);
    return;
  }

  // This will send a string to the server
  Serial.println("sending data to server");
  if (client.connected()) {
    client.println("hello from ESP8266");
  }

  // wait for data to be available
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 15000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      delay(60000);
      return;
    }
  }

  // Read all the lines of the reply from server and print them to Serial
  Serial.println("receiving from remote server");
  // not testing 'client.connected()' since we do not need to send data here
  while (client.available()) {
    char ch = static_cast(client.read());
    Serial.print(ch);
  }

  // Close the connection
  Serial.println();
  Serial.println("closing connection");
  client.stop();

  if (wait) {
    delay(300000); // execute once every 5 minutes, don't flood remote service
  }
  wait = true;
}



回帖(1)

郭中

2024-4-1 18:12:51
可能是你的代码有些问题。请参考下面的代码示例:

#include

const char* ssid = "your_ssid";
const char* password = "your_password";

void setup() {
  Serial.begin(115200);
  delay(10);

  // Connect to 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 loop() {
  delay(5000);

  WiFiClient client;

  if (!client.connect("192.168.1.100", 80)) {
    Serial.println("Connection failed");
    return;
  }

  // We now create a URI for the request
  String url = "/php1.php";
  Serial.print("Requesting URL: ");
  Serial.println(url);

  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1rn" +
               "Host: 192.168.1.100rn" +
               "Connection: closernrn");

  // Read all the lines of the reply from server and print them to Serial
  while (client.available()) {
    String line = client.readStringUntil('r');
    Serial.print(line);
  }

  Serial.println();
  Serial.println("closing connection");
}

请注意,此示例使用固定的 IP 地址和端口号。如果你的 php 脚本运行在不同的端口上,这里需要做出相应的修改。如果仍然存在问题,你可以通过观察在串口监视器中的输出来找出问题所在。如果没有成功,可能是网络或路由器问题,或者你的 php 脚本还需要进行更多的调试。
举报

更多回帖

发帖
×
20
完善资料,
赚取积分