完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
这是我的代码版本。它不喜欢访问示例中的站点,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个回答
|
|
可能是你的代码有些问题。请参考下面的代码示例:
#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 脚本还需要进行更多的调试。 |
|
|
|
只有小组成员才能发言,加入小组>>
528浏览 6评论
438浏览 5评论
有没有办法在不使用混杂模式的情况下实现Wifi驱动程序接收缓冲区访问中断呢?
425浏览 5评论
422浏览 4评论
398浏览 4评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-7 23:00 , Processed in 0.915613 second(s), Total 77, Slave 61 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号