tag:blogger.com,1999:blog-214645252024-03-07T03:02:13.077-05:00Just an illusionKeith Hekkerhttp://www.blogger.com/profile/14927940751983969101noreply@blogger.comBlogger104125tag:blogger.com,1999:blog-21464525.post-8166098456732887422021-02-07T15:54:00.003-05:002021-02-07T17:22:40.883-05:00Coupling a home made microphone to Bluetooth <p><span style="font-family: verdana;">I stumbled across a tweet a few months ago from @helenleigh who was doing/had done a presentation at Hackaday's Remoticon, showing the world how to make your own microphone using mostly materials found around the home. This got me really intrigued, since I have an affinity for using articles out of the realm of their original intent.</span></p><p><span style="font-family: verdana;">So I watched the video on YouTube</span><span style="font-weight: normal;"><span style="font-size: small;"><span style="font-family: verdana;"> <a href="https://www.youtube.com/watch?v=ZD7eq1f0wWg">Remoticon 2020 // Finding Sound and Making Microphones Workshop</a></span></span></span><span style="font-family: verdana;"> and then went to work acquiring the supplies. Our local dollar store proved instrumental in supplying the quilting hoop and hair bands. I could have used ones we already had at home, but I do want to get along with the rest of the family. We did have enough binder clips, I also had a spare USB cable, as a matter of fact, I probably have a box full of them, but then again, who doesn't?</span></p><p><span style="font-family: verdana;">So I got to work and built the mic, using a slightly different method than Helen. Instead of rubber bands, I used hair bands, no need to tie knots. Also, rather than plastic, I used the inner circle cut out of an aluminum pie plate. Plus the aforementioned USB cable (which is shielded to prevent the leads from picking up hum.)</span></p><p><span style="font-family: verdana;">The only real electronic components are 2 piezo pickups, which I got from Amazon (in a pack of multiples).</span><br /></p><p><span style="font-family: verdana;">I didn't have an amp of any sort to plug it into, and connecting the output from the mic straight to a set of headphones barely produced any output, so I hunted around for a preamp circuit. I found this:https://samtechpro.blogspot.com/2013/12/dynamic-mic-pre-amplifier.html</span></p><p><span style="font-family: verdana;">I simply doubled up on the circuit to make it stereo (ya gotta be professional about these things), prototyped it on a breadboard, it worked, so I got to transferring it to KiCad (version 5.1.8) </span></p><p><span style="font-family: verdana;">Not being too familar with KiCad, it took me a lot longer than I anticipated, but here is the design seen from the bottom and then from the top:</span><span style="font-family: verdana;"> </span></p><p><span style="font-family: verdana;"> </span></p><p><span style="font-family: verdana;"> </span></p><p><span style="font-family: verdana;"></span></p><div class="separator" style="clear: both; text-align: center;"><span style="font-family: verdana;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjn6aRT0gCehyphenhyphenrDOSS3Rvf-DQBWAl73rXx-OYpTORezuoWgO5_QVwLNDivp3NMARwyUOW5vG9Vv5kPJabD5qd7ROL0oyrL2bSy9eqFd8NESyFeIyfCAy8fry6kPskRXyR1tXhweHA/s338/microphoneampcoppertraces.JPG" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="202" data-original-width="338" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjn6aRT0gCehyphenhyphenrDOSS3Rvf-DQBWAl73rXx-OYpTORezuoWgO5_QVwLNDivp3NMARwyUOW5vG9Vv5kPJabD5qd7ROL0oyrL2bSy9eqFd8NESyFeIyfCAy8fry6kPskRXyR1tXhweHA/s320/microphoneampcoppertraces.JPG" width="320" /></a></span></div><span style="font-family: verdana;"><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiknbhB-vSz98u9uwwi89y7tMkCnomE2JShp5o2dXbFf6SoNqZVLAp3xtg0OFx-UL4UrKPIyUQaaFF_nBep87WBp5n41-KmFGJvu6gamXE7VSB18zEg35-shpDbeczeZXYlEwxNHA/s329/microphonepreamptopsilkscreen.JPG" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="188" data-original-width="329" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiknbhB-vSz98u9uwwi89y7tMkCnomE2JShp5o2dXbFf6SoNqZVLAp3xtg0OFx-UL4UrKPIyUQaaFF_nBep87WBp5n41-KmFGJvu6gamXE7VSB18zEg35-shpDbeczeZXYlEwxNHA/s320/microphonepreamptopsilkscreen.JPG" width="320" /></a></div><br />Next, I undertook the usual routine of creating my own PCB, always fraught we difficulties aplenty. In the end though, I ended up with a nice looking print and PCB.</span><p></p><p><span style="font-family: verdana;">I mounted the components and decided that the output would go to a 3.5mm jack. The 5V power would come to the board via a micro USB jack. Next, I installed all this in a small box. Finally, I hooked it up to a very small set of speakers and it worked.<br /></span></p><p><span style="font-family: verdana;">Not being satisfied with that, I decided to try and employ a set of Bluetooth transmitters and receivers. Not having any idea if it would work, I tried to get the two to pair, and surprise, surprise, they did. The transmitter is an <a href="https://www.amazon.ca/Aigital-Bluetooth-Transmitter-Player-Stereo-Transmission/dp/B08CGBHZ68/ref=sr_1_10?dchild=1&keywords=Aigital+bluetooth&qid=1612725546&s=electronics&sr=1-10">Aigital Bluetooth transmitte</a>r, the receiver is <a href="https://www.amazon.ca/SZMDLX-Bluetooth-Launcher-Rechargeable-Automotive/dp/B0897GQNHV/ref=sr_1_7?dchild=1&keywords=SZMDLX+bluetooth&qid=1612725955&s=electronics&sr=1-7">SZMDLX transmitter/receiver combo</a>, the latter being used by me obviously only in receive mode.</span></p><p><span style="font-family: verdana;">I hooked up the mic to the input of my preamp, the output to the Bluetooth transmitter. Then the Bluetooth receiver was plugged into the 3.5mm AUX jack on my computer. Upon insertion, a MaxxAudioPro screen popped up, asking me what sort of device I was plugging in. I chose Microphone in. Then over to Control Panel, Hardware and Sound, Manage Audio Devices, Recording, Jack Mic, then choose Properties and place a tick mark in the Listen checkbox. And voila, I heard my own voice!</span></p><p><span style="font-family: verdana;">This way I can record as well. I must say, the sound quality is pretty decent. Here is a <a href="https://drive.google.com/file/d/1FWJr2cejqsLp5sLnGRNv-Z1b--7gQTZr/view?usp=sharing" target="_blank">sample</a>:</span></p><p><span style="font-family: verdana;"><br /></span></p><p><span style="font-family: verdana;">Additionally, some photographs of how it all looks:</span></p><p><br /></p><table cellpadding="0" cellspacing="0" class="tr-caption-container" style="float: left;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh4mC5wajGsjbxobj7B1fkXv7NF0aSVror952Lnvww_dxAZW9JY7RaEyqSW1Xs63gFCQnW73NmRAvSiT0bsqpUR1as6k7jwDO5dJmkSM2EZZBPUQO1o93hBWwecPGqyGAjWnxGYmw/s1200/microphonefront.jpg" style="clear: left; margin-bottom: 1em; margin-left: auto; margin-right: auto;"><img border="0" data-original-height="800" data-original-width="1200" height="445" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh4mC5wajGsjbxobj7B1fkXv7NF0aSVror952Lnvww_dxAZW9JY7RaEyqSW1Xs63gFCQnW73NmRAvSiT0bsqpUR1as6k7jwDO5dJmkSM2EZZBPUQO1o93hBWwecPGqyGAjWnxGYmw/w669-h445/microphonefront.jpg" width="669" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;">Microphone front above, microphone back below.<br /></td></tr></tbody></table><br /><span style="font-family: verdana;"><br /></span><p></p><p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjyAo9bfgY9sfRboGDuORucZRwu7O0u7FyyXfavsgZJwq7xnpRec_n_VEXyLj7vz1IOZC9xvgwSUMBDlY1PWQR7WG4PFGEr_87ILFriyWu6N2_vgSLZP3sNKnqib6LunjvmtaDmmw/s1200/microphoneback.jpg" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="800" data-original-width="1200" height="442" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjyAo9bfgY9sfRboGDuORucZRwu7O0u7FyyXfavsgZJwq7xnpRec_n_VEXyLj7vz1IOZC9xvgwSUMBDlY1PWQR7WG4PFGEr_87ILFriyWu6N2_vgSLZP3sNKnqib6LunjvmtaDmmw/w664-h442/microphoneback.jpg" width="664" /></a></div><br /><div class="separator" style="clear: both; text-align: center;"></div><br /><table cellpadding="0" cellspacing="0" class="tr-caption-container" style="float: left;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhK-wr4EyJ18Jgg_4ye0N1RT7CaQZrwH5LN-Vpm165Ea3Pb5WF-Gm4E50IU3HjzpZ7kZbrwGO8Mal_Pa_2XvZnDSyRee27rHHAqFsnyEmoCamzBOO3901KQqjOzxyFF8QCN-gbXSA/s1200/microphonesetup2.jpg" style="clear: left; margin-bottom: 1em; margin-left: auto; margin-right: auto;"><img border="0" data-original-height="800" data-original-width="1200" height="447" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhK-wr4EyJ18Jgg_4ye0N1RT7CaQZrwH5LN-Vpm165Ea3Pb5WF-Gm4E50IU3HjzpZ7kZbrwGO8Mal_Pa_2XvZnDSyRee27rHHAqFsnyEmoCamzBOO3901KQqjOzxyFF8QCN-gbXSA/w672-h447/microphonesetup2.jpg" width="672" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;">The complete setup. For microphone stand, I used a slightly modified cell phone holder, bought at the dollar store for a couple of bucks. Plus, of course, a binder clip.<br /></td><td class="tr-caption" style="text-align: center;"><br /></td></tr></tbody></table><br /><table cellpadding="0" cellspacing="0" class="tr-caption-container" style="float: left;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHRFuQD29vyfIPTu6uUqrQ3kRTwy04JXOX8ZX3c-B9SYJkJxMZ83FxoXwh2ZlKii2dqD3wOEIBhS-LG_9_glnk1O0JYm1BqLxokWegQCiBRzOrQuA1fyIqQ7nnTXvFWLSw7_ObTg/s1200/microphonesetup.jpg" style="clear: left; margin-bottom: 1em; margin-left: auto; margin-right: auto;"><img border="0" data-original-height="800" data-original-width="1200" height="444" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHRFuQD29vyfIPTu6uUqrQ3kRTwy04JXOX8ZX3c-B9SYJkJxMZ83FxoXwh2ZlKii2dqD3wOEIBhS-LG_9_glnk1O0JYm1BqLxokWegQCiBRzOrQuA1fyIqQ7nnTXvFWLSw7_ObTg/w667-h444/microphonesetup.jpg" width="667" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;">From left to right, battery pack, preamp in a case, Bluetooth transmitter, Bluetooth receiver, laptop. The circuit board on the top is a prototype of the final result.<br /></td></tr></tbody></table><span style="font-family: verdana;"></span><p></p><p><span style="font-family: verdana;"><br /></span></p><p></p><br /><span style="font-family: verdana;"><br /></span><p></p><p></p><p><span style="font-family: verdana;"></span></p><span style="font-family: verdana;"></span><p></p>Keith Hekkerhttp://www.blogger.com/profile/14927940751983969101noreply@blogger.com3tag:blogger.com,1999:blog-21464525.post-82872153702536685482016-05-17T14:38:00.000-04:002016-05-17T14:38:57.907-04:00Circuit Chaser: Raspberry Pi + NFC = timer for children's run thru a playground.Circuit Chaser uses a Raspberry Pi along with an NFC reader to clock outgoing and incoming times for cards (or wristbands in this case) being placed near it.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhZgnWGFV2yarKJAdFhaO7Yl5hyphenhyphenOSn_eZtW9fjKYTI_jOiKNYhUWVaXstDny36xj-Bf6BPCtBBcRXO1QeefoU-j9xap5Xjfu0XyrR3BPQpujsJoW5jTAEUZFvUn2RRt_6_KMT96lw/s1600/CircuitChaserOnWall.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="416" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhZgnWGFV2yarKJAdFhaO7Yl5hyphenhyphenOSn_eZtW9fjKYTI_jOiKNYhUWVaXstDny36xj-Bf6BPCtBBcRXO1QeefoU-j9xap5Xjfu0XyrR3BPQpujsJoW5jTAEUZFvUn2RRt_6_KMT96lw/s640/CircuitChaserOnWall.jpg" width="640" /></a></div>
<br />
Hardware wise, it consists of 3 major components:<br />
<ul>
<li>Raspberry Pi (from the B+ on up)</li>
<li><a href="https://www.adafruit.com/product/364" target="_blank">PN 532 NFC/RFID controller breakout board from Adafruit</a></li>
<li><a href="http://hdmipi.com/photos/" target="_blank">HDMIPi screen</a> </li>
</ul>
Software wise, the main components are:<br />
<ul>
<li>Python application using NFC.py to track card touches</li>
<li>Python application using CherryPy to supply latest info to web browser display</li>
<li>Web page running in Epiphany browser using some heavy duty JavaScripting to update screen continuously</li>
<li>Python application monitoring usage and enabling/disabling screen when required </li>
</ul>
And then, of course, there is the case, which is actually a case within a case. But more about that later.<br />
<br />
This project got started when my daughter bought <a href="http://www.zoomzooms.ca/" target="_blank">Zoom Zoom's</a>, an indoor children's playground. Always looking to get an edge up on the competition, we brainstormed several ideas around until the idea of Circuit Chaser came to the fore. This device lets kids clock out, run through a previously determined course in the playground and then clock back in. Runs can be done simultaneously, i.e. more than 1 kid can be out on course at the same time (up to 11).<br />
<br />
I covered the installation of the NFC reader on the Pi here: <a href="http://khekker.blogspot.ca/2014/01/raspberry-pi-clean-install-of-nfcpy.html">http://khekker.blogspot.ca/2014/01/raspberry-pi-clean-install-of-nfcpy.html</a>.<br />
<br />
As a card is brought near the reader, it triggers the reader into action. It reports the unique id of the card, and my Python application then records this in a SQLite3 database table. If a card currently is 'out', the application records the new time as 'in' (or finish, if you like). Otherwise, it records this as 'out', along with the time of the event. The application next does a retrieval of the last 11 records and creates a text file, which will be used by the CherryPy based application described below.<br />
<br />
The application also retrieves the pseudonym of the card, making it more palatable for human eyes.<br />
<br />
The application based on CherryPy continuously monitors this text file for changes. If found, it will send this file to any browser currently connected to it. If none are found, it goes to sleep for .25 seconds and repeats.<br />
<br />
Along with the file sent to the browser is a variable containing the number of seconds expired since midnight, since this is important to keep the times shown on the display accurate. Each system does not necessarily have the same time as the next.<br />
<br />
An insight as to how the browser gets to display this information I provided here: <a href="http://khekker.blogspot.ca/2013/12/raspberry-pi-cherrypy-and-asynchronous.html">http://khekker.blogspot.ca/2013/12/raspberry-pi-cherrypy-and-asynchronous.html</a><br />
<br />
Once received by the browser, the JavaScript takes over and runs continuously, every second updating times for every card/wristband line shown on the display. It uses the variable containing seconds since midnight sent from the CherryPy application and compares it to its own, and either adds or subtracts the difference.<br />
<br />
Lastly, the application monitoring usage runs continuously and shuts down the display if no activity has been detected in the last half hour and turns it back on when a card is entered near the reader. The shutting off and starting up is based on this <a href="http://raspi.tv/2015/hacking-hdmipi-power-switch" target="_blank">hack by Alex Eames for the HDMPi</a>.<br />
<br />The normal screen blanking on the Pi has been disabled, as explained here:<a href="http://khekker.blogspot.ca/2015/12/screen-blanking-raspberry-pi-b.html">http://khekker.blogspot.ca/2015/12/screen-blanking-raspberry-pi-b.html</a><br />
<br />
I spent an inordinate amount of time on the case. I knew I wanted a transparent case. First, I found a lexan case on the web, which I spent alot of time hacking so the Pi and all its connectors would fit. It looked really good. Then I realized, hey, this has to be used with kids, they can break 'pretty near' (Canadian for 'almost') anything. So I built yet another lexan case around it. Next time, I'll simply go to the specialty place where they sell lexan sheets and for not too much money they can build me a case. A couple of aluminum strips attached to the back of the case allow for attaching to a wall.<br />
<br />
The device has been running for over three months now, and has been received well. Better still, it hasn't been broken, hardware nor software wise. It is connected wirelessly to ZoomZooms LAN, and its output could, if port forwarding was employed, be visible on the greater Interweb.<br />
<br />
All files are up on Github: <a href="https://github.com/khekker/CircuitChaser">https://github.com/khekker/CircuitChaser</a>, with the exception of NFC.py and CherryPy, as explained above.<br />
<br />
The NFC wristbands I obtained from <a href="https://www.overair.ca/shop/index.php?route=product/product&path=60_109&product_id=138" target="_blank">Overair.ca</a>Keith Hekkerhttp://www.blogger.com/profile/14927940751983969101noreply@blogger.com5tag:blogger.com,1999:blog-21464525.post-61956324097566319692016-05-16T19:11:00.000-04:002016-05-16T19:11:09.328-04:00Raspberry Pi Zero + HD44780 in a slim package for use as fuel computerBack in August 2013, I blogged about the fact that I had constructed a <a href="http://khekker.blogspot.ca/2013/08/raspberry-pi-mpg-fuel-economy-carputer.html">car fuel computer</a>. In a nutshell, this device displays instantaneous fuel consumption (last 3 seconds), as well as the trip average, using any one of 4 output formats on a dashboard mounted LCD. The device is hooked up thru a USB port to an <a href="http://www.ebay.ca/sch/i.html?_odkw=elm327&_osacat=0&_from=R40&_trksid=p2045573.m570.l1313.TR0.TRC0.H0.Xelm327+usb.TRS0&_nkw=elm327+usb&_sacat=0">ELM327 Car scanner</a>, which, in turn, is plugged into the car's OBDII port. <br />
<br />
Creation of the device was based on earlier work done by Martin O'Hanlon. It used a Nokia 5110 LCD to display the output. One might argue that it was more of a prototype than anything else, since the display was held to the dash with electrical tape and a case of any sort was absent. Proof of concept.<br />
<br />
In doing further testing with the Nokia display, my findings were that it was somewhat unreliable in the harsh environment that a car interior can present. For 'no good reason', the display at times would blank. Reboot the Pi, and everything would be fine again. Not good when you are travelling down the freeway at 100 km/h. (I'll never admit to speeding).<br />
<br />
So in early 2014, I adapted the fuel computer application to use the very standard HD44780 LCD (16 characters by 2 lines) display. This time, I used a display case, but it only contained the LCD, since the Raspberry Pi 2 was too big to fit inside.<br />
<br />
Enter the Raspberry Pi Zero and its small form factor. Just as soon as it was released November 2015, I knew that I would like to take a crack at incorporating this little miracle along with the LCD in the display box. So, early February, I started on this quest. Of course, what seemed simple from the outset, turned out to be quite the opposite: never have I stared so hard for so long at so few square inches, trying to fit it all in and still have it work reliably as well as allowing for maintenance and accessibility. Here is the result, with the device showing data for the last 3 seconds and the trip average in litres per 100 km (Canadian standard):<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgMSUEnwcb4JIFhLOUTlLkyNbYsG-S536_Wqx7owmKkBXvVA3fn-QjBDKdLBY1iCIKvl5YpiIxK8LxhsSkCc8EMUVRofxTML1V9ldV_n54DKDPvvkO2Y0keqYTp57fREau2gWyzug/s1600/FuelMiserInCar.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="426" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgMSUEnwcb4JIFhLOUTlLkyNbYsG-S536_Wqx7owmKkBXvVA3fn-QjBDKdLBY1iCIKvl5YpiIxK8LxhsSkCc8EMUVRofxTML1V9ldV_n54DKDPvvkO2Y0keqYTp57fREau2gWyzug/s640/FuelMiserInCar.jpg" width="640" /></a></div>
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHTDHbM79RIyhT-xlmj7dZO7359HMeN29TlMBcwzm8SYBT1jvPqHB4NrUxcNEm_WhsWuJXGqfOLAB6bAf5-1wD40Z1y-9gRcNEks__fO_z7LjAL0I4FZDuXqZ5CWACm1A9YC5H6A/s1600/raspizero5.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="426" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHTDHbM79RIyhT-xlmj7dZO7359HMeN29TlMBcwzm8SYBT1jvPqHB4NrUxcNEm_WhsWuJXGqfOLAB6bAf5-1wD40Z1y-9gRcNEks__fO_z7LjAL0I4FZDuXqZ5CWACm1A9YC5H6A/s640/raspizero5.jpg" width="640" /></a></div>
<br />
<br />
This is what you see when you take the 4 screws out of the back and open up the case. Note that the SD card can still be removed without taking anything else out.<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhPovKl-cv7lB73hDLx89zEAMzn_hYEdMyp9EeIBjg_wXbLo2ITgXBb2L69dWbpod-270jkFNVXIGt12WLZkhd-kQ8PgDrdBRiR3xeynkOg6N7RwzRew8H5Qhk0vLnUmX1_liNxWg/s1600/raspizero1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="430" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhPovKl-cv7lB73hDLx89zEAMzn_hYEdMyp9EeIBjg_wXbLo2ITgXBb2L69dWbpod-270jkFNVXIGt12WLZkhd-kQ8PgDrdBRiR3xeynkOg6N7RwzRew8H5Qhk0vLnUmX1_liNxWg/s640/raspizero1.jpg" width="640" /></a></div>
<br />
For reasons explained below, I had to create an intermediate board between the Zero and the LCD. Here, the HD44780 is on its back here ready for mating with my sandwich board:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjAYPIkRYlfjBqx3GDP52CEhfk713mztQR3VmFWkM4oEb-iLliHc7LLJSqeC8LbC3ElA65sirbJSr5BaOt8-nxqCwLdAIOojkuRJ3_N_xs0K4oU4M1Kb-3kfbqxWaHCXw_eHvqa4A/s1600/raspizero2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="433" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjAYPIkRYlfjBqx3GDP52CEhfk713mztQR3VmFWkM4oEb-iLliHc7LLJSqeC8LbC3ElA65sirbJSr5BaOt8-nxqCwLdAIOojkuRJ3_N_xs0K4oU4M1Kb-3kfbqxWaHCXw_eHvqa4A/s640/raspizero2.jpg" width="640" /></a></div>
<br />
And here is the Raspberry Pi Zero and the sandwich board. Notice that only 1 row of GPIO pins from the Raspi Zero are being used.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgPjFWlXOvXxvP8KSp29H9p8kXJo9MXwNPYZBFYfMzWs8pHQDNdb9Zy_G6UnFEOIdSojMMwqRt9r5Rea2Stbo9Zn-EvY6PTK7ess0zI3fFKAPQS0Rmzf9xFlKMBwoV7geMBfv3UHw/s1600/raspizero3.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="426" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgPjFWlXOvXxvP8KSp29H9p8kXJo9MXwNPYZBFYfMzWs8pHQDNdb9Zy_G6UnFEOIdSojMMwqRt9r5Rea2Stbo9Zn-EvY6PTK7ess0zI3fFKAPQS0Rmzf9xFlKMBwoV7geMBfv3UHw/s640/raspizero3.jpg" width="640" /></a></div>
<br />
This is a side view, the HD44780 LCD display on top and the Raspberry Pi Zero on the bottom:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgv3JXRuYLqb-PfmG-w_oBcSuYj1fTgvTKq9_LAdybWON4a1Gr0hJPRw-Dl-Xh69H0ST1iWUdOfiGCb81mLsr4Lyos47xYIVs7DUec53-P264kmBwagnqE5HKYj68qLcn8v3HHaxQ/s1600/raspizero4.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="426" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgv3JXRuYLqb-PfmG-w_oBcSuYj1fTgvTKq9_LAdybWON4a1Gr0hJPRw-Dl-Xh69H0ST1iWUdOfiGCb81mLsr4Lyos47xYIVs7DUec53-P264kmBwagnqE5HKYj68qLcn8v3HHaxQ/s640/raspizero4.jpg" width="640" /></a></div>
<br />
The three buttons perform the following actions: pressing the top one steps thru the brightness level from 1 to 10. Pressing the middle one steps thru the contrast level from 1 to 10. Pressing the bottom one changes the readout mode from litres per 100km to miles per gallon US, then to miles per gallon Imperial, and lastly to km per litre. In addition, if the device detects the car is idling and you press this button, it will change the display to "Press shutdown again within 3 sec". If you do, the Raspberry Pi will shutdown, which is far better than simply turning the ignition key off right away.<br />
<br />
So, to construct this, at first I thought I'll just connect individual wires from the Pi to the HD44780, plus a few more to the buttons for contrast, backlighting and display mode and then simply (and cleverly I thought) use short right OTG cables for power and data. Wrong, wrong and wrong. Loose wires tend to break if wiggled once too often or pushed into too tight a corner too vigorously. Too many wires, and you don't know what is what anymore. Besides, what length should they be? And 2 right angle OTG cables won't fit, because one overlaps the other, which makes it physically impossible for it to fit in the intended USB port.<br />
<br />
Plan B: use male and female headers soldered directly onto an intermediate PCB (a 'sandwich' board) from both the Pi and the HD44780, then have the PCB traces hook up to the correct pins. In theory, this should work. The total depth of the case is 25 mm, a male and female combination makes up 10 mm, you have 2 of those, what could possibly go wrong? A whole lot, that is.<br />
<br />
First off, my soldering skills are level amateur. This means heaps of solder at the base of a pin amounting to probably a few mm. Then you have the thickness of an extra PCB. There are also a few capacitors sticking out of the sandwich board.<br />
<br />
Plan C: use right angles male and female headers. After an overnight deliver from <a href="https://www.buyapi.ca/product/0-1-36-pin-strip-right-angle-femalesocket-header-5-pack/">buyapi.ca</a> (thanks guys!), I was able to start development of a PCB in Fritzing. I had to get out my micrometer for super accurate measurements. After 12 iterations, (each time I would print a version on paper and fit the connectors on there, trying desperately not to prick my fingers too often) I decided to make a PCB. I will spare you the details, suffice to say, it took a lot of time and it wasn't half bad. Physically fitting it all together seemed to work so... <br />
<br />
With loads of confidence, I fired it up in the car and, of course, it didn't work. It turned out, I had soldered and desoldered the HD44780 so often, I had fried it. Fortunately, I had another one at hand.<br />
After soldering on the right angled header, another trial run. More problems. At that point, I realized I still had a database in my possession from the Raspberry Pi 2 prototype. (The Fuel Miser generates a new SQLite database every time it is run.) Since in-car testing is very time consuming and difficult, I decided to create an emulation mode where the Python 2.7 script steps through entries in the database and presents them on the LCD display as though we are driving in a car. Not perfect, but a lot quicker. This weeded out yet another pile of bugs. Next, I also incorporated in the application for info that is normally written to the console to be redirected to a text file. Very handy. This allowed me to, upon return from a trip, to review debug data I had 'print'ed. Finally, success.<br />
<br />
But: the case wasn't put together yet and this proved impossible to do without screws through the three boards keeping it altogether. Since I had made no provision in my sandwich board for mounting screws (duh!), it was back to PCB design. A few more hours (or was it days?) in Fritzing, moving components around, making room for the screw heads, and, more importantly, the nuts. I was ready. But, by that time, I just wasn't up to making yet another PCB board myself. Instead, I sent it off to a fab house.<br />
<br />
Waiting, waiting, waiting was the name of the game for the next 28 days. That's how long it took to get the board back. Then, soldering it all together and fitting it into the box. Time to test: it works!<br />
<br />
I just had to do some software fine tuning. I may still have to do some more.<br />
<br />
I used GPIO Zero to use PWM for both the brightness and the contrast levels. You can actually use RPi.GPIO in the same script as GPIO Zero. Who knew! In testing, I found that GPIO Zero was not as fast as RPi.GPIO when displaying text, so I decided to use RPi.GPIO for that.<br />
<br />
I also incorporated the writing of text and that of the PWM for the contrast and brightness buttons into a separate class called HD44780.py. Even though I am using the whole unit for fuel economy purposes, as such it could easily be employed for some other use. Another right angle header could be mounted on the Pi, giving access to 20 more GPIO pins.<br />
<br />
I kept the readout mode button out of this class, since it is not necessarily related to the functions of the LCD panel.<br />
<br />
All the files are on github here: <a href="https://github.com/khekker/Raspi-Fuel-Miser">https://github.com/khekker/Raspi-Fuel-Miser.</a> There you will also find a not-so-neat hand drawn wiring diagram. Sorry 'bout that, my Fritzing skills are limited. Check out the readme.md file for further information.<br />
<br />
The PCB's from the fab house turned out really nice. I have some spares, in case anyone is at all interested. I'll sell them for $3.00 Canadian plus postage. Let me know in the comments.<br />
<br />
Since the following parts are hard to get, I could supply them as well:<br />
<br />
7 pieces 2.6 mm screws 10 mm long with nuts $1.00<br />
7 standoffs (3 short and 4 long pieces) $0.50<br />
3 pairs of right angle headers (male and female) (2x6 and 1x20) $1.50<br />
1 project case $5.00 <br />
<br />
That's $11.00 Canadian, approximately $8.75 US or <span class="st" data-hveid="48">€7.30 plus postage.</span><br />
<br />
<span class="st" data-hveid="48">If you want me to 'hack' the appropriate holes in this case, then there is a $5.00 charge for that. Canadian of course, $3.90 US, or </span><span class="st" data-hveid="48">€3.30.</span><br />
<br />
<span class="st" data-hveid="48">Just 2 observations I made using the device in-car, both of which would likely make safety experts cringe. The first is that driving behind a large truck makes a difference in fuel consumption (around 10%), the second is that on a car with standard transmission ('stick shift'), putting your foot on the clutch while coasting down the hill cuts (the already greatly reduced) fuel consumption in half. But it is not safe to do, so I strongly advise against it.</span><br />
<br />
<span class="st" data-hveid="48">Note that the device stores a whole range of parameters generated by the engine's computer in a database, which could be reviewed or otherwise analyzed afterwards. For every trip, a new database table is created in SQLite3. </span><br />
<br />
<span class="st" data-hveid="48">Now if only the Raspberry Pi was more readily available! Oh, wait a minute...hot off the press...a new Raspberry Pi Zero released today...it adds a camera function. The camera connector sticks out a few mm from the Pi, but that should not present any problems fitting it into this particular case.</span>Keith Hekkerhttp://www.blogger.com/profile/14927940751983969101noreply@blogger.com0tag:blogger.com,1999:blog-21464525.post-17812781408005632882016-03-26T00:15:00.000-04:002016-03-26T00:15:07.907-04:00How to configure ITEAD PN532 NFC Module on Raspberry Pi<span style="font-size: large;">The other day I received the Itead PN532 NFC module in the mail. Time to install it on Raspberry Pi.</span><br />
<span style="font-size: large;"><br /></span>
<span style="font-size: large;">The PN532 allows for Near Field Communications, e,g, for using an access card to enter a building or transferring info from a smart phone to a computer.</span><br />
<span style="font-size: large;"><br /></span>
<span style="font-size: large;">The installation instructions that came with the device were rather vague or shall I say non existent. And so was any other information about it on the Interweb. Piecing together various snippets allowed me to install it and actually have it work. That is why I am documenting it here just in case someone else comes across the same problem.</span><br />
<span style="font-size: large;"><br /></span>
<span style="font-size: large;">In am installing it on Raspberry Pi 2, with Raspbian Jessie as the OS. Furthermore, I will be using it with NFC.py, a Python library I am quite familiar with. It uses the UART set up (serial).</span><br />
<span style="font-size: large;"><br /></span>
<span style="font-size: large;">First off, we will not use the double row of pins (26 pins) on the PN532. We will use the single row of 8 pins, of which only 4 will actually be connected to the Pi. Note that you will have to flip the device over to read the markings for each pin on the back of the PN532.</span><br />
<br />
<span style="font-size: large;">The connection from the Pi to the PN532 is as follows:</span><br />
<span style="font-size: large;"><br /></span>
<span style="font-size: large;">Connect Pin 2 (5V) on the Pi to the Pin marked 5V on the PN532</span><br />
<span style="font-size: large;">Connect Pin 6 (Ground) on the Pi to the Pin marked GND on the PN532</span><br />
<span style="font-size: large;">Connect Pin 8 (GPIO14) (UART TX) on the Pi to the Pin marked NSS/RCL/RX on the PN532</span><br />
<span style="font-size: large;">Connect Pin 10 (GPIO15) (UART RX) on the Pi to the Pin marked MO/SDA/TX on the PN532</span><br />
<span style="font-size: large;"><br /></span>
<span style="font-size: large;">So much for the hardware side. Now we go on to the software side. Of course, we bring the entire system up to the latest value by issuing:</span><br />
<span style="font-size: large;"><br /></span>
<span style="font-family: "Courier New", Courier, monospace; font-size: large;">sudo apt-get update</span><br />
<span style="font-family: "Courier New", Courier, monospace; font-size: large;">sudo apt-get upgrade </span><span style="font-size: large;"><br /></span><br />
<span style="font-size: large;"><br /></span>
<span style="font-size: large;">Then, we need to modify the file /boot/cmdline.txt. Do that by issuing the following command:</span><br />
<br />
<span style="font-family: "Courier New", Courier, monospace; font-size: large;">sudo nano /boot/cmdline.txt</span><br />
<span style="font-family: "Courier New", Courier, monospace; font-size: large;"> </span><span style="font-size: large;"><br />Within this file, change the line</span><br />
<br />
<pre><span style="font-size: large;">dwc_otg.lpm_enable=0 console=tty1 console=serial0,115200</span></pre>
<pre><span style="font-size: large;"> root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait</span></pre>
<pre><span style="font-family: Georgia,"Times New Roman",serif;"><span style="font-family: Times, "Times New Roman", serif; font-size: small;"> </span></span></pre>
<pre><span style="font-size: large;"><span style="font-family: Georgia,"Times New Roman",serif;"><span style="font-family: Times, "Times New Roman", serif;">to this:</span></span></span></pre>
<pre><span style="font-size: large;"><span style="font-family: Georgia,"Times New Roman",serif;"><span style="font-family: Times, "Times New Roman", serif;"> </span></span></span></pre>
<pre><span style="font-size: large;">dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 </span></pre>
<pre><span style="font-size: large;">elevator=deadline fsck.repair=yes rootwait</span></pre>
<pre><span style="font-size: large;"> </span></pre>
<pre><span style="font-size: large;"><span style="font-family: Georgia,"Times New Roman",serif;"><span style="font-family: Times, "Times New Roman", serif;">Next, if you are running Raspbian Jessie on a Pi 3, add this to the bottom of /boot/config.txt file:</span></span></span></pre>
<pre><span style="font-size: large;"><span style="font-family: Georgia,"Times New Roman",serif;"><span style="font-family: Times, "Times New Roman", serif;"> </span></span></span></pre>
<pre><span style="font-size: large;"><span style="font-family: Georgia,"Times New Roman",serif;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;">dtoverlay=pi3-miniuart-bt</span></span></span></span></pre>
<pre><span style="font-size: large;"><span style="font-family: Georgia,"Times New Roman",serif;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"> </span></span></span></span></pre>
<pre><span style="font-size: large;"><span style="font-family: Georgia,"Times New Roman",serif;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;">This will disable Bluetooth, but allow the PN532 to work.</span></span></span></span></span></pre>
<pre><span style="font-size: large;"><span style="font-family: Georgia,"Times New Roman",serif;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;"> </span></span></span></span></span></pre>
<pre><span style="font-size: large;"><span style="font-family: Georgia,"Times New Roman",serif;"><span style="font-family: Times, "Times New Roman", serif;"><span style="font-family: "Courier New", Courier, monospace;"><span style="font-family: Times, "Times New Roman", serif;">Reboot the Pi, and install NFC.py by </span></span></span></span><span style="font-family: Georgia,"Times New Roman",serif;"></span><span style="font-family: Georgia,"Times New Roman",serif;"></span><span style="font-family: Georgia,"Times New Roman",serif;"></span><span style="font-family: Georgia,"Times New Roman",serif;"><span style="font-family: Times, "Times New Roman", serif;"> issuing the following commands:</span></span></span></pre>
<pre><span style="font-size: large;"><span style="font-family: Georgia,"Times New Roman",serif;"><span style="font-family: Times, "Times New Roman", serif;">
</span></span></span></pre>
<pre><span style="font-size: large;">sudo apt-get install python-dev </span></pre>
<pre><span style="font-size: large;">curl -O <a href="http://python-distribute.org/distribute_setup.py">http://python-distribute.org/distribute_setup.py</a> </span></pre>
<pre><span style="font-size: large;">sudo python distribute_setup.py </span></pre>
<pre><span style="font-size: large;">curl -O <a href="https://raw.githubusercontent.com/pypa/pip/master/contrib/get-pip.py">https://raw.githubusercontent.com/pypa/pip/master/contrib/get-pip.py</a> </span></pre>
<pre><span style="font-size: large;">sudo python get-pip.py </span></pre>
<pre><span style="font-size: large;">sudo pip install virtualenv</span></pre>
<span style="font-size: large;"> </span><br />
<span style="font-size: large;">The above 6 commands install (fairly) standard Python utilities. They may be present on your machine already.</span><br />
<span style="font-size: large;"> </span><br />
<span style="font-size: large;"><span style="font-family: "Courier New",Courier,monospace;">sudo pip install pyserial</span></span><br />
<br />
<span style="font-size: large;"> </span><span style="font-size: large;">This line will install serial library for Python used by NFC.py.</span><br />
<span style="font-size: large;"> </span><br />
<span style="font-size: large;"><span style="font-family: "Courier New",Courier,monospace;">sudo apt-get install bzr</span></span><br />
<br />
<span style="font-size: large;"> </span><span style="font-size: large;">This command will install Launchpad bazaar, a VCS (Version Control System), which allows you to download NFC.py.</span><br />
<span style="font-size: large;"> </span><br />
<span style="font-size: large;"><span style="font-family: "Courier New",Courier,monospace;">mkdir pythonprogs </span></span><br />
<span style="font-size: large;"><span style="font-family: "Courier New",Courier,monospace;">cd pythonprogs</span></span><br />
<br />
<span style="font-size: large;"><span style="font-family: "Courier New",Courier,monospace;"> </span></span><span style="font-size: large;">Above 2 commands create and then switch to the directory where my Python programs will be stored on this machine. You may already have a favourite directory on your machine.</span><br />
<span style="font-size: large;"> </span><br />
<span style="font-size: large;"><span style="font-family: "Courier New",Courier,monospace;">bzr branch lp:nfcpy</span></span><br />
<br />
<span style="font-size: large;"> </span><span style="font-size: large;">This line downloads the latest version of NFC.py.</span><br />
<span style="font-size: large;"> </span><br />
<span style="font-size: large;"><span style="font-family: "Courier New",Courier,monospace;">cd /pythonprogs/nfcpy/examples</span></span><br />
<br />
<span style="font-size: large;"> </span><span style="font-size: large;">Now you've changed to the examples directory of NFC.py.</span><br />
<pre><span style="font-size: large;"><span style="font-family: Georgia,"Times New Roman",serif;"><span style="font-family: Times, "Times New Roman", serif;"> </span></span></span></pre>
<pre><span style="font-size: large;">python tagtool.py --device tty:AMA0:pn53x show</span></pre>
<pre><span style="font-size: large;"> </span></pre>
<span style="font-size: large;">Run the Python program tagtool. The ‘device’ switch following tagtool.py specifies that we want to use the serial device. If there is no error message, things should be working!</span><br />
<span style="font-size: large;"><br /></span>
<span style="font-size: large;">Touch a card on the reader. You should get a response on your screen. Notice that you cannot use a Mifare Classic 1K card, they are incompatible with NFC.py. However, I've had good luck with NTAG203 cards, which are readily available.</span>Keith Hekkerhttp://www.blogger.com/profile/14927940751983969101noreply@blogger.com5tag:blogger.com,1999:blog-21464525.post-74558345869231121072016-02-04T20:20:00.000-05:002016-02-04T20:20:45.797-05:00Raspberry Pi Zero: How to first access it headlesslyI obtained a Raspberry Pi Zero a few weeks ago, but hadn't really done anything with it. I was puzzled as to how to access it initially, since it only has one USB port and no RJ45 connector built in for accessing a network.<br />
<br />
The easiest thing would be to get a USB hub, but I wanted a minimalist approach. No screen, keyboard, mouse. This method only requires a wireless dongle. So I did some sleuthing on the old Interweb and stumbled and <a href="http://davidmaitland.me/2015/12/raspberry-pi-zero-headless-setup/">David Maitland's</a> blog.<br />
<br />
My approach varies slightly from his, that is why I describe it here:<br />
<br />
get access to Raspberry Pi Zero on the network by:<br />
- downloading Raspbian Jessie and dump the image on a microSD card using your PC<br /> - taking another Raspberry Pi, a B+, inserting the 'Zero' microSD card in it<br /> - hooking this up to the network (or screen, keyboard and mouse)<br /> - booting the Pi B+<br /> - modifying the file /etc/network/interfaces file<br />
specifically: add 'auto wlan0' as a separate line above 'allow-hotplug wlan0'<br /> - modifying the file /etc/wpa_supplicant/wpa_supplicant.conf<br />
specifically: add to the end of this file<br />
<pre><span style="font-family: Arial,Helvetica,sans-serif;"><code class="language-text" data-lang="text"> network={
ssid="MYNETWORKNAME</code><code class="language-text" data-lang="text"></code><code class="language-text" data-lang="text">"
psk="MYNETWORKPASSWORD"
proto=RSN
key_mgmt=WPA-PSK
pairwise=CCMP
auth_alg=OPEN
}</code></span></pre>
<pre><span style="font-family: Arial,Helvetica,sans-serif;"><code class="language-text" data-lang="text"> </code></span> </pre>
Obviously you will replace MYNETWORKNAME and MYNETWORKPASSWORD with your specific values.<br />
<br />
- gracefully shut down the B+<br /> - take the microSD card from the B+ and insert it into the Zero<br />
- attach a wireless dongle to the Zero's USB port<br /> - boot the Zero, it should work. If not, check the entries you made for the network values.Keith Hekkerhttp://www.blogger.com/profile/14927940751983969101noreply@blogger.com0tag:blogger.com,1999:blog-21464525.post-50849523575095448882015-12-21T21:21:00.000-05:002015-12-21T21:21:20.781-05:00Raspberry Pi how to show less info at bootupYou can substantially decrease the amount of info scrolling by on the screen at bootup time by adding the word 'quiet' (without quotes) to the end of the first line of the file /boot/cmdline.txtKeith Hekkerhttp://www.blogger.com/profile/14927940751983969101noreply@blogger.com0tag:blogger.com,1999:blog-21464525.post-6578833812454801752015-12-21T21:14:00.001-05:002015-12-21T21:14:48.952-05:00Raspberry Pi Custom Splash Screen (blank screen problem)I followed the instructions found at <a href="http://www.edv-huber.com/index.php/problemloesungen/15-custom-splash-screen-for-raspberry-pi-raspbian" title="http://www.edv-huber.com/index.php/problemloesungen/15-custom-splash-screen-for-raspberry-pi-raspbian">http://www.edv-huber.com/index.php/problemloesungen/15-custom-splash-screen-for-raspberry-pi-raspbian</a> for
a customized splash screen on the Pi. Everything went well, except….at the end
of the boot sequence, the splash screen disappeared and all I was left with was
a blank screen. I found out (by trying) that I could type the userid and
password blindly, followed by startx (also blindly) and then LXDE would appear
as normal.<br />
<br />
Hunting around on the forums, I found a post that stated that if you were
presented with a blank screen after booting, you should try pressing Ctrl+Alt+F2
to get back to a regular console screen. It worked. I hunted around some more
for a better solution, but to no avail.<br />
<br />
By the way, this problem does not occur if you have it setup so that you
automatically startup startx after booting.Keith Hekkerhttp://www.blogger.com/profile/14927940751983969101noreply@blogger.com0tag:blogger.com,1999:blog-21464525.post-49480795457573020372015-12-19T11:34:00.000-05:002015-12-19T11:34:10.870-05:00Screen blanking Raspberry Pi B+I have a Raspberry Pi running headlessly as a display. Therefore, the screen should never blank.<br />
<br />
Here's the link where I got the solution to my problem:<br />
<br />
<a href="https://www.raspberrypi.org/forums/viewtopic.php?f=91&t=57552">https://www.raspberrypi.org/forums/viewtopic.php?f=91&t=57552</a><br />
<br />
User jwzumwalt came up with the post, I'm just repeating it here for my own reference.<br />
<br />
The long and the short of it: there are 2 files to be modified<br />
<br />
#1<br />
<br />
change two settings in <span style="font-weight: bold;">/etc/kbd/config </span><br /> BLANK_TIME=0<br /> POWERDOWN_TIME=0<br />
<br />
#2<br />
<br />
Add these lines to <span style="font-weight: bold;">/etc/xdg/lxsession/LXDE/autostart</span><br /> @xset s noblank <br /> @xset s off <br /> @xset -dpms<br />
<br />
Then reboot and test.Keith Hekkerhttp://www.blogger.com/profile/14927940751983969101noreply@blogger.com0tag:blogger.com,1999:blog-21464525.post-69856981609567327622015-12-17T21:07:00.000-05:002015-12-17T21:07:30.363-05:00Error 98 Address already in use in PythonSometimes bottlewebserver in Python on my Raspberry Pi hangs 'for no good reason'. Upon restarting from the LX Terminal window, the above error appears and Bottle Web Server will not start.<br />
<br />
I've read Stack Overflow postings where, if you wait for one and a half minutes the port will free up and the server can then be started. This does not seem to happen in my case.<br />
<br />
The only way to restart the server, other than a reboot, is to do the following:<br />
<br />
<pre class="lang-py prettyprint prettyprinted"><b><code><span class="pln">$ ps </span><span class="pun">-</span><span class="pln">fA </span><span class="pun">|</span><span class="pln"> grep python</span></code></b></pre>
<br />
The result will then give a line something like this:<br />
<br />
<pre class="lang-py prettyprint prettyprinted"><b><code><span class="pln">root 2130 1 0 11:08 ? 00:00:00 sudo python bottlewebserver4.py</span></code></b></pre>
<br />
Then simply issue:<br />
<br />
<pre class="lang-py prettyprint prettyprinted"><b><code><span class="pln">$ sudo kill 2130</span></code></b></pre>
After this the bottlewebserver will start.Keith Hekkerhttp://www.blogger.com/profile/14927940751983969101noreply@blogger.com0tag:blogger.com,1999:blog-21464525.post-75438098019064580282015-12-14T16:15:00.001-05:002015-12-14T16:15:28.155-05:00Putty network error connection refusedOne of my Raspberry Pi’s that performs sump pump depth measurements every 5
minutes works headlessly. Last week it stopped supplying values via the network
to my other Pi, which does the reporting via a web page.<br />
<br />
So I tried to reach the Pi via putty. Putty responded with “network error
connection refused”. This led me to checking cables, and I found that everything
was fine. To make a long story short, it turned out that a recent power loss
must have caused a corruption within the networking setup of the Pi.<br />
<br />
I hooked up a monitor, keyboard and mouse and did a “sudo apt-get update”
followed by “sudo apt-get upgrade”, followed by “sudo apt-get
upgrade --fix-missing”. Lastly, I reset the SSH keys by issuing “sudo rm
/etc/ssh/ssh_host_*” followed by “sudo dpkg-reconfigure openssh-server”.<br />
<br />
After this, things were back to normal.Keith Hekkerhttp://www.blogger.com/profile/14927940751983969101noreply@blogger.com0tag:blogger.com,1999:blog-21464525.post-40748747960335958822015-04-01T11:11:00.001-04:002015-04-01T11:11:53.690-04:00PrintIsolationProxy.dll corrupted<p> </p> <p> </p> <p>I have a notebook computer, ASUS VivoBook S400CA, Intel i5 processor, running Windows 8.1, around 18 months old now.</p> <p>The computer had been running fine for all this time, of course it is never as fast as you would like it to be, but still…</p> <p>The other day I wanted to print something (I only use the printer on average about twice a month), and Windows told me I had to install a printer? What??? I clicked on Find Printer in the Printer Settings Box and up pops dialog box telling me the local print spooler service is not running and to turn it on. What??? So I went to services (Click Start and types ‘services.msc’) and sure enough Print Spooler had stopped. I started it, it seemed to run, but after 5 seconds or so, it stopped. What???</p> <p>To make an extreme long story short, what fixed it is that I ran the sfc utility from within an Administrator’s command window (type ‘command’ from Start and then right click on Command Prompt, then select Run as Administrator). To run sfc, I typed ‘sfc /scannow’. After this command completed (it takes a while), I went to Notepad (any text editor will do) and looked at the log file that sfc created. This files was stored in c:\windows\system32\logs\cbs\cbs.log. Using the Find command, I looked for occurrences of ‘Cannot’, I found 2 (well actually more than 2, but they always pointed to the same 2 files), amstream.dll and PrintIsolationProxy.dll. The ‘cannot’ refers to the fact that sfc found these files to be corrupted, but it couldn’t repair them.</p> <p>Next, I took a guess and decided to try and replace PrintIsolationProxy.dll with another copy from somewhere else. I downloaded this copy from <a href="http://www.dllme.com">www.dllme.com</a>, ran it through a couple of virus checkers and malware detectors, then renamed the existing version to PrintIsolationProxyOld.dll and popped the new one in its place. Even that is fairly complicated as in order to be able to rename a file, you need to be the owner of it so in the Properties box of this file I had to change that from ‘TrustedInstaller’.</p> <p>Once the new file was copied to c:\windows\system32, I opened up services again, started the Print Spooler, and voila: it didn’t die after the mandatory 5 seconds went by. Next I tried to print and the printer works again!</p> <p>I ran virus checkers and malware detectors over the old version, and none of them detected anything untoward. So I can only surmise that Windows Update did something.</p> <p>I have no idea what amstream.dll does or which process uses it, that is a problem still remaining to be solved.</p> <p>Ironically, PrintIsolationProxy.dll was added to Windows around 2009 (Vista?), to prevent wayward printer drivers from crashing the print subsystem…..</p> Keith Hekkerhttp://www.blogger.com/profile/14927940751983969101noreply@blogger.com0tag:blogger.com,1999:blog-21464525.post-778247522324476602014-07-08T21:19:00.001-04:002014-07-08T21:19:41.629-04:00Using an Adafruit PowerBoost 500 charger as UPS for a Raspberry Pi<p> </p> <p>One of the main tasks of a Raspberry Pi I have is monitoring my automatic backup generator and sending out alerts via SMS and email when the generator kicks in. This works fine, with up to now 1 glitch: when the outside power to the house is cut off, it takes at least a minute for the generator to kick in and start providing electricity to the house. So, under that scenario, the Raspberry Pi reboots and although the monitoring applications start up again, it just didn't seem to me as though it was foolproof.<u></u><u></u></p> <p><u></u><u></u></p> <p>So, I had been searching for a long time for a reliable, affordable Uninterruptable Power Supply (UPS) for this Raspberry Pi. I could probably have used one for a PC, but that was just too bulky and expensive. I even went so far as to try and design e backup power circuit using supercapacitors (after all, I don't need backup power for more than a minute), however, 'miserable fail' is the only term I can accurately and honestly use to describe that effort. Theoretically, it should have worked, in practice my circuit couldn't supply power for more than about 15 seconds, before the voltage dropped below acceptable levels and the Pi conked out.<u></u><u></u></p> <p><u></u><u></u></p> <p>Enough about that. By having Adafruit on my Twitter feed, I became aware of their PowerBoost 500C charger. Digging a little deeper, I found that this could also act as a UPS for Raspberry Pi, Arduino, etc. So, I decided to purchase one.<u></u><u></u></p> <p><u></u><u></u></p> <p>It arrived in mere days, which is pretty much a miracle when you realize it had to cross the US-Canada border. I soldered the USB connector in place (this is the output side from where the power is fed to the micro USB connector on the Pi), plugged in the Lithium Ion Polymer Battery I purchased at the same time and plugged the whole ensemble into the wall outlet via a standard USB wallmount charger. No smoke! On the contrary, the on-board LEDs lit up (and bright they are). I had to wait a little while until the yellow LED (indicating battery is charging) was replaced by the green LED (indicating battery charged).<u></u><u></u></p> <p><u></u><u></u></p> <p>Following that, a made a little YouTube video illustrating what happens when you plug a Pi into the output side, and then unplug the wall power etc.</p> <p><iframe height="315" src="//www.youtube.com/embed/MiE6Q16FCBE" frameborder="0" width="560" allowfullscreen="allowfullscreen"></iframe></p> <p><u></u><u></u></p> <p><u></u><u></u></p> <p><u></u><u></u></p> <p><u></u><u></u></p> <p><u></u><u></u></p> <p><u></u><u></u></p> <p>In the video, I only have the Pi running off the battery for a short while (approximately 30 seconds), which should be sufficient for my purposes. However, in a subsequent test, I had the wall plug removed for more than 3.5 hours and the battery (2500 mAh) was still going strong.<u></u><u></u></p> <p><u></u><u></u></p> <p>So I think I finally have a reliable solution for my UPS problems.</p> Keith Hekkerhttp://www.blogger.com/profile/14927940751983969101noreply@blogger.com5tag:blogger.com,1999:blog-21464525.post-26497552946693605752014-02-02T20:30:00.001-05:002014-02-02T20:31:23.584-05:00Raspberry Pi and peer to peer networking<p> </p> <p>Normally, when using the Pi and doing development work, I use it headlessly. It sits somewhere in the basement and I communicate with it using Remote Desktop. However, since I am testing NFC (Near Field Communications), I had a need to be close to the Pi when doing development work.</p> <p>So I installed peer-to-peer networking. After the usual Google searches, I was able to piece together how to do it. This write up really is for my own reference so I will know how to do it at some future point in time. Note that I am doing this using a cable, not a wireless dongle. Apparently using a wireless dongle is possible as well, I just haven’t tried it.</p> <p>First my setup: I work from a Windows 8.1 lap top and and when the Pi is setup in the basement, I wirelessly connect to it, via my router of course.</p> <p>Be forwarned, unfortunately, there is no way to make the necessary changes on the Pi without hooking it up to a monitor of sorts, be it through HDMI, VGA.</p> <p>So, to install peer-to-peer networking: Take an Ethernet cable of any length, I used a short six feet (2m) cable, and plug the ends into the PC and the Pi. Restart the PI. Don’t worry about this cable needing to be a cross over type: the Pi is auto sensing.</p> <p>Next, in Windows, go to Start, run and type in ‘cmd’ (no quotes). In the command window that appears type ‘ipconfig’ (no quotes). In the listing that follows, look for a heading that says ‘Ethernet adapter Ethernet’. Within that section, look for Autoconfiguration IPv4 address and write down the address that follows this. Mine was 169.254.212.0. On the next line, you should see Subnet Mask, write this down well, mine was 255.255.0.0</p> <p>On the Pi, type ‘sudo nano /etc/network/interfaces’ (no quotes) In the file that opens make sure that the following lines appear:</p> <p><code>auto lo</code></p> <p><code></code><code>iface lo inet loopback</code></p> <p><code>auto eth0</code></p> <p><code>iface eth0 inet static</code></p> <p><code>address 169.254.212.1</code></p> <p><code></code><code>netmask 255.255.0.0</code></p> <p>Modify the address line with your own IP address that you found on your PC, but add 1 to the last number (look at my example). Furthermore, use exactly the same subnet mask on the Pi as on the PC. You can comment out any other lines that appear by putting a # hastag in front of it.</p> <p>Save (Control-x, then press Y). Reboot the Pi and test.</p> <p>I found Remote Desktop as well as my wireless to the router and Internet to be working as usual.</p> Keith Hekkerhttp://www.blogger.com/profile/14927940751983969101noreply@blogger.com0tag:blogger.com,1999:blog-21464525.post-66346707714857221282014-01-19T23:20:00.001-05:002014-08-02T13:55:41.736-04:00Raspberry Pi: clean install of NFCPY<p>NFCPY is a Python library allowing for Near Field Communications readers to be accessed directly from Raspberry Pi. As part of a project I am working on, I needed to install this library on a clean Raspbian install of a Raspberry Pi.</p> <p>Once the installation was completed I used the base ‘history’ command to retrieve all the commands that I typed in the terminal to accomplish this task. I removed all the commands that were wrong or superfluous.</p> <p>Please note that this was a brand new install: I used a NOOBs SD card and chose Raspbian. Once the operating system was installed, the following commands were typed in:</p> <p>$  sudo nano /boot/cmdline.txt <br />$  sudo nano /etc/inittab</p> <p>The above two commands need to be modified in order to free up the UART on the Pi (to be totally honest, I’m not sure if this needs to be done for nfcpy.) Follow <a href="http://www.irrational.net/2012/04/19/using-the-raspberry-pis-serial-port/">these</a> directions from Clayton Smith’s website. Initially, I used Adafruit’s website for these instructions, but they were not totally clear.</p> <p>$  sudo raspi-config</p> <p>This allows you to personalize your Pi through the Raspbian Configuration utility.</p> <p> <br />$  sudo reboot –n</p> <p>A reboot to ensure it all still works.</p> <p> <br />$  sudo rpi-update</p> <p>Update the firmware on the Pi</p> <p>$  sudo apt-get update <br />$  sudo apt-get upgrade</p> <p>Install the latest Raspbian operating system updates and upgrades</p> <p> <br />$  sudo apt-get install xrdp</p> <p>Install Remote Desktop</p> <p> <br />$  sudo apt-get install python-dev <br />$  curl -O <a href="http://python-distribute.org/distribute_setup.py">http://python-distribute.org/distribute_setup.py</a> <br />$  sudo python distribute_setup.py <br />$  curl -O <a href="https://raw.githubusercontent.com/pypa/pip/master/contrib/get-pip.py">https://raw.githubusercontent.com/pypa/pip/master/contrib/get-pip.py</a></p> <p>$  sudo python get-pip.py <br />$  sudo pip install virtualenv</p> <p>Install (fairly) standard Python utilities</p> <p> <br />$  sudo pip install pyserial</p> <p>Install serial library for Python used by NFCPY.</p> <p> <br /> <br />$  sudo apt-get install bzr</p> <p>Install Launchpad bazaar, a VCS (Version Control System), which allows you to download NFCPY.</p> <p> <br />$  mkdir pythonprogs <br />$  cd pythonprogs</p> <p>Above 2 commands create and then switch to the directory where my Python programs will be stored on this machine.</p> <p> <br />$  bzr branch lp:nfcpy</p> <p>Download the latest version of NFCPY.</p> <p> <br />$  cd /pythonprogs/nfcpy/examples</p> <p>Switch to the examples directory of NFCPY.</p> <p> <br />$  python tagtool.py --device tty:AMA0:pn53x show</p> <p>Run the Python program tagtool. The ‘device’ switch following tagtool.py specifies that we want to use the serial device.</p> <p>Then put a card near the reader: partial success. There is output on the screen, but the reader cannot read the card, apparently NFCPY cannot handle MIFARE Classic 1K cards. So I ordered some other ones, hopefully they will work!</p> <p><strong>UPDATE 2014/01/29 The cards I ordered from OverAir Proximity Technologies do work! They are </strong></p> <p>NFC Card - White - NTAG203 PAC-N3Card</p> <p><strong>UPDATE 2014/08/02 I purchased a Raspberry Pi B+ (mainly for the form factor) and found I needed to modify to contents of some of the links above in order to successfully install nfcpy.</strong></p> Keith Hekkerhttp://www.blogger.com/profile/14927940751983969101noreply@blogger.com5tag:blogger.com,1999:blog-21464525.post-43410128611177311582013-12-16T19:06:00.001-05:002013-12-16T19:06:14.395-05:00Raspberry Pi, CherryPy and asynchronous communication<p>I have a project I am working on where graphical output is required to be sent to the screen. Using Python, where by default the output only goes to the console, one then immediately thinks of Tkinter, which is a Python module that provides a graphical interface to Python.</p> <p>However, that would mean learning yet another ‘language’ as such. So I decided to pursue a different avenue. I am fairly familiar with HTML and JavaScript, even though the latter is extremely quirky and non-intuitive. Therefore, I let my gaze wander to a browser based solution.</p> <p>Now what I am looking for goes beyond the request-response model usually found in browser based interfaces: the user clicks on a button (or link), the browser sends the information to the server, the server responds by sending the requested information and the connection closes. A classic case of synchronous communication.</p> <p>What I am looking for is a scenario where the browser’s window is automatically updated with new information as it becomes available and no if any user action is required. Asynchronous communication.</p> <p>So I started researching. What I really wanted was a short, sweet, concise code example of how to do asynchronous communication with Python. There were lots of examples, code pieces, snippets for Bottle, Flask, Tornado (all Python based webservers), but nothing that was simple enough to just run.</p> <p>I even spent some time installing node.js, which is supposed to be the next best thing since HTML. Once installed though, I couldn’t find any examples that were straight forward enough or worked ‘out-of-the-box’.</p> <p>Finally, I hit on <a href="http://www.cherrypy.org">CherryPy</a>, yet another Python based web server module, specifically this page in their wiki:<a title="http://tools.cherrypy.org/wiki/Comet" href="http://tools.cherrypy.org/wiki/Comet">http://tools.cherrypy.org/wiki/Comet</a>. On this page, author Dan McDougall discusses CherryPy approach to ‘Comet’. Comet (a cleanser). Ajax (a cleanser), however in IT lingo, Ajax can provide feedback to a browser without the entire browser window having to be refreshed. I decided to download CherryPy, run the application in Python, open a browser and voila: it worked. Success, end of a long searching marathon.</p> <p>CherryPy does multithreading on its own. At its peak I had three web browsers open on various machines and the CPU Usage meter barely moved. Another neat feature that I discovered is that when I saved a new version of the application in Geany, with the old application still running, it will shut the old application down and start the new one all on its own.</p> <p>The output of the application I downloaded spits out the results of pings to an ip address or web site you provide. The results just keep on streaming by on the screen. Great example, but not what I needed: what I wanted was a number of string that simply replaced the previous string, i.e. automatic updating of a value. More searching. This time I stumbled on a neat solution provided by <a href="http://encosia.com/easy-incremental-status-updates-for-long-requests/">Encosia</a> (Dave Ward), which I adapted to work with my application.</p> <p>The original CherryPy application created an iframe at the bottom of the HTML (just before the </body> tag). The asynchronous info being sent from the server gets displayed there. An iframe is really a webpage within your web page, usually used for ads.</p> <p>Dave Ward’s solution is to hide the iframe (style=”display:none”), then have the web server send a function call embedded in the asynchronous communication along with the latest value of whatever we are trying to display. The function called is on our main web page (‘the parent’) and so can update anything on that page. The iframe is still there and working, but invisible to us.</p> <p>In my case, I have a table and the content of the function simply reads:</p> <p>document.getElementById(‘11’).innerHTML = cTimeReceived</p> <p>where ‘11’ is the id of the first table data cell and cTimeReceived the new value received from the server.</p> <p>I tested this on the Raspberry Pi’s various assortment of web browsers. It worked properly on Midori, Chromium, Luakit, Iceweasel but not on Dillo or Netsurfer. I let it run for hours without a problem. I ran it from a laptop elsewhere and  intentionally interrupted the network connection, and, once the connection was re-established, the page kept right on going.</p> <p>On the Pi, Luakit by far required the least amount of resources (around 30% in CPU Usage Monitor), while Chromium maxed out at 100%, the others somewhere in between.</p> <p>Obligatory screen shot:</p> <p><a href="http://lh6.ggpht.com/-EHkPkEoTBbs/Uq-VcqUo3DI/AAAAAAAAKTA/PhYhBHpKVmQ/s1600-h/cherrypyscreenshot4.jpg"><img title="cherrypyscreenshot" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="cherrypyscreenshot" src="http://lh6.ggpht.com/-SVCv32cClxQ/Uq-VdFniScI/AAAAAAAAKTI/4inqBPSXFTo/cherrypyscreenshot_thumb1.jpg?imgmax=800" width="475" height="179" /></a></p> <p>Below the application. I ran this on the Raspberry Pi Model B, 512 Mb, with Raspbian updated to 2013-12-15. For it to run, you need to have Python (my version 2.7). To install CherryPy:</p> <p>pip install cherrypy</p> <p>Then copy the application below, change my IP address (towards the bottom) to your own, change my ‘pythonprogs’ directory name to you directory name and run. Open up a browser, point to your IP address, followed by :8080 and press enter. Once the page appears click’ Start’. That’s it!</p> <p>Note that the HTML for the starting page is embedded in the application, hence 1 file is all you need to run this example.</p> <pre class="csharpcode">#!/usr/bin/env python<br /># -*- coding: utf-8 -*-<br /><br /># string.Template requires Python 2.4+<br />from string import Template<br />import cherrypy<br />import time,datetime<br /><br />__author__ = 'Dan McDougall <span class="kwrd"><</span><span class="html">YouKnowWho</span>@<span class="attr">YouKnowWhat</span>.<span class="attr">com</span><span class="kwrd">></span>'<br /><br /># Trying to cut down on long lines...<br />jquery_url = 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js'<br />jquery_ui_url = 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js'<br />jquery_ui_css_url = \<br />'http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/black-tie/jquery-ui.css'<br /><br />class Comet(object):<br /> """An example of using CherryPy for Comet-style asynchronous communication"""<br /> @cherrypy.expose<br /> def index(self):<br /> """Return a basic HTML page with a ping form, a kill form, and an iframe"""<br /> # Note: Dollar signs in string.Template are escaped by using two ($$)<br /> html = """\<br /><span class="kwrd"><!</span><span class="html">DOCTYPE</span> <span class="attr">html</span> <span class="attr">PUBLIC</span> <span class="kwrd">"-//W3C//DTD XHTML 1.0 Transitional//EN"</span> <span class="kwrd">"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"</span><span class="kwrd">></span><br /><span class="kwrd"><</span><span class="html">html</span> <span class="attr">xmlns</span><span class="kwrd">="http://www.w3.org/1999/xhtml"</span> <span class="attr">lang</span><span class="kwrd">="en"</span><span class="kwrd">></span><br /> <span class="kwrd"><</span><span class="html">head</span><span class="kwrd">></span><br /> <span class="kwrd"><</span><span class="html">link</span> <span class="attr">rel</span><span class="kwrd">="stylesheet"</span> <span class="attr">type</span><span class="kwrd">="text/css"</span> <span class="attr">href</span><span class="kwrd">="${jquery_ui_css_url}"</span> <span class="attr">media</span><span class="kwrd">="screen"</span> <span class="kwrd">/></span><br /> <span class="kwrd"><</span><span class="html">script</span> <span class="attr">type</span><span class="kwrd">="text/javascript"</span> <span class="attr">src</span><span class="kwrd">="${jquery_url}"</span><span class="kwrd">></</span><span class="html">script</span><span class="kwrd">></span><br /> <script type=<span class="str">"text/javascript"</span> src=<span class="str">"${jquery_ui_url}"</span>></script><br /> <br /> <style><br /> input[type=button], input[type=submit], input[type=reset] {<br /> background: #eee;<br /> color: #222;<br /> border: 1px outset #ccc;<br /> padding: .1em .5em;<br />}<br /></style><br /> <script type=<span class="str">"text/javascript"</span>><br /> <span class="kwrd">function</span> UpdateProgress(cTimeReceived)<br /> {<br /> document.getElementById(<span class="str">'score'</span>).innerHTML = cTimeReceived<br /> }<br /><span class="kwrd"></</span><span class="html">script</span><span class="kwrd">></span><br /> <br /> </head><br /> <body><br /> <script type=<span class="str">"text/javascript"</span>><br /> $$(<span class="kwrd">function</span>(){<br /> $$(<span class="str">'#result'</span>).hide();<br /> $$(<span class="str">'#kill_ping'</span>).click(<span class="kwrd">function</span>() {<br /> $$.ajax({<br /> url: <span class="str">"/kill_proc"</span>,<br /> cache: <span class="kwrd">false</span>,<br /> success: <span class="kwrd">function</span>(html){<br /> window.frames[0].stop();<br /> $$(<span class="str">"#result"</span>).html(html);<br /> $$(<span class="str">"#result"</span>).show(<span class="str">'slow'</span>);<br /> }<br /> });<br /> <span class="kwrd">return</span> <span class="kwrd">false</span>;<br /> });<br /> });<br /> <span class="kwrd"></</span><span class="html">script</span><span class="kwrd">></span><br /> <script type=<span class="str">"text/javascript"</span>><br /> $$(<span class="kwrd">function</span>(){<br /> $$(<span class="str">'#ping'</span>).click(<span class="kwrd">function</span>() {<br /> $$(<span class="str">'#result'</span>).hide();<br /> });<br /> });<br /> <span class="kwrd"></</span><span class="html">script</span><span class="kwrd">></span><br /> <h3>CherryPy Comet Example</h3><br /> <form id=<span class="str">"ping_form"</span> target=<span class="str">"console_iframe"</span> method=<span class="str">"post"</span> action=<span class="str">"/ping"</span>><br /> <input id=<span class="str">"ping"</span> type=<span class="str">"submit"</span> value=<span class="str">"Start"</span>></input><br /> </form><br /> <BR><br /> <form id=<span class="str">"kill_form"</span> method=<span class="str">"post"</span> action=<span class="str">"/kill_proc"</span>><br /> <input id=<span class="str">"kill_ping"</span> type=<span class="str">"submit"</span> value=<span class="str">"Stop"</span>></input><br /> </form><br /> <div id=<span class="str">"result"</span> <span class="kwrd">class</span>=<span class="str">"ui-state-highlight"</span>><br /> <span <span class="kwrd">class</span>=<span class="str">"ui-icon ui-icon-check ui-icon-left"</span> style=<span class="str">"margin-right: .3em;"</span>><br /> </span><br /> </div><br /> <div><table><tr><td id=<span class="str">"score"</span> style=<span class="str">"font-size:96px"</span>></td><td></td></tr></table></div><br /> <iframe name=<span class="str">"console_iframe"</span> style=<span class="str">"display:none"</span>/> <br /> </body><br /></html><br /><span class="str">""</span><span class="str">"<br /> t = Template(html)<br /> page = t.substitute(<br /> jquery_ui_css_url=jquery_ui_css_url,<br /> jquery_url=jquery_url,<br /> jquery_ui_url=jquery_ui_url)<br /> return page<br /><br /> @cherrypy.expose<br /> def ping(self, **kw):<br /> "</span><span class="str">""</span>Start a time loop (reporting time and stream the output<span class="str">""</span><span class="str">"<br /> def run_command():<br /> while True: <br /> now = datetime.datetime.now()<br /> cTime = str(now)[:22]<br /> yield '<script>parent.UpdateProgress("</span><span class="str">'+ cTime + '</span>")<span class="kwrd"></</span><span class="html">script</span><span class="kwrd">></span>'<br /> time.sleep(2)<br /> <br /> return run_command()<br /> <br /> # Enable streaming for the ping method. Without this it won't work.<br /> ping._cp_config = {'response.stream': True}<br /><br /> @cherrypy.expose<br /> def kill_proc(self, **kw):<br /> """Kill the process """<br /> return "<span class="kwrd"><</span><span class="html">strong</span><span class="kwrd">></span>Success:<span class="kwrd"></</span><span class="html">strong</span><span class="kwrd">></span> The process was stopped successfully."<br /><br />cherrypy.config.update({<br /> 'log.screen':True,<br /> 'tools.sessions.on': True,<br /> 'checker.on':False,<br /> 'server.socket_host':'192.168.0.135',<br /> 'tools.staticdir.root': '/home/pi/pythonprogs',<br /> 'tools.staticdir.on': True,<br /> 'tools.staticdir.dir':'static'})<br />cherrypy.tree.mount(Comet(), config=None)<br />cherrypy.engine.start()<br />cherrypy.engine.block()</pre><br /><style type="text/css"><br />.csharpcode, .csharpcode pre<br />{<br /> font-size: small;<br /> color: black;<br /> font-family: consolas, "Courier New", courier, monospace;<br /> background-color: #ffffff;<br /> /*white-space: pre;*/<br />}<br />.csharpcode pre { margin: 0em; }<br />.csharpcode .rem { color: #008000; }<br />.csharpcode .kwrd { color: #0000ff; }<br />.csharpcode .str { color: #006080; }<br />.csharpcode .op { color: #0000c0; }<br />.csharpcode .preproc { color: #cc6633; }<br />.csharpcode .asp { background-color: #ffff00; }<br />.csharpcode .html { color: #800000; }<br />.csharpcode .attr { color: #ff0000; }<br />.csharpcode .alt <br />{<br /> background-color: #f4f4f4;<br /> width: 100%;<br /> margin: 0em;<br />}<br />.csharpcode .lnum { color: #606060; }</style> Keith Hekkerhttp://www.blogger.com/profile/14927940751983969101noreply@blogger.com0tag:blogger.com,1999:blog-21464525.post-70193926359299264652013-12-16T15:41:00.001-05:002013-12-16T15:41:29.178-05:00Raspberry Pi: 1.6 million records in SQLite database.<p>Yesterday was a bit of a milestone for me in that my <a href="http://khekker.blogspot.ca/2013/01/keeping-tabs-on-my-standby-generator.html">generator monitoring application</a> had been in production for one year exactly.</p> <p>During this time, the SQLite database logged 1,630,000+ temperature records, (3 records every minute). There have been a few minor hiccups, programming bugs, which I corrected, but other than that, the Raspberry Pi and Python have acted flawlessly. No mysterious hang ups, no unresponsiveness, it just plain old works.</p> <p>It faithfully still sends out SMS messages and emails when needed. The webserver also works flawlessly all of the time.</p> <p>I’m especially impressed with the <a href="http://www.gnuplot.info/">gnuplot</a> utility. Quirky to set up, yes, but very solid and quick to generate complex graphs.</p> <p>The graph below (from today), as an example, shows high and low temperatures, by the hour, over the last year at our house. The SQLite database selected 8,750 records out of the 1.6 million, handing them over to Gnuplot which plotted this dataset, in about 3 seconds. Not bad at all.</p> <p>Interesting things about the graph:</p> <p>- spread between daytime highs and nighttime lows in a lot larger in summer than in winter.</p> <p>- frost free period was from end of April till end of October, not bad considering the cold spring we had here in southern Ontario.</p> <p>- so far December this year is shaping up to be a heck of a lot colder than last year.</p> <p><a href="http://lh5.ggpht.com/-nqLN26trWLo/Uq9lcs3bYAI/AAAAAAAAKSo/WyYWioWaRY0/s1600-h/tempsextended%25255B5%25255D.gif"><img title="tempsextended" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="tempsextended" src="http://lh4.ggpht.com/-ElFhEixAbTw/Uq9ldnKqiZI/AAAAAAAAKSw/Nl_TxgKgO_M/tempsextended_thumb%25255B3%25255D.gif?imgmax=800" width="468" height="416" /></a></p> Keith Hekkerhttp://www.blogger.com/profile/14927940751983969101noreply@blogger.com0tag:blogger.com,1999:blog-21464525.post-19921747325042324612013-09-30T23:24:00.001-04:002013-09-30T23:24:28.925-04:00Raspberry Pi and a solution waiting for a problem…<p>Well, not quite, maybe. Let me explain. In previous posts, I played around getting the Nokia 5110 LCD to properly work with the Raspberry Pi. After the usual set of problems, it fully cooperated and I incorporated it as my display in my carputer.</p> <p>Now, I would like to mount the display on the dash of my car somewhere, but, although the Pi is small, it is still too big to fit in properly.</p> <p>So I got to thinking, what if I place the display at some distance from the Pi? But how? I decided to go out on a limb and use RJ45 connectors and jacks.</p> <p>I bought some RJ45 jacks via Ebay and some RJ45 breakout <a href="http://www.cooking-hacks.com/shop/shields/breakout-boards/breakout-board-for-rj45" target="_blank">boards from Cooking Hacks</a>. These breakout boards, at a whopping $0.50 a board, allow for easy hookups of the RJ 45 jack to the wires that lead either from the Pi’s GPIO pins or go to the input pins on the Nokia LCD.</p> <p>After soldering the jacks and the wires to the breakout boards, it was time to grab an Ethernet cable (6 feet in this case), slip the ends into the jacks and turn on the Pi. At first, the results were ambiguous. Sometimes, the display showed correct results, sometimes there were errors in the text, sometimes nothing. So I decided to do some experimenting. </p> <p>I noticed that the speed at which the Nokia LCD was processing data was set at 5Mhz. I reduced this to .5 Mhz. Voila, it worked.</p> <p>Next, I tried a longer cable, 24 feet. No problem! I don’t have a longer cable, it would be interesting to see what would happen.</p> <p>Here’s the 24 foot cable in action (the error message on the display has nothing to do with this project).</p> <p> <a href="http://lh3.ggpht.com/-8mPSBp1fghs/UkpAZPOMBWI/AAAAAAAAKPs/FX0WwH7CSmI/s1600-h/IMG_9673%25255B4%25255D.jpg"><img title="IMG_9673" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="IMG_9673" src="http://lh5.ggpht.com/-Q2a2vF0KERg/UkpAa8xfPEI/AAAAAAAAKP0/wFiu20BBD78/IMG_9673_thumb%25255B1%25255D.jpg?imgmax=800" width="456" height="317"></a></p> <p>So, if you need a display a long way away from the Pi itself, may be this is the way to go…</p> Keith Hekkerhttp://www.blogger.com/profile/14927940751983969101noreply@blogger.com0tag:blogger.com,1999:blog-21464525.post-40829652365851384632013-09-03T18:03:00.001-04:002013-09-03T18:03:20.590-04:00Raspberry Pi and “sudo poweroff”<p>A few weeks ago I ordered a power switch for the Raspberry Pi from <a href="http://www.mausberrycircuits.com">www.mausberrycircuits.com</a> with the aim of using this on my Raspi Fuel Miser setup. (see previous post).</p> <p><img alt="Illuminated LED shutdown switch" src="http://cdn.shopify.com/s/files/1/0262/4243/products/photo-4_1024x1024.jpg?13" width="438" height="312"></p> <p>When this switch is situated between your Raspberry Pi’s power jack and your phone charger and you plug in the phone charger, nothing happens. You first need to press the button before the Pi lights up. Then, once you are through using it, you keep the button pressed for 2 seconds and the Pi powers off.</p> <p>You need to hook up 2 wires from the switch to the GPIO pins, suggested are 23 and 24. If you do use other ones, then you need to modify /etc/switch.sh after you run setup .sh.</p> <p>I first ran it using the switch.sh configuration, but then I got to thinking, why run it separately when it can be included in my Python script, which is running continuously anyway, dutifully computing gas mileage.</p> <p>So I amended my script, created a new function has_ShutdownButtonBeenPressed and inserted a call to it at the start of the main loop in the obd_capture.py program. This way, it will check for a pressed key once every 3/4 second or so, the time it takes to complete a cycle of OBDII readings. I disabled the switch.sh and remove the entry in /etc/rc.local which started up switch.sh upon start up.</p> <p>The ‘sudo poweroff’ command, by the way, cuts all power to the board, so it is completely disabled, unlike the ‘shutdown’ command, which still leaves the main red LED lit.</p> <p>Now, at the end of my trip, if I have used the fuel miser, all I need to do is press the power button for 2 seconds, and the Python script will close the SQlite database gracefully before issuing the os.system(“sudo poweroff”) command.</p> Keith Hekkerhttp://www.blogger.com/profile/14927940751983969101noreply@blogger.com3tag:blogger.com,1999:blog-21464525.post-86742177034662838282013-08-27T20:28:00.001-04:002013-08-31T12:36:04.274-04:00Raspberry Pi MPG Fuel Economy Carputer<p>I’ve always had an interest in getting the best fuel economy out of cars that I drove. On June 14, 2006, <a href="http://khekker.blogspot.com/2006/06/toyota-echo-gas-mileage.html" target="_blank">I blogged</a> about my Toyota Echo and its gas mileage. I’ve often wanted to buy a device like <a href="http://www.scangauge.com/" target="_blank">ScanGauge</a> so I could see the instantaneous fuel economy I was getting.</p> <p>A few weeks ago, the Raspberry.org site’s main item had to do with carputers. That, along with an article about the <a href="http://www.raspberrypi.org/archives/3109" target="_blank">Quite Rubbish clock</a> got me to thinking, how about a Raspberry Pi MPG reader?</p> <p>Here’s the final result, well final, there is no case and I’m also thinking of adding various buttons to vary the output of the display. But for the moment…</p> <div id="scid:5737277B-5D6D-4f48-ABFC-DD9C333F4C5D:5df74c2b-9a53-493a-8174-b408ff307ec3" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"><div><object width="420" height="315"><param name="movie" value="//www.youtube.com/v/6dJqTy2xBUk?version=3&hl=en_GB"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="//www.youtube.com/v/6dJqTy2xBUk?version=3&hl=en_GB" type="application/x-shockwave-flash" width="420" height="315" allowscriptaccess="always" allowfullscreen="true"></embed></object></div><div style="width:420px;clear:both;font-size:.8em">The MPG computer in action</div></div> <p>The top line lists the average of the last 3 seconds. The bottom line the average for the entire trip, followed by the duration of the trip in minutes. Since I’m in Canada, where fuel economy is expressed in litres consumed per 100 km driven, that’s what it is set up for. However, it would be fairly trivial to change the formula and output based on the user’s preference.</p> <p>The code is based on Martin O’Hanlon’s obd_capture.py. However, I made significant changes in that the data is being captured into a SQLite3 database (which is actually used to compute the 3 second average). Furthermore, I included the output to the LCD in this class as well. (Yeah, I know, you shouldn’t be mixing data with user interface, but hey, I’m not getting paid for this, so I can do whatever I want.)</p> <p>Every time the car starts, a new database with a unique name is created. It takes about 15 seconds for the Pi to boot and another 5 seconds for the Python script to start running. This database can be retrieved later for further analysis of the car’s performance.</p> <p>The fuel economy is derived from the values given by the ELM327 (about $12 from China) OBDII reader for MAF (Mass Airflow meter) and VSS (vehicle speed). The formula for litres per 100 km is:</p> <p>(3600 * MAF)/(9069.90 * VSS)</p> <p>Credit for this formula should go to <a href="http://www.lightner.net/lightner/bruce.html" target="_blank">Bruce Lightner</a>, who is probably one of the most knowledgeable people on this planet when it comes to OBDII (and moon rocks and more stuff)</p> <p>The output goes to a Nokia 5110 LCD (Deal Extreme $5.60). In order to get a fairly large readout for driver visibility, I actually created an image in Python and then place that image on the LCD as a whole. </p> <p>The OBDII connector in my car points downwards, and that might interfere with the driver feet, so I’ve ordered an extension cable from China for a few dollars which will alleviate this problem. This cable exits at right angles from the connector.</p> <p>If you intend to build this device using the Nokia 5110 LCD, you’ll need to read my previous post regarding this device.</p> <p>For Martin O’Hanlon excellent code, and related libraries type this at the command prompt:</p> <p><code>sudo apt-get install python-serial<br>sudo apt-get install git-core<br>cd ~<br>git clone https://github.com/martinohanlon/pyobd<br>cd pyobd<br>python obd_capture.py</code></p> <p>Here is my version of obd_capture.py:</p> <div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: 'Courier New', courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 200px; width: 97.5%; background-color: #f4f4f4"> <div id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum1" style="color: #606060"> 1:</span> #!/usr/bin/env python</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum2" style="color: #606060"> 2:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum3" style="color: #606060"> 3:</span> import obd_io</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum4" style="color: #606060"> 4:</span> import serial</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum5" style="color: #606060"> 5:</span> import platform</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum6" style="color: #606060"> 6:</span> import obd_sensors</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum7" style="color: #606060"> 7:</span> from datetime import datetime</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum8" style="color: #606060"> 8:</span> from PIL import Image,ImageDraw,ImageFont</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum9" style="color: #606060"> 9:</span> import ImageOps</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum10" style="color: #606060"> 10:</span> import nokiaSPI</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum11" style="color: #606060"> 11:</span> import time</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum12" style="color: #606060"> 12:</span> import os</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum13" style="color: #606060"> 13:</span> import sqlite3</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum14" style="color: #606060"> 14:</span> #from datetime import timedelta</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum15" style="color: #606060"> 15:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum16" style="color: #606060"> 16:</span> from obd_utils import scanSerial</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum17" style="color: #606060"> 17:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum18" style="color: #606060"> 18:</span> <span style="color: #0000ff">class</span> OBD_Capture():</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum19" style="color: #606060"> 19:</span> def __init__(self):</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum20" style="color: #606060"> 20:</span> self.port = None</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum21" style="color: #606060"> 21:</span> localtime = time.localtime(time.time())</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum22" style="color: #606060"> 22:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum23" style="color: #606060"> 23:</span> def connect(self):</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum24" style="color: #606060"> 24:</span> portnames = scanSerial()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum25" style="color: #606060"> 25:</span> print portnames</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum26" style="color: #606060"> 26:</span> <span style="color: #0000ff">for</span> port <span style="color: #0000ff">in</span> portnames:</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum27" style="color: #606060"> 27:</span> self.port = obd_io.OBDPort(port, None, 2, 2)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum28" style="color: #606060"> 28:</span> <span style="color: #0000ff">if</span>(self.port.State == 0):</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum29" style="color: #606060"> 29:</span> self.port.close()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum30" style="color: #606060"> 30:</span> self.port = None</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum31" style="color: #606060"> 31:</span> <span style="color: #0000ff">else</span>:</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum32" style="color: #606060"> 32:</span> <span style="color: #0000ff">break</span></pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum33" style="color: #606060"> 33:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum34" style="color: #606060"> 34:</span> <span style="color: #0000ff">if</span>(self.port):</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum35" style="color: #606060"> 35:</span> print <span style="color: #006080">"Connected to "</span>+self.port.port.name</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum36" style="color: #606060"> 36:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum37" style="color: #606060"> 37:</span> def is_connected(self):</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum38" style="color: #606060"> 38:</span> <span style="color: #0000ff">return</span> self.port</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum39" style="color: #606060"> 39:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum40" style="color: #606060"> 40:</span> def nokiprint(self,cShortTerm,cLongTerm,cLongTermMinutes):</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum41" style="color: #606060"> 41:</span> cShortTerm = cShortTerm[:4]</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum42" style="color: #606060"> 42:</span> cLongTerm = cLongTerm[:4]</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum43" style="color: #606060"> 43:</span> noki.cls()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum44" style="color: #606060"> 44:</span> im = Image.<span style="color: #0000ff">new</span>(<span style="color: #006080">'1'</span>, (84,48))</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum45" style="color: #606060"> 45:</span> draw = ImageDraw.Draw(im)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum46" style="color: #606060"> 46:</span> print cShortTerm</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum47" style="color: #606060"> 47:</span> draw.text((0,0),cShortTerm, font=font, fill=1)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum48" style="color: #606060"> 48:</span> draw.text((0,24),cLongTerm, font=font, fill=1)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum49" style="color: #606060"> 49:</span> draw.text((54,7),<span style="color: #006080">"3 s"</span>, font=fontsmall, fill=1)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum50" style="color: #606060"> 50:</span> draw.text((54,31),cLongTermMinutes + <span style="color: #006080">" m"</span>, font=fontsmall, fill=1)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum51" style="color: #606060"> 51:</span> # Copy it to the display</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum52" style="color: #606060"> 52:</span> noki.show_image(im)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum53" style="color: #606060"> 53:</span> #noki.next_row()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum54" style="color: #606060"> 54:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum55" style="color: #606060"> 55:</span> def ComputeFuelConsumption(self):</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum56" style="color: #606060"> 56:</span> nCurrentTime = time.time()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum57" style="color: #606060"> 57:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum58" style="color: #606060"> 58:</span> <span style="color: #0000ff">try</span>:</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum59" style="color: #606060"> 59:</span> nStart = nCurrentTime - 20</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum60" style="color: #606060"> 60:</span> cLimit = <span style="color: #006080">" and time_read > "</span> + str(nStart) + <span style="color: #006080">" order by time_read desc limit 6"</span></pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum61" style="color: #606060"> 61:</span> cursor.execute(<span style="color: #006080">''</span><span style="color: #006080">'SELECT maf,speed from SensorReadings where speed > "0" and maf > "0" and rpm != "NODATA" '</span><span style="color: #006080">''</span> + cLimit)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum62" style="color: #606060"> 62:</span> data = cursor.fetchall()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum63" style="color: #606060"> 63:</span> except sqlite3.OperationalError,msg:</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum64" style="color: #606060"> 64:</span> <span style="color: #0000ff">return</span> msg</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum65" style="color: #606060"> 65:</span> #print len(data)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum66" style="color: #606060"> 66:</span> <span style="color: #0000ff">if</span> (len(data) > 0):</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum67" style="color: #606060"> 67:</span> nFuelConsumption = 0</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum68" style="color: #606060"> 68:</span> <span style="color: #0000ff">for</span> x <span style="color: #0000ff">in</span> range(0,len(data)):</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum69" style="color: #606060"> 69:</span> nFuelConsumption += (3600 * <span style="color: #0000ff">float</span>(data[x][0]))/(9069.90 * <span style="color: #0000ff">float</span>(data[x][1]))</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum70" style="color: #606060"> 70:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum71" style="color: #606060"> 71:</span> nAvgFuelConsumption = nFuelConsumption/len(data)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum72" style="color: #606060"> 72:</span> print nAvgFuelConsumption</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum73" style="color: #606060"> 73:</span> print type(nAvgFuelConsumption)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum74" style="color: #606060"> 74:</span> #print data[x][0],data[x][1],data[x][2]</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum75" style="color: #606060"> 75:</span> <span style="color: #0000ff">return</span> <span style="color: #006080">"{:5.2f}"</span>.format(nAvgFuelConsumption).lstrip()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum76" style="color: #606060"> 76:</span> <span style="color: #0000ff">else</span>:</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum77" style="color: #606060"> 77:</span> <span style="color: #0000ff">return</span> <span style="color: #006080">"No data"</span></pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum78" style="color: #606060"> 78:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum79" style="color: #606060"> 79:</span> #print nAvgFuelConsum</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum80" style="color: #606060"> 80:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum81" style="color: #606060"> 81:</span> def is_number(self,DataToTest):</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum82" style="color: #606060"> 82:</span> <span style="color: #0000ff">try</span>:</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum83" style="color: #606060"> 83:</span> <span style="color: #0000ff">float</span>(DataToTest)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum84" style="color: #606060"> 84:</span> <span style="color: #0000ff">return</span> True</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum85" style="color: #606060"> 85:</span> except ValueError:</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum86" style="color: #606060"> 86:</span> <span style="color: #0000ff">return</span> False </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum87" style="color: #606060"> 87:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum88" style="color: #606060"> 88:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum89" style="color: #606060"> 89:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum90" style="color: #606060"> 90:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum91" style="color: #606060"> 91:</span> def capture_data(self):</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum92" style="color: #606060"> 92:</span> #Creating <span style="color: #0000ff">new</span> database</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum93" style="color: #606060"> 93:</span> <span style="color: #0000ff">for</span> kounter <span style="color: #0000ff">in</span> range(10000):</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum94" style="color: #606060"> 94:</span> cKounter = <span style="color: #006080">"{0:05d}"</span>.format(kounter)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum95" style="color: #606060"> 95:</span> cNewDatabase = <span style="color: #006080">"obdii"</span> + cKounter + <span style="color: #006080">".db"</span></pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum96" style="color: #606060"> 96:</span> #print cNewDatabase</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum97" style="color: #606060"> 97:</span> <span style="color: #0000ff">if</span> not (os.path.exists(cNewDatabase)):</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum98" style="color: #606060"> 98:</span> #print <span style="color: #006080">"New database name: "</span> + cNewDatabase</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum99" style="color: #606060"> 99:</span> <span style="color: #0000ff">break</span></pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum100" style="color: #606060"> 100:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum101" style="color: #606060"> 101:</span> global conn</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum102" style="color: #606060"> 102:</span> global cursor</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum103" style="color: #606060"> 103:</span> conn = sqlite3.connect(cNewDatabase)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum104" style="color: #606060"> 104:</span> cursor = conn.cursor()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum105" style="color: #606060"> 105:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum106" style="color: #606060"> 106:</span> #Find supported sensors - by getting PIDs from OBD</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum107" style="color: #606060"> 107:</span> # its a <span style="color: #0000ff">string</span> of binary 01010101010101 </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum108" style="color: #606060"> 108:</span> # 1 means the sensor <span style="color: #0000ff">is</span> supported</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum109" style="color: #606060"> 109:</span> self.supp = self.port.sensor(0)[1]</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum110" style="color: #606060"> 110:</span> self.supportedSensorList = []</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum111" style="color: #606060"> 111:</span> self.unsupportedSensorList = []</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum112" style="color: #606060"> 112:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum113" style="color: #606060"> 113:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum114" style="color: #606060"> 114:</span> # loop through PIDs binary</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum115" style="color: #606060"> 115:</span> <span style="color: #0000ff">for</span> i <span style="color: #0000ff">in</span> range(0, len(self.supp)):</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum116" style="color: #606060"> 116:</span> <span style="color: #0000ff">if</span> self.supp[i] == <span style="color: #006080">"1"</span>:</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum117" style="color: #606060"> 117:</span> # store index of sensor and sensor <span style="color: #0000ff">object</span></pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum118" style="color: #606060"> 118:</span> self.supportedSensorList.append([i+1, obd_sensors.SENSORS[i+1]])</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum119" style="color: #606060"> 119:</span> <span style="color: #0000ff">else</span>:</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum120" style="color: #606060"> 120:</span> self.unsupportedSensorList.append([i+1, obd_sensors.SENSORS[i+1]])</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum121" style="color: #606060"> 121:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum122" style="color: #606060"> 122:</span> sqlCreateTable = <span style="color: #006080">"CREATE TABLE SensorReadings (time_read real, "</span></pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum123" style="color: #606060"> 123:</span> sqlInsertTemplate = <span style="color: #006080">"INSERT INTO SensorReadings(time_read, "</span></pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum124" style="color: #606060"> 124:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum125" style="color: #606060"> 125:</span> <span style="color: #0000ff">for</span> supportedSensor <span style="color: #0000ff">in</span> self.supportedSensorList:</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum126" style="color: #606060"> 126:</span> #print <span style="color: #006080">"supported sensor index = "</span> + str(supportedSensor[0]) + <span style="color: #006080">" "</span> + str(supportedSensor[1].shortname)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum127" style="color: #606060"> 127:</span> sqlCreateTable += str(supportedSensor[1].shortname) + <span style="color: #006080">" text,"</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum128" style="color: #606060"> 128:</span> sqlInsertTemplate += str(supportedSensor[1].shortname) + <span style="color: #006080">","</span></pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum129" style="color: #606060"> 129:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum130" style="color: #606060"> 130:</span> sqlCreateTable = sqlCreateTable[:sqlCreateTable.rfind(<span style="color: #006080">","</span>)] + <span style="color: #006080">")"</span></pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum131" style="color: #606060"> 131:</span> #print sqlCreateTable</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum132" style="color: #606060"> 132:</span> <span style="color: #0000ff">try</span>:</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum133" style="color: #606060"> 133:</span> cursor.execute(sqlCreateTable)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum134" style="color: #606060"> 134:</span> conn.commit()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum135" style="color: #606060"> 135:</span> cursor.execute(<span style="color: #006080">''</span><span style="color: #006080">'CREATE INDEX time_read_index on SensorReadings(time_read)'</span><span style="color: #006080">''</span>)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum136" style="color: #606060"> 136:</span> cMessage = <span style="color: #006080">"Database "</span> + cNewDatabase + <span style="color: #006080">" created..."</span></pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum137" style="color: #606060"> 137:</span> except sqlite3.OperationalError,msg:</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum138" style="color: #606060"> 138:</span> cMessage = msg</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum139" style="color: #606060"> 139:</span> noki.cls()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum140" style="color: #606060"> 140:</span> noki.text(cMessage,wrap=True)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum141" style="color: #606060"> 141:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum142" style="color: #606060"> 142:</span> sqlInsertTemplate = sqlInsertTemplate[:sqlInsertTemplate.rfind(<span style="color: #006080">","</span>)] + <span style="color: #006080">") VALUES ("</span></pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum143" style="color: #606060"> 143:</span> #print sqlInsertTemplate</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum144" style="color: #606060"> 144:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum145" style="color: #606060"> 145:</span> time.sleep(3)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum146" style="color: #606060"> 146:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum147" style="color: #606060"> 147:</span> <span style="color: #0000ff">if</span>(self.port <span style="color: #0000ff">is</span> None):</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum148" style="color: #606060"> 148:</span> <span style="color: #0000ff">return</span> None</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum149" style="color: #606060"> 149:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum150" style="color: #606060"> 150:</span> #Loop until Ctrl C <span style="color: #0000ff">is</span> pressed </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum151" style="color: #606060"> 151:</span> <span style="color: #0000ff">try</span>:</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum152" style="color: #606060"> 152:</span> nRunningTotalFuelConsumption = 0</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum153" style="color: #606060"> 153:</span> nStartTime = time.time()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum154" style="color: #606060"> 154:</span> x = 0</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum155" style="color: #606060"> 155:</span> <span style="color: #0000ff">while</span> True:</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum156" style="color: #606060"> 156:</span> current_time = time.time()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum157" style="color: #606060"> 157:</span> #current_time = str(localtime.hour)+<span style="color: #006080">":"</span>+str(localtime.minute)+<span style="color: #006080">":"</span>+str(localtime.second)+<span style="color: #006080">"."</span>+str(localtime.microsecond)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum158" style="color: #606060"> 158:</span> #log_string = current_time + <span style="color: #006080">"\n"</span></pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum159" style="color: #606060"> 159:</span> sqlInsert = sqlInsertTemplate + <span style="color: #006080">'"'</span> + str(current_time) + <span style="color: #006080">'",'</span></pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum160" style="color: #606060"> 160:</span> results = {}</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum161" style="color: #606060"> 161:</span> <span style="color: #0000ff">for</span> supportedSensor <span style="color: #0000ff">in</span> self.supportedSensorList:</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum162" style="color: #606060"> 162:</span> sensorIndex = supportedSensor[0]</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum163" style="color: #606060"> 163:</span> #print sensorIndex</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum164" style="color: #606060"> 164:</span> (name, value, unit) = self.port.sensor(sensorIndex)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum165" style="color: #606060"> 165:</span> #log_string += name + <span style="color: #006080">" = "</span> + str(value) + <span style="color: #006080">" "</span> + str(unit) + <span style="color: #006080">"\n"</span></pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum166" style="color: #606060"> 166:</span> #print value,type(value)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum167" style="color: #606060"> 167:</span> sqlInsert += <span style="color: #006080">'"'</span> + str(value) + <span style="color: #006080">'",'</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum168" style="color: #606060"> 168:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum169" style="color: #606060"> 169:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum170" style="color: #606060"> 170:</span> sqlInsert = sqlInsert[:sqlInsert.rfind(<span style="color: #006080">","</span>)] + <span style="color: #006080">")"</span></pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum171" style="color: #606060"> 171:</span> #print sqlInsert</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum172" style="color: #606060"> 172:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum173" style="color: #606060"> 173:</span> <span style="color: #0000ff">try</span>:</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum174" style="color: #606060"> 174:</span> cursor.execute(sqlInsert)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum175" style="color: #606060"> 175:</span> conn.commit()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum176" style="color: #606060"> 176:</span> except sqlite3.OperationalError,msg:</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum177" style="color: #606060"> 177:</span> noki.cls()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum178" style="color: #606060"> 178:</span> noki.text(msg,wrap=True) </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum179" style="color: #606060"> 179:</span> <span style="color: #0000ff">continue</span></pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum180" style="color: #606060"> 180:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum181" style="color: #606060"> 181:</span> cFuelConsumption = self.ComputeFuelConsumption()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum182" style="color: #606060"> 182:</span> <span style="color: #0000ff">if</span> (cFuelConsumption != <span style="color: #006080">"No data"</span>):</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum183" style="color: #606060"> 183:</span> x += 1</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum184" style="color: #606060"> 184:</span> <span style="color: #0000ff">if</span> (x > 0):</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum185" style="color: #606060"> 185:</span> <span style="color: #0000ff">if</span> (self.is_number):</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum186" style="color: #606060"> 186:</span> nRunningTotalFuelConsumption += <span style="color: #0000ff">float</span>(cFuelConsumption)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum187" style="color: #606060"> 187:</span> nTripAverage = nRunningTotalFuelConsumption/x</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum188" style="color: #606060"> 188:</span> cTripAverage = <span style="color: #006080">"{:5.2f}"</span>.format(nTripAverage).lstrip()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum189" style="color: #606060"> 189:</span> <span style="color: #0000ff">else</span>:</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum190" style="color: #606060"> 190:</span> cTripAverage = <span style="color: #006080">"Nodata"</span></pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum191" style="color: #606060"> 191:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum192" style="color: #606060"> 192:</span> cDurationInMinutes = <span style="color: #006080">"{:3.0f}"</span>.format((current_time - nStartTime)/60).lstrip()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum193" style="color: #606060"> 193:</span> self.nokiprint(cFuelConsumption,cTripAverage,cDurationInMinutes)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum194" style="color: #606060"> 194:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum195" style="color: #606060"> 195:</span> #print log_string,</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum196" style="color: #606060"> 196:</span> #time.sleep(0.5)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum197" style="color: #606060"> 197:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum198" style="color: #606060"> 198:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum199" style="color: #606060"> 199:</span> except KeyboardInterrupt:</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum200" style="color: #606060"> 200:</span> self.port.close()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum201" style="color: #606060"> 201:</span> print(<span style="color: #006080">"stopped"</span>)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum202" style="color: #606060"> 202:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum203" style="color: #606060"> 203:</span> <span style="color: #0000ff">if</span> __name__ == <span style="color: #006080">"__main__"</span>:</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum204" style="color: #606060"> 204:</span> font = ImageFont.truetype(<span style="color: #006080">"/usr/share/fonts/truetype/freefont/FreeSansBold.ttf"</span>,26)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum205" style="color: #606060"> 205:</span> fontsmall = ImageFont.truetype(<span style="color: #006080">"/usr/share/fonts/truetype/freefont/FreeSansBold.ttf"</span>, 16)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum206" style="color: #606060"> 206:</span> # New b-w image</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum207" style="color: #606060"> 207:</span> im = Image.<span style="color: #0000ff">new</span>(<span style="color: #006080">'1'</span>, (84,48))</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum208" style="color: #606060"> 208:</span> noki = nokiaSPI.NokiaSPI(brightness=268) # create display device</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum209" style="color: #606060"> 209:</span> noki.cls()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum210" style="color: #606060"> 210:</span> noki.text(<span style="color: #006080">"Initializing.."</span>,wrap=True)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum211" style="color: #606060"> 211:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum212" style="color: #606060"> 212:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum213" style="color: #606060"> 213:</span> o = OBD_Capture()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum214" style="color: #606060"> 214:</span> o.connect()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum215" style="color: #606060"> 215:</span> time.sleep(3)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum216" style="color: #606060"> 216:</span> <span style="color: #0000ff">if</span> not o.is_connected():</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum217" style="color: #606060"> 217:</span> print <span style="color: #006080">"Not connected"</span></pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum218" style="color: #606060"> 218:</span> noki.cls()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum219" style="color: #606060"> 219:</span> noki.text(<span style="color: #006080">"Error: Not connected to OBDII..."</span>,wrap=True)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum220" style="color: #606060"> 220:</span> time.sleep(10)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum221" style="color: #606060"> 221:</span> noki.set_brightness(0)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum222" style="color: #606060"> 222:</span> noki.cls()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum223" style="color: #606060"> 223:</span> exit()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum224" style="color: #606060"> 224:</span> <span style="color: #0000ff">else</span>:</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum225" style="color: #606060"> 225:</span> o.capture_data()</pre><!--CRLF--></div></div><br /><p>In order for the Python script to automatically start when the Pi boots up, I added this line to /etc/rc.local :</p><br /><p>(cd /home/pi/pythonprogs;python obd_capture.py)&</p><br /><p>That line should only be added when you are relatively confident it will all work. First, for testing, you will need to run a network cable to your car (or tap in wirelessly, if you are within range). This is what my setup looked like, during early testing:</p><br /><p><a href="http://lh6.ggpht.com/-z9oi8elWbM0/Uh1EJ3VBNBI/AAAAAAAAKPU/9eNmDjczVOI/s1600-h/IMG_1136%25255B6%25255D.jpg"><img title="IMG_1136" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="IMG_1136" src="http://lh5.ggpht.com/-lqqoqUwobd0/Uh1EOD0XNfI/AAAAAAAAKPc/2PWBO892R8A/IMG_1136_thumb%25255B3%25255D.jpg?imgmax=800" width="449" height="706"></a></p><br /><p>The blue cable was networking, the yellow powered the Pi. Later on, I removed the power cable and ran off the car’s battery. It is quite tricky to have to program for something that has to run headless and detached from everything. So when making changes to your code, check, check and double check again.</p><br /><p>I’ve also ordered from <a href="http://mausberrycircuits.com" target="_blank">MausBerryCircuits.com</a> an illuminated LED shutdown switch, which will gracefully shut down the Pi before power is removed, reducing the chance of corrupting the SD card.</p><br /><p>That’s it!</p><br /><p>Update 2013-08-31:</p><br /><p>I’ve optimized the fuel consumption function and made SQLite do the average calculation, alleviating the need do to the calculation in a Python ‘for’ loop. Also, made the SQLite connection and cursor class attributes rather than global variables, which are generally considered undesirable.</p> Keith Hekkerhttp://www.blogger.com/profile/14927940751983969101noreply@blogger.com0tag:blogger.com,1999:blog-21464525.post-68797845903549439222013-08-26T19:54:00.001-04:002013-08-26T19:54:55.597-04:00Raspberry Pi and the Nokia 5110 LCD: contrast value is important<p>There comes a time in every tinkerer’s life that you think of a project for which a small display device would be a nice addition. Think of displaying a temperature, the time, status ‘on’ or ‘off’, etc.</p> <p>It is of course easy to go to Adafruit or Maplin’s and order an LCD and for roughly $20 you would get something useful. Plus shipping naturellement.</p> <p>I happened to stumble on the blog of <a href="http://scruss.com/blog/2013/01/19/the-quite-rubbish-clock/" target="_blank">scruss</a> and his Quite Rubbish Clock. He uses a Nokia 5110 LCD, a 1.6” device. I believe these are surplus from old Nokia 5110 phones from around the year 2000. I had me one of those phones, nothing fancy but very reliable. The first text message I ever received was on it!</p> <p>Anyway, I searched around on <a href="http://dx.com/p/arduino-1-6-lcd-display-screen-for-nokia-5110-red-silver-140226" target="_blank">dx.com</a> (Deal Extreme) and ordered 3 of these displays for the grand price of $16.20. Wait a few weeks for them to arrive and voila: time to play.</p> <p>I followed the installation instructions contained in scruss’ blog. I won’t repeat them here.</p> <p>However, when I ran my first test, the back light came on but then: nothing. After checking and rechecking my wiring, re-installing the software and still nothing, I decided to browse the Raspberry Pi forums to see if anyone had any advice. After a few fruitless hours, I found that the right contrast value of the display is crucial to its ability to display something.</p> <p>User ‘bgreat’ on the <a href="http://www.raspberrypi.org/phpBB3/viewtopic.php?f=32&t=9814&start=225" target="_blank">Raspberry Pi forum</a> had written a Python class to make using this device possible (NokiaSPI.py). User ‘pest’ on the same forum had enhanced this class even further to the point where, if the class is run as ‘main’, it goes through a number of self tests, which I think test every possible scenario. So I downloaded that version and am using it for my next project. Which, by the way, is almost ready for public showing…</p> Keith Hekkerhttp://www.blogger.com/profile/14927940751983969101noreply@blogger.com1tag:blogger.com,1999:blog-21464525.post-44323333454454048902013-07-07T13:12:00.001-04:002013-07-07T13:12:04.123-04:00Raspberry Pi Ultra Cheap Camera Mount and Remote Bird Photography<p>So I got me a Raspberry Pi camera and have experimented a little bit with it. Now, it is somewhat inconvenient to have the camera dangling at the end of the ribbon cable all the time. For one thing, it makes it very hard to aim, never mind vibration control.</p> <p>So, without doing much planning ahead, I started constructing a camera mount. The only thought I put into it from the outset was that it probably shouldn’t be made out of metal, in order to a) prevent accidentally shorting out any components and b) to prevent electro-magnetic interference in some of the high speed circuits that are undoubtedly part of the camera’s innards.</p> <p>So I scoured the garage and found a strip of PVC, left over from the basement reno from a couple of years ago. This strip is 1 1/2” by 3/16” (38mm x 5mm), available at most home improvement stores, usually 8’ (2.44 m) in length. It is lightweight, relatively strong and easy to work with.</p> <p>I cut 3 pieces, a 5 1/2” (140mm), a 3 1/2” (90mm) and an 1 1/2” (38 mm) piece. The latter piece I diagonally cut in half, so I ended up with 2 little triangles. I then used PVC glue to weld these together, using the little triangles as braces. Then I mounted the camera on the short upright, predilling 2 holes in the PVC and using 2 very fine self tapping screws I had left over from disassembling an old piece of electronics some time ago. I figure more than 2 screws would be overkill.</p> <p>Then I mounted the PI on the long 5 1/2” (140mm) strip, using 2 screws and nuts. To offset the PI from the PVC, I used a short strip of plastic tubing left over from what I believe was a Heathkit project back in the seventies! </p> <p>The result looks something like this:</p> <p><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjuo37gA4otkf9pA6iw-d23aOLeqeRdpQ2PtSF3Qvr9hkETx6F4rhH3dP31A-IzONDM41GYLz3j0zhq7xxBDHpFAcXjqAefqbyFjJ__Urv6PhHvv2nYZwzQxciRibNxt4eE6Gg4fw/s1600-h/raspberrypi_cameramount%25255B4%25255D.jpg"><img title="raspberrypi_cameramount" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="raspberrypi_cameramount" src="http://lh3.ggpht.com/-1GWo4Oxpmbw/UdmhR_AevlI/AAAAAAAAKMY/rmE2eH9FtO0/raspberrypi_cameramount_thumb%25255B2%25255D.jpg?imgmax=800" width="462" height="328"></a></p> <p>What’s the purpose of having a camera mount without a purpose? So I got to thinking, how about some bird photography? There are plenty of birds that gather around our back yard bird feeder.</p> <p>Then I remembered that I had an old X10 pan and tilt in a box somewhere. I had previously modified this to see if I could mount and control my DSLR with this: FAIL!!!, way to heavy. As part of that process, I had mounted a modified construction bracket on it with a couple of screws, plus a hole which would fit a 1/4-20 UNC screw, normally used on consumer cameras for tripod mounting.</p> <p>So I used one of those screws along with a nut to fasten the PVC camera mount to the construction plate below it (It is not visible in the picture as it is behind the front triangular brace.)</p> <p>I even found the remote control for the pan and tilt. Since this Raspberry Pi is the model ‘A’, I couldn’t hook up an Ethernet cable to it (I need to order a dongle that allows me to do this to the USB port), so I used a wireless dongle based on the Ralink RT5370 chip set.</p> <p>The bird feeder is about 75’ (25m) into the backyard, but at least 150’ (50m) from the router, which is stationed in the basement to begin with. So connecting to it from the bird feeder was a no-go, believe me, I tried. And tried. And tried.</p> <p>Then I remembered I had an old router still available. I replaced it because the wireless on it had gone flaky (Dlink DIR-615). But it still worked (most of the time). So I hauled it out, hooked the Pi up to my monitor, keyboard, mouse combination using a powered hub, plugged in the dongle, powered up, powered up the old router next to it and used the Wifi Config (wpa_gui) utility in startx (LXDE) to connect to the router.</p> <p>Once this was working, I mounted the pan-and-tilt on my tripod and mounted the camera with Pi on it. I placed the whole contraption near the bird feeder (about 5’ (1.5m)) away. I placed the router on the ground, right close to it. Here’s how it looked.</p> <p><a href="http://lh3.ggpht.com/-I-CnYnnPkGY/UdmhS_raTpI/AAAAAAAAKMg/Ou9E5gAndNE/s1600-h/raspberrypi_cameramountandpantilthead%25255B4%25255D.jpg"><img title="raspberrypi_cameramountandpantilthead" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="raspberrypi_cameramountandpantilthead" src="http://lh6.ggpht.com/-bBMVZOJEFq0/UdmhTmgq1KI/AAAAAAAAKMo/Wu3FRWvEYOA/raspberrypi_cameramountandpantilthead_thumb%25255B2%25255D.jpg?imgmax=800" width="465" height="330"></a></p> <p><a href="http://lh6.ggpht.com/-55ll2rI260Q/UdmhU_BZuoI/AAAAAAAAKMw/sJa_bOxGerg/s1600-h/raspberrypi_birdfeedercamerasetup%25255B4%25255D.jpg"><img title="raspberrypi_birdfeedercamerasetup" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="raspberrypi_birdfeedercamerasetup" src="http://lh6.ggpht.com/-rGDSz4FVTBo/UdmhWPDMRMI/AAAAAAAAKM4/fOSEuzScqp0/raspberrypi_birdfeedercamerasetup_thumb%25255B2%25255D.jpg?imgmax=800" width="427" height="768"></a></p> <p>I started up the router first, gave it some time to stabilize, then fired up the Pi. Then I went to sit on the deck and used Remote Desktop (xrdp) to connect to the Pi using my note book. From there, I started up a console session (LXTerminal) and played around with raspistill and raspivid. I also streamed video back to my lap top using raspivid on the Pi and mplayer on the laptop. I found that I couldn’t do this for an indeterminate amount of time, as the Pi would become unreachable. I don’t believe it locked up, I think just the network dropped out, but since it was running headless, there was no way to tell.</p> <p>Anyway, from my position on the deck operating the laptop I could see birds on the feeder, so I waited, patiently for an exotic bird to land. Then I fired the raspistill command, with varying output file names to capture still images.</p> <p>I then downloaded these using WinSCP to my laptop and modified them in Photoshop 5 for color and contrast. Since this was my first attempt, I don’t consider it bad at all. Here’s about the best one of the lot, an American finch, trespassing in Canada:</p> <p><a href="http://lh6.ggpht.com/-7ym3tLRLV5U/UdmhXuiyC3I/AAAAAAAAKNA/UDb6tgWFW_Q/s1600-h/raspberrypi_americanfinch%25255B4%25255D.jpg"><img title="raspberrypi_americanfinch" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="raspberrypi_americanfinch" src="http://lh6.ggpht.com/-6A6xD70aF5g/UdmhYkkuAvI/AAAAAAAAKNI/Uv-obH313jw/raspberrypi_americanfinch_thumb%25255B2%25255D.jpg?imgmax=800" width="465" height="370"></a></p> <p>With the remote for the pan and tilt, I can adjust where the camera looks.</p> <p>Since the whole camera/Pi/pan-and-tilt combination is fairly light weight I bet it could be mounted on a mini tripod as well for use just about anywhere. Oh, to be a kid again…</p> Keith Hekkerhttp://www.blogger.com/profile/14927940751983969101noreply@blogger.com0tag:blogger.com,1999:blog-21464525.post-44798141471207769042013-06-30T15:35:00.001-04:002013-06-30T15:35:48.574-04:00Raspberry Pi Garage Door Status Indicator with Ultrasonic GPIO without ‘sudo’<p>As anybody who has a garage knows, sometimes the door get left open inadvertently. Since the door is usually not visible from inside the house, that is easily done. Most of the time, there is no harm, but, if left overnight, raccoons and other undesirables may enter and create havoc.</p> <p>Raspberry Pi to the rescue. In short, what I did was mount an ultrasonic sensor (HC-SR04, from eBay, $2.00, free shipping from China) on the ceiling in one of the bays of the garage (our garage is a double with a single 5 m door). When operating, if the sensor finds that is measure less than 20 cm, then the garage door is open. If the distance is between 50 and 150 cm, then the garage door must be closed with a car parked in the spot. If the distance is greater than 150 cm, the door is closed with the car gone. (Total distance from ceiling to floor is 244 cm)</p> <p><a href="http://lh4.ggpht.com/-EgvcbTr0nWw/UdCIg6YAIkI/AAAAAAAAKLY/antRGFZ9tos/s1600-h/sensorandgarageceiling%25255B3%25255D.jpg"><img title="sensorandgarageceiling" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="sensorandgarageceiling" src="http://lh3.ggpht.com/-wSalKcz4lvg/UdCIhkPMddI/AAAAAAAAKLg/ufBhrYk4pQg/sensorandgarageceiling_thumb%25255B1%25255D.jpg?imgmax=800" width="414" height="264"></a></p> <p>My primary Pi does temperature logging every minute (see earlier posts), so I decided to make the the garage door check a part of this process.</p> <p>Since I used the wiringpi library as part of the temperature logging process, I do not need to invoke ‘sudo’ before starting python. However, the sample ultrasonic sensor script did use sudo, so I was forced to find a way around. In the end, I used Richard Ryniker’s <a href="http://ryniker.ods.org/raspberrypi/MagPi/gpio_control.c" target="_blank">gpio_control C program</a> to export the required GPIO pins. I found it gives great results. Now I can run my Python program without using sudo.</p> <p>Sometime the ultrasonic sensor gives erroneous results. Therefore, I take 20 readings (only takes less than 200 millisecond the accomplish), then drop the highest 3 and the lowest 3 readings and average the rest. This gives very accurate results. Then I write the output, i.e. the distance and the time to a text file so I can use it when a web request comes in.</p> <p>Here’s the final code:</p> <div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: 'Courier New', courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 200px; width: 97.5%; background-color: #f4f4f4"> <div id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum1" style="color: #606060"> 1:</span> def write_once(path, value):</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum2" style="color: #606060"> 2:</span> f = open(path, <span style="color: #006080">'w'</span>)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum3" style="color: #606060"> 3:</span> f.write(value)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum4" style="color: #606060"> 4:</span> f.close()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum5" style="color: #606060"> 5:</span> <span style="color: #0000ff">return</span></pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum6" style="color: #606060"> 6:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum7" style="color: #606060"> 7:</span> def checkGarageDoorStatus():</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum8" style="color: #606060"> 8:</span> trigger = <span style="color: #006080">'/sys/class/gpio/gpio25/'</span> #TRIGGER</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum9" style="color: #606060"> 9:</span> write_once(trigger + <span style="color: #006080">'direction'</span>, <span style="color: #006080">'out\n'</span>)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum10" style="color: #606060"> 10:</span> ftrigger = open(trigger + <span style="color: #006080">'value'</span>, <span style="color: #006080">'w'</span>)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum11" style="color: #606060"> 11:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum12" style="color: #606060"> 12:</span> #pin_base24a = <span style="color: #006080">'/sys/class/gpio/gpio24/'</span> #RESET ECHO TO LOW FIRST</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum13" style="color: #606060"> 13:</span> #write_once(pin_base24a + <span style="color: #006080">'direction'</span>, <span style="color: #006080">'out\n'</span>)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum14" style="color: #606060"> 14:</span> #f24a = open(pin_base24a + <span style="color: #006080">'value'</span>, <span style="color: #006080">'w'</span>)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum15" style="color: #606060"> 15:</span> #f24a.write(<span style="color: #006080">'1'</span>)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum16" style="color: #606060"> 16:</span> #f24a.flush()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum17" style="color: #606060"> 17:</span> #f24a.close()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum18" style="color: #606060"> 18:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum19" style="color: #606060"> 19:</span> echo = <span style="color: #006080">'/sys/class/gpio/gpio8/'</span> #ECHO</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum20" style="color: #606060"> 20:</span> write_once(echo + <span style="color: #006080">'direction'</span>, <span style="color: #006080">'in'</span>)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum21" style="color: #606060"> 21:</span> write_once(echo + <span style="color: #006080">'edge'</span>, <span style="color: #006080">'both'</span>) #<span style="color: #006080">'both'</span>,<span style="color: #006080">'rising'</span>, <span style="color: #006080">'falling'</span> are the keywords</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum22" style="color: #606060"> 22:</span> #Note: On a longer (40<span style="color: #006080">'), thinner cable (24 AWG), I had to change the parameter in the above line to '</span>falling<span style="color: #006080">'.</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum23" style="color: #606060"> 23:</span> #Also needed to subtract a constant of 6 cm from the distance total to arrive at the correct result.</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum24" style="color: #606060"> 24:</span> #print "Ultrasonic Measurement"</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum25" style="color: #606060"> 25:</span> #time.sleep(0.5)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum26" style="color: #606060"> 26:</span> z = 0</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum27" style="color: #606060"> 27:</span> while z < 20:</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum28" style="color: #606060"> 28:</span> fecho = open(echo + '</span>value<span style="color: #006080">', '</span>r<span style="color: #006080">')</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum29" style="color: #606060"> 29:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum30" style="color: #606060"> 30:</span> zero_last = fecho.read(1)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum31" style="color: #606060"> 31:</span> #sys.stdout.write('</span>Initial pin value = {}\n<span style="color: #006080">'.format(repr(state_last)))</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum32" style="color: #606060"> 32:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum33" style="color: #606060"> 33:</span> po = select.poll()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum34" style="color: #606060"> 34:</span> po.register(fecho, select.POLLPRI)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum35" style="color: #606060"> 35:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum36" style="color: #606060"> 36:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum37" style="color: #606060"> 37:</span> z = z + 1</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum38" style="color: #606060"> 38:</span> ftrigger.write('</span>0<span style="color: #006080">')</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum39" style="color: #606060"> 39:</span> ftrigger.flush()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum40" style="color: #606060"> 40:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum41" style="color: #606060"> 41:</span> # Allow module to settle</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum42" style="color: #606060"> 42:</span> #time.sleep(0.5)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum43" style="color: #606060"> 43:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum44" style="color: #606060"> 44:</span> # Send 10us pulse to trigger</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum45" style="color: #606060"> 45:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum46" style="color: #606060"> 46:</span> ftrigger.write('</span>1<span style="color: #006080">')</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum47" style="color: #606060"> 47:</span> ftrigger.flush()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum48" style="color: #606060"> 48:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum49" style="color: #606060"> 49:</span> time.sleep(0.00001)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum50" style="color: #606060"> 50:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum51" style="color: #606060"> 51:</span> ftrigger.write('</span>0<span style="color: #006080">')</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum52" style="color: #606060"> 52:</span> ftrigger.flush()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum53" style="color: #606060"> 53:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum54" style="color: #606060"> 54:</span> events = po.poll(1000)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum55" style="color: #606060"> 55:</span> t1 = time.time()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum56" style="color: #606060"> 56:</span> fecho.seek(0)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum57" style="color: #606060"> 57:</span> first_last = fecho.read(1)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum58" style="color: #606060"> 58:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum59" style="color: #606060"> 59:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum60" style="color: #606060"> 60:</span> events = po.poll(1000)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum61" style="color: #606060"> 61:</span> t2 = time.time()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum62" style="color: #606060"> 62:</span> fecho.seek(0)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum63" style="color: #606060"> 63:</span> second_last = fecho.read(1)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum64" style="color: #606060"> 64:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum65" style="color: #606060"> 65:</span> if len(events) == 0:</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum66" style="color: #606060"> 66:</span> #sys.stdout.write('</span> timeout delta = {:8.4f} seconds\n<span style="color: #006080">'.format(t2 - t1))</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum67" style="color: #606060"> 67:</span> nDistance = 0</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum68" style="color: #606060"> 68:</span> else:</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum69" style="color: #606060"> 69:</span> #sys.stdout.write('</span>value = {} delta ={:8.4f}\n<span style="color: #006080">'.format(state_last, t2 - t1))</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum70" style="color: #606060"> 70:</span> TimeElapsed = t2 - t1</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum71" style="color: #606060"> 71:</span> nDistance = TimeElapsed * 34300/2</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum72" style="color: #606060"> 72:</span> #print zero_last</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum73" style="color: #606060"> 73:</span> #print first_last</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum74" style="color: #606060"> 74:</span> #print second_last</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum75" style="color: #606060"> 75:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum76" style="color: #606060"> 76:</span> #print ("Distance: ={:8.1f}".format(nDistance)) + " cm"</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum77" style="color: #606060"> 77:</span> fecho.close</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum78" style="color: #606060"> 78:</span> if z == 1:</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum79" style="color: #606060"> 79:</span> lResult = [nDistance]</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum80" style="color: #606060"> 80:</span> else:</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum81" style="color: #606060"> 81:</span> lResult.append(nDistance)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum82" style="color: #606060"> 82:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum83" style="color: #606060"> 83:</span> lResult.sort()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum84" style="color: #606060"> 84:</span> #print lResult</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum85" style="color: #606060"> 85:</span> del lResult[19]</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum86" style="color: #606060"> 86:</span> del lResult[18]</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum87" style="color: #606060"> 87:</span> del lResult[17]</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum88" style="color: #606060"> 88:</span> del lResult[2]</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum89" style="color: #606060"> 89:</span> del lResult[1]</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum90" style="color: #606060"> 90:</span> del lResult[0]</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum91" style="color: #606060"> 91:</span> #print '</span>\n<span style="color: #006080">'</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum92" style="color: #606060"> 92:</span> #print lResult</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum93" style="color: #606060"> 93:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum94" style="color: #606060"> 94:</span> nTotalDistance = 0</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum95" style="color: #606060"> 95:</span> for l in lResult:</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum96" style="color: #606060"> 96:</span> nTotalDistance = nTotalDistance + l</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum97" style="color: #606060"> 97:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum98" style="color: #606060"> 98:</span> nAverageDistance = nTotalDistance/14</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum99" style="color: #606060"> 99:</span> #print nAverageDistance</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum100" style="color: #606060"> 100:</span> dDateTimeRecorded = datetime.datetime.now()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum101" style="color: #606060"> 101:</span> if (nAverageDistance > 5 and nAverageDistance < 20):</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum102" style="color: #606060"> 102:</span> cStatus = "OPEN"</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum103" style="color: #606060"> 103:</span> else:</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum104" style="color: #606060"> 104:</span> if (nAverageDistance > 30 and nAverageDistance < 150):</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum105" style="color: #606060"> 105:</span> cStatus = "CLOSEDWITHCAR"</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum106" style="color: #606060"> 106:</span> else:</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum107" style="color: #606060"> 107:</span> cStatus = "CLOSEDWITHOUTCAR"</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum108" style="color: #606060"> 108:</span> with open('</span>GarageDoorStatus<span style="color: #006080">','</span>w') <span style="color: #0000ff">as</span> file:</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum109" style="color: #606060"> 109:</span> file.write(<span style="color: #006080">"{}\n"</span>.format(cStatus))</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum110" style="color: #606060"> 110:</span> file.write(<span style="color: #006080">"{}\n"</span>.format(dDateTimeRecorded))</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum111" style="color: #606060"> 111:</span> file.close()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum112" style="color: #606060"> 112:</span> <span style="color: #0000ff">return</span> cStatus </pre><!--CRLF--></div></div><br /><p>This is how it is called:</p><br /><div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: 'Courier New', courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 200px; width: 97.5%; background-color: #f4f4f4"><br /><div id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum1" style="color: #606060"> 1:</span> def main():</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum2" style="color: #606060"> 2:</span> os.system(<span style="color: #006080">"gpio export 11 out"</span>)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum3" style="color: #606060"> 3:</span> os.system(<span style="color: #006080">"gpio export 18 out"</span>)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum4" style="color: #606060"> 4:</span> os.system(<span style="color: #006080">"gpio_control 25 export"</span>)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum5" style="color: #606060"> 5:</span> os.system(<span style="color: #006080">"gpio_control 8 export"</span>)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum6" style="color: #606060"> 6:</span> wiringpi.wiringPiSetupSys()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum7" style="color: #606060"> 7:</span> wiringpi.GPIO(wiringpi.GPIO.WPI_MODE_SYS)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum8" style="color: #606060"> 8:</span> wiringpi.pinMode(11,wiringpi.OUTPUT)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum9" style="color: #606060"> 9:</span> wiringpi.pinMode(18,wiringpi.OUTPUT)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum10" style="color: #606060"> 10:</span> #wiringpi.pinMode(24,wiringpi.OUTPUT)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum11" style="color: #606060"> 11:</span> loadDrivers()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum12" style="color: #606060"> 12:</span> nSlaveCount = findNumberOfSensors()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum13" style="color: #606060"> 13:</span> lSlave = findSlaves(nSlaveCount)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum14" style="color: #606060"> 14:</span> cAmbientTemperature = <span style="color: #006080">"N/A"</span></pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum15" style="color: #606060"> 15:</span> <span style="color: #0000ff">while</span> True:</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum16" style="color: #606060"> 16:</span> wiringpi.digitalWrite(18,1)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum17" style="color: #606060"> 17:</span> checkTemps(nSlaveCount,lSlave)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum18" style="color: #606060"> 18:</span> wiringpi.digitalWrite(18,0)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum19" style="color: #606060"> 19:</span> <span style="color: #0000ff">if</span> (checkGarageDoorStatus() == <span style="color: #006080">"OPEN"</span>):</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum20" style="color: #606060"> 20:</span> print <span style="color: #006080">"Open"</span></pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum21" style="color: #606060"> 21:</span> wiringpi.digitalWrite(11,1)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum22" style="color: #606060"> 22:</span> <span style="color: #0000ff">else</span>:</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum23" style="color: #606060"> 23:</span> print <span style="color: #006080">"Closed"</span></pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum24" style="color: #606060"> 24:</span> wiringpi.digitalWrite(11,0)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum25" style="color: #606060"> 25:</span> nCurrentSecond = datetime.datetime.now().second</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum26" style="color: #606060"> 26:</span> time.sleep(60-nCurrentSecond)</pre><!--CRLF--></div></div><br /><p>Note that when the status is found to be open, GPIO Pin 11 is turned high. This I use to turn on an LED which I mounted next to the furnace thermostat in the living room. Now, in order to make this more attention grabbing, I wanted it to be a flashing LED. Of course the Pi could do this, but in this case that seemed like overkill. So I bought an LED that flashes all on its own, at about 2 Hz. (KLF-336HD-3P).</p><br /><p><a href="http://lh6.ggpht.com/-Bug7NUnweow/UdCIiKGhZhI/AAAAAAAAKLo/KdVDu2WveZk/s1600-h/thermostatwithled%25255B4%25255D.jpg"><img title="thermostatwithled" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="thermostatwithled" src="http://lh6.ggpht.com/-FnXhj6HancM/UdCIjA9ZgnI/AAAAAAAAKLw/Fvk5aEur5x4/thermostatwithled_thumb%25255B2%25255D.jpg?imgmax=800" width="417" height="490"></a></p><br /><p>I also made it part of the web page that my Pi can produce (see earlier posts).</p><br /><p><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj_HilTc_VV7kfpPhyG8pIIwL3nnkycgTo33ed3VCrPgwyVqNcBvyQoU5s836Ly0sRFYQ-Ygp92P8Jreya-wnUXG4jhIIfkfJg9f6nJjJv_edIDaIwixxt2vGX0TlsBFRemahzd3A/s1600-h/garagedoorstatusonwebpage4.jpg"><img title="garagedoorstatusonwebpage" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="garagedoorstatusonwebpage" src="http://lh3.ggpht.com/-ov9JK--VbBc/UdCIkhm4tyI/AAAAAAAAKMA/R0rKA1kDIbg/garagedoorstatusonwebpage_thumb1.jpg?imgmax=800" width="450" height="127"></a></p><br /><p>In case you are wondering, the first 7 lines show temperatures inside, outside and in my generation housing (see earlier posts).</p><br /><p>Although it seems a rather trivial project, it was nonetheless very time consuming. Pulling wires from the utility room where the Pi is located to the garage, as well as to the living room next to the thermostat (doing it decently, that is) takes far more time than you wish it would. Then, finding a workaround for sudoless ultrasonic sensing, fitting the new into the existing Pi setup, testing a flashing LED on the Pi (ya never know what it might do) all took its sweet time. But then, hey, it is all worth it when in the end it works and it is useful.</p> Keith Hekkerhttp://www.blogger.com/profile/14927940751983969101noreply@blogger.com6tag:blogger.com,1999:blog-21464525.post-14446850181645189402013-06-25T16:43:00.001-04:002013-06-30T14:14:59.207-04:00Raspberry Pi Ultrasonic Sensor. Use GPIO Without Sudo<p>Search the web, and you’ll find quite a few pages discussing the HC-SR04 ultrasonic distance sensor. It is cheap, easy to configure and great for experimenting or non crucial measuring jobs. When used on the Raspberry Pi, however, almost all examples I have seen force you to run the sensor application as superuser (sudo). This means that the entire application has elevated privileges. In general, this is not a good idea.</p> <p>Search as I might, I was not able to find an example of a non sudo application that used an interrupt where timing was crucial. So I decided to do some research to find a way around this.</p> <p>Background: I am trying to add distance measurement capability to my Pi, which already does temperature measurements and tracks the sump pump level using a second Pi. All this is done without using ‘sudo’, and since the new functionality is to be integrated into the existing functionality I had to find a solution. I am using Python 2.7 as my main development language.</p> <p>I am using wiringPi in the existing setup, but it does not handle interrupts. So a Google search is next.</p> <p>First, I found an article in the November 2012 edition of MagPi by Richard Ryniker entitled ‘GPIO and Interrupts’, which gave a good overview of sudo, interrupts and the like. You can read a pure text version of the article <a href="http://ryniker.ods.org/raspberrypi/MagPi/GPIO_interrupts" target="_blank">here</a>, without having to look up the magazine version.</p> <p>The first thing to take away from that is that if you want to use GPIO pins without using sudo you need to <a href="http://ryniker.ods.org/raspberrypi/MagPi/gpio_control.c" target="_blank">download</a> and compile gpio_control.c from Richard’s site. All the instructions on how to compile and then copy and chmod the executable are embedded in the text of the download.</p> <p>Once you have done that, then you can give a command like this</p> <p>$ gpio_control export 23</p> <p>which simply means any user can now use pin 23 without using sudo</p> <p>To revert control once you do not need this anymore:</p> <p>$ gpio_control unexport 23</p> <p>Next, I studied his <a href="http://ryniker.ods.org/raspberrypi/MagPi/gpio23-max.py" target="_blank">application</a> for the maximum LED blink rate in Python. This explains how to write a value to a pin. After that, I studied his <a href="http://ryniker.ods.org/raspberrypi/MagPi/interrupt_test23.py" target="_blank">interrupt example</a>, showing how to detect when a designated pin goes high (or low), i.e how to read the value of a pin.</p> <p>I then modified the ultrasonic sensor ‘sudo’ <a href="http://www.raspberrypi-spy.co.uk/2012/12/ultrasonic-distance-measurement-using-python-part-1/" target="_blank">application</a> that Matt Hawkins wrote over at <a href="http://www.raspberrypi-spy.co.uk/" target="_blank">Raspberry Pi Spy</a>, to account for the new sudo free approach using interrupts. When running the sudo version and this version, the results are virtually identical, any differences are probably due to the CPU load at that very instant.</p> <p>Here is the application:</p> <div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: 'Courier New', courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 200px; width: 97.5%; background-color: #f4f4f4"> <blockquote> <div id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum1" style="color: #606060"> 1:</span> #!/usr/bin/python</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum2" style="color: #606060"> 2:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum3" style="color: #606060"> 3:</span> # Test interrupts.</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum4" style="color: #606060"> 4:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum5" style="color: #606060"> 5:</span> import time,select,sys</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum6" style="color: #606060"> 6:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum7" style="color: #606060"> 7:</span> def write_once(path, value):</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum8" style="color: #606060"> 8:</span> f = open(path, <span style="color: #006080">'w'</span>)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum9" style="color: #606060"> 9:</span> f.write(value)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum10" style="color: #606060"> 10:</span> f.close()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum11" style="color: #606060"> 11:</span> <span style="color: #0000ff">return</span></pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum12" style="color: #606060"> 12:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum13" style="color: #606060"> 13:</span> pin_base23 = <span style="color: #006080">'/sys/class/gpio/gpio23/'</span> #TRIGGER</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum14" style="color: #606060"> 14:</span> write_once(pin_base23 + <span style="color: #006080">'direction'</span>, <span style="color: #006080">'out\n'</span>)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum15" style="color: #606060"> 15:</span> f23 = open(pin_base23 + <span style="color: #006080">'value'</span>, <span style="color: #006080">'w'</span>)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum16" style="color: #606060"> 16:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum17" style="color: #606060"> 17:</span> pin_base24 = <span style="color: #006080">'/sys/class/gpio/gpio24/'</span> #ECHO</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum18" style="color: #606060"> 18:</span> f24 = open(pin_base24 + <span style="color: #006080">'value'</span>, <span style="color: #006080">'r'</span>)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum19" style="color: #606060"> 19:</span> write_once(pin_base24 + <span style="color: #006080">'direction'</span>, <span style="color: #006080">'in'</span>)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum20" style="color: #606060"> 20:</span> write_once(pin_base24 + <span style="color: #006080">'edge'</span>, <span style="color: #006080">'both'</span>) #<span style="color: #006080">'rising'</span>, <span style="color: #006080">'falling'</span> are other keywords</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum21" style="color: #606060"> 21:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum22" style="color: #606060"> 22:</span> print <span style="color: #006080">"Ultrasonic Measurement"</span></pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum23" style="color: #606060"> 23:</span> state_last = f24.read(1)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum24" style="color: #606060"> 24:</span> #sys.stdout.write(<span style="color: #006080">'Initial pin value = {}\n'</span>.format(repr(state_last)))</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum25" style="color: #606060"> 25:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum26" style="color: #606060"> 26:</span> po = select.poll()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum27" style="color: #606060"> 27:</span> po.register(f24, select.POLLPRI)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum28" style="color: #606060"> 28:</span> f23.write(<span style="color: #006080">'0'</span>)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum29" style="color: #606060"> 29:</span> f23.flush()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum30" style="color: #606060"> 30:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum31" style="color: #606060"> 31:</span> # Allow module to settle</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum32" style="color: #606060"> 32:</span> time.sleep(0.5)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum33" style="color: #606060"> 33:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum34" style="color: #606060"> 34:</span> # Send 10us pulse to trigger</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum35" style="color: #606060"> 35:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum36" style="color: #606060"> 36:</span> f23.write(<span style="color: #006080">'1'</span>)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum37" style="color: #606060"> 37:</span> f23.flush()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum38" style="color: #606060"> 38:</span> time.sleep(0.00001)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum39" style="color: #606060"> 39:</span> </pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum40" style="color: #606060"> 40:</span> f23.write(<span style="color: #006080">'0'</span>)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum41" style="color: #606060"> 41:</span> f23.flush()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum42" style="color: #606060"> 42:</span> events = po.poll(600)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum43" style="color: #606060"> 43:</span> t1 = time.time()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum44" style="color: #606060"> 44:</span> f24.seek(0)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum45" style="color: #606060"> 45:</span> state_last = f24.read(1)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum46" style="color: #606060"> 46:</span> #print state_last</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum47" style="color: #606060"> 47:</span> events = po.poll(600)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum48" style="color: #606060"> 48:</span> f24.seek(0)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum49" style="color: #606060"> 49:</span> state_last = f24.read(1)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum50" style="color: #606060"> 50:</span> #print state_last</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum51" style="color: #606060"> 51:</span> t2 = time.time()</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum52" style="color: #606060"> 52:</span> <span style="color: #0000ff">if</span> len(events) == 0:</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum53" style="color: #606060"> 53:</span> sys.stdout.write(<span style="color: #006080">' timeout delta = {:8.4f} seconds\n'</span>.format(t2 - t1))</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum54" style="color: #606060"> 54:</span> <span style="color: #0000ff">else</span>:</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum55" style="color: #606060"> 55:</span> #sys.stdout.write(<span style="color: #006080">'value = {} delta ={:8.4f}\n'</span>.format(state_last, t2 - t1))</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum56" style="color: #606060"> 56:</span> TimeElapsed = t2 - t1</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum57" style="color: #606060"> 57:</span> nDistance = TimeElapsed * 34300/2</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum58" style="color: #606060"> 58:</span> print (<span style="color: #006080">"Distance: ={:8.1f}"</span>.format(nDistance)) + <span style="color: #006080">" cm"</span></pre><!--CRLF--></div><br /><p> </p></blockquote></div><br /><p>In theory, using interrupts is cleaner and leaner than the old way where the application sits in a loop, uses a lot of CPU cycles to check continuously whether or not the state of the pin has changed.</p> Keith Hekkerhttp://www.blogger.com/profile/14927940751983969101noreply@blogger.com0tag:blogger.com,1999:blog-21464525.post-41519894258259875092013-06-14T11:52:00.001-04:002013-06-14T11:52:24.600-04:00Removing stubborn metal fence posts with wood splitter<p>Metal fence posts that have been embedded into the ground can be extremely hard to remove. In the past, I have tried with muscle power (no result, except back pain), sledge hammers (very hard to hit a sledge with an upward motion, car jacks (minimal result, usually the fence post bends in an unpredictable fashion).</p> <p>So, I got to thinking, looked around in the garage for some object, some tool to use to remove the one post still in the front yard, having long ago fulfilled its useful purpose. Then my eye struck my 10 ton manual hydraulic wood splitter, and thought about the possibilities. On a limb (pun intended), I decided to drag it outside and give it a whirl. I placed the wood splitter in the upright (storage) position, wrapped a chain around the base of the post, looped it through the welded on U clamp on the top of the piston and started cranking. It worked, literally no sweat. One advantage over the car jack is that the top of the wood splitter I beam (the part with the cutting edge) prevents the fence post from bending over.</p> <p>This is what it looked like:</p> <p> </p> <a href="http://lh4.ggpht.com/-L_JguYISq6Q/Ubs8M8K9z6I/AAAAAAAAKKQ/5Tv0dl-Sluo/s1600-h/IMG_0712%25255B6%25255D.jpg"><img title="IMG_0712" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="IMG_0712" src="http://lh4.ggpht.com/-17Yt1D6npcc/Ubs8N071e5I/AAAAAAAAKKY/sGco9Q6unww/IMG_0712_thumb%25255B3%25255D.jpg?imgmax=800" width="336" height="530"></a> <p>As you can see, the splitter sits on a piece of 2x10, to prevent it from sinking into the ground.</p> <p>Here’s the video: <div id="scid:5737277B-5D6D-4f48-ABFC-DD9C333F4C5D:be72bd83-790c-4dc5-bbc9-46bb9cc53595" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"><div><object width="448" height="252"><param name="movie" value="http://www.youtube.com/v/3h_geLI-CqE?hl=en&hd=1"></param><embed src="http://www.youtube.com/v/3h_geLI-CqE?hl=en&hd=1" type="application/x-shockwave-flash" width="448" height="252"></embed></object></div><div style="width:448px;clear:both;font-size:.8em">Alternate use for wood splitter!</div></div></p> Keith Hekkerhttp://www.blogger.com/profile/14927940751983969101noreply@blogger.com1tag:blogger.com,1999:blog-21464525.post-2702730421724684262013-04-18T20:33:00.001-04:002013-04-18T20:33:59.719-04:00Raspberri Pi – problem with power supply cable<p>Last week, one of my Raspberry Pi's locked up after it was physically moved while powered up. The symptoms showed up when I tried to connect to it through Remote Desktop. To troubleshoot and solve this, I hooked up a monitor to the HDMI port (through an HDMI-DVI converter) and tried rebooting. The Pi didn’t start. Since the SDHC card was rather stock, I reimaged it with Raspian Wheezy. Another reboot. This time it started, but the SDHC was corrupted, and showed sectors invalid. I tried a new card. It booted properly. Solved! (or so I thought). I installed xrdp, the remote management protocol and tried to reach the Pi from my laptop. It hung, after closer inspection I noticed that networking had dropped out, the message in syslog was "Link beat lost". During scanning of the forums I noticed how there seemed to be a general trend to blame this on the power supply. So I replaced it with a spare. Same problem. I then hooked up a volt meter to the 5V pin and GND. When running, but more or less idling, it showed roughly 4.65 volts. When I tried to start Remote Desktop, it dropped to 4.55 volts and then networking failed.</p> <p>At long last, I decided to swap power supply cables. Problem solved. DVM showed steady voltage level of 5.05 volts. Remote Desktop started properly and remained connected for the duration of the session.</p> <p>I bought a bunch of micro USB cables on eBay a number of weeks ago. The faulty cable was one of them.</p> <p>The moral of the story: not everything on eBay is naturally a bargain. </p> Keith Hekkerhttp://www.blogger.com/profile/14927940751983969101noreply@blogger.com0