Introduction: Webserver Using Pi Pico and ESP01

ESP-01 WiFi module is developed by encapsulates Tensilica L106 integrates industry Clock speed support 80 MHz, 160 MHz, supports the RTOS, integrated Wi The module supports standard IEEE802.11 add modules to an existing device networking or building a separate network controller. ESP8266 is high integration wireless SOCs, designed for space and power-constrained mobile platforms It provides unsurpassed ability to embed Wi application, with the lowest cost, and minimal space requirement. ESP8266EX offers a complete and self or to offload Wi-Fi networking functions from another application processor When ESP8266EX hosts the application, it boots up directly from an external flash. It has improved the performance of the system in such applications. Alternately, serving as a Wi-Fi adapter, wireless internet access can be added to any microcontroller-based design with simple connectivity (SPI/SDIO or I2C/UART interface). http://www.ai-thinker.com Ai-thinker Team. core processor ESP8266 in smaller sizes of the module industry-leading ultra low power 32-bit MCU micro, with the 16 Wi-Fi MAC/BB/RF/PA/LNA, on b/g/n agreement, complete TCP/IP protocol stack. Users can use the Wi-Fi capabilities within other systems, or to function as a standalone self-contained Wi-Fi networking solution; it can be used to host processors. integrated 3 16-bit short mode, /on-board antenna.

Supplies

1. Raspberry Pi Pico

2. ESP01

Step 1: Connect the ESP01 With Raspberry Pi Pico According to the Diagram.

Step 2: Open Thonny Write the Code and Run It.

from machine import UART
import machine
import _thread
import time


def ESP8266Webserver(HTMLFile):
    #Set variables
    recv=""
    recv_buf="" 
    uart = UART(1,115200) # uart on uart1 with baud of 115200
    # wifi credentials (if needed)
    wifi_ssid = ("TP-LINK_9950")
    wifi_password = ("manodeep")
    
    #Function to handle reading from the uart serial to a buffer
    def SerialRead(mode):
        SerialRecv = ""
        if mode == "0" :
            SerialRecv=str(uart.readline())
        else:
            SerialRecv=str(uart.read(mode))
        #replace generates less errors than .decode("utf-8")
        SerialRecv=SerialRecv.replace("b'", "")
        SerialRecv=SerialRecv.replace("\\r", "")
        SerialRecv=SerialRecv.replace("\\n", "\n")
        SerialRecv=SerialRecv.replace("'", "")
        return SerialRecv

    print ("Setting up Webserver...")
    time.sleep(2)
# Connect to wifi, this only needs to run once, ESP will retain the CWMODE and wifi details and reconnect after power cycle, leave commented out unless this has been run once.
#     print ("  - Setting AP Mode...")
#     uart.write('AT+CWMODE=1'+'\r\n')
#     time.sleep(2)
#     print ("  - Connecting to WiFi...")
#     uart.write('AT+CWJAP="'+wifi_ssid+'","'+wifi_password+'"'+'\r\n')
#     time.sleep(15)
    print ("  - Setting Connection Mode...")
    uart.write('AT+CIPMUX=1'+'\r\n')
    time.sleep(2)
    print ("  - Starting Webserver..")
    uart.write('AT+CIPSERVER=1,80'+'\r\n') #Start webserver on port 80
    time.sleep(2)
    print ("Webserver Ready!")
    print("")

    while True:
        while True:
            #read a byte from serial into the buffer
            recv=SerialRead(1)
            recv_buf=recv_buf+recv
                       
            if '+IPD' in recv_buf: # if the buffer contains IPD(a connection), then respond with HTML handshake
                HTMLFileObject = open (HTMLFile, "r")
                HTMLFileLines = HTMLFileObject.readlines()
                HTMLFileObject.close()
                uart.write('AT+CIPSEND=0,17'+'\r\n')
                time.sleep(0.1)
                uart.write('HTTP/1.1 200 OK'+'\r\n')
                time.sleep(0.1)
                uart.write('AT+CIPSEND=0,25'+'\r\n')
                time.sleep(0.1)
                uart.write('Content-Type: text/html'+'\r\n')
                time.sleep(0.1)
                uart.write('AT+CIPSEND=0,19'+'\r\n')
                time.sleep(0.1)            
                uart.write('Connection: close'+'\r\n')
                time.sleep(0.1)
                uart.write('AT+CIPSEND=0,2'+'\r\n')
                time.sleep(0.1)
                uart.write('\r\n')
                time.sleep(0.1)
                uart.write('AT+CIPSEND=0,17'+'\r\n')
                time.sleep(0.1)
                uart.write('<!DOCTYPE HTML>'+'\r\n')
                time.sleep(0.1)
                # After handshake, read in html file from pico and send over serial line by line with CIPSEND
                for line in HTMLFileLines:
                        cipsend=line.strip()
                        ciplength=str(len(cipsend)+2) # calculates byte length of send plus newline
                        uart.write('AT+CIPSEND=0,' + ciplength +'\r\n')
                        time.sleep(0.1)
                        uart.write(cipsend +'\r\n')
                        time.sleep(0.1) # The sleep commands prevent the send coming through garbled and out of order..
                uart.write('AT+CIPCLOSE=0'+'\r\n') # once file sent, close connection
                time.sleep(4)
                recv_buf="" #reset buffer
                
            

    
#Place in main code
website = ("/webpage.html") # this is the path to the html file on the pico filesystem
_thread.start_new_thread(ESP8266Webserver(website), ()) # starts the webserver in a _thread

Step 3: Open a Web Browser, Enter the IP Address of the ESP01.