IOT

Raspberry Pi Pico Web Server with ESP8266 & MicroPython

In this article, we will learn how to use ESP8266 WiFi Module with RP2040 Raspberry Pi Pico Board using MicroPython Code and develop our own Web Server.

The dual-core Arm Cortex-M0+ processor on the Raspberry Pi Pico RP2040 board has 264KB of onboard RAM and up to 16MB of off-chip Flash. Although the RP2040 is far quicker than the Arduino, its wireless network access is severely limited. Because of this, the Raspberry Pi Pico cannot be utilized for wireless or IoT applications on its own at this time. The Raspberry Pi Pico could benefit from wireless connectivity provided by an ESP8266 WiFi module, which is relatively inexpensive.

The ESP8266 WiFi module will be used to show the basic web server capability of the Raspberry Pi Pico in this tutorial. The ESP8266 comes preloaded with a set of AT commands that don’t need to be programmed. We’ll be using the Raspberry Pi Pico’s serial port (UART) to deliver these commands to the device. There are two ways to program the Thonny: MicroPython and Thonny IDE. The on-chip temperature sensor reading is displayed on a web page in the web browser by the Web Server.

Hardware Required

S.N.COMPONENTS NAMEDESCRIPTION
1Raspberry Pi PicoRP2040 Microcontroller Board
2WiFi ModuleESP8266-01
3Connecting WiresJumper Wires
4Breadboard

Circuit Diagram & Connections

The ESP8266 WiFi Module is now ready to be connected to the RP2040 Raspberry Pi Pico Board. Here’s a quick look at how things are connected.

ESP8266 Raspberry Pi Pico

Both the UARTs on the Pi Pico are on the Raspberry Pi. We’ll be using UART-0 in this design. As with the ESP8266’s default baud rate of 115200, so it is with the ESP8266. It is imperative that we configure the Raspberry Pi Pico to use the same baud rate as the ESP8266.

For example, you can connect the ESP8266 3.3V pin to the Pico’s GND. Connect the ESP8266’s Rx and Tx to the Pico UART-O Pin in the same manner, i.e. Rx & Tx.

Project PCB Gerber File & PCB Ordering Online

Here is a PCB for you if you don’t want to assemble the circuit on a breadboard and prefer a finished product. An online circuit schematic and PCB design tool called EasyEDA was used to create the Raspberry Pi Pico & ESP8266-01 board. Here’s a picture of what the PCB looks like.

The device is powered via 3.7V Lithium-ion Battery. The Gerber File for the PCB is given below. You can simply download the Gerber File and order the PCB from https://www.nextpcb.com/

Download Gerber File: Pi Pico ESP8266

Now you can visit the NextPCB official website by clicking here: https://www.nextpcb.com/. So you will be directed to NextPCB website.

You can now upload the Gerber file to the website and order. The quality of the printed circuit board (PCB) is excellent and really good. As a result, the majority of customers turn to NextPCB when they need help with PCBs and PCBAs.

Source Code/Program

The complete MicroPython code for the RP2040 Raspberry Pi Pico Web Server with ESP8266 is available below. Instead of using a WiFi network, we’re using an ESP module to build an access point, thus there’s no need to change any code.

Temperature data will be sent over a webpage that can be accessed by any web browser, such as Chrome or Firefox.

#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 the required libraries.

from machine import UART
import machine
import _thread
import time

After that, we set up the Raspberry Pi Pico’s UART-0 port with a 115200 baud rate and display its status to a terminal.

uart = UART(1,115200)
print(‘UART Serial’)
print(‘>’, end=”)

You’ll next need to use the command “AT+CWMODE=3” to put your ESP8266 into the Soft AP+ Station mode. Pico’s UART is used to send this command. The access point’s name is set with the command ‘AT+CWSAP.’ The current setup does not have a password.

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.

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.

send=’AT+CIPSERVER=1,80′
uart.write(send+’\r\n’)
time.sleep(2)
res=uartSerialRxMonitor(send)
print(“Server configured successfully-> “+res)

Take input from the Pico board’s internal temperature sensor and use Python’s inbuilt method to convert it to a 16-bit unsigned integer value

sensor_temp = machine.ADC(4)
conversion_factor = 3.3 / (65535)

The infinite loop begins after this point. Before the next line is executed, the time.sleep() function provides some halting/waiting. Data transmission can be started with the “AT+CIPSEND” command. The data string is then transferred to ESP after this instruction has been sent. A web page containing the HTML code inside ‘val’ will display data.


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

Using the Thonny IDE, run the code and save it as main.py in Pi Pico. This is what you can expect to see on the Serial Terminal.

Then, in Google Chrome, enter 192.168.4.1 as the URL. Then, press Enter. Temperature data will begin to appear in the browser. Data will be printed indefinitely because the server’s connection has not been ended.

Conclusion

I hope all of you understand how to design a Raspberry Pi Pico Web Server with ESP8266 & MicroPython. We MATHA ELECTRONICS will be back soon with more informative blogs.

Leave a Reply

Your email address will not be published.