Advertisement

Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Saturday, February 23, 2019

Setting up Development Environment

I want to setup a development environment that I can use from anywhere.  The goal is to be able to do coding on my laptop, one of my Raspberry Pis or, possibly, a Chromebook.  I would like to use Eclipse as the IDE and Github as the code repository.

Laptop Setup
First, I am going to get the laptop working.  

I have Eclipse installed on my laptop with the PHP add-in. In order to use Eclipse with Python, I need to install PyDev. The PyDev site has instructions to install and configure PyDev in Eclipse.

I have Java 8 on my laptop and Eclipse 2018-12 (4.10.0). I do not have Python installed.

Step one was to download the Python 2.7.16 64-bit installer for Windows 10 and install it on my laptop. I was able to execute Python from the command prompt so I know it is installed.

Next, I followed the instructions on the PyDev site to install PyDev from within Eclipse.  All you need to do is click Help / Install New Software and put in the URL for PyDev: http://www.pydev.org/updates

After installing, I had to configure the Python Interpreter in Eclipse. I had to tell Eclipse where the python.exe file was on my PC.

Git
A few days ago, I used BitBucket on the web to create a GitHub repository for the HESA.  I also installed Git for Windows on my laptop.  I then cloned the GitHub repository to my PC.  You must do this before you can do anything with Git on the PC.

Today, I copied all of the HESA code on my laptop to the folder created by Git when I cloned the repository.  Then, I used command line commands to upload my HESA py files to GitHub. 

git add --all

Then, I had to add my BitBucket email address by doing 

git config --global user.email prodigal67@bitbucket.org

Next, had to do 

git commit -m 'Mit comments'

This does a local commit.   Finally, I did 

git push

 to upload the files to the git repository. Now, I can see the files in the repository.

I repeated this process to get my Python module code in another repository in GitHub.

Raspberry Pi
Next, I moved on to my new Raspberry Pi 3+. It already has git installed and Python 3 and Eclipse.

I used the command line options to clone the github repositories to the pi.

Since Eclipse does not have the pydev extensions, I followed the same process to install them inside Eclipse.

That was all there is to it.  Now, I can make code changes on my Pi or my PC and synchronize those changes through GitHub. 

Friday, October 9, 2015

FBI Exit Problem Fixed

I finished the first prototype for the MMS Photo-inator Xsi a few months back.  It will take a picture when someone presses the button.  Then, it displays the picture on the screen for a few seconds.  Next, it flashes the lights on the green and red buttons and waits for the person to press one.  If the person presses the red button, the program deletes the picture and resets.  If the person presses the green button, the program keeps the picture and resets.



One thing that I need the Photo-inator to do is stop displaying the picture if the person presses either the red or green button.  I have not been able to figure that out, until now.  

I tried sending a keystroke to the session but that did not work.  I also tried killing the process but that did not work.  I tried other software and solutions but could not find one.  I spent the last few months trying to figure this out.  It has been extremely frustrating.  Here is how I finally resolved the problem.

I heard that the uinput module for python will allow me to send keystrokes to a session.  I downloaded the source code from here: https://pypi.python.org/pypi/python-uinput

Before I could install the source code, I had to install a supporting program by typing
sudo apt-get install libudev.so

Next, I installed the source code by running
sudo python setup.py buildsudo python setup.py install
I could not get fbi to work in the python program.  Tried it from the terminal and it will not work either.  I keep getting an error message that says, "ioctl VT_GETSTATE: Inappropriate ioctl for device (not a linux console?)"

Used Ctrl-Alt-F1 to switch to tty1 .  Executing fbi on tty1 works perfectly.

If I have the python program execute this: "fbi -a -T 1 -t 1 BBB.jpeg", fbi will display the image.  The -T 1 tells fbi to display the program on tty1.  I need to specify this on my PC but it apparently did that by default when I ran the program on the Raspberry Pi. 

Next, I wrote a small python program to display a picture with fbi, wait a few seconds, then send a q to the uinput subsystem on Linux to simulate a user pressing the letter q.  This worked perfectly.

The test code looked like this:

#!/usr/bin/python
import subprocessimport psutilimport timeimport uinput

subprocess.call("fbi -a -T 1 -1 /home/ken/Pictures/BBB.jpeg", shell=True)time.sleep(3)
#Uinput testsevents = (    uinput.KEY_Q,    uinput.KEY_H,    uinput.KEY_L,    uinput.KEY_O,    )

device = uinput.Device(events)time.sleep(1)
device.emit_click(uinput.KEY_Q)
Finally, I modified the photo-inator program to use the uinput code.  I need to fire up the whole system to test it out.  I'll do that some other day.

Saturday, April 25, 2015

Problem finding the Internet

One feature of the HESA program is that it checks to see if it can connect to the internet before trying to make an update.  It does this because it will throw an error and kick out of the program if it can't write to the database because the internet is down.

There is a built-in python function to check for internet access.  The code looks like this.
urllib2.urlopen(reference, timeout=1)

The URL I use as a reference is 74.125.228.100.  That IP address is supposed to be one of google's mail servers.  I figured it would be available forever.  However, about a week ago, it stopped responding to the python command and pings.

I did a quick search on the internet about the best IP address to use to test if the internet is up.  This site recommended using 8.8.8.8.  That is Google's DNS server.  Another good idea is to use Open DNS's DNS server IP address.

I made simple test program that uses the urlopen function.  I sent both the 74.125 and 8.8 addresses to the function.  It could not find the internet with either address.

Then, I used the function to look for http://google.com.  The function found that address.  So, the python function does not work with IP addresses.

I changed the code to always look for google.com.  Now, it is seeing the internet.  Problem solved.


Monday, June 23, 2014

MySQL and Python

Over the weekend, I decided to write some python code to work with MySQL databases.  I already have experience working with MySQL and PHP and I have a small library of PHP functions to access MySQL.  The goal was to get the same functionality working in python.  Then, I can start storing data from the HESA in MySQL tables.

The first thing I had to do was install a python library.  I did this by typing
sudo apt-get install build-essential python-dev libmysqlclient-dev
at the command prompt.  This library is supposed to allow PIP to install the actual python package. 

Next, I had to install PIP, the app that lets you install python packages.  I did this by typing 

sudo apt-get install python-pip

Once PIP was installed, I used it to install the MySQL python library by typing
sudo pip install MySQL-python
The documentation for MySQL and python is not really that great.  I spent way too much time researching how to do all this.  Once I got this far however, I was able to talk to the MySQL server.

I found a simply python program to connect to a SQL server and display the version of the database.  I was able to use it to query both my local copy of MySQL and my version on the internet.

Next, I converted all of my PHP SQL code to python functions.  I have functions to connect and disconnect from a SQL server.  I also have functions to create insert statements, fix values in insert statement to guard against injection attacks, and execute a SQL statement and return the results.  I was able to successfully test all the functions with my test database.

Now that I have my SQL libraries, I will create a table in my test database to store readings from the HESA.  If only I had the HESA reading stuff...