#223 ESP-NOW simple demo - easy IOT data transfer on ESP32 or ESP8266

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
and welcome back now today we have a combination of esp8266s and esp32s talking to each other via a well a new protocol to me although it's been around since 2016. and when do we want them to transmit to each other well the answer is now of course because this is the esp now protocol that expressive have developed and whilst it doesn't use your wi-fi necessarily it does use wi-fi to communicate between these modules but it's so much easier than trying to do it via say html with a get and a post and all that kind of stuff if making a pcb is a bit of a puzzle to you let jlc pcb help you put all the pieces in the right place easy eda is an intuitive electronic design tool just as i'm showing you here and when you've done that you can convert it into a pcb like this one here and you can view it in 3d and see exactly how it's going to look before you order it it's easy to order from jlc pcb directly from within ezda with fast reliable deliveries and now two dollars for five pieces including aluminium boards they can even assemble them for you at no extra charge why don't you see what jlc pcb can offer you today so yeah now why am i using this well first of all it was brought to my attention um by a viewer who said have you there we are there's the comment look have you actually heard of esp now protocol i thought no i haven't why not why why don't i know about this so i looked it up and i found the espressifs pdf on the whole subject so i started reading through this and it seemed great you can have encrypted communications if you want um all mixed some encrypted some not there is a 250 byte payload limit but 250 bytes is quite a bit isn't it if you're thinking about iot type things you know transmitting from one sensor back perhaps to a base unit or something like that so i'm thinking this is all right yeah let's have a look at the code shall we and there is a coded sample on the bottom of this document and here we are here's the sample code at which point i thought oh okay yeah i get it but it's not exactly user friendly is it not certainly not beginner friendly is there another resource i can find on the internet i don't know there's not much on the internet is there really oh then i found this i mean it's very well presented now this is rui santos who's got an excellent youtube website let me just uh bring that up here he is ruiz santos um now he does an awful lot of videos on esp32 and esp8266 stuff and i noticed there's one there for ota over-the-air updates which i'll be looking at sometime soon there anyway but the thing is what he did here was uh describe in general terms esp now and then he's got this um sight esp now that's esp32 but it does another one almost identical for esp8266 now from my point of view i needed to know that um my devices could be mixed and matched if you like so i wanted an esp8266 sender unit and an esp32 receiver unit now i saw no reason why they shouldn't work and indeed they do work let's have a just have a very brief look at them working and then we'll talk about the sort of protocol and everything around them okay here's the two things running now i am running the arduino ide pro it's renamed now isn't it ide 2.0 and it's still a bit of an uphill struggle i've got to say i'm sure arduino.cc are doing stuff behind the scenes but i do wish they'd get their act together because it's hard work doing this sort of stuff on that particular platform but it's it's still better than the original classic arduino ide anyway that's aside um let me just take myself off so you can we can see exactly what's going off what's going on here oh technology in it wonderful i'm hitting the button and nothing's happening right i'm going to take myself off right so what we've got on the left hand side then so over here is the receiver and as you can see up here look it says esp now for the esp32 all right over here it's got esp8266 wi-fi so that's the esp8266 sender now forget the code for a minute because this is basically cribbed from louis santos's site and just changed a little bit make it slightly easier to follow you can see i've got the baud rate really really slow that 300 board on both of them because it was too fast otherwise you couldn't really tell what was going on so what happens now is there's a five second delay when this one starts transmitting you should see the same result come out over here we are like 3.5 16 and it's false exactly what it says over here brilliant okay that's that's something at least now the the data that's being transmitted here what um what rue did was have a struct so if we um scroll down they're both exactly the same but this is the transmitter unit so let's have a look at this one here so here's a struct um we've got this character string here now i've got to say up front i hate character strings being used in structs or you know payloads generally there's nothing really you can't send in a payload that can't be depicted or represented by some sort of integer value but he put it in i thought all right we'll just leave it for now there's an integer there's a float and there's a boolean and this is just to show you he says that you can put anything in there which is absolutely spot on and so what the transmitter needs here up the top you can just oh for goodness sake look at it all these okay as we're here as it's popping up and i can't get rid of it this um these values that are now showing you in decimal but in fact our hex are the is the mac address of this receiver unit so we just go back to my desktop quickly so what we're saying is over here this one this one here which is the sender unit is the esp8266 and there's a reason for that and the receiver unit this one here esp82682 esp32 the mac address of this one has to be put into the code of the transmitter because that's how the protocol works it has to do the mac address that is the the media access control address that each item of um iot type stuff has you know your phone your computer every single little bit so if you've got multiple network cards you're going to have multiple mac addresses and they are unique to you there are probably billions of them in the world now to get the mac address out of there it's just a four-line bit of code you just fire up wi-fi and you say give me the mac address that's all in rui's example code and indeed the code that i've stolen from him really sorry borrowed from him so and it's a one-time operation you find out what the mac address will say it never ever ever changes it sort of hardwired into the actual well hardware so that's that's one good thing isn't it unlike an ip address which changes even if you you say it's fixed it can still change occasionally so the mac address of this is put into the code of this so it knows what to talk to i've got this set up at the moment as a station which means that transmits but is not receiving other than being told by this yes i've got it okay a bit like nrf24l01s even though you might have the controlling nrf transmitting a value as i'm going to be doing my green bin lids yeah no they're still not done so they transmit a value they're not being really set up in receiver mode as such but they are and effectively because they transmit the receiver receives it and says ah that's fine i've received a packet and it auto acknowledges back to the transmitter saying got it job done so the transmission says oh good i didn't just send it out to the ether like a broadcast and never know whether it got received or not it's confirmation that it worked and this works in exactly the same way although this one here is the station mode transmitter and it's sending to this one this one does also acknowledge that it got the data and says job done got it interestingly enough that works even if you haven't got a sketch on here or you've got some rubbish sketch that's got nothing to do with the esp now protocol how do i know that well when i first loaded up the transmitter sketch on here this was running something from about two years ago and this one said yeah i've sent the stuff out and it was successfully received hmm how does that work then now the esp now protocol works by registering what's known as a callback so within your code you say this function here i want you to execute when you've received a payload from a transmitter and it seems that the receiving unit can do nothing about that it will get it as long as the transmitter unit knows the mac address of the receiving unit that receiving unit gets it regardless okay even if you haven't got a callback registered it's just one of those things it appears to be now in this code here this is the this is the receiving code we've got here and what we're saying is uh we first of all initialize it in the setup and then we register that we're registering the receiving callback and that name there that on data receive is a function that was written by louis and it's up here this one here on data receive so what happens is the transmitter sends a packet out of up to 230 bytes i think it was wasn't it the esp32 or whatever receiver unit you've got can be an esp8266 grabs that automatically regardless of anything else and says to the transmitter got it and then it goes right okay i've got this this packet now is there a callback registered somewhere that i've got to execute oh there is look at lo and behold this callback here that all it does is just uh tell you what it's received we're not doing anything with the data at this point and it goes just tell me what that payload consisted of right that's all that's doing obviously in real life you'll probably do a lot more with it you might log it on something i'll delay whatever it is you're going to do with it if you haven't got this registered this this callback the esp32 still receives it but does nothing with it because there's no code to run for you haven't told it now the good thing about this if you notice is that once you've registered this callback the loop is empty you can do other stuff in the loop because all that stuff works behind the scenes as sort of a a separate task in fact for what i know um the esp could be running that as a separate task i'm not 100 sure how they've set that up especially if that is and to be quite honest i'm not i'm not that bothered how they've set it up because what it means is your loop in the receiving unit can do everything else and when it receives one of these packets it will jump in and execute that callback function nice yes you'd have to worry about it it just does it like an interrupt really in fact it could be registered as an interrupt as well okay so that's that's great um why why why am i doing it like this because in the video about my workshop heater unit down there yeah which i said i've got to make it intelligent i want it as an esp32 um it's not actually released yet but you'll be you'd have seen it by now um one thing that occurred to me late on once i've done the video is that it's alright having a temperature sensor down there on the wall right next to the socket and you know a foot away from the heater yes it will register the temperature but it's not really the temperature i want to register is it i want to register the temperature basically here at my workbench either on top of my workbench or maybe underneath my workbench where my legs are you know is it cold down there whatever because i noticed that from the previous workshop the um the temperature registered at various points in my workshop was quite different and it's where you are that it's important to have the temperature right isn't it and i thought well i don't all right i'll have a a standby temperature sensor down there in the unit probably a ds18 b20 they're not the most accurate and they do jump about a bit but it means up here on my workbench i can have an esp8266 esp now unit running um with something like a a dse 180 temperature module is it was it bse whatever it is it's it's a very good temperature monitor there is a little bit of self-heating going on but i mean you can compensate for that in the software but it's it's very very very finely granulated so instead of jumping about in quarter degree steps or whatever it's doing it in minuscule steps and it's very very nice to use i've been reading up about it but that's that's for a future video which temperature sensor i actually use so that means i can have something here on my workbench or maybe up there on the shelf or something plugged into a 5 volt supply and that will be running a little tiny esp8266 hence the reason for me using one like an esp right it's an esp8266 on a little tiny board i've had lots of issues with in the past but i think i can probably get around that or just use some other esp8266 little tiny cheap module that's the whole point isn't it and the only thing it's going to do is read the temperature from some external device and transmit it to the actual esp32 module down there next to my heater that's going to control not just the heater but the universe in general so that sounds to me like a go and of course what it also means is like if i wanted another temperature sensor for example or humidity sensor i could place anyone anywhere in this workshop all transmitting back to that single receiver unit sounds like a go to me now how do i know that it's working though great well if on this um on in the transmitter mode if it says i've failed to transmit that or the receiver did not respond that it got it you can do something about it let's have a look now this one here is the transmitter unit which i'm running on in esp8266 but it'll work equally as well on an esp32 obviously being the transmitter it needs to know the mac address of the receiver so it can send stuff to it same struct as we had before whatever suits your needs now the bit that's interesting is for us that we have another call back here so this callback here is saying when i've sent stuff did it did it transmit successfully in other words was it received by the underlying unit and all it does is say was the send status zero yes it was or no it wasn't simple as that and if it wasn't i'm thinking what i can do rather than transmit and re-transmit and re-transmit you might want to do it a couple of times but after that you think hmm it's not working is it and that could be important because after all if the the heater unit down there is saying you've told me you want more heat and then the transmitter unit packs up or can't transmit in some way it's going to keep heating up this workshop until it gets to a silly level so i thought in this device in this function here i could light up something like a little red led or something or flash an led to say there's something going on it's not working yeah something as simple as that to make it as simple as possible for a sensor that transmits the temperature we haven't actually seen what happens then when it does fail so let me just let me just bring in the um receiver unit alongside this like we had a minute ago and just see what happens right we've got the transmitter now on the left and the receiver on the right okay and they're they're quite happily transmitting stuff even if i'm sitting in the way a little bit um so it's all working yeah but if i disconnect the receiver you'd expect the transmitter to say hang on something's gone wrong wouldn't you so i'm just going to literally go down to my workbench here and pull the plug out the usb plug from the receiver so what should happen is the receiver should just stop and the transmitter after you know an attempt to say um don't think that worked let's have a look oh it's gone now what's the transmitter going to say see transmission fail it says fail it didn't get a response back just as an aside i absolutely hate the way this new ide clears the output just because i've disconnected the usb cable i mean come on i was looking at that and that's trying to reconnect oh yeah i know they're not doing themselves any favors are they right i've reconnected now what happens is now it just automatically works again so if and if you notice look this started having success um over here even before this one here was really up and running because once that esp32 is running regardless of any code that might be in there it's working it's a protocol that sort of works on a different layer by the looks of it so that's that's pretty good it's self-correcting so if you switch something off or unplug something and move it somewhere else it would just stop whilst you've unplugged it and then as soon as you plugged it in again that's it it knows about it and just works which is absolutely ideal for i want and i'm sure there's a dozen things you can think of where this underlying sort of wi-fi enabled protocol but not your wi-fi its own little wi-fi can talk to each other now of course the next step for me i suppose is to connect the esp32 receiver unit that's going to be in my heater control to my wi-fi so just to make sure that it's it can connect to that as well as this but that was that would be too much code all in one go to sort of take in so i'll be doing that uh over the next week or two just make sure it all works and i thought i think i'm pretty much there don't you that's a remote sensor if i've known about this i might well have chosen this particular way of doing things for my bin lids because obviously i'm using a an arduino based module out there with an nrf24 and they are very reliable and they work great but i'm thinking hmm this this could have been an alternative quite frankly and just keep things all the same you know and there's always the option then if you are using esp32s or 82660s you can always have them as this little web interface on your phone and things just to check there's lots of advantages i think of doing it this way okay that's it it's a pretty quick one today because as i say i've stolen the code from rui santos and i'll give you links plus the two um sketches that i've sort of modified a little bit just to make it that tiny bit easier to understand for beginners but i think this is something you may want to explore and as i say although i'm using an esp32 receiver and an esp8266 transmitter i've also got this one here which is a transmitter which is how i started this whole thing just to make sure it worked and that worked obviously just the same basically it seems as long as we know the mac address of the receiver this is almost forced to take that packet even if it does nothing with it and you can encrypt it of course between peers so that's another good thing okay lots of links then underneath i'll put the sketches up in my github with some more links and pdfs especially that one from espressif it's always good to read the data sheet in case there's things in there you think oh i didn't know about that gotchas in other words yeah and the code at the at the very bottom end of that esp32 data sheet is probably worth a quick glance at if you're a coder or experience or whatever then you will understand it and it's it's good to know the official way on how to do things but as i say louis santos is way seems to work absolutely fine that's it that's enough for me this week okay thanks for watching don't forget i'd love your comments down below do give me a like um anything else what do you have to do subscribe tick the bell i know they keep telling me they keep telling me to tell you this i think that's it okay we're all done see you in the next video i hope you're finding these videos useful and interesting there are plenty more videos to choose and a couple are shown below and if you'd like to subscribe to this channel just click on my picture below and enjoy the rest of the videos thanks for watching you
Info
Channel: Ralph S Bacon
Views: 35,591
Rating: undefined out of 5
Keywords: Arduino, Beginners, electronics, C++, microcontrollers, programming, gadgets, ardiuno
Id: lkHdk_mKGUw
Channel Id: undefined
Length: 21min 31sec (1291 seconds)
Published: Fri Aug 27 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.