The Pico In MicroPython: Sockets
Written by Mike James & Harry Fairhead   
Monday, 05 August 2024
Article Index
The Pico In MicroPython: Sockets
Socket Address
A Socket Web Client
SSL Socket Based HTTPS Client

Sockets sound difficult but they are fairly simple.  This is an extract from our book all about the Raspberry Pi Pico in MicroPython.

Programming the Raspberry Pi Pico/W In MicroPython Second Edition

By Harry Fairhead & Mike James

picopython2e360

Buy from Amazon.

Contents

  • Preface
  • Chapter 1 The Raspberry Pi Pico – Before We Begin
  • Chapter 2 Getting Started
  • Chapter 3 Getting Started With The GPIO
  • Chapter 4 Simple Output
  • Chapter 5 Some Electronics
  • Chapter 6 Simple Input
             Extract: Simple Input 
  • Chapter 7 Advanced Input – Events and Interrupts
  • Chapter 8 Pulse Width Modulation
             Extract: PWM 
  • Chapter 9 Controlling Motors And Servos
             Extract: DC Motors
  • Chapter 10 Getting Started With The SPI Bus
  • Chapter 11 A-To-D and The SPI Bus 
  • Chapter 12 Using The I2C Bus
  • Chapter 13 Using The PIO   
  • Chapter 14 The DHT22 Sensor Implementing A Custom Protocol
             Extract: A PIO Driver For The DHT22  
  • Chapter 15 The 1‑Wire Bus And The DS1820
  • Chapter 16 The Serial Port
  • Chapter 17 Using The Pico W - WiFi
             Extract: HTTP Client 
             Extract: Sockets***NEW!
  • Chapter 18 Asyncio And Servers
  • Chapter 19 Direct To The Hardware
             Extract: Direct To The Hardware

Also of interest:

Raspberry Pico File System

<ASIN:B0BR8LWYMZ>

<ASIN:B0BL1HS3QD>

Using The Pico W - WiFi

The Pico and the Pico W are very similar and all of the programs presented so far work on both models. The important difference between them is that the Pico W has additional hardware that enables it to use WiFi. From the programming point of view, however, the task is to learn how to use the new WiFi driver and the LwIP library that provides higher-level networking. This is made more difficult than need be by the inadequate documentation provided for both. This will probably improve over time.

In this extract we look at how to use sockets.

In chapter but not in this extract

  • The Pico W
  • A Practical Connect
  • WiFi Scan
  • A Simple HTTP Client
  • A Custom Server
  • The urequests Module
  • A Temperature Sensor Client

Sockets

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 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:

type

proto

SOCK_STREAM
IPPROTO_TCP
SOCK_DGRAM
IPPROTO_UDP

 

In a more general setting there could be more types of proto supported for each type.



Last Updated ( Tuesday, 06 August 2024 )