Warning: Declaration of Db::query($query) should be compatible with mysqli::query($query, $resultmode = NULL) in /var/www/u1300519/data/www/acoptex.com/_lib/class.Db.php on line 29

Deprecated: Function get_magic_quotes_runtime() is deprecated in /var/www/u1300519/data/www/acoptex.com/_lib/CacheLite/Lite.php on line 757

Deprecated: Function get_magic_quotes_runtime() is deprecated in /var/www/u1300519/data/www/acoptex.com/_lib/CacheLite/Lite.php on line 757

Warning: Use of undefined constant MYSQL_ASSOC - assumed 'MYSQL_ASSOC' (this will throw an Error in a future version of PHP) in /var/www/u1300519/data/www/acoptex.com/_lib/class.Db.php on line 64

Warning: mysqli_fetch_array() expects parameter 2 to be int, string given in /var/www/u1300519/data/www/acoptex.com/_lib/class.Db.php on line 64

Deprecated: Function get_magic_quotes_runtime() is deprecated in /var/www/u1300519/data/www/acoptex.com/_lib/CacheLite/Lite.php on line 757

Deprecated: Function get_magic_quotes_runtime() is deprecated in /var/www/u1300519/data/www/acoptex.com/_lib/CacheLite/Lite.php on line 757

Warning: Cannot modify header information - headers already sent by (output started at /var/www/u1300519/data/www/acoptex.com/_lib/class.Db.php:0) in /var/www/u1300519/data/www/acoptex.com/_config/config.php on line 168

Warning: session_start(): Cannot start session when headers already sent in /var/www/u1300519/data/www/acoptex.com/_config/config.php on line 169

Warning: Use of undefined constant MYSQL_NUM - assumed 'MYSQL_NUM' (this will throw an Error in a future version of PHP) in /var/www/u1300519/data/www/acoptex.com/_lib/class.Db.php on line 92

Warning: mysqli_fetch_array() expects parameter 2 to be int, string given in /var/www/u1300519/data/www/acoptex.com/_lib/class.Db.php on line 92

Warning: Use of undefined constant MYSQL_ASSOC - assumed 'MYSQL_ASSOC' (this will throw an Error in a future version of PHP) in /var/www/u1300519/data/www/acoptex.com/_lib/class.Db.php on line 64

Warning: mysqli_fetch_array() expects parameter 2 to be int, string given in /var/www/u1300519/data/www/acoptex.com/_lib/class.Db.php on line 64
jobs.html_title
0

members

Easy Basics: Project 076e ESP32 Development board - MicroPython Web Server

of Acoptex.com in ESP8266 ESP-32

Basics: Project 076e

Project name: ESP32 Development board - MicroPython Web Server

Tags: EESP32 Dev Module, ESP32 development board, ESP32 Development board with WiFi and Bluetooth, ESP32-DevKitC V4 development board, ESP-WROOM-32 module with ESP32‑D0WDQ6 chip, Espressif Systems, ESP32-based development board, ESP32 modules, ESP32-WROOM-32, ESP32-WROOM-32U, ESP32-WROOM-32D, ESP32-SOLO-1, USB-UART bridge, IOT, ESP-WROOM-32 Dev Module, ESP32 DEVKITV1, MicroPython Web Server

Attachments: bootsketch, mainsketch

In this project, you need these parts :

1. ESP32 development board with WiFi and Bluetooth and USB A / micro USB B cable 1 pc

2. uPyCraft IDE (you can download and read more about ithere)

3.Jumper cables F-M, M-M

4. Resistor 2 pcs (220 Ohm each)

5.LED 2 pc (can be of different color)

 

6. Breadboard 1 pc

General

We will learn how to build a web server to control the ESP32 development board outputs using MicroPython.

We will build a web server to control the LEDs and use sockets with the Python socket API.

In order to program the ESP32 development board with MicroPython you need to:

  1. Install uPyCraft IDE in your PC;
  2. Upload MicroPython firmware to your ESP32 development board.

Understanding the ESP32 Development board with WiFi and Bluetooth

You can read more about it here.

Signals and connections of the ESP32 Development board with WiFi and Bluetooth

You can find more information (datasheets, schematics, pins descriptions, functional desgn descriptions) about each board (made by Espresiff Systems) by pressing Getting started link close to each board here.

Let's check our development board - ESP32 DEVKITV1with ESP-WROOM-32 module from Espressif Systems:

Pinout diagram for the ESP Wroom 32 breakout:

ESP32-WROOM-32 - ESP32-WROOM-32 module soldered to the development board. Optionally ESP32-WROOM-32D, ESP32-WROOM-32U or ESP32-SOLO-1 module may be soldered instead of the ESP32-WROOM-32.

USB-UART Bridge - A single chip USB-UART bridge provides up to 3 Mbps transfers rates.

BOOT button - Download button: holding down the Boot button and pressing the EN button initiates the firmware download mode. Then user can download firmware through the serial port.

EN button - Reset button: pressing this button resets the system.

Micro USB Port - USB interface. It functions as the power supply for the board and the communication interface between PC and the ESP module.

TX0, TX2 - transmit pin. GPIO pin

RX0, RX2  - receive pin.  GPIO pin

3V3 (or 3V or 3.3V) - power supply pin (3-3.6V). 

GND - ground pin.

EN - Chip enable. Keep it on high (3.3V) for normal operation.

Vin - External power supply 5VDC.

Wiring


Step by Step instruction
  1. Install uPyCraft IDE on your PC;
  2. Plug the ESP32 development board to your PC and wait for the drivers to install (or install manually any that might be required).
  3. Upload MicroPython firmware to your ESP32 Development board .
  4. Open uPyCraft IDE. Double-click uPyCraft_V1.1.exe file. A new window opens with the uPyCraft IDE software.
  5. Go to Tools -> Board. If you are using ESP32 development board select esp32.
  6. Go to Tools -> Serial and select your ESP32 Development board COM port (in our case it’s COM8). If you don’t see the COM Port, you need to install the ESP32 CP210x USB to UART Bridge VCP Drivers , check your USB cable too (it should be data cable).
  7. You have established a serial communication with your board. The >>> should appear in the Shell window after a successful connection with your board.
  8. Press New file button to create a new file.
  9. Press Save file button to save the file in your PC. Input file name main.py and press ok button.
  10. You should see the following in your uPyCraft IDE -the boot.py file in your device and a new tab with the main.py file.
  11. Press Download and run button to upload the file to your ESP32 Development board. The device directory should now load the main.py file. Your ESP32 Development board has two files - boot.py and main.py stored now.
  12. You should see a message download ok in the Shell window.
  13. Copy the following bootsketch to the Editor on the boot.py file. Modify SSID and password for you local network to be able to connect to your router.
  14. Press Save button to save the changes.
  15. Press Stop button to stop any script from running in your board.
  16. Press Download and Run button to upload the script to the ESP32 Development board.
  17. You should see a message download ok in the Shell window.
  18. Copy the following mainsketch to the Editor on the main.py file.
  19. Press Save button to save the changes.
  20. Press Stop button to stop any script from running in your board.
  21. Press Download and Run button to upload the script to the ESP32 Development board.
  22. You should see a message download ok in the Shell window.
  23. Press the ESP32 development board on-board EN button to reboot it.
  24. After a few seconds it should establish a connection with your router and print the IP address on the Shell.
  25. Open your browser (IE, Google Chrome or Microsoft Edge) and type your ESP32 development board IP address you have just observed (In our case it is 192.168.0.116). You should see the web server page as shown below.
  26. When you press the ON button, you make a request on the ESP32 development board IP address followed by /led1=on or /led2=on. The corresponding LED turns on and its GPIO state updated on the page.
  27. When you press the OFF button, you make a request on the ESP32 development board IP address followed by /led1=off or /led2=off. The corresponding LED turns off and its GPIO state updated on the page.

Code

1. bootsketch

We create our web server using sockets and the Python socket API. First we import the socket library:

try:  import usocket as socket

except:  import socket

We need to import the Pin class from the machine module to be able to interact with the GPIOs: from machine import Pin

We need to import the network library. The network library allows us to connect the ESP32 development board to a Wi-Fi network: import network

The following lines turn off vendor OS debugging messages: import esp esp.osdebug(None)

We run a garbage collector: import gc gc.collect()

A garbage collector is a form of automatic memory management. This is a way to reclaim memory occupied by objects that are no longer in used by the program. This is useful to save space in the flash memory.

The following variables hold your network credentials: ssid='' and password=''. You need to replace them with your local network access data.

Then we set the ESP32 development board as a Wi-Fi station: station = network.WLAN(network.STA_IF) and activate it: station.active(True)

ESP32 development board connects to your router using the SSID and password defined earlier: station.connect(ssid, password)

The following statement ensures that the code doesn’t proceed while the ESP is not connected to your network: while station.isconnected() == False:   pass

After a successful connection print network interface parameters like the ESP32 development board IP address – use the ifconfig() method on the station object:

print('Connection successful')

print(station.ifconfig())

Create a Pin objects called led1 and led2 - outputs for the ESP32 development board GPIO23 and GPIO22 accordingly:

led1 = Pin(23, Pin.OUT)

led2 = Pin(22, Pin.OUT)

2. mainsketch

The script starts by creating a function called web_page(). This function returns a variable called html that contains the HTML text to build the web page: def web_page():

The web page displays the current GPIO states. So, before generating the HTML text, we need to check the LEDs state. We save LEDs state on the gpio_state1 and gpio_state2 variable:

if led1.value() == 1:

  gpio_state1="ON"

else: 

  gpio_state1="OFF"

if led2.value() == 1:

  gpio_state2="ON"

else: 

  gpio_state2="OFF"

The gpio_state1 and gpio_state2 variable is incorporated into the HTML text using "+" signs to concatenate strings.

After creating the HTML to build the web page we need to create a listening socket to listen for incoming requests and send the HTML text in response. The following picture shows how to create sockets for server-client interaction:

Create a socket using socket.socket() and specify the socket type. We create a new socket object called s with the given address family and socket type. This is a STREAM TCP socket: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

Then we bind the socket to an address (network interface and port number) using the bind() method. The bind() method accepts a tupple variable with the ip address, and port number: s.bind(('', 80))

In our project, we are passing an empty string ' ' as an IP address and port 80. In this case, the empty string refers to the localhost IP address (this means the ESP32 development board IP address).

The next line enables the server to accept connections; it makes a socket to listen. The argument specifies the maximum number of queued connections. The maximum is 5: s.listen(5)

In the while loop is where we listen for requests and send responses. When a client connects, the server calls the accept() method to accept the connection. When a client connects, it saves a new socket object to accept and send data on the conn variable, and saves the client address to connect to the server on the addr variable: conn, addr = s.accept()

Then we print the address of the client saved on the addr variable: print('Got a connection from %s' % str(addr))

The data exchange between the client and server done using the send() and recv() methods.

The following line gets the request received on the newly created socket and saves it in the request variable.

request = conn.recv(1024)

The recv() method receives the data from the client socket (remember that we’ve created a new socket object on the conn variable). The argument of the recv() method specifies the maximum data that can be received at once.

The next line simply prints the content of the request:

print('Content = %s' % str(request))

Then we create a variable called response that contains the HTML text returned by the web_page() function: response = web_page()

Finally we send the response to the socket client using the send() method: conn.send(response) and close the created socket: conn.close()

Summary

We have learnt how to build a web server to control the ESP32 development board outputs using MicroPython.

Libraries

  • No libraries required

Sketch

  • See attachments on the begining of this project


Other projects of Acoptex.com
Medium Basics: Project 083w Sipeed Maixduino board - Using PlatformIO IDE of Acoptex.com in Sipeed Maixduino 08-08-2019
Medium Basics: Project 083e Sipeed Maixduino board - Uploading MaixPy of Acoptex.com in Sipeed Maixduino 04-08-2019
Medium Basics: Project 083f Sipeed Maixduino board - Using MycroPython of Acoptex.com in Sipeed Maixduino 04-08-2019

jobs.published_at
jobs.viewed