The Pysense expansion board from Pycom has many sensors. In Pycom Pytrack Getting Started, I showed how with some Processing code we can see the real-time position of the board in 3D. That project works with the Pysense too. How about we show a real-time display on screen of the light sensor and we push temperature and humidity data out to Adafruit IO to make a nice graph from!

Transcript

G'day guys Chris from Core Electronics with you again. Today I've got a Pysense from PyCom and I'd like to show you what it's doing. If you look on the bench with me now, I've got a WiPy mounted on the Pysense and its reading temperature and humidity values and displaying them here on my phone and if you come over to the screen with me you'll see that not only are we displaying the temperature and humidity on Adafruit IO, which I can get to from anywhere but on the left side here, you can also see light values being displayed in blue and red light, which is what the sensor gives us there. So how about I'll show you how we put this thing together.

So first, we need to have a look at our hardware, on the above camera hopefully you can see me here I've got a WiPy sitting on my Pysense I'm just going to remove that for a moment. Now under here we've got, I'll just go through the sensors quickly in the centre here is the 3D Accelerometer, on the side here we have the light sensor and temperature and humidity sensor and a barometric pressure sensor, now the accelerometer we've done on the Pytrack video and that code works just the same on Pysense, they have the same accelerometer. So, all we're going to do today is have a look at the sample code available from PyCom that gives you all the Pysense outputs and we'll move from the sample code to what I'm running here now, pushing some data out to Adafruit IO and showing some data on the screen. Alright so let's go over to the PC, I'm just going to snap this back together as I do that, better get it the right way around would not be good if it didn't, plug that back in.

Alright now on the PC here, I'll just go away from the code that I'm showing now and show you that in the browser, this is the tutorial documentation for what I'm going through now, if you want to take your time and do this slowly it's all documented here. I mainly want to take you to the, where is it now, okay down under the code heading I have a code here sorry is heading here and then a link to sample code if I right-click on that and open that in a new tab, I've already done that. So, here's my new tab, so I won't go into what GitHub is or how it works you really don't need to know anything other than, this is where PyCom releases all its code for us to download and experiment with and the whole thing is packaged up as one thing. On the right-hand side of the page here you can see the green download, clone or download button. I'm just going to hit that and then the download zip file option comes up. So, I'm going to download that and by default let's just leave it in downloads, so look at that I've got two of them now because I've already downloaded that before obviously. So just right-click on the file and extract it all, that's what I did and then if we go in through the folders that are created is PyCom library's master and then within that we have the same named folder again and then once inside that, we have what seven folders, then the second last one there is Pysense. So, this looks like a project we can use straight away with Atom, so we'll do that. I haven't covered how to install Atom or the pie maker plug-in for Atom, that's all part of our Internet of Things from scratch which is linked in the documentation. So, I'm just going to assume you have Adam IDE and pie maker plug-in already installed.

So, what I'm going to do is just open the folder, that's in the download files, let's go across to downloads, working my way in through the folders again, once I get inside the Pysense folder, I'm just going to select that as our folder. All right so now we have another Atom window I'm just going to put that on top of the other one and as is usual with PyCom devices, Boot.Py is the file that each device looks for when it boots up and if it finds that it runs it once and then the main dot PI file is the one that it runs after that. So, let's happen have a look here, I'll open the library these are the add-ons that have been included with this project from Python and if we go straight into main dot pi, there's all the code, it's all there there's no scroll bar that's the entire code. So, we'll just have a quick run through that at the top here we have five import statements, these import statements apart from the first one looks a bit odd, li something something something, because they're all named after the actual integrated circuit that's soldered there onto the Pysense. So yeah, some odd names there but you'll start to see what they do. The next five lines down from where we're setting up objects in memory that load those libraries and can now interact with the hardware and you see where these short two letter names but start to be implemented just below that. So, we can see in line 15 MP. Temperature, that's simple to include a file, create an object, there's a little two-step process there, where we create a Pysense object and then we pass the Pysense object to the other library, so they know how to connect through the board to get to the chip. So, we're printing the temperature there and then next below that we're printing the altitude and the barometric pressure sensor on the board has two functions and you can see them both here;
The first time MP is created it has mode equals altitude on the end of it.
The second time here we create another object called NPP, which has mode equals pressure.
So, it's possible to use the barometric pressure sensor, both as an altimeter and a raw pressure output. So that's what that first block of code is doing temperature and pressure. Then below that there's some more lines there for temperature and dew point, I don't quite understand how the humidity is calculated but, in my application, I’ve taken the measured temperature and added that in here, so that on the end of the line, I'll just scroll a little bit to the right there, si. humid underscore ambient, requires a parameter which is the temperature. So, I've just sent in the temperature in my code by default, this T ambient according to PyCom has been set at 24.4. I don't know why that is exactly.

Okay the light sensor gives us two channels, we have a blue Lux and a red Lux output. So, when we read LT light bracket bracket, that function returns, I was going to say a duplex that's not correct, a, Uppal which is two values, so we can assign two values we can say ((red, blue=LT.light)) and it will populate both of those values. As I've mentioned before in the previous Pytrack getting started video, we had a look at the roll pitch and acceleration and modelled those in 3D on the screen. So, if you want to see how that works that's there, the battery voltage seems to report the line voltage on the board because I don't have a battery attached and when we do run this code it'll show that there's a voltage measured there but it's not for the battery. So the only thing I need to do now is check that I've got the right serial port, so there we go found comm 11, go into project settings and comm 10 has been added by default, so I'm going to change that to comm 11 and save that file and what actually happens to me most times at this point, is when I say connect it connects to the wrong comport number but it seems we're actually connected that's good.


So back to main.PI, if we upload we'll send all those libraries that are in the Lib folder, up to the WiPy and the main program there won't be able to run any of those files that are in the import statement at the top unless they've been loaded first. So, because of the last project I've been doing most of them already there, Pysense.Py and main.Py were the two that were added. So now we can just run, and we have an error in the import statement, cannot import Pysense and that's usually because it hasn't given me the full message, normally it says that Pysense is trying to import "Pycoproc". So here it is at the top of the Pysense.Py file, so back in the bottom right-hand corner of the screen. I'm going into the back to PyCom libraries master. Let me think, it's in here somewhere, it's not in there, the Lib folder in the middle, right so in the Lib folder there's a Pycoproc folder and we need to grab Pycoproc.Py, copy that, jump back into the folder that we're using and put it in the library folder, so I'll paste that in there and now we can do another upload and you should see just that one file being added and we definitely don't want to run Pysense.Py, want to make sure we go back to our main program file and there we go everything is started automatically because the code is in main.Py. So, all of what is coming up down here, except for the top three lines, is actual measured data that's coming from the device. So, there we go the MPL3115A2, has measure temperature at 30.52 degrees. I thought it was hot in here. Somehow, I'm underground the altitude there is -15, I would think that you must zero the barometer to get ground level and then you can use air pressure to estimate altitude, so it makes sense that that's not calibrated and not making much sense. Air pressure is 1015 millibar, I think now or is that deci something's, and we have temperature and relative humidity there, below that we have a dew point calculation of 20 degrees and ambient humidity 78, so that's sticky and we have a blue and red Lux of light available there at 240 and 116. There's the accelerometer roll and pitch, as well as the battery voltage at the bottom. So, 4.65-volts being seen on the board now, that doesn't make a lot of sense to me if the battery is not connected.

All right, so how do we get from this state now where we just have the sample code and we want to get to what I showed first where Adafruit IO is integrated and we can see a graph and, we can see the temperature data, sorry not the temperature data, the light data, red and blue coming off the device. Well what I'm going to do, is I'm going to cheat a little bit and say you really need to go and have a look at the tutorial, because the final code file is there and getting through to it is a little bit time-consuming and not very interesting. The main thing I would recommend for you though, is as you try to put those two projects together, you end with a lengthy list of included libraries, so on the screen I'm reading there "LIS2HH12.Py" all those files I managed to fill up the memory, I assume in the microcontroller because I had too many included files. So, I ended up removing the included files from the "Lib" folder, which meant I had to take out the import lines from the program and I had to take out the objects that were created from those import lines. So, we're dealing with just light and temperature and humidity, that worked fine, but when I tried to include all the sensors data for some reason I got an upload fail at the end, maybe too many files, maybe too much memory used.

So, I'll just go back again have a look at the screen, I'll drop out this is the example code from PyCom we have on the left. If I drop that off the screen, we're now back to the code that I originally ran, apologies for scrolling up the entire file and you can see here, that the list of import files at the top here, these are the ones that I have used to contact Adafruit IO and packaged up my data as MQTT format and the include files below that, are the ones from the sample that came from PyCom. You can see the two that I've excluded there. So, there are some extraneous lines here I was sending random numbers in the previous example, but basically, this is the setup, we need to connect to the Wi-Fi access point and everything AIO underscore, is part of talking to Adafruit. You might find this last line a little strange, there's actually a dictionary object there, that has two different feeds configured for Adafruit IO, one for "temp" and one for "humi" and on the right hand side of screen here, if I go over and have a look at my Adafruit IO configuration, I can go into feeds, again this is all been touched on in IOT from scratch, this is the list of feeds, I have one called humidity and one called temperature, if I open the humidity one and scroll to the bottom of the page, I can see what data has come into that feed over the past hour or so. So, if I really want to drive the data wild, I can't really affect the temperature much because my body temperature is about the same as the room and all I can really do is play with the humidity. So, what I've done is put a graph onto a dashboard if I go to my dashboards and then scroll to the bottom again and grab the conditions dashboard, here is the graph that we saw first, now it has no data, because each time I open this page it resets and that's how I've configured it.

So, I'll start running the code again, let's see what happens here, I've left the other, it's always the trick with having to code windows open, disconnect one before you can connect the other. That makes total sense, right now we're connected, now I want to run this sense. Py which is the main file "UMQTT' is missing. All right, so why have I lost all my libraries for this, am I in the right project, yes, I am, UMQTT is there, oh, of course, I have not uploaded. These are the regular issues that you come upon and if you are fighting a problem with your code that you just can't fix it's usually, for me something simple, where I haven't uploaded the files. So, there we go we're pushing all the files onto the controller the main PI file, there it is it's empty, sorry boot. Py is empty, main. Py is empty, so in this case, I must select the code file that I want to run on the controller and run it. So, I've configured the device, so the LED changes colour, so that I know what's going on on the device, so red means it's connecting, orange means connected to Wi-Fi, green means connected to Adafruit IO and here we go again. We've now got light values coming out if I put my hand over the device, you can see those light values have gone down from 230 and 111 down to 21 and 10, those are small numbers. If I take my hand away again, you should see those numbers bounced back up and the graph would be lovely if I could drive something interesting on the graph. All I'm going to do is pretend that my WiPy is a microphone and hold it right in front of my face and let it absorb all the humidity from my exhaled breath and hopefully you'll see very quickly, there we go the humidity part of my dashboard "my conditions dashboard", is flying upwards obviously there's a limit to that, you can't make more than 100% humidity or even relative humidity. So, if I put that back on the bench the values will start to decline and head back towards what is the ambient value.

So, the final code and for making those two things happen, displaying light values and a dashboard showing humidity and temperature. Those are the final code files the linked in the tutorial.

All right so I hope you've enjoyed that this is a little preview on what you can do with the Pysense, I'm going to be doing another bigger project shortly, which will use Lora radio and will use the Pysense as well. So as always, the documentation is below, read along with it take your time.

Thank you, Chris, signing off.

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.