Overview
In this post, we will learn how to use ESP8266 WiFi Module with RP2040 Raspberry Pi Pico Board using MicroPython Code and create our own Web Server.
Raspberry Pi Pico RP2040 board features a dual-core Arm Cortex-M0+ processor with 264KB internal RAM & up to 16MB of off-chip Flash. The RP2040 is much faster compared to Arduino but it has a limitation of wireless network connectivity. This is the reason why Raspberry Pi Pico alone can’t be used for wireless & IoT-related applications. Hence a low-cost ESP8266 WiFi module could be a good choice to add wireless connectivity to Raspberry Pi Pico.
Here in this tutorial, we will demonstrate the Raspberry Pi Pico simple web server functionality using the ESP8266 WiFi module. The set of AT commands is preloaded into the program memory of ESP8266 and does not require additional programming. We will be sending these specific commands through the serial port (UART) of Raspberry Pi Pico. The coding is done in MicroPython & the device is programmed using the Thonny IDE. The Web Server displays the on-chip temperature sensor reading on a web page running on the web browser.
Bill of Materials
S.N. | Components Name | Quantity | Purchase Links |
---|---|---|---|
1 | Raspberry Pi Pico Board | 1 | Amazon | AliExpress |
2 | ESP8266-01 WiFi Module | 1 | Amazon | AliExpress |
3 | Connecting Wires | 15 | Amazon | AliExpress |
4 | Breadboard | 1 | Amazon | AliExpress |
Circuit Diagram & Connections
Now, let us see how we can connect the ESP8266 WiFi Module to RP2040 Raspberry Pi Pico Board. Here is a simple connection diagram.
The Raspberry Pi Pico has two inbuilt UART. In this design, we will use UART-0. Similarly, the default baud rate of the ESP8266 is 115200. We need to configure the Raspberry Pi Pico with the same baud rate in order to maintain synchronization with the ESP8266.
Connect the VCC and EN Pin of ESP8266 to Pico 3.3V pin & connect GND to GND. Similarly, connect the Tx and RX of ESP8266 to Pico UART-O Pin, i.e Rx & Tx.
Source Code/Program
Here is the complete MicroPython Code for RP2040 Raspberry Pi Pico Web Server with ESP8266.There is no need to modify the code as we are not connecting to any WiFi Network, rather we are creating the Web Server through access point created by ESP module.
We will read the temperature data from the internal temperature sensor and send it over a webpage which can be retrieved from any web browser.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
#Code for Simple Rasberry Pi PICO Web server #Using ESP8266 wifi module from machine import UART import machine import _thread import time uart = UART(0,115200) print('UART Serial') print('>', end='') def uartSerialRxMonitor(command): recv=bytes() while uart.any()>0: recv+=uart.read(1) res=recv.decode('utf-8') erase_len=len(command)+5 res = res[erase_len:] return res #configure as SoftAP+station mode send='AT+CWMODE=3' uart.write(send+'\r\n') time.sleep(1) #Set SoftAP name send='AT+CWSAP="pos_softap","",11,0,3' uart.write(send+'\r\n') time.sleep(1) res=uartSerialRxMonitor(send) print(res) #enable multi connection mode send='AT+CIPMUX=1' uart.write(send+'\r\n') time.sleep(1) res=uartSerialRxMonitor(send) print("Configured as Dual mode ->" + res) # Enable the TCP server with port 80, send='AT+CIPSERVER=1,80' uart.write(send+'\r\n') time.sleep(2) res=uartSerialRxMonitor(send) print("Server configured successfully-> "+res) #temperature reading sensor_temp = machine.ADC(4) conversion_factor = 3.3 / (65535) #Here the code runs indefinitely while True: #temperature reading reading_temp = sensor_temp.read_u16() * conversion_factor temperature = 27 - (reading_temp - 0.706)/0.001721 #Place basic code for HTML page display val='<head><title>Pi Pico Server</title></head><body><p>Temperature: '+str(int(temperature))+' deg'+'</p></body>' print(val) length=str(len(val)) send='AT+CIPSEND=1,'+length uart.write(send+'\r\n') time.sleep(2) res=uartSerialRxMonitor(send) print("Data sent-> "+res) send=val uart.write(send+'\r\n') time.sleep(10) |
Code Explanation
First we need to import required libraries.
1 2 3 4 |
from machine import UART import machine import _thread import time |
Then we configure UART-0 of Raspberry Pi Pico with a baud rate of 115200 & print status in a serial terminal.
1 2 3 |
uart = UART(1,115200) print('UART Serial') print('>', end='') |
Then we configure the ESP8266 in Soft AP+ Station mode using the command ‘AT+CWMODE=3‘. This command is sent through the UART of Pico. To set the name of the Access point we use ‘AT+CWSAP’. No password is set for the current configuration.
1 2 3 4 5 6 7 8 9 |
send='AT+CWMODE=3' uart.write(send+'\r\n') time.sleep(1) #Set SoftAP name send='AT+CWSAP="pos_softap","",11,0,3' uart.write(send+'\r\n') time.sleep(1) res=uartSerialRxMonitor(send) print(res) |
Using ‘AT+CIPMUX=1‘ enable multi-connection for ESP module.
1 2 3 4 5 |
send='AT+CIPMUX=1' uart.write(send+'\r\n') time.sleep(1) res=uartSerialRxMonitor(send) print("Configured as Dual mode ->" + res) |
Then start the server which will run on Port 80. Also, print the status on the serial terminal.
1 2 3 4 5 |
send='AT+CIPSERVER=1,80' uart.write(send+'\r\n') time.sleep(2) res=uartSerialRxMonitor(send) print("Server configured successfully-> "+res) |
Read internal temperature sensor data of Pico board and convert it into 16bit unsigned integer using Python’s inbuilt function. You can learn in detail about Internal Temperature Sensor in previous post.
1 2 |
sensor_temp = machine.ADC(4) conversion_factor = 3.3 / (65535) |
Then we start the infinite loop. The time.sleep() function provides some halt/waiting before execution of the next line. The command “AT+CIPSEND” will initiate data transmission. After sending this command to ESP, next the string of data is sent. The HTML code is inside ‘val’ will display data on a web page.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
while True: #temperature reading reading_temp = sensor_temp.read_u16() * conversion_factor temperature = 27 - (reading_temp - 0.706)/0.001721 #Place basic code for HTML page display val='<head><title>Rasberry Pi Pico Server</title></head><body><p>Temperature is: '+str(int(temperature))+' deg'+'</p></body>' print(val) length=str(len(val)) send='AT+CIPSEND=1,'+length uart.write(send+'\r\n') time.sleep(2) res=uartSerialRxMonitor(send) print("Data sent-> "+res) send=val uart.write(send+'\r\n') time.sleep(10) |
Testing Raspberry Pi Pico & ESP8266 based Web Server
Run the code on Thonny IDE, save it into Pi Pico as main.py. The Serial Terminal will display the following messages.
Then open Google Chrome, type 192.168.4.1 in the address field. Then press Enter. The browser will start to display the temperature data. Since the connection of the server is not closed, it will continuously print data on the page.
5 Comments
Thank you for the tutorial.
is there any way to show input fields and store data from user’s input?
Thank you
I get this error message:
Pi Pico Server
Temperature: 17 deg
Data sent-> Server
Temperature: 17 deg
ERROR
AT+CIPSEND=1,81
link is not valid
I’m also getting the same error, link is not valid.
Someone can you help me to resolve this issue.
Thanks
For those wondering why the “link is not valid” error is showing, it happens becase AT+CIPMUX=1 means the ESP should allow multiple connections. Each connection has an ID, and the ID is what’s been used here AT+CIPSEND=1,81 (the “1” is the ID). That should read AT+CIPSEND=0,81 to send to the first connection (“0”).
i couldnt fix it like that can u explain more pls