ESPNOW mix with #ESP32 and #ESP8266

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
- [Mike] Do you wanna build a network of wireless sensors using EPS32s and ESP8266s, all mixed together? In this lesson, I'm gonna demonstrate using ESP-Now with EPS32s and ESP8266s. What I'll show you is a way of taking an existing sketch that's used with ESP32s and change it, make some simple modifications, so that you can use it with the ESP8266s. This example sketch even comes pre-installed with the Arduino IDE when you get it set up for using ESP boards. By the end of this lesson, you're gonna know the exact sketch that I'm talking about, and the tiny changes in the sketches that are gonna make all the difference. Let's go. (gentle chiming music) Well, before we dive in, I'd really appreciate if you could take a moment and click that subscribe button. It doesn't cost you but a click, but it really helps us grow our YouTube channel and bring you great content like this. Also, if at any point, while you're watching this video and you're thinking like, "What is this guy talking about?" please take a moment and ask a question in the comments. I do my very best to answer all the questions I can. All right, so let's jump into this. First off, I wanna make sure that we're on the same page. I'm assuming if you're watching this, you know what an ESP8266 and an ESP32 board is. But if you don't, just real quick, these boards are WiFi-enabled microcontrollers made by a company called Espressif. They're super cheap, and they can be easily programmed using the Arduino IDE. For this reason, they've made a huge splash for newer folks who want to build really cool wireless applications for hardware devices without some huge learning curve. So those are what ESP boards are. You probably already know that. But what is ESP-Now? You might be thinking that, since we're talking about all these cool WiFi-enabled microcontrollers, that ESP-Now would have something to do with WiFi. But actually, ESP-Now is a different protocol developed by Espressif, which enables multiple ESP devices to communicate to one another without using WiFi. It's a wireless protocol that's similar to like a low-power, 2.4 gigahertz wireless connectivity that is often used in wireless mouses. Using ESP-Now, you can design a network of ESP devices that can communicate in a huge variety of ways. For example, you can set up a network so that you've got a transmitter transmitting to one receiver, or you could set up a network where you have a transmitter transmitting to multiple receivers, or you could have a bunch of transmitters transmitting to a single receiver, or, you know, it can be like a cocktail party and everybody's just talking to each other. Now, what I'm gonna demonstrate is having my ESP32 transmitting data to a bunch of different receiving ESP8266s. So, just for example, what I have here are four different ESP boards. This one is an ESP32 SparkFun Thing. This one is an Adafruit Feather HUZZAH ESP8266. This is some Wemos D1 Mini knockoff clone. And this is an ESP8266 12E Module by NodeMCU. This is like a little gang of ESP boards that were causing trouble in my drawer. So I'm like, all right, I gotta do something useful with you guys. Now I have these set up so that when I press a button on the ESP32 Thing, it sends the state of a digital pin to the connected ESP8266 devices and it turns off the LEDs. So, you know, this is like a trivial example. But you could imagine a situation where a setup like this could be pretty useful. You've got one central transmitter and it's gonna push out, you know, a message to a bunch of different receivers. And what I wanna show you is a sketch that comes pre-installed on the Arduino IDE that is designed for the ESP32, but we can modify it pretty simply so that it works on ESP8266s. And I really think that this is a great starting point for getting these two boards to work together. All right, so here we are in the Arduino IDE. And I'm gonna be using Arduino IDE 2.0 for this lesson. You could use Arduino IDE 1.0 just as well, or VS Code, or whatever IDE you want to use, but there's some certain features of the Arduino IDE 2.0 that are kind of useful for demonstrating some of the ideas here. So that's why I'm using the 2.0 version. Now I wanna point out the sketches I am about to show you are not gonna show up unless you have already installed the ESP32 core in your Arduino IDE. In fact, if you wanna work through this, you're gonna need to install the ESP32 core and the ESP8266 core in your Arduino IDE. It turns out to be extremely simple, but we've already covered that in other lessons so I will link to those lessons in the description. So if you're not sure how to set that up, just go to the description and you'll have it set up in like, two minutes flat. Okay, so the first thing we're gonna do is connect an ESP board to the computer. Then I'm gonna go to Tools, Board, and I'm gonna come down to ESP32, and I am gonna select the model that I'm using. So right now I'm using the SparkFun, see if I can find it, SparkFun ESP32 Thing. So I'll go ahead and select that. Now I'm gonna go back up to Tools, down to Port, and I'm showing a list of ports. Now you might have a really big list of ports and it can kind of get confusing. And depending on the development board you're using, sometimes there's like a human readable port name in the list. For example, I'll just go ahead and let me hook up and Arduino Uno. When I hook up my Uno, it'll take a second to show up. I go to Tools, Port. You can see that it's marked with Arduino Uno. So, hey, that makes life really easy. But a lot of these boards, they just have some, you know, stuff like that that really doesn't mean too much to me. So chances are, you're gonna feel like Gollum, trying to guess what's in Bilbo Baggins' pocket as you're trying to like figure out these ports. So one strategy is just to disconnect all of the boards that you have connected to your computer, and then see what's on the list, and then just connect one of them and see what gets added. And then what gets added is usually what you had connected. So anyway, this is the one for my ESP32. I'll go ahead and select that. All right, so now we have the appropriate board and the appropriate port selected for our ESP32. So now what I'm gonna do is go up to File, Examples, and I'm gonna come down to ESP32. Notice that this says, "Examples for SparkFun's Thing ESP32." So depending on the board that you select, you're gonna see different examples in this section. So I'm gonna come down to ESP32, ESPNow, and then there's two folders in here. One is called Basic and one is called Multi-Slave. The one with Basic has a master and a slave, and the Multi-Slave also has two sketches, one for master, one's for slave. So the master sketch is the transmitter sketch, and the slave sketch is the receive sketch. In today's example, I'm gonna work through the multi-slave example. Now in a previous video, I went through, in-depth, how this basic example works with ESP-Now. And so if you're kind of new with ESP-Now, I think that video might be super helpful for you. And I will make sure to link to it in the description. But both of these options, both of these options, Basic and Multi-Slave, are fantastic places to start when you're trying to figure out this ESP-Now. They're just rock solid examples. In fact, my recommendation is that you would use these sketches as like your boilerplate code when you start developing your projects for ESP-Now. Okay, so what I'm gonna go ahead and do, is I'm gonna upload the master sketch right here to my ESP32. So this is one Arduino IDE window, and I've got this master sketch opened in it, and I've got the ESP32 selected with that appropriate port. And again, if you're curious how this sketch works, check out that other video we did where I kinda walk through and boil down a lot of this stuff. But here, really, I just wanna show you how you can get ESP32s working with ESP8266s using ESP-Now. Okay, so I'm gonna go ahead and upload this sketch to my ESP32. All right, so I've got that uploaded. Now, what I wanna do is plug in one of my ESP8266 boards. So I'm gonna go ahead and plug in that Adafruit HAZZAH. Got that plugged in. And what I'm gonna do is open a new IDE window. So I'll just go to File, New. And I'm gonna go through that same rigamarole of, you know, selecting the board and the port. (gentle chiming music) All right, so I've selected the ESP8266 board that I'm using and the appropriate port. Now, this is where things kind of get interesting, right? Because what we wanna do is we want to set up ESP-Now on an ESP8266, right? And when I go up to File, Examples, and I see examples for Adafruit Feather, you know, ESP8266 board, and I come down, there's nothing in here that jumps out as far as like ESP-Now, right? Like there's no, you know, I don't see a similar example for using ESP-Now in here. Maybe I'm not a looking close enough. Maybe it is in here, but I've kind of searched around in here and I never really saw one. There is this WiFiMesh, right? And this one says HelloEspnow. But, you know, when you take a look at it, it doesn't appear to be using, at least directly, the ESP-Now library. So this isn't really what I'm looking for. So here's what I'm gonna recommend. What we're gonna do is we're gonna come over to this other window right here, where we've got our ESP32 connected. We're gonna go to File, Examples. We're gonna go down to that ESP32 example. We're gonna come down to ESPNow. Here's that Multi-Slave. And we're gonna go to the slave example sketch. And we're gonna open this up. Then we're gonna copy this code, or I suppose you could Save As, but I'm just gonna copy that and I'm gonna paste it in here. So this is code that was written for the ESP32. And what I'm proposing you do is you use this as a starting point, you make some changes, and you make it so that it can work on an ESP8266. So that the ESP8266 is going to act like the slave, like the receiver device. So what we're gonna do is we're gonna save this and then we're just gonna try to run it. You know, we'll try to compile it. We'll see what errors we get. And then we're gonna walk through each error, correct each error, and by the end of it, we should have a sketch that's gonna work just fine on our EP8266. Now, maybe this is a bad approach, but I gotta be honest. I love the examples that come with the ESP32 for ESP-Now. And I don't think modifying the sketch is all too crazy. So I think this is a reasonable approach. Now you could probably just search the internet and find some code that's gonna work for an ESP8266. But I feel like going through this process helped me understand the differences between how ESP-Now is implemented on an ESP8266 and how it's implemented on an ESP32. But, you know, I could be crazy. All right, so first thing, I'll go ahead and save this. (gentle chiming music) Okay, so I got that. I'm just gonna get rid of these comments. And here we are. Okay, so I'm just gonna go ahead and try to verify this, and we should get an error. Okay, so the first thing we get is it says, "Hey, compilation error. esp_now.h no file or directory." Okay, so the header file that's used with the ESP8266 is simply espnow.h. Now you might be like, "Now how the heck do you know that?" Like, well, I know that, I mean, realistically, 'cause I was just searching around on the internet and I was just looking at differences between ESP8266 and ESP32 implementations of ESP-Now, and that's really the only difference. If you do go to the documentation for the ESP8266, they have an SDK, and you search for ESP-Now, and you go to ESP-Now API. It says ESP-Now APIs can be found in ESP, NONOS_SDK/include/espnow.h. So I mean that's one way of like, getting to the end of that, but anyway. Okay, so you gotta get rid of, again, that underscore. Okay, so that should fix that error. Now what I'm gonna do is try to verify again. And it says, "Exit status 1. Compilation error: WiFi.h, no such file or directory." And that's because the WiFi library that ESP8266 uses is different than the one for an ESP32. So all we have to do is write an ESP8266 WiFi and we should be good. Let's try verifying that again. Okay, so two easy changes. You just change the library header files that we're using. Okay, but it doesn't stop there. We are getting another error. Now this one is highlighting line nine, and it says, "ESP_OK was not declared in this scope; did you mean ERR_OK? So it's saying right here, this thing right here, it has no idea what ESP_OK is. Okay, so what we've gotta do is figure out, well, you know, what is ESP_OK? This is like a constant, right? It's all caps. So with the ESP32 library, this must be defined somewhere, but it's clearly not being defined in the ESP8266 libraries. Okay, so if we put our mouse over this, you can see we're getting some information here. And this looks like it's referring to this ERR_OK, right? But we're not interested in ERR_OK, we're interested in this ESP_OK. So what if we come back over to our other Arduino IDE instance, right? So this is the one I've still got the ESP32 connected. I'm gonna come down and I'm gonna go look. We've got ESP_OK here. And what I'm gonna do is right-click on ESP_OK and I'm just gonna peek at the definition. So what this did, when I peeked at the definition, it is taking me to where they are defining the file, where they're defining ESP_OK, right? So this is just a file that's living on my computer right now. You know, this like shows where the file path is, but it's saying that ESP_OK is the value 0, right? That's what it's saying it is. So if I come back over here and I replaced ESP_OK with 0, it would be like the exact same thing. But, you know, they're recommending this thing, like, "Hey, did you mean ERR_OK?" Well, what if we check out this ERR_OK thing? What if we just type in ERR_OK, right? And now what if we Peek here, Peek Definition. Now this is saying, "Hey, ERR_OK is also equal to 0." So, hey, this should be like a pretty reasonable substitution for us, right? Anyway, that's at least how my thinking is. Okay, so that was one of the errors we had. We've now changed it over, so let's keep going down this path. Again, you might be like, "Man, this is way too technical." If you just wanna grab the code that is gonna work, just follow the link in the description. You can go to a page, we'll have all the changed code there for you for this example. Again, it's really not too much. I think there's like, four lines of code you need to change. But if you'd rather just have this code and you're like, "Man, this is way too down in the weeds for me," that's cool, I get it. But if you're interested in this kind of thing, then let's go on. All right, so I'm gonna go ahead and click the Verify button. And now we get the grand poo-pa of error messages. And I mean, I don't know, if you're anything like me and you see an error message like this, you just start wanting to cry. It's like, I got a tissue right now to my eye. Like, I'm just gonna go ahead and copy this message and then let's open it up. This is what I'll do. If I get a really complicated error message, or what at least appears to me to be complicated, I like to paste it in something else and just kind of like analyze it as best I can. (gentle chiming music) All right, so what I've done is I got a split screen here. I took that error message and I just broke it down as best I could, line by line, into kind of each little statement that it was making. But let's go ahead. We'll look at the sketch right here. The line that got highlighted for this error was this function, esp_now_register_recv_cb. And let's just take a moment just to kind of talk about what this function is doing. So this function, register_recv_cb, takes as its argument, a different function, right? Which is kind of cool. You've got a function that takes a function as its argument. And as developers, we have to come up with what this function is, but there's some requirements on how we actually make this function. So first let's talk about what is this OnDataRecv function? Well, this callback function right here, whatever we put in here is going to become like the defacto callback function. And it's gonna get called when our receiving device receives data. So we have this network, right? There's one transmitter and there's one receiver. And when the receiver receives data from the transmitter, it's going to execute this function right here that we pass into this function. So this is a really important function. Like you're probably gonna want your receiver to do something when it receives a message from the transmitter. So this function in this program is just defined right down here. And here, what they're doing inside this function is all they do is print off some stuff. They say, "Hey, this is the data that I received." You know, so they just print it to the serial monitor. Really not doing too much. I think they also print the Mac address of the device itself. So they're not doing too much in here, but you could imagine this is an important function where you would be executing different tasks based on the data that you receive as the receiver. Okay, so as developers, again, we're the ones responsible for writing this callback function. But when we write this callback function, we have to follow some rules that are defined in this esp_now_register_recv_cb function. So this function, esp_now_register_recv_cb, is expecting an argument that is going to be a function, and it expects that function to be of a certain design. That is, it expects the function signature to take certain things. So what this error is saying is that we're trying to pass in a function that does not match what esp_now_register_recv_cb function is expecting. So it's saying like, "Hey, I'm expecting a function that's gonna, you know, take these kind of parameters and you gave me a function that does these type of things. Like, what's up, dude?" So it's saying, "Hey, you're trying to pass me this stuff right here, but I need something that's more like this." Now, if we wanna figure out exactly what this function is expecting, we can kind of like dig in, like we were before, and we can go to the definition of this. So I'm just gonna go to Peek, Peek Definition. And now I'm gonna look at where this function, esp_now_register_recv_cb, this takes us to the function prototype. And so I'm actually interested in this esp_now_recv_cb. Like what is this thing that is being defined that I have to match, right? So I'm gonna go ahead and right-click that. I'm gonna go to the definition of this. Okay, and now, so here is this function that I'm passing in. It has to match this. And so when you see u8, just think to yourself, u int 8, or like a byte essentially, right? So this says we're supposed to be passing in an 8-bit pointer to the Mac address, an 8-bit pointer to the data, that's like the data that's actually getting sent, and then an 8-bit pointer to the length of the data being sent. But what are we actually sending in, right? So what are we actually sending in? If we come down to our function, on receive callback, so, hey, here's a 8-bit pointer to a Mac address, here's an 8-bit pointer to data, and then here's an integer to data length. All right, so the difference here is that these are being qualified as constant. So we don't want those to be constant here. So we can take out the constant. And an integer, just int by itself, is actually a 16-bit space. So we don't want a 16-bit space, we want an 8-bit space because that's what the function signature was telling us. So now we'll change this over to an 8-bit size. All right, so now we can verify that and we're not getting any errors. I know, that was like a really big, long description to kind of talk through that. But I don't know if that was helpful or not. Maybe that's just like, you're like, "All right, what you smoking, Mike?" But that's my take on that, I don't know. Open to your thoughts. Okay, so now our sketch is verifying. And essentially, we have a sketch here that can now be used on the ESP8266. It was modified from a sketch that was supposed to run on the ESP32. So I've got one sketch here. This one runs on the ESP32. And then I've got this sketch over here. This one runs on an ESP8266. So of course the end goal here was so that we could get ESP32s working on the same network as ESP8266s. And I hope what I demonstrated here was that there's just some minor changes in how ESP-Now is implemented when you're using an ESP8266, that if you see a sketch that's being used for an ESP32 using ESP-Now, perhaps you'll have enough knowledge about it to be able to make some modifications so that you can make it work for your ESP8266. Now it might be doing some other things that's only ESP32 specific, which isn't gonna work on your ESP8266. But in this case, the sketch here isn't doing anything that an ESP8266 can't do. Okay, so what I've got here is a split screen. On this side is the code we just worked on with the ESP8266, the slave sketch or the receiver sketch. And over here is that master sketch. Now there is one change I wanna make in the master sketch. And it's this channel number. I'm gonna change this to 1 because we need to have these both be on the same channel in order to communicate. All right, so now we've got the channels matching. And what I'm gonna do is go ahead and upload this code to my ESP32, and I'm gonna upload this code to my ESP8266. And then I'll go ahead and open up the serial monitor on this sketch and the serial monitor on this one. So this is the serial monitor for the ESP32 and this is the serial monitor for the ESP8266. The way this default sketch works is it just sends a number that it increments over and over. So now it's sending 5. And now this one is saying, "Hey, I just received 5. I just received 6." So you can see, we have both of these working with ESP-Now. And again, that was kind of the crux of what I was trying to show you, was that you can get an ESP8266 and an ESP32 to communicate on the same ESP-Now network. Hopefully this demonstrated that some. Now, I think in an ideal situation, if you're developing a network, maybe it makes more sense just to have all of the same type of device. You know, like, hey, I'm just gonna go with all ESP32s or I'm just gonna go with all ESP8266s, depending on, you know, the needs you might have. I don't think the cost difference is too much between one or the other. So standardizing your hardware probably isn't a bad idea, but in situations like, you know, I've just got tons of different types of ESP boards, like 32s and 8266s. And getting those sketches to talk to each other with ESP-Now was a little bit confusing. So I thought, hopefully this could help shed some light on that. Well, thanks so much for watching. I really appreciate it. If you haven't subscribed to the channel yet, please do. Like this video. If you have a ton of questions, please feel free just to ask them in the comments. Or if you have some ideas for what would make a fantastic video on this channel, just leave it in the comments. We really take a look at those to help guide our future videos. Also feedback in the comment on the video is much appreciated. I have thick skin, so I'm happy to hear all of your thoughts. And I wanna say, finally, if, you know, you're watching this, but maybe you're not sure on some of the, just the basic programming stuff, you're not feeling super confident there programming with Arduino, then I highly recommend you check out Programming Electronics Academy. There's a link in the description below. That's our training membership site where we have a bunch of courses and a forum on really helping people understand these core Arduino programming ideas. It kinda walks you step by step, starting at the very basics, and really helping you understand how to code and how to code with Arduino. All right, have a great one and take it easy. Bye. (gentle chiming music)
Info
Channel: Programming Electronics Academy
Views: 36,053
Rating: undefined out of 5
Keywords: Arduino, Arduino(Brand), Arduino Tutorial, Arduino Lesson, Open Source Hardware Group, Learning Arduino, Microcontrollers, Electronics, Arduino IDE, Arduino Sketch, Computer programming, C++, Programming Electronics Academy, esp, esp32, esp8266, iot, esp-now tutorial, esp now, esp-now with esp32 explained, esp32 exchange data between boards esp-now, esp32 tutorial, esp32 arduino, esp now esp32, arduino esp8266, arduino programming, esp-now guide, esp now tutorial, esp now led
Id: _eHe7ViXyY8
Channel Id: undefined
Length: 25min 37sec (1537 seconds)
Published: Fri Sep 16 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.