The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.

Introduction to NodeMCU ESP32 & Micro Python Programming

Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by charles, 2023-01-03 02:54:25

Introduction to NodeMCU ESP32 & Micro Python Programming

Introduction to NodeMCU ESP32 & Micro Python Programming

Keywords: ESP32,micropython

Introduction To ESP32 Microcontroller and Micro Python Language Programming BY CHARLES SUMION


Author Charles Sumion Designer and Editor Caroline Dame Siagian Charles Sumion Issue of the 2022 Edition All right reserved. No part of this publication may be stored in the retrieval or transmitted in any form or by any means electronic, mechanical, photocopying recording, or otherwise without the permission from the publisher. Published by: Politeknik Kota Kinabalu No. 4 Jalan Politeknik KKIP Barat, Kota Kinabalu Industrial Park, 88460 Kota Kinabalu, Sabah. Perpustakaan Negara Malaysia Cataloguing in Publishing Data Introduction To ESP32 Microcontroller and Micro Python Language Programming Politeknik Kota Kinabalu (online)


About the Author Charles Sumion has a degree in microelectronic engineering and currently teaching an Internet-Based Controller course in Electronics Computer Engineering, at the Polytechnic of Kota Kinabalu (PKK). He has also taught many different diplomas courses mainly in the computer engineering and technology areas at PKK for the past 12 years. Charles has been involved in the internet of things with the promotion since taught integrated circuit design and computer network engineering besides other basic courses. Since then, he has spent more than 5 years as a professional software developer using C, C++, Python, Micro Python, JSON, JavaScript, Java, and YAML languages in varied development projects. He also has been certified in IoT development and consultant for the last three years. Charles started a consultancy that specializes in developing application solutions using microcontrollers, sensors, and actuators controlling and monitoring Local Area Networks and the cloud. He has been involved in writing a workbook for practical works for Internet-Based Controller using C Programming. Charles likes to think of himself as a perpetual hobbyist and geek and is constantly trying out new technologies and out-of-box experiments. But most importantly a proud father of two great daughters Joyce Elcie and Mercylia Elsa from Lucia Manior.


Synopsis This BOOK will serve both as an introduction to the ESP32 Microcontrollers (MCUs) and Micropython language programming. This book is also an easy-to-follow Getting Started Guide for readers interested in developing an ESP32 MCU. I will be using one of the very inexpensive NodeMCU ESP32-32S development boards for all the book projects, which should make it inviting for most readers to become involved with hardware. In fact, doing the book demonstration projects is really the only way you really be assured that you have gained a good comprehension of the material in this book. I will state from the beginning that it is simply not possible to gain a total understanding of how an ESP32 NodeMCU functions by only reading this book. The manufacturer datasheets describe individual ESP32 MCUs, which describes the enormity of the task of trying to master the voluminous amount of information that describes these devices. Instead, the book’s contents focus on a few of the core components that make up an ESP32 MCU and how to program those components to accomplish simple tasks. Some readers will have trepidation about starting to develop with what are typically considered professional-grade MCUs. I wish to allay that fear and state that I have found developing applications with at least one representative sample ESP32 NodeMCU to be remarkably easy and straightforward. In fact, I will state that in some areas it is easier to develop with ESP32 NodeMCU than with an Arduino or Raspberry Pi, which many readers will already be quite familiar with and probably have already created projects with those boards. Often, the single biggest issue with developing with MCUs is setting up a stable development toolchain. I will describe how to do this simple-to-follow, step-by-step process, which if you rigorously follow will guarantee that you will be able to quickly and without much trouble generate a working binary program. These programs will then be quickly downloaded into the development board for execution.


________________________________________ Table of Contents 1. Introduction to NodeMCU ESP32 and Micro Python ……………………. 1 Started with Thonny Micro Python IDE for ESP32 – Windows PC 3 Running Your First Python Programming to NodeMCU ESP32 11 Programming With ADC and PWM 15 2. Sensors Application and Actuators …………………………………………18 NodeMCU ESP32 Digital and Analogue Inputs and Outputs 19 NodeMCU ESP32 Deep Sleep and Wake-Up Sources 22 NodeMCU ESP32 Analogue LDR Sensor and Interrupts 26 Read Analog Values from the Potentiometer with NodeMCU ESP32 27 3. NodeMCU ESP32 using HTTP Protocol …………………………………….29 Wi-Fi Manager with NodeMCU ESP32 31 Controlling LED Using Web Server 33 Reading Sensor Via Web Server 39 Web Server with Wi-Fi Manager 44 4. Set up and Configure MQTT Protocol ……………………………………… 56 How MQTT works 57 PRE-Installation NODE-RED 59 MOSQUITTO MQTT BROKER Installation 66 START YOUR FIRST FLOW 70 Using Android Apps to Connect to MQTT Broker 77 5. NodeMCU ESP32 using MQTT Protocol …………………………………… 85 Configuring the MQTT Publish and Subscribe Nodes in Node-Red 85 Turn ON and OFF LED NodeMCU ESP32 via Node-Red Dashboard 86 Send Sensor Data from NodeMCU ESP32 to Display on Node-Red Dashboard 96


Chapter 1 – Introduction to ESP32 & Micro Python 1 1 Introduction to NodeMCU ESP32 and Micro Python This book introduces, explores, and explains Micro Python through four typical yet different devices, all of which have a microcontroller at their core. ESP32 NodeMCU NodeMCU is an open-source LUA-based firmware developed for ESP32 Wi-Fi chips. By exploring functionality with the ESP32 chip, NodeMCU firmware comes with ESP32 Development board/kit i.e., NodeMCU Development board. Since NodeMCU is an open-source platform, its hardware design is open for editing/modification/building. NodeMCU Dev Kit/board consists of an ESP32 Wi-Fi-enabled chip. The ESP32 is a low-cost Wi-Fi chip developed by Espressif Systems with TCP/IP protocol. Micro Python Micro Python is a re-implementation of Python 3 targeted for microcontrollers and embedded systems. Micro Python is very similar to regular Python. So, if you already know how to program in Python, you also know how to program in Micro Python.


Chapter 1 – Introduction to ESP32 & Micro Python Python VS Micro Python Apart from a few exceptions, the language features of Python are also available in Micro Python. The biggest difference between Python and Micro Python is that Micro Python was designed to work under constrained conditions. Because of that, Micro Python does not come with the full standard library. It only includes a small subset of the Python standard library. However, it does include modules to access low-level hardware – this means that there are libraries to easily access and interact with the GPIOs. Additionally, devices with Wi-Fi capabilities like the ESP8266 and ESP32 include modules to support network connections. Why Micro Python? Python is one of the most widely used, simple, and easy-to-learn programming languages around. So, the emergence of Micro Python makes it extremely easy and simple to program digital electronics. If you have never programmed digital electronics before, Micro Python is a good starting point.


Chapter 1 – Introduction to ESP32 & Micro Python 3 Started with Thonny Micro Python IDE for ESP32 – Windows PC 1. Download Micro Python firmware for ESP32 - https://micropython.org/download/ Select the type of board you’re using. Here are the quick links for “regular” ESP32 boards. 2. Download Thonny IDE from here - https://thonny.org


Chapter 1 – Introduction to ESP32 & Micro Python 3. Run the .exe file. 4. Follow the installation wizard to complete the installation process. You just need to click “Next”. 5. After completing the installation, open Thonny IDE. A window as shown in the following figure should open. 6. After clicking the “Finish” button, the display likes below will appear. Wait until finish checking for updating, then be ready to use.


Chapter 1 – Introduction to ESP32 & Micro Python 5 7. Now connect you NodeMCU to the computer using a USB cable. 8. If NodeMCU not detected by the PC, install USB driver. Click Window’s Start Menu and search for Device Manager. Expand Ports (COM & LPT) and look for Silicon Labs CP210x USB to UART Bridge. Note the COM number next to it. 9. Now open Thonny IDE. Go to Tools > Options > Interpreter.


Chapter 1 – Introduction to ESP32 & Micro Python 10. Select the port once again, and then click on the Browse button to open the .bin file with the firmware you downloaded in the previous step. Select the options as shown in the picture below and finally click on Install. 11. If successful, Thonny IDE should now be connected to your board, and you should see the prompt on the Shell.


Chapter 1 – Introduction to ESP32 & Micro Python 7 12. If type the command help() into the Shell and see if it responds back. Running Your First Script


Chapter 1 – Introduction to ESP32 & Micro Python To get you familiar with the process of writing a file and executing code on your ESP32 boards, we’ll upload a new script that simply blinks the onboard LED of your ESP32. Mathematical Operators Micro Python can perform mathematical operations. The following table shows the mathematical operators supported: Operator Mathematical Operation + Addition – Subtraction * Multiplication / Division // Division, discarding the decimal point % Remainder after division In the Shell, try several operations to see how it works. For example: 1. >>> 2+2*9-3 2. >>> 28594/2312 3. >>> 214522236/7.5 4. >>> 23//2 5. >>> 25%3 Relational Operators Make comparisons using relational operators. These compare the values on either side and show the relation between them. Operator Description == Equal to


Chapter 1 – Introduction to ESP32 & Micro Python 9 != Not equal to > Greater than < Less than >= Greater than or equal to <= Less than or equal to Try several comparisons and test the result: 1. >>> 2 == 3 2. >>> 4 == 4 3. >>> 3 > 2 4. >>> 489808234 != 2223 5. >>> 4.5 >= 4.5 Data Types Variables can store several types of values, not just whole numbers. That’s where data types come in. A data type is a classification of a value that tells what operations can be done with the value and how it should be stored. The following table shows the data types we’ll use most often in our projects. Data type Description int (Int) Integer (whole number) float (Float) Number with a decimal point str (String) Set of characters between quotation marks bool (Boolean) True or False Create variables with different data types:


Chapter 1 – Introduction to ESP32 & Micro Python 1. >>> a = 6 2. >>> b = 95.32 3. >>> c = 'Hello World!' 4. >>> d = True


Chapter 1 – Introduction to ESP32 & Micro Python 11 Running Your First Python Programming to NodeMCU ESP32 1. Create a Folder in your drive named Project. Inside the folder create a folder named Blink. 2. When you open Thonny IDE for the first time, the Editor shows an untitled file. Save that file as main.py by clicking on the save icon. Select the “This computer” option. We recommend saving the file inside a folder with a name that identifies the project, for example, Blink. The file must be called main.py. Otherwise, it will not work. 3. Save the Editor should now have a tab called main.py. 4. Copy the following code to the main.py file: from machine import Pin from time import sleep led = Pin(2, Pin.OUT) while True: led.value(not led.value()) sleep(0.5)


Chapter 1 – Introduction to ESP32 & Micro Python 5. Uploading the Script to ESP32. Go to File > Save as… or click on the save icon. The following window will open. 6. Select Micro Python device. 7. Name the file main.py, otherwise, it won’t work. Notice that the window that pops up shows the files currently saved on the board filesystem. There should be a file called boot.py. That file is created by default when you burn Micro Python firmware.


Chapter 1 – Introduction to ESP32 & Micro Python 13 8. Finally, click OK to proceed. 9. Now, your board should be blinking the blue onboard LED every 500 milliseconds. The following figure shows the ESP32 onboard LED light up. 10. Now, press the on-board RST/EN button so that the board restarts and starts running the code. 11. Modify the programming to:


Chapter 1 – Introduction to ESP32 & Micro Python 12. Record your observation.


Chapter 1 – Introduction to ESP32 & Micro Python 15 Programming With ADC and PWM 1. Open Thonny IDE program. 2. Create a new folder name ADC and PWM. Create a new file named main.py 3. Follow the next schematic diagram if you’re using an ESP32: 4. Rewrite the code below, then upload the code. from machine import Pin, ADC, PWM


Chapter 1 – Introduction to ESP32 & Micro Python from time import sleep led = Pin(2, Pin.OUT) button = Pin(0, Pin.IN) #Configure ADC for ESP32 pot = ADC(Pin(34)) pot.width(ADC.WIDTH_10BIT) pot.atten(ADC.ATTN_11DB) led_pwm = PWM(Pin(26),5000) while True: button_state = button.value() led.value(button_state) pot_value = pot.read() led_pwm.duty(pot_value) print(‘Potentiometer value = ‘, pot_value) sleep(0.1) 5. Upload the main.py file to your NodeMCU ESP32. 6. After uploading the code, press the ESP32 on-board EN/RST button to run the new script. 7. Modify your script.


Chapter 1 – Introduction to ESP32 & Micro Python 17 8. Observe the LED and record your two types of observations. Summary In this chapter, we can configure the correct interpreter in Thonny IDE to write programs for NodeMCU ESP32 successfully, save scripts in a Micro Python device beside understanding some basic operations in Micro Python programming. Programming with input/output values for digital and analogue signals, setting up ADC and PWM values which are connected to GPIO along with import libraries necessary. The next chapter will discuss how to read and control digital and analogue values from sensors and control switching and interrupt signals to actuators.


Chapter 2 – Sensors Application and Actuators 18 2 Sensors Application and Actuators Before starting to write some scripts, it is better to understand the general purposed input/output (GPIO) in the NodeMCU ESP32 board. Then easily to configure and input value as the specific signal on that GPIO. The board connects to your PC via a micro-USB cable. This connection provides two ways to interact with the device: as a USB flash drive and as a serial-based Python REPL1 . If you are using Microsoft Windows, you may need to install a driver and configure things for the USB serial to work. 1 A Read-Eval-Print Loop, or REPL, is a computer environment where user inputs are read and evaluated, and then the results are returned to the user. REPLs provide an interactive environment to explore tools available in specific environments or programming languages.


Chapter 2 – Sensors Application and Actuators 19 NodeMCU ESP32 Digital and Analogue Inputs and Outputs Button Switch To get the value of a GPIO, first, you need to create a Pin object and set it as an input. For example: button = Pin(4, Pin.IN) Then, to get its value, you need to use the value() method on the Pin object without passing any argument. For example, to get the state of a Pin object called a button, use the following expression: button.value() To control the GPIO, use the value() method on the Pin object and pass 1 or 0 as the argument. For example, the following command sets a Pin object (led) to HIGH: led.value(1) or led.value(0) ESP32 Touch Sensor The ESP32 has 10 capacitive touch GPIOs. These GPIOs can sense variations in anything that holds an electrical charge, like human skin. So, they can detect variations induced when touching the GPIOs with a finger. These pins can be easily integrated into capacitive pads and replace mechanical buttons. Additionally, the touch pins can also be used as a wake-up source when the ESP32 is in deep sleep. Look at your board pinout to locate the 10 different touch sensors – the touchsensitive pins are highlighted in pink colour. Version with 30 GPIOs ***Touch sensor 1 is GPIO 0. However, it’s not available as a pin in this particular ESP32 development board (version with 30 GPIOs). GPIO 0 is available on the version with 36 pins.


Chapter 2 – Sensors Application and Actuators 20 from machine import Pin, TouchPad() Reading the touch sensor is straightforward. In the Thonny IDE, you use the TouchPad(Pin()) function, which accepts as an argument, the GPIO you want to read. TouchPad(Pin(GPIO)) ESP32 ADC – Read Analog Values Reading an analogue value with the ESP32 means you can measure varying voltage levels between 0 V and 3.3 V. The voltage measured is then assigned to a value between 0 and 4095, in which 0 V corresponds to 0, and 3.3 V corresponds to 4095. Any voltage between 0 V and 3.3 V will be given the corresponding value in between. Voltage level between 0V to 3.3V 0 4092 0V 3.3V ADC is Non-linear. Ideally, you would expect linear behaviour when using the ESP32 ADC pins. However, that doesn’t happen. What you’ll get is behaviour as shown in the following chart: This behaviour means that your ESP32 is not able to distinguish 3.3 V from 3.2 V. You’ll get the same value for both voltages: 4095. The same happens for very low voltage values: for 0 V and 0.1 V you’ll get the same value: 0. You need to keep this in mind when using the ESP32 ADC pins.


Chapter 2 – Sensors Application and Actuators 21 Analog Readings – ESP32 There are several pins on the ESP32 that can act as analogue pins – these are called ADC pins. All the following GPIOs can act as ADC pins: 0, 2, 4, 12, 13, 14, 15, 25, 26, 27, 32, 33, 34, 35, 36, and 39. The ESP32 supports measurements in 18 different channels. Only 15 are available in the DEVKIT V1 DOIT board (version with 30 GPIOs). Grab your ESP32 board pinout and locate the ADC pins. These are highlighted with a red border in the figure below. The atten() method can take the following arguments: ADC.ATTN_0DB — the full range voltage: 1.2V ADC.ATTN_2_5DB — the full range voltage: 1.5V ADC.ATTN_6DB — the full range voltage: 2.0V ADC.ATTN_11DB — the full range voltage: 3.3V In the while loop, read the potentiometer value and save it in the variable. To read the value from the potentiometer, simply use the read() method on the object. When you read data from analogue sensor, you get values from 0 to 4095 – that’s because the ADC pins have a 12-bit resolution by default. You may want to get values in other ranges. You can change the resolution using the width() method as follows:


Chapter 2 – Sensors Application and Actuators 22 ADC.width(bit) The bit argument can be one of the following parameters: ADC.WIDTH_9BIT: range 0 to 511 ADC.WIDTH_10BIT: range 0 to 1023 ADC.WIDTH_11BIT: range 0 to 2047 ADC.WIDTH_12BIT: range 0 to 4095 In summary: 1. To read an analogue value you need to import the ADC class. 2. To create an ADC object simply use ADC(Pin(GPIO)), in which GPIO is the number of the GPIO you want to read the analogue values. 3. To read the analogue value, simply use the read() method on the ADC object. NodeMCU ESP32 Deep Sleep and Wake-Up Sources Having your ESP32 running on active mode with batteries it’s not ideal, since the power from batteries will drain very quickly. Put your ESP32 in deep sleep mode, it will reduce power consumption and the batteries will last longer. Having the ESP32 in deep sleep mode means cutting the activities that consume more power while operating but leaving just enough activity to wake up the processor when something interesting happens. When operating in deep sleep mode, the ESP32 has a current consumption in the μA range. With a custom and carefully designed board, you can get a minimal consumption of only 5 μA. However, if you use a full-feature ESP32 development board with the built-in programmer, onboard LEDs, and so on (like the ESP32 DOIT board) you won’t be able to achieve such a low power state, but you can still save power. Wake Up Sources After putting the ESP32 into deep sleep mode, there are several ways to wake it up: • Timer: waking up the ESP32 after predefined periods of time. • External wake-up: this means the ESP32 can wake up when a change in the state of a pin occurs. • Touch pins: implemented, but not working as expected at the time of writing, so we won’t cover this now. • ULP co-processor to wake up: we haven’t tested this feature yet. Interrupts Signal with NodeMCU ESP32 Interrupts are useful for making things happen automatically in microcontroller programs and can help solve timing problems. With interrupts, you don’t need to constantly check the current pin value. When a change is detected, an event is triggered (a function is called).


Chapter 2 – Sensors Application and Actuators 23 When an interrupt happens, the processor stops the execution of the main program to execute a task, and then gets back to the main program as shown in the figure below: This is especially useful to trigger an action whenever motion is detected or whenever a pushbutton is pressed without the need for constantly checking its state. ESP32 interrupt pins: you can use all GPIOs as interrupts, except GPIO 6 to GPIO 11. Set Up an Interrupt in Micro Python 1. Define an interrupt handling function. The interrupt handling function should be as simple as possible, so the processor gets back to the execution of the main program quickly. The best approach is to signal the main code that the interrupt has happened by using a global variable, for example. The interrupt handling function should accept a parameter of type Pin. This parameter is returned to the callback function, and it refers to the GPIO that caused the interrupt. def handle_interrupt(pin): 2. Set up the GPIO that will act as an interrupt pin as an input. For example: Variable (example pir) = Pin(14, Pin.IN) 3. Attach an interrupt to that pin by calling the irq() method: pir.irq(trigger=Pin.IRQ_RISING, handler=handle_interrupt) The irq() method accepts the following arguments: trigger: this defines the trigger mode. There are 3 different conditions: Pin.IRQ_FALLING: to trigger the interrupt whenever the pin goes from HIGH to LOW. Pin.IRQ_RISING: to trigger the interrupt whenever the pin goes from LOW to HIGH. 3: to trigger the interrupt in both edges (this means when any change is detected) handler: this is a function that will be called when an interrupt is detected, in this case, the handle_interrupt() function.


Chapter 2 – Sensors Application and Actuators 24 Digital Input Output with External Wake Up 1. The ESP32 can also be awakened from sleep when there is a change in the state of a pin. There are two possibilities for external wake-up with the ESP32: ext0 and ext1. 2. The ext0 mode allows you to use one GPIO as a wake-up source. The ext1 mode allows you to set more than one GPIO as a wake-up source at the same time. 3. Only RTC GPIOs can be used as a wake-up source. The RTC GPIOs are highlighted with an orange rectangle box in the next diagram. 4. Wire the circuit by following the next schematic diagram. In this example, we’re using GPIO35 and GPIO36 to wake up the ESP32, but you can use any other RTC GPIO.


Chapter 2 – Sensors Application and Actuators 25 5. Copy the following script shows how ext0 works: it uses one GPIO as an external wake-up source. import esp32 from machine import Pin, deepsleep from time import sleep wake1 = Pin(35, mode = Pin.IN) wake2 = Pin(36, mode = Pin.IN) #Level parameter can be: esp32.WAKEUP_ANY_HIGH or esp32.WAKEUP_ALL_LOW esp32.wake_on_ext1(pins=(wake1,wake2),level=esp32.WAKEUP_ANY_HIGH) #Your main code goes here to perform a task print('I’m awake. Going to sleep in 5 seconds') sleep(5) print('Going to sleep now') deepsleep() 6. Run the program. Before clicking the RUN button, Press EN/RESET button. Make sure that Board Node MCU-32S and select ESP32’s COM port 4. 7. After RUN the script, test your circuit. Press the push button.


Chapter 2 – Sensors Application and Actuators 26 NodeMCU ESP32 Analogue LDR Sensor and Interrupts 1. Open a new sketch, and type the code below: # ESP32 LDR Test & ESP32 Interrupts from machine import Pin, ADC from time import sleep motion = False def handle_interrupt(pin): global motion motion = True global interrupt_pin interrupt_pin = pin led = Pin(26, Pin.OUT) pir = Pin(14, Pin.IN) ldr = ADC(Pin(34)) ldr.atten(ADC.ATTN_11DB) #Full range: 3.3v print("LDR Test") pir.irq(trigger=Pin.IRQ_RISING, handler=handle_interrupt) while True: ldr_value = ldr.read() print(ldr_value) sleep(0.1) if motion: print('Motion detected! Interrupt caused by:', interrupt_pin) led.value(1) sleep(20) led.value(0) print('Motion stopped!') motion = False 2. Now, upload the code to your ESP32 board. Make sure you have the right board and COM port selected. 3. Connect a jumper wire to GPIO 34. You will touch the metal part of this wire so that it senses the touch. 4. In the Thonny IDE window, go to Shell. You’ll see the new values being displayed every second. 5. Cover the sensor connected to GPIO 34 and you’ll see the values decreasing in Shell.


Chapter 2 – Sensors Application and Actuators 27 Read Analog Values from the Potentiometer with NodeMCU ESP32 1. Open a new sketch, and type the code below: # Potentiometer is connected to GPIO 34 from machine import Pin, ADC, PWM from time import sleep pot = ADC(Pin(34)) servo = PWM(Pin(23), freq=50) #Set Frequency: 50Hz pot.atten(ADC.ATTN_11DB) #Full range: 3.3v pot.width(ADC.WIDTH_10BIT) def map(x, in_min, in_max, out_min, out_max): return int((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min) while True: pot_value = pot.read() #print(pot_value) sleep(0.1) angle = pot_value/1023*180 angle = int(angle) print(angle) servo.duty(map(angle, 0, 180, 20, 120)) sleep(0.1) 2. Connect a jumper wire of GPIO34 to the VAR pin at EVAKIT Board. As shown below: 3. Upload the code provided to your ESP32. Make sure you have the right board and COM port selected in the Tools menu. 4. After running the code, rotate the potentiometer and see the values changing. 5. The maximum value you’ll get is 180 and the minimum value is 0. As shown in Shell. Use VAR pin at EVAKIT board.


Chapter 2 – Sensors Application and Actuators 28 Problem Based Learning A photoresistor (or light-dependent resistor, LDR, or photocell) is a light-controlled variable resistor. The resistance of a photoresistor decreases with increasing incident light intensity; in other words, it exhibits photoconductivity. A photoresistor can be applied in light-sensitive detector circuits, and light- and dark-activated switching circuits. Write a code based on your understanding of reading analogue values with ESP32 to solve the problem stated below: 1. Get data from LDR 2. If LDR is in dark condition, light (LED) will be turned On and the servo motor return to 0 degrees. 3. If LDR –gets light from the torchlight, the buzzer will be On and the servo motor turn to 180 degrees. Summary In this chapter, we can read and control digital and analogue signal values from sensors, and control switching and interrupt signals to actuators. Next chapter, we can turn on and turn off LED switches via the web browser. We will study internet communication protocols such as Hypertext Transfer Protocol (http). Use LDR pin at EVAKIT board.


Chapter 3 – NodeMCU ESP32 using HTTP Protocol 29 3 NodeMCU ESP32 using HTTP Protocol Due to the small size of the devices ESP32 upon which Micro Python runs includes capabilities for inter-device communication to do so wirelessly. HTTP Protocol or Hypertext Transfer Protocol is an application-layer protocol for transmitting hypermedia documents, such as HTML. It was designed for communication between web browsers and web servers, but it can also be used for other purposes. In this case, web servers can create or program in the ESP32 so that the web clients can view or monitor all the data sensors from ESP32. What is a Web server and how it works? A web server is a place that stores, processes, and delivers web pages to Web clients. Web Client is nothing but a web browser on our laptops and smartphones. The communication between the client and server takes place using a special protocol called Hypertext Transfer Protocol (HTTP) as shown in Figure 3.1. Figure 3.1: The Communication between Client and Server. In this protocol, a client initiates communication by making a request for a specific web page using HTTP and the server responds with the content of that web page or an error message if unable to do so (like the famous 404 Error). Pages delivered by a server are mostly HTML documents. NodeMCU ESP32 Operating Modes One of the greatest features ESP32 provides is that it cannot only connect to an existing Wi-Fi network and act as a Web Server, but it can also set up a network of its own, allowing other devices to connect directly to it and access web pages. This is possible because ESP32 can operate in three different modes: Station mode, Soft Access Point mode, and both at the same time. This provides the possibility of building mesh networks.


Chapter 3 – NodeMCU ESP32 using HTTP Protocol 30 Station (STA) Mode The ESP32 that connects to an existing Wi-Fi network (one created by your wireless router) is called Station (STA). Figure 3.2: ESP32 as a Web Server connected to existing Wi-Fi. In STA mode ESP32 gets IP from the wireless router to which it is connected. With this IP address, it can set up a web server and deliver web pages to all connected devices under the existing Wi-Fi network. Soft Access Point (AP) Mode The ESP32 that creates its own Wi-Fi network and acts as a hub (just like a Wi-Fi router) for one or more stations is called Access Point (AP). Unlike Wi-Fi routers, it does not have an interface to a wired network. So, such a mode of operation is called Soft Access Point (soft-AP). Also, the maximum number of stations that can connect to it is limited to five.


Chapter 3 – NodeMCU ESP32 using HTTP Protocol 31 Figure 3.3: ESP32 as a router In AP mode ESP32 creates a new Wi-Fi network and sets SSID (Name of the network) and IP address to it. With this IP address, it can deliver web pages to all connected devices under its own network. Further reading is available at: https://lastminuteengineers.com/creating-esp32-web-serverarduino-ide/ Wi-Fi Manager with NodeMCU ESP32 Wi-Fi Manager allows you to connect your ESP32 to different Access Points (different networks) without having to hard-code your credentials and upload new code to your board. How Wi-Fi Manager Works With the Wi-Fi Manager, you no longer have to hardcode your network credentials (SSID and password). The ESP32 will set up an Access Point that you can use to configure the network credentials, or it will automatically join a known saved network. Here’s how the process works:


Chapter 3 – NodeMCU ESP32 using HTTP Protocol 32 • When the ESP32 boots for the first time, it’s set as an Access Point. • You can connect to that Access Point by establishing a connection with the WiFiManager network and going to the IP address 192.164.4.1 • A web page opens that allows you to choose and configure a network. • The ESP32 saves those network credentials so that later it can connect to that network (Station mode). • Once a new SSID and password are set, the ESP32 reboots is set to Station mode, and tries to connect to the previously saved network. • If it establishes a connection, the process is completed successfully. Otherwise, it will be set up as an Access Point for you to configure new network credentials. To set up the Wi-Fi Manager on the ESP32 using Micro Python, we’ll use the WiFiManager library by tayfunulu. On the library GitHub page, you can find the following diagram that shows an overview of how everything works. Wi-Fi Scan 1. Connect ESP32 to the USB port. 2. Type new codding to make ESP32 scan all the Wi-Fi available around. import network wireless = network.WLAN(network.STA_IF) wireless.active(True) network = wireless.scan() print(network) 3. Record your observation.


Chapter 3 – NodeMCU ESP32 using HTTP Protocol 33 Controlling LED Using Web Server A. The Circuit Connect two LEDs to the ESP32 as shown in the following schematic diagram – one LED connected to GPIO 25, and the other to GPIO 26. Figure 3.4: LEDs connected to the ESP32 B. The source codes import machine import usocket as socket import time import network timeout = 0 #WiFi Connection Timeout variable wifi = network.WLAN(network.STA_IF) # Restarting WiFi wifi.active(False) time.sleep(0.5) wifi.active(True) wifi.connect(YOUR_SSID','YOUR_PASSWORD') if not wifi.isconnected(): print('connecting...') while (not wifi.isconnected() and timeout <5): print(5 - timeout) timeout = timeout + 1 time.sleep(1) if(wifi.isconnected()): print('WiFi Connected.')


Chapter 3 – NodeMCU ESP32 using HTTP Protocol 34 print('network config:', wifi.ifconfig()) def web_page(): if LED25.value() == 1: gpio25_state = 'ON' if LED25.value() == 0: gpio25_state = 'OFF' if LED26.value() == 1: gpio26_state = 'ON' if LED26.value() == 0: gpio26_state = 'OFF' # HTML Document html="""<!DOCTYPE html> <hmtl> <center><h2>ESP32 Webserver </h2></center> <form> <center> <p>LED25 State: <strong> """ + gpio25_state + """</strong></p> <h3> LED25 </h3> <button name="LED25" value='ON' type='submit'> ON </button> <button name="LED25" value='OFF' type='submit'> OFF </button> <p>LED26 State: <strong> """ + gpio26_state + """</strong></p> <h3> LED26 </h3> <button name="LED26" value='ON' type='submit'> ON </button> <button name="LED26" value='OFF' type='submit'> OFF </button> </center></form> """ return html # Output Pin Declaration LED25 = machine.Pin(25,machine.Pin.OUT) LED25.value(0) LED26 = machine.Pin(26,machine.Pin.OUT) LED26.value(0) # Initialising Socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # AF_INET - Internet SOcket, SOCK_STREAM - TCP Protocol Host = '' # Empty means, it will allow all IP address to connect Port = 80 # HTTP Port s.bind((Host, Port)) # Host, Port s.listen(5) # It will handle maximum 5 clients at a time #main loop while True: connection_socket, address=s.accept() #Storing Conn_socket & address of new client connected print("Got a connection from ", address)


Chapter 3 – NodeMCU ESP32 using HTTP Protocol 35 request=connection_socket.recv(1024) # Storing Response coming from client print("Content ", request) # Printing Response request=str(request) # Convert Bytes to String # Comparing & Finding the Position of the word in String LED25_ON = request.find('/?LED25=ON') LED25_OFF = request.find('/?LED25=OFF') LED26_ON = request.find('/?LED26=ON') LED26_OFF = request.find('/?LED26=OFF') if(LED25_ON==6): LED25.value(1) if(LED25_OFF==6): LED25.value(0) if(LED26_ON==6): LED26.value(1) if(LED26_OFF==6): LED26.value(0) # Sending HTML document in response every time to all connected clients response= web_page() connection_socket.send('HTTP/1.1 200 OK\n') connection_socket.send('Content-Type: text/html\n') connection_socket.send('Connection: Close\n\n') connection_socket.sendall(response) # Closing the socket connection_socket.close() C. Running the Code Now, RUN the code and the web server will work straight away. Follow the next steps to RUN code in the ESP32: 1. Plug your ESP32 board into your computer. 2. In the Thonny IDE select your board in Run > Run the current script (F5) (in our case we’re using the ESP32 DEVKIT DOIT board). 3. Wait for the “Wi-Fi Connected” message. D. Finding the ESP32 IP Address After running the script, investigate Shell and find the IP address printed.


Chapter 3 – NodeMCU ESP32 using HTTP Protocol 36 Figure 3.5: Shell shows the EPS32’s IP address. The ESP32 connects to Wi-Fi and outputs the ESP IP address on the Shell. Copy that IP address, because you need it to access the ESP32 web server. E. Accessing the Web Server To access the web server, open your browser, type the ESP32 IP address displayed in Shell, and you’ll see the following page. In our case, it is 10.96.20.77 (example). Figure 3.6: Browser displays the web server in ESP32 as programmed. If you look at the Shell, you can see what’s happening in the background. The ESP receives the HTTP request from a new client (in this case, your browser).


Chapter 3 – NodeMCU ESP32 using HTTP Protocol 37 Figure 3.7: Other information about the HTTP request. F. Testing the Web Server Now you can test if your web server is working properly. Click the buttons to control the LEDs. Figure 3.8: Browser display two LEDs to control. At the same time, you can look at the Shell to see what’s going on in the background. For example, when you click the button to turn GPIO 26 ON, ESP32 receives a request on the /26/on URL.


Chapter 3 – NodeMCU ESP32 using HTTP Protocol 38 Figure 3.9: Other information about the HTTP request when the button has been pressed. When the ESP32 receives that request, it turns the LED attached to GPIO 26 ON and updates its state on the web page. Figure 3.10: Button ON has been pressed. The button for GPIO 25 works in a similar way. Test that it is working properly. G. Testing the Web Server with More Than One LED as OUTPUT 1. Write a new script to turn on and off 2 LEDs. 2. Record your result.


Chapter 3 – NodeMCU ESP32 using HTTP Protocol 39 Reading Sensor Via Web Server In this topic, DHT sensors have been taken as an example. The DHT11 and DHT22 sensors are used to measure temperature and relative humidity. These are very popular among makers and electronics hobbyists. Figure 3.11: DHT22 and DHT11 These sensors contain a chip that does analogue to digital conversion and spits out a digital signal with the temperature and humidity. This makes them very easy to use with any microcontroller. DHT11 vs DHT22 The DHT11 and DHT22 are very similar but differ in their specifications. The following table compares some of the most important specifications of the DHT11 and DHT22 temperature and humidity sensors. For a more in-depth analysis of these sensors, please check the sensors’ datasheet. DHT 11 DHT 22 Temperature range 0 to 50 ºC +/-2 ºC -40 to 80 ºC +/-0.5ºC Humidity range 20 to 90% +/-5% 0 to 100% +/-2% Resolution Humidity: 1% Temperature: 1ºC Humidity: 0.1% Temperature: 0.1ºC Operating voltage 3 – 5.5 V DC 3 – 6 V DC Current supply 0.5 – 2.5 mA 1 – 1.5 mA Sampling period 1 second 2 seconds Price $1 to $5 $4 to $10 The DHT22 sensor has a better resolution and a wider temperature and humidity measurement range. However, it is a bit more expensive, and you can only request readings with 2 seconds interval. The DHT11 has a smaller range and it’s less accurate. However, you can request sensor readings every second. It’s also a bit cheaper.


Chapter 3 – NodeMCU ESP32 using HTTP Protocol 40 Despite their differences, they work in a similar way, and you can use the same code to read temperature and humidity. You just need to select in the code the sensor types you’re using. A. The circuit Before proceeding to the web server, you need to wire the DHT11 or DHT22 sensor to the ESP32 as shown in the following schematic diagram. In this case, we’re connecting the data pin to GPIO 14, but you can connect it to any other digital pin. You can use this schematic diagram for both the DHT11 and DHT22 sensors. Figure 3.12: DHT22 connection to ESP32, GPIO 14. You need to install a couple of libraries for this project. There is a dht module that comes with the Micro Python firmware by default. So, it is easy to get temperature and humidity. B. The source scripts Open your Thonny IDE and copy the following scripts. from machine import Pin from time import sleep import dht sensor = dht.DHT22(Pin(14)) #sensor = dht.DHT11(Pin(14)) while True:


Chapter 3 – NodeMCU ESP32 using HTTP Protocol 41 try: sleep(2) sensor.measure() temp = sensor.temperature() hum = sensor.humidity() temp_f = temp * (9/5) + 32.0 print('Temperature: %3.1f C' %temp) print('Temperature: %3.1f F' %temp_f) print('Humidity: %3.1f %%' %hum) except OSError as e: print('Failed to read sensor.') C. Running the scripts Now, RUN the scripts to your ESP32. Make sure you have the right board and COM port selected. After running, observe the Shell. Figure 3.13: Value from sensor DHT22 D. Web server demonstration Open a browser and type the ESP32 IP address. Your web server should display the latest sensor readings. Open your Thonny IDE, create a new file and copy the following scripts. try: import usocket as socket except: import socket import network from machine import Pin


Chapter 3 – NodeMCU ESP32 using HTTP Protocol 42 import dht import esp esp.osdebug(None) import gc gc.collect() led = Pin(2,Pin.OUT) station = network.WLAN(network.STA_IF) station.active(True) station.connect('REPLACE_WITH_YOUR_SSID','REPLACE_WITH_YOUR_PASSWORD ') while station.isconnected() == False: pass print('Connection successful') print(station.ifconfig()) led.value(1) sensor = dht.DHT22(Pin(14)) #sensor = dht.DHT11(Pin(14)) def read_sensor(): global temp, hum temp = hum = 0 try: sensor.measure() temp = sensor.temperature() hum = sensor.humidity() if (isinstance(temp, float) and isinstance(hum, float)) or (isinstance(temp, int) and isinstance(hum, int)): msg = (b'{0:3.1f},{1:3.1f}'.format(temp, hum)) # uncomment for Fahrenheit #temp = temp * (9/5) + 32.0 hum = round(hum, 2) return(msg) else: return('Invalid sensor readings.') except OSError as e: return('Failed to read sensor.') def web_page(): html = """<!DOCTYPE HTML><html> <head> <meta name="viewport" content="width=device-width, initialscale=1"> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css"


Chapter 3 – NodeMCU ESP32 using HTTP Protocol 43 integrity="sha384- fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous"> <style> html { font-family: Arial; display: inline-block; margin: 0px auto; text-align: center; } h2 { font-size: 3.0rem; } p { font-size: 3.0rem; } .units { font-size: 1.2rem; } .dht-labels{ font-size: 1.5rem; vertical-align:middle; padding-bottom: 15px; } </style> </head> <body> <h2>ESP DHT22 Server</h2> <p> <i class="fas fa-thermometer-half" style="color:#059e8a;"></i> <span class="dht-labels">Temperature</span> <span>"""+str(temp)+"""</span> <sup class="units">&deg;C</sup> </p> <p> <i class="fas fa-tint" style="color:#00add6;"></i> <span class="dht-labels">Humidity</span> <span>"""+str(hum)+"""</span> <sup class="units">%</sup> </p> </body> </html>""" return html s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(('', 80)) s.listen(5) while True: conn, addr = s.accept() print('Got a connection from %s' % str(addr)) request = conn.recv(1024) print('Content = %s' % str(request)) sensor_readings = read_sensor() print(sensor_readings) response = web_page() conn.send('HTTP/1.1 200 OK\n') conn.send('Content-Type: text/html\n') conn.send('Connection: close\n\n') conn.sendall(response) conn.close()


Chapter 3 – NodeMCU ESP32 using HTTP Protocol 44 Notice that the temperature and humidity readings are updated automatically without the need to refresh the web page. (a) (b) Figure 3.14(a): Web page display from ESP32 after coding Figure 3.14(b): ESP32 with DHT22 sensor connected. Web Server with Wi-Fi Manager A. Wi-Fi Manager Scripts 1. Open your Thonny IDE, create a new file and copy the following scripts. import network import socket import ure import time ap_ssid = "WifiManager" ap_password = "tayfunulu" ap_authmode = 3 # WPA2 NETWORK_PROFILES = 'wifi.dat' wlan_ap = network.WLAN(network.AP_IF) wlan_sta = network.WLAN(network.STA_IF)


Click to View FlipBook Version