안녕하세요 버섯돌이 유재성입니다. 최저가보다 거금을 주고 라즈베리 파이를 구매한 이유는 여러가지가 있지만.. 평소 아두이노에 관심이 많았던터라 아두이노처럼 외부 기기 제어가 가능한 GPIO가 제일 먼저 눈에 들어오네요^^ 라즈베리 파이 국내 서적도 구매했는데 초보용이라 가격대비 별 도움은 안되는것 같고.. GPIO에 관한 내용이 거의 없어서 서핑하다 케임브리지대학교의 강좌가 보여서 퍼옵니다.^^
출처 : http://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/temperature/
Raspberry Pi Written by Matthew Kirk. In this tutorial, we will be building a circuit to connect a temperature sensor to our Raspberry Pi, and writing a program to read the sensor data. The circuit we will build is going to connect to the Raspberry Pi using the GPIO pins. Once we have built our circuit, the next step is to write a program to read the temperature, and give it to us in a nice format. Once that is done, we will add some LEDs and a button to our circuit, and find out how to measure button presses and turn the LEDs on and off from our program. Finally, we will put all these bits together to make a button controlled temperature logger—a program that will measure the temperature every second and put this in a file, which can be started and stopped by the press of a button. Step One: Updating the Kernel
Step Two: Connecting the Temperature Sensor
Step Three (A): Reading the Sensor from the Terminal
Try opening the file like any other, in the graphical file manager interface. Open up the file browser, go to the directory the file is in and open up the file in Leafpad. The same text as before will be displayed. Step Three (B): Reading the Temperature from Python
In the commands just now, the text is slowly broken down until just the temperature values remain, with several lines of code. This many lines of code is not necessary, temperaturedata = text.split("\n")[1].split(" ")[9] would also work, or splitting by spaces from the start, not newlines. The code may be harder to read if it is squashed onto one line, however. This operation can be made faster—play around with it, and if a quicker way becomes apparent, maybe a classmate can work out what it does? See here for my example code. Step Four: Write a Script to Print Out the Temperature.
Step Five: Connecting an LED
Step Six: Turning On the LED
We've just seen how to turn on and off the LED using the terminal, so now it's time to make it work with Python. The program needs to write to a file. Here's an example of writing "Hello" to a file named "example". Build upon that. # Open the file and write to it, hence "w" Run this program in a terminal using sudo python for the reasons explained above. It should turn the LED on. See here for my example code. Step Seven: There is an Easier Way!We are going to install a library for Python that will let us turn on GPIO pins, without having to go through the hassle of opening and closing lots of files like in the last exercise.
Step Eight: Connecting a Button
Step Nine (A): Checking for the Button Press from the Terminal
Step Nine (B): Checking for the Button Press Using Python
The instructions above have shown how to check for a button press. How about putting this into a loop, so that every time the button is pressed, something happens, like a message printing to the screen. However, the Pi will be able to print out a message and move on to checking the button again much faster than a finger can be taken off the button. So, your program will have to sit and wait for some seconds, maybe with a "while" loop and the "pass" command. Once this works, try to make the program the temperature whenever the button is pressed. See here for my example code. Step Ten: Writing a Temperature Logging ProgramThe final step in this tutorial is putting together all the different things we can do to make a temperature logging program—something that will record the temperature to a file for as long as we want.
See here for my example code. Tutorial over! Enjoy your temperature monitoring, and see below for Appendices. AppendicesPrograms can almost always be improved, perhaps by adding some more code so that it can do something new, or just by changing what is there to make it better, or more readable. This is what this section is all about—pointing out things that don't work as well as they could, or things which would be better done in a different way. Appendix One: Name of the Data FileThe same filename is always used for the script, so old data disappears everytime there is some more. Filenames can be made unique with "temperaturedata1.log", "temperaturedata2.log", but a more helpful way is to add what is known as a timestamp to the name. To do this in Python:
See here for my example code. Appendix Two: Changing the GPIO pinsAnother idea is that up to here, the pin numbers for our two LEDs, button, and temperature sensor have been put straight into the program as needed. Which works fine, but what happens when we move stuff about on the board? Suddenly, everything is connected to a different GPIO pin, and all our clever instructions are being sent to the wrong things—we'll be checking for a push of an LED, and trying to light up a button. Fixing this means going though the whole of our program, checking for each time we used a pin number, and changing each of them to the new one. So instead, how about we fix this properly?
So now if we change the position of the pins on the Pi, all we have to do is change the value of these new variables, and everything will still work. See here for my example code. Appendix Three: Logging at a variable rateWith our program as it currently is, the temperature is always measured as fast as possible, which is about once every three-quarters of a second (this is basically how long it takes the sensor itself to measure the temperature, rather than any slow working by the Pi). If we are measuring temperature over 30 seconds, or a few minutes perhaps, this sampling rate is fine. But if we wanted to look at how the temperature varied over an hour, or even a day, then we would end up with thousands, maybe tens of thousands of readings, which would change quite slowly. In this case, it would be nice to be able to make our program only make measurements at much larger intervals. If we want to do this, we need to make our program wait for the right amount of time between measurements, so that they are done at the right intervals. Using the function time in the Python time module, we can work out how long has passed since we last asked for the temperature, and then compare this to how long we want to wait between measurements.
But there is a problem with this. When the program is sleeping, it isn't paying attention to anything else—in particular, not the button which tells our program to stop logging. This means to stop our program, as it currently is, requires you to press the button during the small amount of time when the Pi is not sleeping or reading the temperature. To solve this, we will make the Pi do lots of small sleeps, and check the button between each of them.
See here for my example code. Where Next?Now we've got a complete temperature sensor program, what can we do next? Well, having written all this code just to measure temperature, wouldn't it be good if we could just write some small piece of code to use a different type of sensor, and then have this just work? Fortunately for you, this is exactly what the next page is all about. So let's move on and explore how we can start using plugins. If you need some hints as to what your code should look like, here are my versions of the Python code after particular steps:
All code licensed under the MIT license. Raspberry Pi Temperature Sensor Tutorial by Matthew Kirk is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. |
'라즈베리파이(RPI)' 카테고리의 다른 글
[영상][강좌] 라즈베리 파이 카메라 사용 법 - RASPBERRY-PI - RPI CAMERA BOARD (0) | 2014.11.21 |
---|---|
[추천][LIB] WebIOPi - Raspberry Pi Internet of Things framework (0) | 2014.11.21 |
[추천][SITE] xbmc - 최고의 미디어 경험(XBMC 사용자 모임) (0) | 2014.11.21 |
[추천][EBook] theMagPi 잡지 (0) | 2014.11.21 |
[펌][Spice] How to install spice client on Raspberry Pi - 라즈베리 파이에 Spice Client 설치하는 방법 (0) | 2014.11.21 |