Hi everybody Chris here again from Core Electronics. I thought you'd like to see an IOT project done from start to finish, as fast as I can possibly do it.
On the IRT device end, we're going to use app PyCom WiPy, connected across the Internet to Adafruit IO, which is an IOT cloud service. Don't worry if I run through things too fast, below the video you'll see the link to the documentation, so you can do it at your own pace. So, let's have a look at the hardware and see what we're using for this project.
Here we have a PyCom WiPy, we're going to use that with the expansion board 2.0, we have a single connector, so that we can put it into firmware upgrade mode and make sure the firmware is up-to-date, we have a micro USB cable connected to our Windows PC (with wireless networking), our access point here obviously has the internet on, but over here on the Left we have a battery and a phone, so that at the end we can see the whole project working disconnected from any cables or computers.
All right the first thing we need to do is getting our WiPy connected to the expansion board, with any of these devices the LED end must be the USB end, that's the way to get them in the right way. Okay now if you're using anything other than a WiPy, make sure you've connected your external antenna at this point, (powering up the radio without an antenna will damage it). Alright so with the PC connected, we can see a little blue flash on the LED and that's the heartbeat saying that the device is happy. Alright, we'll move over now to the PC and we'll install the driver software.
On the right here, you can see the PyCom documentation from docs.pycom.io, on this page we have two links that we're interested; in the windows link at the top gives us the PyCom firmware update app and further down the page, we have a link in the procedure here called FTDI drivers. So that's the one we need the FTDI driver is the one that gets the hardware installed, you can see on this page there's a large table with versions for everything. Ok over on the device manager on the top left of the we have an "FT230X Basic UART", that's our PyCom expansion board and we need to install that, I'll just go back to my downloads folder and extract the zip file for the download. Okay now we have the drivers unzipped, we can use them straightaway, FT230X, update driver, don't search automatically because windows do that anyway and it's failed at this point, so browse my computer, go through the Browse button, find the folder inside your downloads and use that. If you've got the right driver, it installs pretty much instantly, there we go USB serial converter and we'll close that. Now Windows has sorted that device into the USB controllers group, if we go back to the other devices area, we find a USB serial port is detected and not yet installed. So, we do the same again, right click, update driver, follow the bouncing ball and now we have a USB serial port installed, here we go. So, the most important thing to notice there is that if it's installed correctly it's listed under ports and yours may have a different port number, mine is COM4, okay.
Now come back to the bench with me for a moment, I'm going to unplug the WiPy so that we can do a firmware upgrade. To put it in firmware upgrade mode we need; to jump a wire, connected from the ground, across to the g23 pin and when we reconnect the PC at the end of the USB cable, we find the heartbeat has gone, (which is right because we want hardware upgrade mode).
Ok back onto the PC, looking again at my downloads folder, there's the PyCom firmware update application, we run that one, use all the defaults and install it just as it will and at the end of the installer it'll start the application. Now I should mention here that the only authentic way to damage any embedded device, is to start a firmware upgrade and disconnect it while it's upgrading, please don't do that, that will probably destroy your device, but other than that anything else that might go wrong through this process will just result in an error message saying that the firmware was not updated, and you can try again. So, the instructions say to close any software that might be using the WiPy, you need to do something different if you're going to upgrade one of the expansion boards and press the reset button. So, I'm doing that that's the button next to the LED, select the right serial port COM4 matches the one that we just installed in the device manager and there we go. The board will be identified the right download will come down and then it will be flashed on to the WiPy.
While we wait for that to happen let's have a look at Adafruit IO. Again, it's a free service you can click the getting started for free link at the top there, sign in with your name and an email address, there's no payment required. Because I've already done that I'm going to go through the sign on screen and you'll be able to see in here that you have a menu system on the left, the documentation is excellent, anything that you might need to do in there can come from the documentation, I will use just some of the basic features to do the demonstration today, but we'll leave Adafruit IO for a moment we have done the firmware update on the board and we'll have a look at what happens after that.
Okay so firmware update is complete, the jumper is still in the light is not flashing, so we need to remove the jumper and then hit the reset switch and we should see the heartbeat start up again, I realize it's a little tired from there but yes, the heartbeat is on, everything's running fine. So now we can communicate directly with the WiPy, over serial or over Wi-Fi using telnet or FTP. So, telnet just looks like a ms-dos window, it's just a black on a white text box, but you can type Python code into that box and have it run on the device immediately. So, you can have an interactive time with it straight away, if you've used Arduino or something like that you'll know that you write code, you check that it hasn't got errors, you compile it, you upload it and then it runs. Now one of the wonderful things with the PyCom devices is the Python runs a line at a time. FTP is File Transfer Protocol, so if you want to take a copy of the files that are on this device just connect to it with FTP, pull the files down, when you've developed a program that you want on multiple devices you only need to FTP the files to it. So, if you know how to use telnet and FTP they're basic, you need nothing else to work on this. However, it would be much nicer to have an integrated development environment, we would like to have a prompt where we can type code a line at a time and have that run straight away, we'd also like a text editor and something manages files and transfers a file between the devices. So, we should use Atom IDE, because it has a purpose made the plugin for the PyCom devices. So, let's go to the PC and we'll install Atom IDE, and have it customized to work on PyCom.
You can see again in my downloads folder, there's Atom set up I'm just going to double-click that, this needs no interaction at all and installs by itself, where I've got that from though, is if you go to github.com/atoms/atom, you can find a link there under the windows heading to all the latest installer files. So, for this parent version, there's an installer file there at the bottom of the list for AtomSetup-x64, for 64-bit windows and the Atoms setup there is for 32-bit windows. Here we go, so Atom has started up already. Now the most important thing we need to do with Atom has installed the Pymakr package, which will have everything set up for us. So, on the right menu here, we'll go down the list under get to know Atom, to install a package and the button below that, now open installer and the package we want to search for is pymakr (just Makr on the end) and when we search for that package, we should find it immediately and then we can click the install button.
Now I'm going to run "PuTTY", which as I mentioned is a simple app that can talk telnet, I'm going to connect it over serial, to COM4 at 115200 board rate and hit open now, here we are we have a telnet connection, it does nothing until you press Enter, there you go. You have a triple greater than sign, this is the micro Python prompt. So, any code you write here execute immediately on the WiPy and the results come straight back. So, if I say print ("WiPy ROCKS!"), that command goes down the serial port to the WiPy and the respondent result of that comes straight back up the cable. Now all that I do in this prompt is in the same context, so I can say "import PyCom" the Python library will let me deal with the hardware on the WiPy. So now I can talk to PyCom on and say "pycom.heartbeat(False)" there you go and I'm looking at the WiPy and the heartbeat has stopped. So, I can say "PyCom. rgbled(0xFF0000)" and the LED is now red. Okay, so I can build a program up line by line and all that code is executed at the end of the line when I press ENTER, of course in Python if I put a colon in, I'll create a block that's indented, and I'll have to finish that entire block before it executes. So, if you've played with the commands here and you decide you want to reset, start again, press Ctrl D and it does a soft reboot for you and it's forgotten whatever you were doing. Okay we'll close that now, because we want to have a look at our IDE.
Now here we have a term again it has this big black box at the bottom, which is like the telnet session we were looking at and if we were to click in it automatically wants to connect to 192.168.4.1 which is the default for all these Python devices. Now the only reason we can't connect now is we haven't joined the right network, if we have a look at Wi-Fi here, there is a Wi-Fi access point called "WiPy-WLAN-238c", I'm sure your device will have a slightly different probably 4-character code on the end but if I connect to that and use the password www.pycom.io which is the default. So, when Windows has joined this access point, this IP address will work. So that just requires a second.
Right, there we go, I jumped out of it just before it showed up, so there you go it is connected and now when we click into this bottom box, we have the same dialogue with the device directly through the rebel prompt. Now "REPL" which is Read Evaluate Print Loop. So, any code we type is executed and the results come back. So again, I can say "print("HI")" and I get a hi, so just as we did before. Now the just a few more features of this, is we have a whole lot of tabs up here open by default, so I might just close these off the Welcome tabs and the setting tabs and celebrit reconsent. Okay so now in the top half we have, a code file that hasn't been saved yet, this is a next code file, we can create in between these two panels though, there's a set of buttons there's, an upload, a download, a run, a discount, and some settings if we have a look again in settings, global settings, we can see here that by default we're going to connect to 192.168.4.1 to talk to the device. Now just because in this project we want to work on the Wi-Fi and I want the Wi-Fi to change its IP address, we might change this to COM4 and you'll need to change that to the correct COM number for your device. Now we're saying we want to talk over the serial port and not over the Wi-Fi. So, if I disconnect I can just click in this box to reconnect, it now says connecting on a COM4. So that little wireless access point that we joined, we don't need that anymore we can go until windows to forget about that. All right we're disconnected.
Now, let me talk about the cloud service we're connecting to and what we need to do about it. All right so we have our WiPy device, it needs to connect to something on the internet and then in this example it will be Adafruit IO and we need to have a communication protocol so that both ends agreed to talk the same language. In this instance the best thing to use is a protocol called "MQTT", we won't go through the details of how that works however it's designed to be a lightweight fast protocol for small messages across the internet. So, we're going to use MQTT, unfortunately I've had to precook my code, so that you can see code that I've done but you won't be able to see how I develop it, I've heavily documented the Adafruit IO Python file, that I created and the other file that we're going to use is UMQTT, which is Micro MQTT and that will be taken straight from GitHub. Alright so let's go back to the IDE.
Ok now we have a brand-new file here, ready to go however, it's important for us to download the files that are currently on the device. Before I download, I'm just going to create a folder for this, so from the file menu, I'm going to pick open folder, I might just plop it on the desktop and call it Adafruit IO and open that folder, there we go. So now we have a file explorer as part of our project, so now when we hit the download button we should be able to pull back the files that are currently on the WiPy, now whenever you've reset your WiPy it will create two files, there'll be a main.py and a boot.py, these are much like in Arduino your setup function and your loop function, except that main.py doesn't loop automatically. If you have a look in these files, there is just a comment to tell you what to do, there's no actual code running in there yet. But if we wanted to make our WiPy run our application as soon as it's powered, we would put it in main.py. So, we'll start with this blank text file and as I said I'm going to use some precooked code, I hope my comments in here are comprehensive enough for you to understand, I'm going to paste this in and save the file, AdafruitIO.py. Now we get some nice formatting, you can see at the top this is how the top of any Python file would look for micro Python, you would import all the libraries that you need to run your application. In this example I've broken out all the settings that I need to use so you can see the access point name and password that I use for Wi-Fi. Everything that's "AIO_" there is just Adafruit IO, we need a server name, we need a port number, our user, our AOI key, which is kind of an Apy key, we make up a random number to be our client ID and we have two feeds, so we want to talk to the Randoms feed and send it information and we want to read from the lights feed and get settings from there.
Now if we were to run this now, we would have a problem I press run and the error I get is, "Import Error: no module named 'UMQTT'", so all those imports at the top are working fine except for UMQTT. So, we need a new file for that one, I'll just do a ctrl N to make a new file, again I'll use my precooked code, I'll jump over to the UMQTT.py file, select all and copy and paste this in. Now you can see at the top of this file that I've taken it directly from GitHub, it's called UMQTT.simple and I've tried a couple of this MQTT libraries and this is the one that has worked best so far. So, let's say that we need to give it the correct name UMQTT.py.
Now if I went back to my Adafruit IO.py file and read it now, I have the same error because although Adafruit OI.py is sent directly to the device when I click run, the other files are not. So UMQTT.py isn't yet on the device. So, if we now do an upload, sometimes this fails the first time and I reset the device and it succeeds the second time, there we go. So now 4 files have gone to the device, obviously boot.py and main.py, still do nothing. All right, now if we to run this file, we should see on the device we have a Red LED. The device is connected to Wi-Fi and it's publishing.
Now if we go to Adafruit IO, let me just stop this running code now, I just did a Ctrl C there. Okay if we go to Adafruit IO, we need to create the feeds that are the other end of the communications pathway from the WiPy to Adafruit IO. Now again this is all in the documentation, so it's easy for you to follow along, look at that I didn't realize it created the feeds automatically. So, the Randoms feed was created for me because I posted data to it, I didn't know that was a feature. We need another feed, which I'm just going to call "lights" and this feed will be "Command" from the Adafruit IO end, that will be actioned by the WiPy. Right, we need to create a dashboard, where we can put some controls on. So, we'll create a dashboard here and let's just call it "Dash" when we go at the bottom of the screen into Dash, we can add a new block to the dashboard, there's a toggle switch, which would be great for turning on and off the LED, let's use a toggle switch. So, when we toggle the switch, which data feed are we connecting it to that will be our Lights feed, we're click next step, now we don't really need to do anything with the defaults here, when the toggle switch reads "on" the value "on", will be sent out of the data feed. When toggle switch reads "off" the value "off", will be sent out of the data feed. So, create that block.
While we're still in this dashboard, we will go ahead and create another new block with the blue button. Now this time we want to create a chart that displays the random numbers coming in from the WiPy. So, on the right-hand side there, I'm going to select line chart and we'll link that to the Randoms feed for its data source and clicking next step. The defaults for the chart are fine, we'll just use them as they are, and I'll click create block. Now we have a dashboard that has a toggle switch to control the lights on the WiPy and a chart of the random numbers that the WiPy is sending every five seconds, for some reason we just need to refresh the page to get that chart to operate. So, I've just hit refresh at the top of my browser and the Randoms chart should now start plotting some data, there we go.
Now Adafruit have done a wonderful job on Adafruit IO, if you bookmark this page on a phone, on a tablet, anything it will work just as it does here on a PC. So, you really can control this device from anywhere, via whatever is convenient to you. We can also see the entire data set that has come through from the Randoms feed, if I go back to the top of the page and select "Call Chris", which is my user account I can then, from the top select feeds and then I can run down the page, I'm going to pick the random speed at the bottom of the page. Now you need to go down a fairway, but you'll see in there, a line chart that displays all the data in this feed not just what's happened in the latest session since you opened your web page and below the chart is an event list of what value was submitted and exactly what date and time. So, it's really a wonderful system it's a suitable place to start and you can do some good IOT projects, using just this basic feature set.
What we would like to do is have this application run as soon as we start the board, I’ll show you one thing first. If you come and have a look at the bench with me, I'm just going to change my dashboard back to their first "Dash" we created, and the lights switch here I will click to switch on and click to switch off, I look at that somehow, I've made the dashboard come alive.
Okay if you look over the top with me here's the WiPy, here's the multi-coloured LED and as it's sending out a new random number every 5 seconds, I can switch the light switch at Adafruit IO anytime I like and change the value of that light, fully on, fully off. So, we have two data streams here completely asynchronous, the random number will not be interfered with by the light switch and vice-versa.
All right, now we want to go fully disconnected with this project, so come back with me to the PC for one last time, if we take the code here that works in Adafruit IO.py, select it all, copy it, put it into main.py. Now PyCom devices, look for these two programs and if they find them, they run them, so I've saved main.py with our main program code in it. If I now upload, I'll take all 4 of these files again and copy them back to the WiPy, upload failed I'm going to do a Ctrl C, that stops my ripple connection running its default program. I'm going to disconnect and reconnect, and this happens from time to time the upload button has gone. So, I'm just going to restart Atom and I should land directly where I was, don't worry about that main.py file, now we can upload. There we go 4 files going to the WiPy, it looks like a reboot is happening straight afterwards, I can't see that the upload occurred there, uploading done. Okay realized that there was only one file that needed uploading.
Okay so the board has reset because we uploaded, now let's come back to the bench and see what's happening here. The green light is on saying that it's connected to Adafruit IO. If I disconnect that now, we're not using the PC at all, let's put a battery on. So, this is just an an ordinary 3.7V Lithium Polymer cell. Red, not connected to anything, should go orange for Wi-Fi and then green for Adafruit IO. So, if I now have a look at my screen again, I'm graphing random numbers again and I realize you're still looking at the device, I'll turn the light on full white, and turn it off to black, and white, and black.
Now wouldn't it be great if we could do that from another device, let's go directly into my phone, I've got an outdated version of the page, I need to re-authenticate, sign-in, now I should have an up-to-date list, there we go, there's the Dash, hopefully my Randoms graph will appear on there in a second, there we go, there's the graph, here's my light control. So, as you can see I'm graphing and controlling the light, data is running both directions, we're completely disconnected except that we're using Wi-Fi for the WiPy and those two things are happening asynchronously.
All right so I hope you've enjoyed our quick demonstration of how to build an Internet of Things device with the WiPy and Adafruit IO. Check the description down below for the documentation and otherwise hit us up on the forums.
Thank you for watching, Coreelectronics.com.au.
Makers love reviews as much as you do, please follow this link to review the products you have purchased.