Advertisement

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, December 29, 2017

Setting up an EdgeRouter, Follow-up 1

As mentioned in previous posts, I purchased and setup an  EdgeRouter X from Ubiquiti.  That all went relatively well and the device has been performing great. 

Minor Performance Issues
Well, I did have a few instances where the internet port kept getting disconnected.  This happened twice.  Both times, it was later at night.  I just stopped using the internet and went to bed.  No one else complained so, it was no big deal.  However, I'll have to keep an eye on it.  I'd like to find a way to record the log file entries in a database.

Rules Update
Last time, I created rules for all of the devices on my network.  I decided to make rules for all devices, not just the ones I blocked.  For now, there are some rules that block kids devices overnight.  In the future, I will change the main ruleset to drop all traffic.  Then, I will change the individual rules to allow traffic during specific times.  This will keep the kids from spoofing their MAC address or using "unauthorized" devices.

I may also make an additional rule to allow access to any device during daylight hours for any guests that come over.  On the other hand, almost everyone who comes over has a phone with a data plan so it probably is unnecessary

Configuration Backed Up
Today, I backed up the configuration.  That was very easy.  I probably should test it by restoring it but I'm scared I will brick the router.

System Image Upgraded
Finally, I downloaded the latest system image from the Ubiquiti web site and installed it.  That also went smoothly.  Interestingly, the options that were missing compared the manual are now there.  For example, there is now a "Basic Setup" wizard option.  I guess I should have done a system image update before I did any another configuration.


Thursday, December 28, 2017

Setting up an EdgeRouter -- Blocking by device and time

In may last post, I described the initial setup of my new EdgeRouter X by Ubiquiti.  I connected it to my ISP, and pointed it to OpenDNS as the DNS server. Now, I am going to setup a rule to block access to the internet for specific devices at specific times. In this way, I can effectively turn off the internet for my kid's devices overnight without turning off my internet access. Plus, each kid can have a different schedule. If I want to take away internet access as a punishment, I can easily do this by changing the settings for the specific kid's rule.

First Failed Attempt
As an aside, on my first attempt, I managed to block all traffic on my internal network.  I created a ruleset that dropped all packets as a default action.  I turned it on for the interface for the switch and immediately lost connection to the router.  The only way to fix it was to reset the device to factory settings and start over.  Doh!

Resetting the Router
There are several ways to reset the router.  I tried simply holding in the reset button while the device was powered on.  This did not seem to do anything.  In order to get the reset to work, I had to push in the reset button, then plug in the power until the reset sequence finished.

One other thing.  After I changed the IP address of the switch to 0.1, I had to plug the cable from my PC into port eth1 on the switch.  It took me a few minutes to figure out why I could not connect to the switch while plugged in to port eth0 after it was configured to connect to the internet on port eth0.  Maybe this will help someone who reads this blog.

Configuring the Router to Block Traffic
Here are the steps I followed to configure the router.

After logging in to the device, I clicked on the Firewall/NAT button and then the Firewall Policies tab.

I clicked on the "Add Ruleset" button and created a new ruleset named "Blocking".  This ruleset will contain all of the rules for each device I want to block on my network.  The default action is set to Accept all traffic.  This way, all traffic is allowed by default.  The firewall will only block traffic for specific rules.  The mistake I made was to set the Default Action to Drop.



After saving the new ruleset, I clicked on the Actions dropdown for the new Blocking ruleset and chose the Interfaces option.  I set the Interface to eth0 and the Direction to out.  The ruleset was now finished after saving it.



The next step was to create a default rule.  I clicked on the Actions dropdown and chose "Edit Ruleset".  Next, I clicked on the "Add New Rule" button.  On the Basic tab, I entered a description that was for the device I wanted to block.  The Enable box was checked by default.  Drop was selected as the Action.  (Reject would have also worked.)  The "All protocols" radio button was selected.  I did not choose logging because I don't want an entry in the log for every blocked packet.  Here is a screen shot of the settings.


That is it for the Basic tab.  Nothing needs to be set on the Advanced tab. 

The MAC Address of the device to block is entered on the Source tab.  I could also use the IP address to identify the device but that is easy for a kid to change.  In theory, the MAC address can also be spoofed to get around the rules.  If I find out that they are doing that, I will have to drop all packets by default and make rules to allow traffic by specific MAC address.


Finally, the time to block the device is entered on the Time tab.  In this case, I wanted to block traffic from this device from midnight to 4am every day.  I could have blocked traffic for certain days and could even make multiple rules for the same device to have different times during the week and on weekends.


That is it.  Now that I have one rule made, I can use the Actions button to copy the rule and simply change the MAC address and time for each kid's device.

Saturday, December 23, 2017

Setting up an EdgeRouter, Part I

Introduction
Ever since my kids got devices that could get on the internet, I have been looking for ways to manage and monitor their access.  We do randomly check their devices for "bad stuff" and we talk about what they should not be doing on the internet.  Those are good, basic steps that any good parent should take.

I always point my routers to OpenDNS as the DNS server to filter out objectionable content.  I am sure that helps, but savvy kids could install VPN apps to get around this.

Finally, the wireless AP is setup to turn off the wireless radios at 10:00pm until 4:00am.  This keeps the kids off of the internet on their phones overnight since they don't have data plans.  Also, they are supposed to leave their phones downstairs for charging overnight so they can't play offline games.  The downside is that all wireless devices are affected by this and not just the kid's phones.

What I need is a router that
  1. allows me to block internet access at specific times for specific devices
  2. allows me to see what sites people are going to
  3. allows me to block access to VPN by device. I sometimes need to login to work using VPN.  I can't just block all VPN access.
Hopefully, the EdgeRouter X by Ubiquiti will let me do everything I want.

I bought an EdgeRouter X on Amazon for about $50.  There are more expensive models but this one seemed like it would do everything I wanted.  Plus, we have Amazon Prime right now so shipping was free.  Some people said that the router stopped working after a few days.  Amazon offered buyer protection for <$2, so I paid for that as well.  If I get a lemon, I hope they will give me my money back.

Initial Thoughts
If you read reviews of the EdgeRouter, some people complain that it is hard to setup.  I did not find that to be true at all.  I am in IT but I am not a network engineer and do not work with routers normally.  So, configuring the EdgeRouter is by no means something I can do without help.

The manual that the router comes with is simply a quick start guide.  It explains how to plug everything in and nothing more.  You need the full user guide to get anywhere.  Fortunately, that can be downloaded from the Ubiquity website.  I have not read much of it yet but, at 104 pages, it appears to be pretty comprehensive.  Also, I have had good luck googling things.  Some of the information Google finds is outdated but still helpful.

Setting up to Connect to the Internet
The main thing I need the EdgeRouter to do is connect my home network to the internet through my cable modem.  This was very easy to setup. 

First, I followed the instructions in the quick start guide to power up the EdgeRouter.  I plugged a network cable from my laptop into port ETH0.  Then, I configured the Ethernet port on my laptop to have a fixed IP address of 192.168.1.1.  I brought up the web configuration interface and logged in as the ubnt user.

There is a wizard to configure the device to connect to the internet.  The user guide says that the wizard is named, "Basic Setup".  However, my device did not have a wizard with that name.  Fortunately, the "WAN+2LAN2" wizard is the same as the Basic Setup wizard.  I used the WAN+2LAN2 wizard and followed the instructions in the user guide for the Basic Setup wizard.

For the internet port choices, I left the defaults.



The wizard screen also has a section to setup the LAN.  You need to expand the section at the bottom of the screen to see the choices.  One thing that through me off a bit is the DHCP setup.  The address box is for the default address of the switch for the LAN.  This is the address that DHCP will give to the clients as the router address.  At first, I thought that this box was for the lower IP address setting.  I set the address as 192.168.0.1.



Also, by default, the software starts the DHCP IP range at .38 and ends it at .243.  That is more than enough for a home network.

The last step is to click the Apply button and reboot the router.

Physically Connecting to the Cable Modem
It took a bit of fiddling to get the router installed on my network.  I connected the Ethernet cable from the cable modem to eth0 on the EdgeRouter and powered up the EdgeRouter.  The cable modem's network light lit up to indicate that the EdgeRouter was connected.  However, I could not get to the internet even though I could see that the EdgeRouter had a routable IP from my ISP and was downloading traffic.  A simple reboot of the cable modem and the EdgeRouter fixed the problem.

OpenDNS
Setting up the EdgeRouter to use OpenDNS was a bit harder but still pretty easy.  The user guide does not specify how to do this.  Fortunately, I am not the only one who tried to do this.  Google to the rescue.

Part of the configuration can be done with the gui tools in the EdgeOS.  Part of the configuration must be done by entering unix commands into the Command Line Interface (CLI).

First, I clicked on the System button in the bottom-left corner of the screen.  This brings up the System configuration screen.  I entered the two OpenDNS server IP addresses in the system name server box.  Those addresses are 208.67.222.222 and 208.67.220.220.  I also added the local IP address of 127.0.0.1 and saved those changes.



Next, I opened up the CLI interface and logged in as an administrator.  I entered the following commands in the interface.
configure
set service dns forwarding system
commit
save
exit
exit

Finally, I had to go to the OpenDNS web site and refresh my network's IP address.  It took about five minutes for the OpenDNS servers to propagate my changes but eventually OpenDNS was doing it's job.

That's it for now.  I have my EdgeRouter connected to my cable modem.  I can get to the internet and am using OpenDNS for the DNS servers.  

Next, I want to configure the router to block specific devices from accessing the internet at specific times.


Friday, January 22, 2016

Uncanny Movie

I just finished watching the movie Uncanny on Netflix.  It's a science fiction movie about a guy working on artificial intelligence.  He created an android and made it as lifelike as possible.  He calls himself an illusionist.  The android is incredibly lifelike and creepy.

This movie appeals to me as someone who is into robotics, AI, making, and science in general.  It's an intellectual thriller.  I enjoyed watching it and would like to watch it again.  The ending was a surprise but more because of how unlikely it seems when you think about how the android and the maker acted during the week.

I am left with feeling confused and a bit creeped out at the same time.




Friday, January 1, 2016

Setting up Multiple WAP Routers on a Home Network

I have a main WAP / router (Netgear WNDR3700v4) in my house that connects to the cable modem (Motorola Surfboard SB6141) and the outside world.  I also have a second WAP (Cisco RV220W) and a wired-only router (D-Link EBR-2310) on my home network that extend the network.  Only the main router provides DHCP functionality.  The other devices connect to the main router through a LAN port.  This works fine but the secondary devices do not connect to an NTP time server.  They also will not look for firmware upgrades.  Both of these functions go through the WAN port.  Since the WAN port is not used, the device will not automatically update its time nor firmware.

Today, I figured out how to fix that problem.  I found some good information at this web site.  Basically, the way to setup a secondary router on a home network is to give the WAN port a static IP address on the main network and put the LAN ports on a different network.  The LAN ports can give out IP addresses using DHCP.  The secondary router will use NAT to route traffic from the LAN ports to the WAN port.  The router thinks the WAN port is connected to the internet and will use it to connect to the NTP time servers.

It is a good idea to turn on the feature to return a ping on the WAN port on the secondary router.  Also, turn on remote management so you can get to the router from the home network.

My devices are configured like this.

Netgear (main router)

  • WAN IP: DHCP from ISP
  • LAN IP: 192.168.0.1
  • DHCP: On
Cisco
  • WAN IP: 192.168.0.1
  • LAN IP: 192.168.1.1
  • Remote management: 192.168.1.1:443
  • DHCP: On
D-Link
  • WAN IP: 192.168.0.11
  • LAN IP: 192.168.2.1
  • Remote management: 192.168.2.1:8080
  • DHCP: On



Electric Toothbrush Storage

I have an electronic toothbrush.  There is no good way to store it.  The toothbrush is too big to fit in a normal toothbrush holder.  Having the toothbrush standup on the sink is not good because the water builds up on the bottom and creates slime.

I saw an article in a Family Handyman magazine about how to store an electronic toothbrush on a wall using a strip of metal and a magnet.  The article mentioned connecting a strip of metal to a wall, then using a neodymium magnet to attach to the metal.   The toothbrush simply clings to the magnet because it has metal in it.

I happen to have a bunch of neodymium magnets from computer hard drives in my stash of junk.  Those magnets are very strong and are attached to pieces of metal that have built-in mounting holes.  I attached one of those magnets to the wall in my bathroom using drywall screws.

One added benefit is that the magnet appears to charge the battery in the toothbrush.



Friday, December 18, 2015

Simple Basement Storage Platform

I was at the Milwaukee Makerspace for the last time this year and possibly for the next few months.  Tonight, I spent my time making some simple basement storage platforms in the woodshop.  Altogether, I made four platforms.  The materials needed was:

  • Two (2) eight foot long 2x6 boards
  • Two (2) two foot by four foot, 1/8 inch thick sheets of hardboard
  • A handful of nails
The Makerspace has several great woodcutting tools that made the job go very quickly.  In about an hour, I had four pretty nice looking platforms.  The plan is to give them to a friend of ours from our weekly Bible Study group.




Merry Christmas!

Friday, November 20, 2015

Injection Molding Machine Simulator

For the last month or so, I have been spending my weekly Makerspace time working on simulating an injection molding machine on a microprocessor.  This is a project for work.  I want to setup a test environment to work with Mattec, our MES software used to control injection molding machines in our plastic parts plants.  To make a proper test system, I would like to send signals from a simulated injection molding machine into the system.

Beaglebone Black Fail
I first started working with a Beaglebone Black.  I was able to write a simple python program to turn LEDs on an off at random times.  However, that program only worked once.  After the first time, I could not get it to work.  Not sure what went wrong.  I may have fried the BBB somehow.  It still boots, I can connect to it.  I can execute BoneScript code.  I just can't get any python code to work.

At some point, (may have been before I tried the python code but also certainly after) I tried to upgrade the OS on the BBB to the latest version of Debian dates 2015-03-01.  I followed the same instructions from the last time I did this.  I did not get any errors creating the SD card.  When I bootup the BBB in install mode, the lights start flashing.  All three LEDs are supposed to light solid when the install is done.  However, this never happens.  I've tried several times with no success.

Raspberry Pi and Basic Simulation Definition
So, I moved on to the Raspberry Pi.  I wrote a program to randomly turn LEDs on an off.  If I ever actually get to use this on my test system, rather then turning on the LEDs, the Pi will send an electrical signal to an interface unit.  There will be a signal to say that the fill process is complete, one to say a cycle is complete, and one to indicate that the last piece produced was scrap.

Classes
A part of functionfunctionfunctionthe process, I learned how to use a Class to store data.  I wish I would have learned about this earlier.  I love using arrays in other languages.  There is nothing really like an array in Python.  However, a class is even better than an array.  I made a class that represents a molding machine with sub-classes for the sensors on the machine.  If I want to access the cycle time for the machine, I simply access the data at IMM10.Cycle.AvgTime.  Very cool!

Down Event Probability Using a Poisson Distribution
The machine class has a function that will calculate the probability that a machine will go down during a given cycle.  It uses the poisson distribution function to determine this.  Just input the average number of cycles per month and the average number of down events that machine per month.  The poisson distribution function uses these numbers to determine the probability.  Excellent!

General Python Program Functionality
Multiple machines can be defined with different fill rates, cycle times, scrap probabilities, and down probabilities.  The program continuously simulates cycles for each machine.  It will send a signal when the fill part ends, then another signal when the complete cycle ends.  Next, it decides if the machine functionfunctionproduced scrap during that cycle.  If so, it sends that signal.  Finally, it determines if the machine when down at the end of the cycle.  If so, it sets a timer and waits for the down event to end before resetting the fill and cycle timers.

Determining Times Randomly Using a Normal Distribution and scipy
The time for the fill and cycle are randomly determined based on the average times for that machine. The probability calculation is not a simply random number generator.  It gets a random time that is within one standard deviation of the average, assuming that the probability has a normal distribution.  This is better than a purely random number between the minimum and maximum range.  The probability could be outside of that range but should be normally close to the average.  In a future release, I'd like to add in some kind of randomality to generate outliers.

In order to generate this kind of random number, I had to install a Python library called scipy (www.scipy.org). That library has a function called "percent point function" that will compute the inverse of the cumulative distribution function of a standard normal distribution.

I'm not exactly how the function works, but if I send it a random number between 0 and 1, and tell it to use a mean of 40 and 3 standard deviations, its sends back numbers that appear to be distributed around 40.

Here is one helpful link that explains more about using scipy in this way:

scipy can be installed on the Raspberry Pi simply by typing  
sudo apt-get install python-scipy
Future Enhancements
For now, when a machine goes down, the duration of the down event is a purely random number between 5 seconds and 4 hours.  In reality, most down time events last less than five minutes, but some can last for days or weeks.  I'd like to have a formula that will mostly pick down event durations that are short but could pick longer ones.  I can't figure that out just yet.

Another future enhancement would be read in machine and sensor definitions from a text file or some other source.  It would be cool if the source data could be managed with a web page.  Then, I could control the parameters of the device remotely.

Also, the Raspberry Pi outputs about 3 volts.  If I need to generate more volts or a higher current, I might have to make a circuit board with transistors.

This brings us up to date.  I'll add more about generating random downtime durations later.

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.

Friday, August 7, 2015

I want to display a picture on the screen for at most 30 seconds.  If the user presses a button, the picture should no longer be displayed and the program should move on to the next step.

There are several options to do this.
  1. Display a picture in a gui user interface like xwindows
  2. Display the picture on a smaller monitor but use an LED screen as the user interface.
  3. Display a picture as part of another document from the command prompt
  4. Display a picture from the command prompt
I played around with some of the options.  Here are my notes from working on this.  They are not well organized.

Display a picture as part of another document from the command prompt
PDF
Need to make a PDF file.
Display a PDF file.
Close the PDF file on demand.

The Python Imaging Library or PIL might work.  It will display an image but it looks like it uses the computer's default image display program.  Might only work in windows.

Display a picture from the command prompt.
Some people said you could use fbi in another tty session.  fbi does have an option -T to display the image in a different tty.  That does work but it enters the commands at the current prompt on that session.

I can use fbi -t 5 -1 -a -T 2 <image.jpg> to display an image on the second tty and then exit.

Starting the fbi process does not make fbi the "session leader".  The shell is the session leader.  fbi is the process group leader for a process group containing only itself.  See this page for more details.

I started the fbi process on tty1. Then, went to tty2 and killed the process in tty1.  That worked.

I started the fbi process on tty1.  Then, killed it from a ssh session on my laptop.  That worked.  So, if I can just find the session number of the fbi session, I should be able to kill it.

Tried running this command to kill the fbi session:

ps axf | grep <process name> | grep -v grep | awk '{print "kill -9 " $1}' | sh

It locked up the tty sessions.  I could still access the pi using ssh but not with the keyboard.  Had to cycle power on the pi.

After rebooting, did the same thing several more times.  It worked every time.  Not sure what this means.

The psutil code has a function that will get a list of all the processes and put them in a dictionary.  I can use that function to find the process running fbi.  However, fbi just seems to mess up the process when it is kicked off from python.

Tried eog.  It only works with x windows.  Same with evince.

It looks like PDF files can only be viewed in x windows.

Maybe the tty idea will work without killing the process.  The idea would be to display the image in tty2 but switch back to tty1 when the user presses a button.

Googling this topic makes it look too complicated.

Friday, July 10, 2015

MMS Photo-inator Xsi Initial Prototype Worked

The next phase of the MMS Photo-inator project was to complete the prototype.  I spent the afternoon and evening of July 3 working on the prototype.

First, I had to desolder a few more 2N6045 NPN transistors from the board I found on the hack rack at the Milwaukee Makerspace.  I needed two more transistors but I removed four just to have some spares.  I broke one of the feet off by wiggling it too much.  Putting the electrical plug under the board gave me the leverage I needed to pull the transistors off the board after the solder was removed.

The trick to removing soldered components from a circuit board is to, first, use flux to better transfer the heat to the board.  The second tip is to use copper desoldering braid to remove solder from the pins.  After removing the solder, the transistors come off pretty easily with a little force.

The next step was to add some small strips of wood to the board that the buttons were mounted to so it would stand up on its own.  I found some extra strips of wood at the Makerspace and attached them to the plywood.

Now that the board is ready, I needed to wire the connections.  Since I want to be able to assemble and disassemble the prototype easily, I decided to use terminal strips.  I attached three terminal strips to the board.  One would be used for the 12v power coming from the power supply to each light.  One strip would be used mainly to complete the 12v circuit to the collector on the transistors.  I also used one connection on that strip to connect the negative wire on the PSU to common ground.  The final strip would be used to connect the switches to the Raspberry Pi -- three sets of connectors for power in, and three sets for power out when a button was pushed.  I used either u-shaped or blade connectors on the ends of the wires to make it easier to connect the wires to the strips, switches, and lights.

After all the wiring on the prototype was done, I made ten connector wires to connect the prototype board to the breadboard for the Pi.  There are four terminal blocks on the breadboard to accept the connector wires.  One block is for the wires that send 3.3v from the Pi to the buttons.  One block is for the wires that receive the 3.3v back from the buttons.  A third block connects the lights to the transistors, completing the circuit.  The final block connects the ground wire from the PSU to the ground on the Pi.  Finally, I had to wire up all of the connections from the Pi GPIO pins to the various parts on the breadboard.  Here is the schematic.  It is also on SchemeIt at this link: http://www.digikey.com/schemeit/#1reo.

The final prototype worked exactly as I hoped.  The lights lit up and were bright.  The Pi recognized that the buttons were being pushed.  And, best of all, the Pi did not get fried.


Next, I need to work on the Python code and get all of the features working.

Saturday, June 27, 2015

Building an NPN Transistor Switch Circuit

Introduction
The next step in the MMS Photo-inator project is to design a circuit to switch the light bulbs in the buttons on an off using transistors.  The Raspberry Pi 2 can't provide enough power to make the lights bright enough but it can control electronic switches that allow a higher powered power source to turn the lights on.

Using transistors turned out to be very difficult to me.  The concept of a transistor is pretty simple but actually getting a circuit working is not.  I burned out several transistors trying different things.  I have also read various sources to learn more about transistors but it does not seem to help.  Last weekend, Royce from the Milwaukee Makerspace helped me build a circuit that worked.  I'll try to document what I learned for my own edification.

Learn About Transistors (good luck)
Here are some links to learn more about how to use NPN transistors as a switch:


Transistorized Switching Circuit
The objective is to turn three individual light bulbs on and off from my Raspberry Pi using transistors.  The basic circuit is shown below.  Note that everything, 12v PSU, RPi, and lights share the same ground.  This is important.


Transistors will be used as electronic switches to allow current from the 12v, 1.8A power supply to flow through the light bulbs.  The Pi will send a 3.3v signal to the base pin on the transistor.  This is the signal that tells the transistor to turn on the light bulb.

According to this site, the Raspberry Pi can safely put out a maximum of 16 mA per GPIO pin and a total of 50 mA on all GPIO pins.  So, I have to be careful and limit the current from the Raspberry Pi to the base of the transistor to 16mA per transistor.

Transistor Specifications
The first thing to do was to determine the characteristics of the transistor my project needed.  This site was helpful to determine what transistor to use.

The transistor has to handle the output from the power supply I will use -- 12v and 1.8A (max) -- to power the lights.  Even though the lights only draw 0.25A, the PSU can put out 1.8A.  I think it is possible for the transistor to draw the full current in some circumstances so I need a transistor that can handle the maximum current from the PSU.   The parameter for current is called Ic(max) on the datasheets.  The parameter for voltage is called VCEO (max).  When I look at the datasheet for a transistor, it has to have characteristics that fall within these two specifications.

Finally, the amount of current that the base needs must be <= 16mA.  This parameter is called IB.  To determine the IB parameter, you have to find the formula on the datasheet.  It is usually something like IC = X * IB.  Basically, divide the current for the load by X to find the current to send to the base.  In my case, the load current is 1.8A maximum.  If, for example, a particular transistor has a formula of IC = 100IB, IB would be equal to 1.8A / 100 or 18mA.

Finding a Transistor
Next, I had to find a transistor that meets the specifications.  The Milwaukee Makerspace has several NPN transistors in bulk.  I looked up the specifications for each one on the http://www.datasheets360.com/ website.  Unfortunately, none of the NPN transistors on-hand matched the requirements.  It seemed like I was going to have to buy a MOSFET transistor like this one on SparkFun.  On a hunch, I looked through the hack rack at the 'space for any parts that may have transistors and found a circuit board that had sixteen 2N6045 NPN transistors.  According to the datasheet, these transistors have an IC (max) of 10A, and a VCEO (max) of 100V.  Both of these are sufficient for this project.  The IB formula for this transistor is IC/100 or 18mA in my case.  This is just a bit higher than the Pi would like but, the load should not draw that many amperes.  The nominal current that the lights will draw is 0.25A.  This means that the base current needs to be 2.5mA under normal load--well within the Pi's capacity.  Finding the circuit board was an unexpected blessing.  Thanks, Big Guy.

I was able to remove a couple of transistors from the circuit board using a soldering iron.

Resistor for the Base
Its important to prevent the transistor from pulling too much current from the Pi and damaging it.  The Pi can be protected from this by placing a resistor between the Pi and the base pin.  Determining the size resistor that is needed was a bit complicated.

First, we made a circuit with one resistor and one 12v lamp.  A regulated power supply was used to determine the minimum current that would turn on the 2N6045 transistor.  To find the exact current level that the circuit was pulling, I unplugged the positive cable from the PSU, connected the red cable from the multimeter to the positive out on the PSU, then, connected the black lead on the multimeter to the circuit.  The minimum current required to activate the transistor turned out to be 8mA.  Finally, using a multimeter to measure the volts from the base to the emitter while the light bulb was on, showed that the PSU was supplying about 1.4v at this current level.

This means that the voltage from the pin on the Pi needs to be lowered from 3.3v to 1.4v at 10mA (rounded up from 8 just to be safe).  The resistor has to remove 3.3-1.4 or 1.9 volts.  Using Ohm's law to calculate the resistance works like this:

R = V/I or
R = 1.9/0.01A or
R = 190 ohms.

Putting a 190 ohm resistor between the Pi GPIO pins and the base pins of the transistors should keep the Pi safe from pushing out too much current, while providing enough volts to activate the transistor.

I put a 180 ohm resistor (the 'space had a 180 or 220 ohm but no 190) in the circuit and connected the Pi's 3.3v output pin to the base.  The light turned on!

That was a lot of work and very frustrating at times.  I would have never made it this far without Royce.  That is what is so great about the Milwaukee Makerspace.  There is usually someone around who can help you through the rough spots.

Next, I need to remove some more resistors from the board and build a prototype of the complete MMS Photo-inator project.

Friday, May 29, 2015

MMS Photo-inator Xsi -- 22-25 May session

While at the Milwaukee Makerspace for my weekly Maker-time, I continued working on my idea to use a Raspberry Pi 2 Model B to make a device that will take pictures by controlling an SLR camera. This time, I found some software to display an image, made a prototype with hardware, and wrote most of the python code.

Installed Avahi on the pi so I can connect to the Pi over a network.  This allows me to do coding on my Linux laptop and FTP the files to the Pi.  If something happens to the Pi, I won't lose the code.  The instructions to install Avahi are here.  Changed the name of the device to photoinator in both the /etc/hostname and /etc/hosts files.

I kicked around ideas for a human to interact with the photo-inator.  One of my fellow Makers suggested doing everything in web pages.  That would probably make everything easy including printing the picture but it wouldn't make much use of the Perma-Proto Pi Hat from Adafruit.  I'd like to have the user interact with the photo-inate by pressing buttons and seeing flashing lights.

Also, I would like to display a preview of the picture and let the user decide if it is good enough to keep or not.  How do I get the Pi to display a jpg file without using xwindows?

First, I tried an application called feh to display images.  This web page has the instructions for installing feh: http://feh.finalrewind.org/

After messing around with feh, I realized that it only works from within xwindows.  That does not help -- especially since gphoto2 only runs from the command line and will not work in a terminal session running in xwindows.

The application that I needed is called fbi.  I installed the app using sudo apt-get install fbi.  After that, it just worked.  I ran the program in the directory that has the photos in it.  The way to run fbi is fbi image.jpg -a -t 10 -1.  The -a will auto re-size the image to fit the screen.  -t 10 tells fbi to display the image for 10 seconds.  -1 means that fbi should exit after the timeout.

Next, I wrote a quick shell program to take a picture with gphoto2 then display it with fbi.  I pass the filename to save the picture to as a parameter.  The shell file looks like this:

gphoto2 --capture-image-and-download --filename=$1.jpgfbi $1.jpg -a -t 10 -1

The $1 lets me pass in a file name on the command line.

Next, I used a python program that I found on this Instructable and modified it a bit to control a button and an LED on the Adafruit T-cobbler Plus proto board.  It worked.  I was able to press a button to start the process.  The program flashed an LED, took a picture, and displayed it on the screen for 10 seconds.  Nice.

I spent a good part of the day on Sunday (after church of course) writing a much more robust Python program with subroutines and global variables and all of that nerdy goodness.  

Finally, I spent Memorial Day back at the Makerspace to build the prototype and debug the code.  The prototype has three buttons and four LEDs.  One button is to start the picture-taking process.  Another will let the human choose to Keep the picture they just saw.  The third button will tell the Pi to reset everything and start over.  One of the LEDs will light when the picture-inator is ready to take a picture.  The second LED will blink increasingly fast to warn the user that the picture is about to be taken.  The final two LEDs blink alternately next to the Keep and Reset buttons to encourate the human to push one.  This all works fine.  For the final version, I would like to use small light bulbs instead of LEDs to increase the brightness.  I probably need a stronger power source than the PI to light them.  I will also need a more complicated circuit that includes a transistor.  I'll design and build that in the next session.

Another idea I had was to add a toggle switch to designate the mode that the picture-inator is in -- one shot or continuous.  That should be pretty easy to do but I did not get to it this time.

One of the things I struggled with on the coding side was the GPIO.add_event_detect function.  I originally wanted the program to call a subroutine when a button was pressed.  However, I could not get that to work.  The subroutine might get called the first time the button was pressed but not after that.  Sometimes, pressing the button never did anything.  I ended up punting and doing several things to make this work.

First, the main loop uses GPIO.event_detected to see if any of the buttons were pressed.  Anytime a button is pressed or a state changes, the program turns off event detection for all buttons.  Then, it does whatever the function is meant to do, and turns on even detection for any buttons that need to have it.  Doing all of this should allow the program to always catch button presses and never have false-positives or multiple button presses.

One other problem is that I don't know how to interrupt the picture being displayed.  So, if the person pushes the Keep button while the picture is being displayed, nothing will happen.  I don't think I can fix that.  I spent a lot of time researching a way to call an external program in a separate thread but I'm not sure it is possible.

The bug I am currently working on has to do with debug mode.  When the program is running in debug mode everything works.  However, when I run the program with debug mode turned off, it turns on the ready LED but will not do anything when any button is pressed.  I have not been able to figure out why.

Another bug is if the camera is off, gphoto2 returns an error.  The program cannot handle the error.  Need to figure that out.

Here is a picture of the prototype at this point.