IOT

Raspberry Pi Pico Web Server with ESP8266 & MicroPython

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

The Raspberry Pi Pico RP2040 board is equipped with a dual-core Arm Cortex-M0+ processor, 264KB of internal RAM, and up to 16MB of off-chip Flash. The RP2040 is substantially quicker than the Arduino, however, it has a wireless network connectivity restriction. This is why the Raspberry Pi Pico cannot be utilized for wireless or IoT applications on its own. As a result, a low-cost ESP8266 WiFi module could be an excellent option for adding wireless connectivity to the Raspberry Pi Pico.

In this article, we’ll show how to use the ESP8266 WiFi module to illustrate the Raspberry Pi Pico’s simple web server feature. The AT commands are stored in the ESP8266’s program memory and do not require any extra programming. We’ll use the serial port (UART) on the Raspberry Pi Pico to deliver these commands. The device is programmed using the Thonny IDE and the coding is done in MicroPython. The on-chip temperature sensor reading is displayed on a web page in the web browser by the Web Server.

Hardware Required

  • Raspberry Pi Pico
  • ESP8266-01WiFi Module
  • Connecting Wires
  • Breadboard

Introduction to Raspberry Pi Pico

Raspberry Pi PICO is a new device produced by the Raspberry Pi Foundation that is significantly less expensive than existing Raspberry Pi products. Raspberry Pi PICO is a Single Board Computer (SBC) with the smallest size development board, and it includes an RP2040 Microcontroller chip made by the Raspberry Pi Foundation. The Raspberry Pi Pico breakout board is specifically built for the RP2040. It resembles other microcontroller boards in appearance, with the MCU in the center, a micro-USB connector on one end, and a row of contacts on each side. At the opposite end of the board, there is a 3-pin debug connection.

 It combines the RP2040 with 2MB of Flash memory and a power supply chip that accepts input voltages ranging from 1.8 to 5.5V. This enables Pico to be powered from a wide range of sources. This comprises two or three AA cells connected in series, as well as a single lithium-ion cell.

Raspberry Pi Pico

The Raspberry Pi Pico is 51 by 21 mm in size, which is the same as an ESP32 Pico Kit and somewhat larger than an Arduino Nano or Micro. The Pico has 2 MB of QSPI Flash memory and 25 of the RP2040’s 30 GPIO pins have been extended out on extension connections. The board is breadboard compatible and will fit flawlessly on a breadboard.

Features of Raspberry Pi Pico:

  • Operating Voltage: 1.8-5.5 VDC
  • Processor: RP2040 (Dual-core Arm Cortex M0+) By Raspberry Pi
  • RAM: 264 KB
  • GPIO: 26 Pins
  • Clock Speed: 133 MHz
  • On-Board Port: Micro-USB 5V/2.5A DC Power Input Port
  • 2MB of onboard Flash memory
  • The castellated module allows soldering directly to carrier boards
  • USB 1.1 with device and host support
  • Low-power sleep and dormant modes
  • Drag-and-drop programming using mass storage over USB
  • 26 multi-function GPIO pins
  • 2 SPI, 2 I2C, 2 UART, 3 12-bit ADC, 16 controllable PWM channels
  • Accurate clock and timer on-chip
  • Operating Temperature Range: -20 to 85 degrees C
  • Temperature sensor
  • Accelerated floating-point libraries on-chip
  • 8 Programmable I/O (PIO) state machines for custom peripheral support

What is MicroPython?

MicroPython is a Python interpreter designed for microcontrollers and embedded devices. Damien P. George created it, and it is written in C. MicroPython is a Python 3 compatible compiler and runtime that may be run on microcontrollers.

Although it is a subset of Python 3, it does not include all of Python’s standard libraries because it was designed for limited systems. Only a few carefully chosen Python libraries are supplied, while the rest are created specifically for MicroPython (mostly for low-level hardware access).

If you’re a software developer who’s never dealt with hardware but wants to build programs for microcontroller-based embedded systems (for personal or professional projects), MicroPython is the way to go.

Circuit Diagram & Connections

Let’s have a look at how to connect the ESP8266 WiFi Module to the RP2040 Raspberry Pi Pico Board. A simple connection schematic is shown below.

ESP8266 Raspberry Pi Pico

Two UARTs are incorporated inside the Raspberry Pi Pico. UART-0 will be used in this design. Similarly, the ESP8266’s default baud rate is 115200. In order to preserve synchronization with the ESP8266, we must configure the Raspberry Pi Pico with the same baud rate.

Connect the ESP8266’s VCC and EN pins to the Pico 3.3V pins, and GND to GND. Connect the ESP8266’s Tx and RX to the Pico UART-O Pin, i.e. Rx & Tx.

Project PCB Gerber File & PCB Ordering Online

If you don’t want to put the circuit together on a breadboard and instead prefer a PCB, this is the PCB for you. EasyEDA online Circuit Schematics & PCB Design tool was used to create the PCB Board for Raspberry Pi Pico & ESP8266-01. The PCB appears as seen below.

A 3.7V Lithium-ion battery powers the device. Below is the Gerber File for the PCB. Simply download the Gerber File and place an order for the PCB.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 the NextPCB website.

You can now upload the Gerber File to the Website and place an order. The PCB quality is superb & high. That is why most people trust NextPCB for PCB & PCBA Services.

Source Code/Program

The MicroPython code for the RP2040 Raspberry Pi Pico Web Server with ESP8266 is available here.

We don’t need to change the code because we’re not connecting to any WiFi networks; instead, we’re constructing the Web Server using the ESP module’s access point.

The temperature data from the internal temperature sensor will be read and sent to a webpage that can be accessed via 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(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

We must first import the necessary libraries.

from machine import UART
import machine
import _thread
import time

Then we set UART-0 on the Raspberry Pi Pico to 115200 baud rate and print the status on a serial terminal.

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

The ESP8266 is then configured in Soft AP+ Station mode with the command ‘AT+CWMODE=3’. Pico’s UART is used to send this command. ‘AT+CWSAP’ is used to set the name of the access point. The current configuration has no 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)

AT+CIPMUX=1′ allows the ESP module to have multiple connections.


send=’AT+CIPMUX=1′
uart.write(send+’\r\n’)
time.sleep(1)
res=uartSerialRxMonitor(send)
print(“Configured as Dual mode ->” + res)

The server will then be started on Port 80. Also, use the serial terminal to print the status.

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

Using Python’s inbuilt function, read the internal temperature sensor data from the Pico board and convert it to a 16bit unsigned integer. Internal Temperature Sensor is covered in-depth in a previous post.

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

The unending loop then begins. The time.sleep() function offers some pause/waiting time before the following line is executed. Data transmission is started with the instruction “AT+CIPSEND.” The string of data is sent once this instruction is submitted to ESP. ‘val’ contains HTML code that will display data on a web page.

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 in Thonny IDE and save it as main.py in Pi Pico. The following messages will appear on the Serial Terminal.

““

Open Google Chrome and type 192.168.4.1 into the address bar. After that, press Enter. The temperature data will be displayed in the browser. Because the server’s connection is open, data will continue to print on the page.

CONCLUSION:
Hope this blog helps you to understand how to use Raspberry Pi Pico Web Server with ESP8266 & MicroPython. We, MATHA ELECTRONICS  will come back with more informative blogs.

Leave a Reply

Your email address will not be published.