如何通过Internet使用python读取arduino数据输出?

时间:2017-09-01 01:47:37

标签: python python-2.7 arduino arduino-uno ethernet

我有使用uno和以太网盾的arduino项目。我不想在python中打印arduino数据输出。我已经使用pySerial python尝试了这个案例并且成功了。 那么现在,我不想尝试我的python可以通过互联网读取arduino数据输出,而不是串行python。

拓扑结构: Arduino与eth。 shield ---> switch< --- laptop

*注意:交换机与我的路由器有互联网连接。

你能帮助我吗,伙计们?

2 个答案:

答案 0 :(得分:0)

如果您对尝试套接字感到满意并希望在非常基本的级别上操作,则可以设置Python脚本以使用UDP发送和接收数据。 Here是Arduino提供的用于发送UDP的快速示例,here是用于在Python端接收UDP消息的示例。一旦你完成了这个设置,你可以发送和接收你想要的任何东西!

答案 1 :(得分:0)

你在这里使用的IP应该是" localhost"因为只有arduino需要计算机的地址,而不是相反。您的Python脚本只是在监听任何人连接。试试这个:

import socket

UDP_IP = "localhost" # this computer
UDP_PORT = 3939 # the port that the arduino should connect to

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((UDP_IP, UDP_PORT))

while True:
    data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
    print "received message:", data