UDP data reception through Python running on a PC

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
skywalker7861
Posts: 1
Joined: Sun Jan 15, 2017 9:27 pm

UDP data reception through Python running on a PC

Post by skywalker7861 » Mon Jan 16, 2017 2:43 am

i have an arduino ESP8266 running as server with the below details. The arduino is streaming some sensor data at 100 Hz rate.

IPAddress ipServer(192,168,4,1);
IPAddress ipClient(192,168,4,10);
IPAddress Subnet(255,255,255,0);

I am running a Python code on a PC whose IP address is 192.168.4.10 and is on the same subnet. i am able to ping 192.168.4.1 and get 0% packet loss. So the connection is working. When attempting to bind a socket, I get no response and it timesout.

Wondering if someone could help me with this?
thanks - V

The python code on the PC is here

from socket import *
import time

HOST = "192.168.4.1"
PORT = 9047

address = (HOST, PORT) #Defind who you are talking to (must match arduino IP and port)
client_socket = socket(AF_INET, SOCK_DGRAM) #Set Up the Socket
#client_socket.bind((HOST, PORT))
client_socket.settimeout(5) #only wait 1 second for a response

while(1): #Main Loop
rec_data, addr = client_socket.recv(100) #Read response from arduino
print rec_data #Print the response from Arduino
sleep(10/1000000) # sleep 10 microseconds

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: UDP data reception through Python running on a PC

Post by SpotlightKid » Thu Jan 26, 2017 1:17 pm

I think you may be a bit confused about how UDP communication works. In UDP there are no connections, just senders and receivers.

In your set up, if the arduino is "streaming" (i.e. sending) data, it is the UDP sender and it must know the IP address (and listening port) of your the receiver (your PC). It simply opens a socket and uses socket.sendto() (or the equivalent of the Arduino API) to send UDP packet to the receiver.

The PC is the receiver and must open a socket, bind to the address and port used in the sento() calls by the sender (arduino), e.g. the external IP of your PC and port 9047 in your example, and then use socket.recvfrom() to receive UDP datagrams from any client, which sends to this address and port.

In your code, HOST should not be the address of the sender (arduino), but the address of the PC. And you need to use socket.recvfrom() instead of socket.recv().

This is all assuming, the arduino code is really "streaming" data, as you say, i.e. sending it via UDP. But it really uses UDP, it makes not much sense otherwise, since you can't easily connect to a server and request the data.

Or maybe I misunderstood your setup completely. In this case, please provide the relevant arduino code, so we can see what the esp does.

Post Reply