Usocket port in ESP8266

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
andermutu99
Posts: 18
Joined: Thu Sep 27, 2018 10:19 am

Usocket port in ESP8266

Post by andermutu99 » Thu Sep 27, 2018 3:37 pm

Dear Community;

I am trying to transfer data using usocket module. I want to transfer data from ESP8266 to my computer. So in my computer I am running this python script:

import socket # Import socket module

s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345 # Reserve a port for your service.
s.bind(('localhost', port)) # Bind to the port
s.listen(5) # Now wait for client connection.

while True:
c, addr = s.accept() # Establish connection with client.
print 'Got connection from', addr
l = c.recv(1024)
while (l):
print "Receiving..." +str(l)
f = open('receive.txt','a')
f.write(l + '\n')
l = c.recv(1024)
f.close()
print "Done Receiving"
c.send('Thank you for connecting')
c.close()

f.close()

In the ESP8266 I want to run a script that I have tested on another computer. But it appears that due to usocket limitations it is not compatible: This is the code:

import usocket as socket # Import socket module

s = socket.socket() # Create a socket object
port = 12345 # Reserve a port for your service.

s.connect(('localhost', port))
s.send("Hello server, I am sending data!")
print 'Data Sent'

The problem is that in the line s.connect(('localhost', port)) I get this error:

>>> s.connect(('localhost', port))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid arguments

It appears that I can not specify the port. So please how can I do this?

Thanks in advance;

Ander.

andermutu99
Posts: 18
Joined: Thu Sep 27, 2018 10:19 am

Re: Usocket port in ESP8266

Post by andermutu99 » Thu Sep 27, 2018 4:25 pm

I have found a solution, by code was WRONG completely.

Server:

#!/usr/bin/env python3

from socket import socket, gethostbyname, AF_INET, SOCK_DGRAM
import time
PORT_NUMBER = 12345
SIZE = 1024

hostName = gethostbyname( '0.0.0.0' )

mySocket = socket( AF_INET, SOCK_DGRAM )
mySocket.bind( (hostName, PORT_NUMBER) )

print ("Test server listening on port {0}\n".format(PORT_NUMBER))

while True:
(data,addr) = mySocket.recvfrom(SIZE)
print data
time.sleep(.05)

ESP8266:

from socket import socket, AF_INET, SOCK_DGRAM

SERVER_IP = '192.168.1.60'
PORT_NUMBER = 12345
SIZE = 1024
print ("Test client sending packets to IP {0}, via port {1}\n".format(SERVER_IP, PORT_NUMBER))

mySocket = socket( AF_INET, SOCK_DGRAM )
myMessage = "Hello; I am transmiting data!"

mySocket.sendto(myMessage.encode('utf-8'),(SERVER_IP,PORT_NUMBER))

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

Re: Usocket port in ESP8266

Post by SpotlightKid » Fri Sep 28, 2018 9:26 am

Your first version wasn't completely wrong, it just used the wrong arguments to connect().See the documentation on how to do it correctly (section "Socket address format(s)":

http://docs.micropython.org/en/latest/e ... ocket.html

You second version works rather differently than the frst one. It uses UDP instead of TCP and is thus inherently less reliable due to no protection against packet loss.

andermutu99
Posts: 18
Joined: Thu Sep 27, 2018 10:19 am

Re: Usocket port in ESP8266

Post by andermutu99 » Fri Sep 28, 2018 4:58 pm

SpotlightKid wrote:
Fri Sep 28, 2018 9:26 am
Your first version wasn't completely wrong, it just used the wrong arguments to connect().See the documentation on how to do it correctly (section "Socket address format(s)":

http://docs.micropython.org/en/latest/e ... ocket.html

You second version works rather differently than the frst one. It uses UDP instead of TCP and is thus inherently less reliable due to no protection against packet loss.
Thank you for your answer. I will try to use TCP instead of UDP. However, UDP protocol works perfectly. :D :D

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

Re: Usocket port in ESP8266

Post by SpotlightKid » Fri Sep 28, 2018 5:04 pm

As long as you're on a local network, UDP usually works fine. The problem is, that the recipient can't know whether it missed any packets. So if you're sending files, you should at least send some sort of checksum along with it, so the recipient can verify the transmission.

Post Reply