RPi Pico B.Tech/Diploma Beginner

Raspberry Pi Pico Web Server with ESP8266 & MicroPython

RP2040 Raspberry Pi Pico Web Server with ESP8266 WiFi Module & MicroPython Code to display internal temperature sensor data on Web Page

  • Suitable forB.Tech/Diploma RPi Pico students
  • Build time1-2 weeks
  • LevelBeginner
  • Available atAjvika Labs, Kakinada

About this project

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

Raspberry Pi Pico Board

Amazon
AliExpress

ESP8266-01 WiFi Module

Amazon
AliExpress

Connecting Wires

Amazon
AliExpress

Breadboard

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.

#Code for Simple Rasberry Pi PICO Web server
#Using ESP8266 wifi module
from
machine
import
UART
import
machine
import
_thread
import
time
uart
=
UART
(

115200
)
(
'UART Serial'
)
(
'>'
end
=
''
)
def
uartSerialRxMonitor
(
command
)
:
recv
=
bytes
(
)
while
uart
any
(
)
>

:
recv
+=
uart
read
(

)
res
=
recv
decode
(
'utf-8'
)
erase_len
=
len
(
command
)
+

res
=
res
[
erase_len
:
]
return
res
#configure as SoftAP+station mode
send
=
'AT+CWMODE=3'
uart
write
(
send
+
'\r\n'
)
time
sleep
(

)
#Set SoftAP name
send
=
'AT+CWSAP="pos_softap","",11,0,3'
uart
write
(
send
+
'\r\n'
)
time
sleep
(

)
res
=
uartSerialRxMonitor
(
send
)
(
res
)
#enable multi connection mode
send
=
'AT+CIPMUX=1'
uart
write
(
send
+
'\r\n'
)
time
sleep
(

)
res
=
uartSerialRxMonitor
(
send
)
(
"Configured as Dual mode ->"
+
res
)
# Enable the TCP server with port 80,
send
=
'AT+CIPSERVER=1,80'
uart
write
(
send
+
'\r\n'
)
time
sleep
(

)
res
=
uartSerialRxMonitor
(
send
)
(
"Server configured successfully-> "
+
res
)
#temperature reading
sensor_temp
=
machine
ADC
(

)
conversion_factor
=
3.3
/
(
65535
)
#Here the code runs indefinitely
while
True
:
#temperature reading
reading_temp
=
sensor_temp
read_u16
(
)
*
conversion_factor
temperature
=

(
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>'
(
val
)
length
=
str
(
len
(
val
)
)
send
=
'AT+CIPSEND=1,'
+
length
uart
write
(
send
+
'\r\n'
)
time
sleep
(

)
res
=
uartSerialRxMonitor
(
send
)
(
"Data sent-> "
+
res
)
send
=
val
uart
write
(
send
+
'\r\n'
)
time
sleep
(

)
Code Explanation
First we need to import required libraries.

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.

uart
=
UART
(

115200
)
(
'UART Serial'
)
(
'>'
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.

send
=
'AT+CWMODE=3'
uart
write
(
send
+
'\r\n'
)
time
sleep
(

)
#Set SoftAP name
send
=
'AT+CWSAP="pos_softap","",11,0,3'
uart
write
(
send
+
'\r\n'
)
time
sleep
(

)
res
=
uartSerialRxMonitor
(
send
)
(
res
)
Using ‘
AT+CIPMUX=1
‘ enable multi-connection for ESP module.

send
=
'AT+CIPMUX=1'
uart
write
(
send
+
'\r\n'
)
time
sleep
(

)
res
=
uartSerialRxMonitor
(
send
)
(
"Configured as Dual mode ->"
+
res
)
Then start the server which will run on Port 80. Also, print the status on the serial terminal.

send
=
'AT+CIPSERVER=1,80'
uart
write
(
send
+
'\r\n'
)
time
sleep
(

)
res
=
uartSerialRxMonitor
(
send
)
(
"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.

sensor_temp
=
machine
ADC
(

)
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.

while
True
:
#temperature reading
reading_temp
=
sensor_temp
read_u16
(
)
*
conversion_factor
temperature
=

(
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>'
(
val
)
length
=
str
(
len
(
val
)
)
send
=
'AT+CIPSEND=1,'
+
length
uart
write
(
send
+
'\r\n'
)
time
sleep
(

)
res
=
uartSerialRxMonitor
(
send
)
(
"Data sent-> "
+
res
)
send
=
val
uart
write
(
send
+
'\r\n'
)
time
sleep
(

)
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.

Technologies used

ESP8266ArduinoRaspberry PiWiFiBLEPythonUARTADCMicroPythonHTML

Hardware components

  • ESP8266
  • Arduino
  • Raspberry Pi

Software & tools

  • Thonny
  • MicroPython

What you get

  • Working project / hardware kit, tested before delivery
  • Complete source code with explanation
  • Project demo and guidance for your viva
  • Project report, PPT and IEEE paper support (on request)
Delivery & Payment
Delivery takes a minimum of two weeks from the date your inquiry is confirmed. Payment is in two parts: • 50% advance to start the project • Remaining 50% before delivery Note: • Documentation and PPT are not included by default • If you need documentation/PPT, it can be arranged for extra time and cost We'll confirm the exact timeline and cost with you after reviewing your requirements.