# (498) Arduino IDE or ESPhome for our Projects?

## Метаданные

- **Канал:** Andreas Spiess
- **YouTube:** https://www.youtube.com/watch?v=zD2ByKUZHcE

## Содержание

### [0:00](https://www.youtube.com/watch?v=zD2ByKUZHcE) Segment 1 (00:00 - 05:00)

using Arduino IDE or ESP home for my future projects is the question I know how to program in C++ and initially I was rejected by yaml the programming language of ESP home because it looked very strange it took me some time to understand the difference time you can save if you stick with me to the end one thing I can promise it is a lot of fun if you get the hang of it gry YouTubers here is the guy with a Swiss accent bringing you a new episode with fresh ideas around sensors and microcontrollers remember if you subscribe you'll always sit in the first row this video is for people familiar with the Arduino IDE esp32 and C++ it is not an introduction to how to use ESP home you find any other videos on this topic what you will find is beyond copying an example into a yaml file therefore in this video we will understand the fundamental difference between C++ and yaml understand how ESP homes yaml Works look at the basics of yaml with a focus on ESP home learn the most important components create a few examples to compare the effort between an Arduino and an ESP Home Project go through the advantages and disadvantages of ESP home versus Arduino IDE and finally you will get my verdict let's start with a fundamental differences a typical Arduino project has a setup and a loop section it starts from the top and proceeds statement after statement to the end where it Loops back e to understand and follow because there is no concurrency things do not happen in parallel an extension to this programming style was the tasks introduced with free OS in video number 381 in reality there still were one or two cores but tasks looked like they ran in parallel this was a big help if you wanted several things to run independently from each other and in parallel the realtime OS took care of the task scheduling ESP home is much more similar to OS than to the one statement of the other of C++ that is the most important learning I had to make by the way ESP home still creates a platform IO project with standard C++ code just not visible to the ordinary user we will later have a look at this code and I promise you will be surprised so let's look at a simple yaml file to understand what it means we start with some definitions like the name and the board is part of the yaml code and so is the framework one word about the framework we still can use Arduino but I suggest using espb IDF because expressive better supports it and frankly you do not have to bother because ESP home covers all the details next come lines like Locker API or OTA no setup Loop or main what do they do Locker writes locking messages either to serial or via Wi-Fi you decide where you want to see it later when the project runs API integrates all the buttons switches and displays into home assist OTA makes sure you can update firmware over the air you see these tasks have to run in parallel and all of them contain a setup and a loop part and they are independent of each other to show you what I mean I can shift this part to the end of the yaml file and everything works the same sequence is not important on this level in yaml the faster you learn this part the easier it is for you we will later see how we can introduce programming logic as with python indention is essential I'm pretty sure you will learn this the hard way as I did Wi-Fi AP and web server behave precisely the same way they provide functionality to our project because these components are the same for most projects ESP home inserts them automatically when you create a new project I changed these

### [5:00](https://www.youtube.com/watch?v=zD2ByKUZHcE&t=300s) Segment 2 (05:00 - 10:00)

definitions a bit to fit my needs and insert them in the beginning of my projects reading through this initial file we can already learn two basic yaml concepts the key value Pairs and objects most lines are key value pairs board ESP def or type ESP idfr examples sometimes a key is sufficient like here Wi-Fi has a few more lines that obviously belong to it they have to be written below and are indented by two spaces together they form an object also called a component in ESP home the same goes for AP where we Define the SSID and the password of the captive portal opened on 192 16841 if something goes wrong you see the fun functionality of Wi-Fi manager in one object cool we can now save and install this code on one of our esp32 boards what do you think will happen first ESP home accepts the configuration compiles a lot of code and uploads it wirelessly then it lcks everything again wirelessly finally home assistant discovers our new device all done with these few lines of yaml code can you imagine how much time this would have taken in the Arduino IDE and there was no testing needed it just runs a big timesaver if you want a Wi-Fi connected board that can be managed remotely we could add more functionality a remote reset switch a switch to avoid deep sleep for firmware upgrades and a factory reset button to delete all private information like SS IDs and passwords if you want to give your project away or sell it because we want to trigger the latter three functions from home assistant user interface we add a button object and three functionalities and here we see the next important yaml feature lists because we have three of the same we list them and add a hyphen the platform contains all the needed functionality including if the button is pressed if we install this code we get three buttons in the user interface everything with just a few lines of code enough infrastructure let's build a sensor a bme280 pressure temperature and humidity sensor is a typical example if you go to the ESP home documentation you see many pre-built components I search for the needed example file and copy the sensor definition into my yam file where I'm sure you know the answer by now it does not matter as good practice I add an ID to each measurement as a reference for future use I get red squiggles below the text not good because they mean trouble why is this as you know the bme280 uses the I Square C pass because I later plan to use another I Square C device I use one of these handy pcbs to connect it to the standard pins 21 and 22 now I have to tell ESP home about that fact as soon as I add the I Square C object the squiggles are gone watch this line scan true we will need it later after installing the new code we see the three sensor values in home assistant but unfortunately with un known values looking at the documentation we see that the BME 280 chip comes with different addresses fortunately we entered the scan line now we wirelessly can connect to the log and see that its address is 76 not the 77 default value if we add this info the sensor Works Cool a free of charge I Square C scanner is also built in very handy now we want some output LEDs are always a good example so I add two of these pre-manufactured ones they are very handy for experiments because they have a built-in resistor for 3. 3 or 5vt again I have to tell ESP home where I connect them output is the correct component for that purpose we add a list of GPI o pin definitions with a dash in

### [10:00](https://www.youtube.com/watch?v=zD2ByKUZHcE&t=600s) Segment 3 (10:00 - 15:00)

front after the upload there is no change in the home assistant display what is lacking we have to add some functionality switch seems to be the correct component again we copy the example and adapt it to our needs because we already defined our LEDs we can reference them now we get the switches and can toggle the LEDs from home assistant the bme280 sensor is not affected and works in parallel as promised in the introduction perfect we were able to add a second component without caring about the first one now it's a good time to look at the Arduino code generated by ESP home it is part of a standard platform IO project unfortunately I do not know how to access it inside home assistant so I had to set up a standalone ESP home which is perfectly possible if you do not want to use home assistant then you could use mqt to transfer the data to node rate for example the file has a few definitions such as button sensor or switch as well as our bme280 and gpios then setup starts and goes on and on finally we arrive at l Loop what a disappointment only one statement app. Loop this shows us what we discovered before everything is predefined in setup and only called inside this app Loop very similar to an Aros sketch no sequence is visible to us this is perfect for our current applications where the sensor and the LEDs are completely independent but how can we create a sequence and how can we execute something 10 times for example not evident at this moment so let's return to our yaml file and add an olet display fortunately we already have the I Square C component defined and we can add the example file from ESP home but this one only displays a text how can we display the values from our BME sensor I had no clue and I did what I recently always do if I have no clue I asked chat GPT and really it helped me with a proposal because we already have the sensor we only need the display part it introduces another ESP home yaml concept Lambda functions everything behind this pipe character is pure C++ code these three display the temperature humidity and pressure on the OLED I adapt it so it uses the IDS of our sensor then I have to add the font component and upload the appropriate arial. ttf file to the ESP home/ fonts folder that can be done in the file editor with the upload file button now we can have a local display in a addition to home assistant very nice the app Loop has to run very often so that we do not have a visible delay between the measurement and the display if we look into the lock file it seems the display is a bit slow at least ESP home informs us about this fact the display is connected in parallel to the BME 28 sensor as foreseen when I added the PCB to show you how we can Implement logic I want to influence the blue LED with a button and a switch if the mode switch is on the button tockles the LED if the mode is off the button does not work again J GPT helped with a yaml file let's check if it works really the LED tockles if the mode switch is on and does not toggle anymore if the mode is off looking at the yaml file we see that the logic is added here in ESP home it is called automation we can trigger on press and execute actions if a condition is met the syntax of an automation consists of two parts a trigger like on press you find a ton of other triggers often specific to components the lvgl widget for example Le has this list of triggers actions

### [15:00](https://www.youtube.com/watch?v=zD2ByKUZHcE&t=900s) Segment 4 (15:00 - 20:00)

like switch toggle a ton of actions exist they are divided into common and component specific actions important for C++ programmers is that if then else repeat while and wait are also actions so we have nested actions in this example because we could have more than one action they usually start with a hyphen which indicates a list as an example we can add the green led to the blinking automations are different from components executed in sequence like a normal program but they are inside a particular component all other components still are executed in parallel the last part of yaml we will cover today is scripts they are very comparable with functions in C+ plus let's transfer the logic into a script named toggle LED script then we just have to call it from our trigger but now we have a problem as said before all components are executed in parallel what about the script it is not executed until called but wait is this all a script like a function can be called from several components this is no problem in a sequential program but here several components could call the same script in parallel to solve this problem you have to choose what happens in such a case you have the following possibilities single restart cued and parallel single is the default you see the parallel execution of components simplifies a lot but makes a few things harder before you get my verdict I want to repeat what we have covered and add a structure ESP home yaml is a collection of components the sequence does not matter most lines consist of key value pairs components include all needed functionality to for example handle Wi-Fi locking or connecting our device to home assistant sensors are the most significant family of components but you find a lot of others like number or text components up to wireless or media player components additional key value pairs can influence the behavior of components they sit indented by two spaces below the component header lists of similar things are marked by a hyphen logic is called automation it consists of triggers and actions very important if then lse or repeat are also actions they usually work with conditions to steer the behavior automations sit in a component and are executed sequentially like a C++ program scripts are similar to functions where you can bundle functionality to either enhance readability or call it from different components so now we come to my verdict first programming with ESP home is fun four things are much easier than in Arduino the infrastructure around the project consisting of Wi-Fi remote logging otaa and integration into home assistant reduces much upfront work also because it is already tested the independence of components and their parallel execution is much easier to understand you just Define what you need and you get it done this is not easy to achieve in C++ because you have to write non-blocking code or you switch to free OS no mqtt or other transport protocol is needed if you use home assistant this saves a lot of time too prototyping is very easy you start with one component and test it out this is particularly easy if you use home assistant because you can immediately see the readings or interact with your sketch this water meter for example consists of only one component with 15 lines of code the second part is only needed to set the initial counter value once and the debugging was very easy because I did it remotely from my desk not in the dark seller the examples of the documentations are already very useful but CH GPT puts one on top often I got a running yaml or at least a fully functional component just with copy paste incredible particular L the intentions are not easy for a beginner

### [20:00](https://www.youtube.com/watch?v=zD2ByKUZHcE&t=1200s) Segment 5 (20:00 - 22:00)

this is where chat GPT helps a lot but there are also negatives just copy paste code sometimes does not lead to success for my water counter I first had to understand whether I needed a pulse counter sensor or a pulse meter sensor to reach this decision I had to experiment with a sensor to understand their behavior that kind cost a lot of time often it is not obvious how to solve a problem and you must search for Solutions this API to set the initial counter value is a good example who would think that we need an API for this task fortunately I found the solution somewhere in the examples the documentation has lots of details however it was not easy for me to understand the overall concept this is the main reason for the video where will I use ESP home in the future and where will I stick with the uino IDE for all sensor and home assistant related projects I will definitely use ESP home for example I want to replace the P sensor that starts the water pump for our cut with a camera sensor to start only if the cut is there it took 22 lines in ESP home to create a webcam with an esp32 I plan now to connect it to a detection software in home assistant for all non-home assistant related projects I will still use the Arduino IDE mainly if I can build on a readymade library or project and if the component is not yet implemented in ESP home all in all ESP home is a significant driver towards using home assistant instead of node red for me one problem I still have not solved is the transfer of data from home assistant into influx DB no not the whole home assistant database just particular sensor values maybe you can help me that was all for today as always you find all the relevant links in the description if you found this video useful or interesting please support the channel thanks for watching and see you in the next video bye

---
*Источник: https://ekstraktznaniy.ru/video/39252*