Arduino 与 Python 通过 UDP 通信
Communication between Arduino and Python via UDP
我正在努力搭建 Arduino 和 Python 之间的沟通桥梁。 Arduino 应该发送一个整数值到 python。为了启用 Udp 通信,我在 Arduino 上使用 Ethernet shield 2 并且必须连接到 IP“192.168.137.30”。
现在,当我尝试连接绑定此 IP 地址时,出现错误,因为“192.168.137.30”是外部 IP 地址。
下面是我的 Arduino 和 python 代码:
#include <SPI.h>
#include <SD.h>
#include <Ethernet2.h>
#include <EthernetUdp2.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
byte mac[] = {
0xA8, 0x61, 0x0A, 0xAE, 0x2A, 0x12
};
IPAddress ip(192.168.137.30);
unsigned int localPort = 8880; // local port to listen on
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
void setup()
{
Ethernet.begin(mac, ip);
Udp.begin(localPort);
Serial.begin(115200);
}
void loop()
{
char ReplyBuffer[] = "acknowledged";
Udp.beginPacket(ip, localPort);
Udp.write(ReplyBuffer);
Udp.endPacket();
}
Python :
import serial
import time
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('192.168.137.30', 8880))
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
sock.close()
我仍然无法将数据从 Arduino 发送到 Python。但是,可以将数据从 Python 发送到 Arduino。
IPAddress ip(169, 254, 114, 130);
IPAddress my_PC_ip(169, 254, 94, 133);
unsigned int localPort = 7476;
unsigned int pythonPort= 7000;
void setup()
{
Ethernet.begin(mac, ip);
Udp.begin(localPort);
Serial.begin(115200);
}
void loop()
{
Udp.beginPacket(my_PC_ip,pythonPort);
Udp.print("hello");
Udp.endPacket();
}
Python :
导入序列号
导入时间
导入套接字
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('169.254.94.133', 7000))
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
sock.close()
您没有将正确的 IP 地址放在正确的位置。这是应该在哪里:
- 在Arduino上
Ethernet.begin
,你应该使用Arduino的IP地址。
- 在Arduino上
Udp.beginPacket
,你应该使用你电脑的IP地址。
- 在
sock.bind
你的电脑上,你应该使用你电脑的IP地址。
您目前在所有三个地方使用同一个,从网络角度来看这没有意义。
我正在努力搭建 Arduino 和 Python 之间的沟通桥梁。 Arduino 应该发送一个整数值到 python。为了启用 Udp 通信,我在 Arduino 上使用 Ethernet shield 2 并且必须连接到 IP“192.168.137.30”。 现在,当我尝试连接绑定此 IP 地址时,出现错误,因为“192.168.137.30”是外部 IP 地址。
下面是我的 Arduino 和 python 代码:
#include <SPI.h>
#include <SD.h>
#include <Ethernet2.h>
#include <EthernetUdp2.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
byte mac[] = {
0xA8, 0x61, 0x0A, 0xAE, 0x2A, 0x12
};
IPAddress ip(192.168.137.30);
unsigned int localPort = 8880; // local port to listen on
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
void setup()
{
Ethernet.begin(mac, ip);
Udp.begin(localPort);
Serial.begin(115200);
}
void loop()
{
char ReplyBuffer[] = "acknowledged";
Udp.beginPacket(ip, localPort);
Udp.write(ReplyBuffer);
Udp.endPacket();
}
Python :
import serial
import time
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('192.168.137.30', 8880))
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
sock.close()
我仍然无法将数据从 Arduino 发送到 Python。但是,可以将数据从 Python 发送到 Arduino。
IPAddress ip(169, 254, 114, 130);
IPAddress my_PC_ip(169, 254, 94, 133);
unsigned int localPort = 7476;
unsigned int pythonPort= 7000;
void setup()
{
Ethernet.begin(mac, ip);
Udp.begin(localPort);
Serial.begin(115200);
}
void loop()
{
Udp.beginPacket(my_PC_ip,pythonPort);
Udp.print("hello");
Udp.endPacket();
}
Python : 导入序列号 导入时间 导入套接字
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('169.254.94.133', 7000))
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
sock.close()
您没有将正确的 IP 地址放在正确的位置。这是应该在哪里:
- 在Arduino上
Ethernet.begin
,你应该使用Arduino的IP地址。 - 在Arduino上
Udp.beginPacket
,你应该使用你电脑的IP地址。 - 在
sock.bind
你的电脑上,你应该使用你电脑的IP地址。
您目前在所有三个地方使用同一个,从网络角度来看这没有意义。