使用Arduino以太网库向远程套接字发送文本数据

2024-10-03 09:19:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在努力完成一些事情。问题是,我有一台装有传感器的arduino,我想把它们的读数从我的办公室记录下来,然后把数据发送到家里的电脑上。我使用Arduino以太网屏蔽将Arduino连接到internet,为了发送数据,我使用了EthernetClient。为了接收数据,我在家用电脑上使用了地址为“1111”的TCP套接字。但是当我想把数据发送到我的家庭电脑时,它说连接失败了。在

我在我的家庭机器上转发了两个端口,端口80和端口1111。当我在浏览器上使用IP地址和端口80登录到家用电脑上时,它显示的是托管在那里的HTTPD服务器。但是,当我使用Arduino连接到相同的IP地址和端口80时,连接失败。在

这是我的代码:

1。Arduino公司:

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress server(100, 65, 91, 226); // <<<<<<< My server Address
IPAddress ip(192, 168, 0, 177);

EthernetClient client;

void setup() {
Serial.begin(9600);
while (!Serial);

if (Ethernet.begin(mac) == 0) {
  Serial.println("Failed to configure Ethernet using DHCP");
  Ethernet.begin(mac, ip);
}


delay(1000);
Serial.println("connecting...");

if (client.connect(server, 1111)) {
  Serial.println("connected");
} else {
  Serial.println("connection failed");
}
}

void loop() {
    client.println("data to be sent");  
}

2。Python

^{pr2}$

我对arduino比较陌生。但是,当我尝试使用另一台网络上的另一台机器向python套接字发送数据时,它是成功的。Arduino好像连不上插座。我能做什么? 谢谢您!在


Tags: 数据端口clientservermacserial发送数据arduino