Webserver Using Pi Pico and ESP01

13K1418

Intro: 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.

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.


12 Comments

Afternoon
we tried out your programme above and we getting stuck on how you created your own webserver. The attachment below is the results we got.
our main objective is to be able to communicate from a ESP-01 to pico pi using a mobile device .at the moment we manage to get commucation between the pico pi and the ESP-01 and we sitting on how to get that to communicate with a mobile device.

please advice or assist where you can .
Are you trying to control Pi's GPIOs using mobile or you're trying to open the server from mobile!! If the 2nd one that you're trying to do, just type the IP address on your mobile device while the mobile and esp01 is connected to the same network and if the 1st one that you're trying to do, then I'll say that I'm working on this exact project RN.
in simple terms ,the aim of my project is to send alarm notifications to either a mobile device or a webserver, using esp01and raspberry pi pico..so ive currently managed to supply the pico with wifi using esp01,and also send and receive server data. (ill attach the program)

from your video ,what im seeing is you are sending data too to a webserver, and thats where im also getting stuck.so i think to answer your question ,we are trying the to achieve the 2nd option.
I CAN SEEM TO BE ABLE TO UPLOAD AN IMAGE
alright understandable...please guide me further on how to make the HTML file, and how to place the hyperlink on the pico
ive attached a program i used to wifi enable the raspberry pi pico, and send and receive data through server using raspberry pi pico and esp01
I made a HTML file named "webpage.html" where I added the hyperlink and placed it in Pico memory.
I wasn't sending data actually. I added a hyperlink of a webpage in my code.That's why I was able to open that page.
Hi, how you get result like that? Why my result only got:
Setting up Webserver...
- Setting Connection Mode...
- Starting Webserver..
Webserver Ready!
Good night sir, If I need to send sensor data from esp-01 to specific website, how I need to do?