|ESP32 In MicroPython: Client Sockets|
|Written by Harry Fairhead & Mike James|
|Tuesday, 12 September 2023|
Page 1 of 3
Sockets are the basis of communication over the internet. This extract is from Programming the ESP32 in MicroPython, part of the I Programmer Library and it shows you how to get started with sockets to implement HTTP and HTTPS clients.
Programming the ESP32in MicroPython
By Harry Fairhead & Mike James
Buy from Amazon.
If you want to go beyond a simple client you need to make use of a more general network connection method. The usual way of doing this is to make use of “sockets”. This is a widely supported internet standard and most servers support socket connections.
MicroPython supports a limited version of the full Python Sockets module. The parts of the module that are not supported are mostly those concerned with making connections using non-IP networks and hence are generally minor.
The most important thing to understand about sockets is that they are a very general way of making a two-way connection between a client and a server. A client can create a socket to transfer data between itself and a server and a server can use a socket to accept a connection from a client. The only difference between the two situations is that the server has to either poll or use interrupts to detect a new connection attempt.
Before you can start to send and receive data you have to create a socket object:
import socket s = socket.socket()
This default constructor creates a socket suitable for IPv4 connections.
If you want to specify the type of socket being created you have to use:
socket(af=AF_INET, type=SOCK_STREAM, proto=IPPROTO_TCP)
The af parameter gives the address family and it is either AF_INET for IPv4 or AF_INET6 for IPv6 – at the time of writing only IPv4 is supported. The type parameter indicates either SOCK_STREAM or SOCK_DGRAM, the stream option. The default, SOCK_STREAM, corresponds to the usual TCP/IP connection used to transfer web pages and files in general. It is a persisted and error-corrected connection whereas SOCK_DGRAM sends individual packets without error checking or confirmation that the data was received. The final parameter, proto, sets the exact type of protocol, but as the only two supported are IPPROTO_TCP and IPPROTO_UDP this is set according to the type parameter:
In a more general setting there could be more types of proto supported for each type.
After defining the type of connection, you need to specify the addresses involved in the connection. As sockets can be used to connect to a wide range of different types of network, the address format can vary a lot and not just be a simple URL or an IP address. For this reason sockets are used with their own internal address representation and you have to use the getaddrinfo method:
socket.getaddrinfo(host, port, af=0, type=0,
The first two parameters are familiar and simple. The host has to be specified using a URL or an IP address. The second is the numeric port to use with the socket. The next three parameters are the same as used in creating the socket. In general you use the same parameter values for an address intended to be used with a socket as used to create the socket. If you don’t specify a parameter than getaddrinfo will return a list of tuples for each possible address type. Each tuple has the following format:
(af, type, proto, canonname, sockaddr)
where af, type and proto are as before and canonname is the canonical name for the address type and sockaddr is the actual address, which may also be a tuple. In the case of an IP address, the sockaddr is a tuple with the format:
At the time of writing ESP32 only supports IPv4 and it only returns a list with a single tuple. The only really useful part of this tuple is the final element which is an IP tuple used by most of the socket methods. You could short circuit everything by simply creating the IP tuple and not use getaddrinfo. However, you should use getaddrinfo to allow for compatibility with the full sockets module and for future development.
ai = socket.getaddrinfo("www.example.com", 80,socket.AF_INET,socket.SOCK_DGRAM)
returns the list:
[(2, 1, 0, '', ('184.108.40.206', 80))]
and is it the final tuple:
which we actually use in calls to socket methods. To extract this final tuple we use:
addr = ai[-1]
While on the subject of addresses, there are two functions which can be used to convert an IP address in “dotted” form to a byte value and vice versa:
gives the dotted text form and:
gives the binary form of the IP address.
|Last Updated ( Tuesday, 12 September 2023 )|