arduino - 如何通过以太网屏蔽与 Arduino 连接到 loaclhost?
问题描述
我需要将数据从 Arduino 发送到将在我的计算机上运行的本地服务器。计算机通过连接到路由器的电缆连接到 Wi-Fi、带有 Eternet Shield 的 Arduino Uno。
我从 WebClient 示例中提取了一个标准草图,并更改了我通过 ipconfig 找到的 IPv4 地址。这应该可以解决问题(正如我在这里读到的),但是端口监视器给出了“连接失败”。可能是什么问题呢?
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(192, 168, 1, 4); // numeric IP for Google (no DNS)
//char server[] = "localhost"; // name address for Google (using DNS)
IPAddress ip(192, 168, 1, 229);
EthernetClient client;
void setup() {
Serial.begin(9600);
Ethernet.init(4);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
// if you get a connection, report back via serial:
if (client.connect(server, 3000)) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /search?q=arduino HTTP/1.1");
client.println("Host: localhost");
client.println("Connection: close");
client.println();
}
else {
// kf you didn't get a connection to the server:
Serial.println("connection failed");
}
}
void loop()
{
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing forevermore:
while (true);
}
}
解决方案
应该禁用 Windows 防火墙
推荐阅读
- c# - 在除 Swagger 之外的所有服务器上从 IIS 接收 401.1 Unauthorized
- function - 在lua中,有前向声明吗?
- python - 无法在 Python 中使用“Strip”从字符串中消除字符串
- uwp - 如何在 UWP 中为不同的颜色主题设置不同的图标
- flutter - 如何在 Flutter App 的文本框右侧添加美元符号
- amazon-web-services - 使用 QoS.EXACTLY_ONCE 订阅主题时使用 awsiotsdk,遇到 AWS_ERROR_MQTT_UNEXPECTED_HANGUP
- javascript - 如何检查是否使用 Jasmine 中的特定选择器调用了 jQuery、$?
- curl - 在 WSO2 API 管理器中使用 POST 请求时出现错误 411
- wordpress - WP_Query 传递一个唯一标识符以用于稍后的操作挂钩?
- python - 代码@discord bot 并让它回复你的前缀和等等等等