0% found this document useful (0 votes)
32 views

micropython-programming-with-esp32-and-esp8266_compress

This eBook serves as a practical guide for programming the ESP32 and ESP8266 using MicroPython, covering installation, basic programming concepts, and various projects. The authors, Rui and Sara Santos, emphasize the educational purpose of the content while disclaiming liability for errors. The course includes community support through a private Facebook group and provides resources for obtaining necessary components.

Uploaded by

kxrc7d82b
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
32 views

micropython-programming-with-esp32-and-esp8266_compress

This eBook serves as a practical guide for programming the ESP32 and ESP8266 using MicroPython, covering installation, basic programming concepts, and various projects. The authors, Rui and Sara Santos, emphasize the educational purpose of the content while disclaiming liability for errors. The course includes community support through a private Facebook group and provides resources for obtaining necessary components.

Uploaded by

kxrc7d82b
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 359
[eX ee eae id faci Yaelo eA MicroPython SoM elma leks 3 : S 8 . ‘ a a Ny N : . a sled and Sara Santos Case eze Version 1.2 Disclaimer This eBook was written for information purposes only. Every effort was made to make this eBook as complete and accurate as possible. The purpose of this eBook is to educate, The authors (Rui Santos and Sara Santos) do not warrant that the information contained in this eBook is fully complete and shall not be responsible for any errors or omissions. The authors (Rui Santos and Sara Santos) shall have neither liability nor responsibility to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by this eBook. Throughout this eBook you will find some links and some of them are affiliate links. This means the authors (Rui Santos and Sara Santos) earn a small commission from each purchase with that link. Please understand that the authors have experience with all those products, and we recommend them because they are useful, not because of the small commissions we made if you decide to buy something. Please do not spend any money on these products unless you feel you need them. Other Helpful Links: + Ask questions in our Forum Join Private Facehook Group * Terms and Conditions Pelee aca sy Join the Private Facebook Group This course comes with an opportunity to join a private community of like-minded people. If you purchased this course, you can join our private Facebook Group today! Inside the group, you can ask questions and create discussions about everything related to ESP32, ESP8266, Arduino, Raspberry Pi, BeagleBone, etc. See it for yourself! + Step #1: Go to > http:/frandomnerdtutorials.com/fb © Step #2: Click “join Group" button ‘* Step #3: We'll approve your request within less than 24 hours. als Communty Random Nerd Tutorials*Community Discussion Members Events Photos Fes a vite post [J Aa pnotoivieo EJ Ask auostion GE) Aaa Fi cnour nro About the Authors This course was developed and written by Rui Santos and Sara Santos. We both live in Porto, Portugal, and we know each other since 2009. If you want to learn more about us, feel free to read our about page. Hi! 'm Rui Santos, the founder of the Random Nerd Tutorials blog. | have a master's degree in Electrical and Computer Engineering from FEUP. 'm the author of “BeagleBone For Dummies", Jand Technical reviewer of the book “Raspberry Pi For Kids For Dummies”. I recently wrote a book with Sara Santos for the NoStarchPress publisher about projects with the Raspberry Pi: “20 Easy Raspberry Pi Projects: Toys, Tools, Gadgets, and More!” Hit ''m Sara Santos! | started working at Random Nerd Tutorials back in 2015 as a hobby: 1 helped Rui with some simple tasks when he had a lot of work to do. Back then, ! knew nothing labout electronics, programming, Arduino, etc... Over time, I started learning everything | could Jabout those subjects and | just loved it! At first, I helped Rui once a week on Saturdays, but then, / started working on the RNT blog alongside him, almost every day. Currently, | work full time lat Random Nerd Tutorials as a Content Editor and | love what | do! Pelee aca sy Table of Contents MODULE 0 Course Intro. Welcome to MicroPython Programming with ESP32 and ESP8266.. MODULE 1 Getting Started with MicroPython on ESP32 and ESP8266 .. Introducing MicroPython. Installing uPyCraft IDE (Windows) Install uPyCraft IDE (Mac OS X).. Install uPyCraft IDE (Linux Ubuntu). Flashing MicroPython Firmware to ESP32/ESP8266 .. Getting Started with uPyCraft IDE Introducing the ESP32 Board.. Introducing the ESP8266 Board MODULE 2 Python/MicroPython Programming Basics.. MicroPython Programming Basic: MODULE 3 Interacting with GPIOs.. Blinking an LED.... Digital Inputs and Digital Outputs. Analog Inputs... PWM ~ Pulse Width Modulation... ESP32 Touch Pins... Interrupts... Timers, 129 Deep Sleep with Timer Wake Up... 1135 MODULE 4 Web Servers and HTTP Clients... Web Server Introduction .. “Hello, World!” Web Server.. 152 163 Web Server - Control Outputs... Web Server with Slider Switch ... 171 Web Server - Display Temperature and Humidity Readings .. 179 Send Sensor Readings via Email (IFTTT) 192 Getting Data Using an API... 212 MODULE 5 MQTT Protocol 221 Introducing MQTT.. 227 Installing Mosquito MQTT Broker on a Raspberry Pi MQTT - Establishing a Two-way Communication... .231 Installing Node-RED and Node-RED Dashboard on a Raspberry Fi. 246 MQTT - Connect ESP32/ESP8266 to Node-RED ... 253 MODULE 6 Other components: OLED, DC Motor, Addressable RGB LED Strip. 274 OLED Display. 275 Control a DC Motor 285 Control an Addressable RGB LED Strip. .299 MODULE 7 Thonny IDE... 316 Thonny IDE .317 Flashing MicroPython Firmware (esptool.py)... Installing Thonny IDE .. Thonny IDE Overview. Troubleshooting Tips for Thonny IDE Appendix: List of Parts Required Other RNT Courses/eBooks MODULE 0 Course Intro Welcome to MicroPython Programming with ESP32 and ESP8266 Welcome to “MicroPython Programming with ESP32 and ESP8266". This is a practical course where you'll learn how to program the ESP32 and ESP8266 using MicroPython firmware. MicroPython is a reimplementation of Python 3 programming language targeted for microcontrollers and embedded systems. The content provided throughout this eBook is compatible with both the CSP32 and ESP8266 boards. How to Follow this Course? If you've never programmed the ESP32/ESP8266 using MicroPython firmware, we recommend following the eBook in order. We start with the most basic and simple concepts, and then welll introduce new concepts along the way. Ifyou already have some experience programming with MicroPython you can skip the most basic sections and go straight to the Modules you find more interesting. Download Source Code and Resources Each section contains the source code, schematics, and all the resources you need to follow the projects and tutorials. You can download each resource by clicking the “Source Code” boxes that appear on each project, or you can download the MicroPython Programming with ESP32/ESP8266 GitHub repository and instantly download all the resources for this course. Getting Parts for the Course To follow this course, you need some electronics components. In each section, we provide a complete list of the needed parts and links to Maker Advisor, so that you can find the part you're looking for on your favorite store at the best price. MAKER ADVISOR If you buy your parts through Maker Advisor links, we'll earn a small affiliate ‘commission (you won't pay more for it). By getting your parts through our affiliate links you are supporting our work. If there's a component or tool you're looking for, we advise you to take a look at our favorite tools and parts here. Note: for the full parts list, consult the Appendix here. Leave Feedback Your feedback is very important so that we can improve the course and our learning materials. Suggestions, rectifications, and your opinion is very important for us. You can use the following channels to leave feedback: « Facebook group RNT Lab Forum MODULE 1 Getting) Started with MicroPython on ESP32\and ESP8266 Introducing MicroPython GETTING STARTED In this section you'll get started with MicroPython firmware on the ESP32 and ESP8266. We'll introduce you to MicroPython, show you the differences between MicroPython and regular Python, and how to program your ESP based boards with MicroPython using uPyCraft IDE. After completing this section, you'll have your first LED blinking using MicroPython. What is MicroPython? MicroPython is a re-implementation of Python 3 programming language targeted for microcontrollers and embedded systems. MicroPython is very similar to regular Python. Therefore, if you already know how to program in Python, you also know how to program in MicroPython. 13 Python vs MicroPython Apart from a few exceptions, the language features of Python are also available in MicroPython. The biggest difference between Python and MicroPython Is that MicroPython was designed to work under constrained conditions. @ python "Vs iT MicroPython Because of that, MicroPython does not come with the full standard library. It 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 ESP32 and ESP8266 include modules to support network connections. Why MicroPython? Python is one of the most widely used, simple and easy-to-learn programming languages around. So, the emergence of MicroPython makes it extremely easy and simple to program digital electronics. If you've never programmed digital electronics before, MicroPython is a good way to start. MicroPython's goal is to make programming digital electronics as simple as possible, so that anyone can use it. Currently, MicroPython is used by hobbyists, researchers, teachers, educators, and even in commercial products. The code for blinking an LED on an ESP32 or ESP8266 is as simple as follows: from machine import Pin from time import sleep led = Pin(2, Pin.OUT) while True: led.value(not led-value()) sleep (0.5) MicroPython - Boards support MicroPython runs on many different devices and boards, such as: + PyBoard + Micro:Bit © Teensy 3X © WiPy - Pycom © Adafruit Circuit Playground Express © Other ESP32/ESP8266 based boards Click the following links for more information about other boards that support MicroPython: © Boards running MicroPython - MicroPython Forum * Boards summary - MicroPython GitHub Throughout this eBook, we'll use MicroPython with the ESP32 and ESP8266 boards. Note: if this is your first time with the ESP32 or ESP8266, we have a quick getting started guide for these boards after the MicroPython introduction. ESP32 and ESP8266 boards are similar, and you wor't feel almost any difference programming those using MicroPython. This means that anything you write for the ESP32 should also run with no changes or minimal changes on the ESP8266 (mainly changing the pin assignment). Limitations ESP32 is the successor of the ESP8266. So, currently, not all features are available in MicroPython to take the most out of the ESP32 - it's still an ongoing project. However, it’s very usable and you can make all the presented projects with it. uPyCraft IDE There are different programs you can use to program the ESP32 and ESP8266 boards. To program the ESP32 or ESP8266 boards with MicroPython firmware, we recommend using uPyCraft IDE because it is simple, intuitive to use and works great with the ESP boards. However, if you are used to other MicroPython programming environment, you can stick with it. uPyCraft IDE runs in any major operating system. We have instructions for installing uPyCraft IDE in Window, Mac OS X and Linux Ubuntu. Install uPyCraft IDE in your computer by following one the installation procedures we describe in the following Units. Alternative - Thonny IDE We've experimented with several IDEs to program the ESP32 and ESP8266 boards using MicroPython, and uPyCraft IDE worked fine for us. However, some users. reported some trouble using uPyCraft IDE. So, we provide an alternative IDE to program your ESP32/ESP8266: Thonny IDE. Note: we have a dedicated section to Thonny IDE that shows how to install and use it with the ESP32/ESP8266. Read “Getting Started with Thonny IDE’ The main differences between uPyCraft IDE and Thonny IDE: uPyCraft is an IDE for MicroPython, whereas Thonny is an IDE for Python with MicroPython support; uPyCraft IDE prints all the debug information in the debugging window which can be confusing. Thonny IDE has a layer on top that doesn’t show all those errors, so it’s a bit more user friendly; uPyCraft IDE has more features dedicated to program ESP8266/ESP32 with MicroPython, but it's harder to install on some Linux computers. Thonny IDE is easier to install and compatible with all operating systems; uPyCraft provides a better way to edit and delete files saved on your boards. On the other hang, Thonny IDE has a builtin feature to upload files as boot py or main.py. That feature makes it easier and faster to upload new scripts. 5, a t=) a B es oS @ o Installing uPyCraft IDE (Windows) uPyCraft IDE This section shows how to install uPyCraft IDE in your Windows PC. The IDE is a software that contains tools to make the process of development, debugging and upload code easier. Installing Python 3.7.x Before installing uPyCraft IDE, make sure you have the latest version of Python 3.7.X installed in your computer. Follow the next installation instructions: 1. Go Lo the Python Downloads page: www.python.urg/duwnluads and dewnlvad the installation file. 2. After a few seconds, you should have a file called python-3.7.X.exe file in your computer. Double-click the file to open it. Poe TRE Donne PIRI ewe teow tanase 26s» & cs om * pete ts ‘older aaron” Sete 99 Pine ei K ef Bete Bee seca Bee Rename 4H» MHP > Locale) > Users > rsantes > Downloade ti # Qvickeccen ma ware Bawonjcte i desttop £B Documents & Donroads D mesic Pitre vies teat ak 3. Enable the option at the bottom “Add Python 3.7 to PATH". Then, press the “Install Now" button: [& Python 3.70 G26 Setup = x Install Python 3.7.0 (32-bit) Select Install Now to install Python with default settings. or choose Customize to enable or disable features. @ Install Now CAUsers\uisantos\AppData\LocahPrograms\Python\Python37-32, Includes IDLE, pip and documentation Creates shorts and file ascocitions > Customize installation Choose location and features th py © instal launcher forall users (recommended) windows oa 4, Wait a few seconds while the software completes the installation process. D Python 3.7.0 (22-bit) Setup = x | Setup Progress Installing: Python 3.7.0 Test Suite (32. Cancel 5. When it's done, you should see the message "Setup was successful” and you can close that window. |S> Python 3.7.0 (22-bit) Setup Fe x Setup was successful ‘Special thanks to Mark Hammond, without whose years of freely shared Windows expertise, Python for Windows would stil be Python for DOS. New to Python? Start with the online tutorial and documentation, See what's new in this release. Installing uPyCraft IDE - Windows PC As mentioned before, throughout this book welll be using uPyCraft IDE to program the ESP32 or ESP8266 boards using the MicroPython firmware. In our opinion, right now uPyCraft IDE is the easiest way of programming ESP based boards with MicroPython. You can learn more about uPyCraft IDE on their GitHub repository or explore the uPyCraft IDE source code. Downloading uPyCraft IDE for Windows Click here to download uPyCraft IDE for Windows or go to the following link: * https://randomnerdtutorials.com/uPyCraftWindows After a few seconds you should see a similar file (uPyCraft VX.exe) in your Downloads folder: vio esbalonteel| Dowland PIRI tore sine ven teoge @Olr.. & Bx com hae Be Deete Rename New 1 Ty New ite = oven EB Elessacess © he Tae + Downloade » H Chicksccee merc Ewch Boob; Wi destop $ Doves D misie Biden 1% tocatonn (cp Note: uPyCraft IDE is an open-source project and all the source code is available ‘online. Most antivirus won't allow you to download .exe files from the internet, and they will flag the uPyCraft file as suspicious. Make sure you tell your antivirus that uPyCraft IDE is not virus in order to be able to runit. Double-click the downloaded file. A new window opens with the uPyCraft IDE software; We'll be using this software to flash the ESP based boards with MicroPython firmware as well as to program the boards. After installing the uPyCraft software, follow the “Flashing MicroPython Firmware to ESP32/ESP8266" Unit: Note: if you have trouble using uPyCraft IDE, you can try using Thonny IDE instea Just follow th Install uPyCraft IDE (Mac OS X) uPyCraft IDE Say+tevaea This section shows how to install uPyCraft IDE in your Mac OS X computer. The IDE is @ software that contains tools to make the process of development, debugging and upload code easier. Installing Python 3.X - Mac OS X Before installing the uPyCraft IDE, make sure you have the latest version of Python 3.x installed in your computer. If you don't, follow the next instructions to install Python 3.X with the brew command. Open a terminal window and type the next command: § brew install python3 Wait a few seconds while the installation procedure is completed coe Rui —-bash— 75x19 on ake ny Test eee treet rs my Core m ena emit cra feats mene ring @ a c at sk eee TT 4 cena} os eee) a fae Cac eaee aa ry a oer and aera POC Cass ral Poe eieiag ar Piet cka nape reed 2 Gael) faa) pe d Allowing Apps Downloaded to Run Since uPyCraft IDE is open source and downloaded from the Internet, it's not a verified app in the App Store. For security reasons, Mac OS X blocks unknown apps to run on your computer. Follow these next instructions to enable any downloaded software to run in your Mac. 1. Open the “System Preferences...” menu. RG a = ad eee MU Ted Ee ee aut lea dae So) cine Beara eta a [Role Re ra. VRS cel ee 2. Open the “Security & Privacy” menu. ee ‘Syatom Preferences onal co ess ae wots i eae < O a S& MY wa #8 conan ine egy Kor me Tans wa Sd *@x @ 6 w eee eee econ as 3. Atthe bottom left corner, click the lock icon to modify your "Security & Privacy’ settings: ee < ‘Security & Privacy Flevauk Frewall_ Privacy Alogin password has been set for this user _ Change Password... Require password Sminutes [9 after sleep or screen saver begins Show @ message when the screen is locked 4) Disable automatic login tha lock to make changes. Pelee aca sy

You might also like