As this post is more of an update, I won’t be adding any explanations, just giving the python code.
This will read 3 values from the adc and put them into the database “adc_database”. It will put them in the table “adc_input_data_4” in the columns “Channel_1″,”Channel_2” and “Channel_3” respectively.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import spidev
import time
import MySQLdb
import sys
import RPi.GPIO as GPIO
pin = 26
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin, GPIO.OUT)
con = MySQLdb.connect('localhost','adc_user','adc_user_pass','adc_database');
cursor = con.cursor()
spi = spidev.SpiDev()
spi.open(0, 0)
count = 0
maxcyclenumber = 5
tmp = "derp"
def readadc(adcnum):
# read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7)
if adcnum > 7 or adcnum < 0:
return -1
r = spi.xfer2([1, 8 + adcnum << 4, 0])
adcout = ((r[1] & 3) << 8) + r[2]
return adcout
for _ in range(maxcyclenumber):
GPIO.output(pin,True)
cursor.execute("INSERT INTO adc_input_data_4(Channel_1,Channel_2,Channel_3) VALUES('%s','%s','%s')",(readadc(0),readadc(1),readadc(2)) )
GPIO.output(pin,False)
count = count+1
print count
time.sleep (1)
if count == maxcyclenumber:
GPIO.cleanup()
con.commit()
con.close()
Now everything should be good to go, now for the python.
You can debug this any way you like, but my favorite way to do it is using the program geany. I like to start up a VNC server with root so I don’t get into any trouble with the GPIO permissions.
But here’s the program.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import spidev
import time
spi = spidev.SpiDev()
spi.open(0, 0)
count = 0
def readadc(adcnum):
# read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7)
if adcnum > 7 or adcnum < 0:
return -1
r = spi.xfer2([1, 8 + adcnum << 4, 0])
adcout = ((r[1] & 3) << 8) + r[2]
return adcout
while True:
tmp1 = int(round(readadc(0)/10.24))
print "in1:",tmp1
count = count +1
time.sleep(0.2)
And that’s pretty much it, the result should look something like this:
Please note that this is more for my sake. To mount a hard drive in raspbian do the following: Make sure you have ntfs-3g installed by running:
sudo apt-get install ntfs-3g
Then mount the drive read/write with the following command:
sudo mkdir /media/USBHDD
sudo mount -t ntfs-3g /dev/sda1/ /media/USBHDD/
And it shout be mounted. /dev/sda1 is the location of your hard drive. Now to configuring minidlna. Location of minidlna.conf file and access command:
sudo vi /etc/minidlna.conf
This is the file I’m running right now. As I type this i’m successfully streaming to my Kindle Fire HD (the reason why I’ve decided to really make this thing work) but I’m not sure if it’s stable. It’s also able to stream to VLC as of now.
It’s time to face facts, minidlna and XBMC won’t run at the same time in Raspbmc. The basic UPNP included in Raspbmc won’t work consistently and Raspmc and is not nearly as stable as minidlna. OpenELEC is fast enough, but does not have the expandability of a full linux OS. I need to restart this project.
First thing’s first I’m going to straight up speed this thing up as much as I can. At the base level, this begins with the SD card. I’m going to go from a junk 4gb standard speed SD card to a 8gb SanDisk Ultra 30mb/s SDHC. On this I’m going to install the latest version of Raspian and overclock it to the maximum 1GHz.
So although I haven’t written about it yet, right now I’ve gotten a Raspbmc Server up and running along with Minidlna. It works great for about an hour at a time and then it really bogs down. This seems to be caused by a combination of Raspbmc and the hard drive spinning down. My solution is to try OpenELEC, overclock the Raspberry Pi, and change out the SD card for one with a faster read/write speed.
For my Raspberry Pi Media Server to be able to stream to mobile (a topic I haven’t yet covered here) I need to convert ALL of the video in my family’s 700+ file, 400+ gb, media collection to H.264/MPEG-4 AVC. I recently acquired a hard drive to consolidate and store all of this hard drive on. For the past week or so I’ve been making some progress in converting the video. On my windows tower I’ve been using the program Format Factory to convert the video. I’m running into a few problems with this. For one, my windows computer is my main machine, and multithread converting really bottlenecks it. It also takes a long time – having to watch it while it does it’s thing waiting for it to convert instead of just going to the next set of files is annoying. I could just convert
So I’ve decided to get back into python and write a program that will perfect the conversion process. Like in my PiScanner project, I’ll be using an existing program within python and my code will automate the process.
Goals are as follows:
1. Convert the video
2. Make sets of folders for the video
3. Have it all done on the external HDD
4. Have it be more “efficient” than the setup I’m running now. I.E be able to be on all the time – which shouldn’t be a problem as the server you’re viewing this website on is on all time time and I can just run the script here.
I’ve made some progress on the RPi Streaming Server.
To replicate this you’ll need to do the following:
1. Install miniDLNA
sudo apt-get install minidlna
2. Edit the config file to how you want it. Edit it using vim
sudo vi /etc/minidlna.conf
3. Here’s what I’m working with, and it works.
# This is the configuration file for the MiniDLNA daemon, a DLNA/UPnP-AV media
# server.
#
# Unless otherwise noted, the commented out options show their default value.
#
# On Debian, you can also refer to the minidlna.conf(5) man page for
# documentation about this file.
# Path to the directory you want scanned for media files.
#
# This option can be specified more than once if you want multiple directories
# scanned.
#
# If you want to restrict a media_dir to a specific content type, you can
# prepend the directory name with a letter representing the type (A, P or V),
# followed by a comma, as so:
# * "A" for audio (eg. media_dir=A,/var/lib/minidlna/music)
# * "P" for pictures (eg. media_dir=P,/var/lib/minidlna/pictures)
# * "V" for video (eg. media_dir=V,/var/lib/minidlna/videos)
#
# WARNING: After changing this option, you need to rebuild the database. Either
# run minidlna with the '-R' option, or delete the 'files.db' file
# from the db_dir directory (see below).
# On Debian, you can run, as root, 'service minidlna force-reload' instead.
media_dir=A,/home/pi/stream_files/media/audio
media_dir=P,/home/pi/stream_files/media/pictures
media_dir=V,/home/pi/stream_files/media/video
media_dir=/home/pi/stream_files/media
# Path to the directory that should hold the database and album art cache.
db_dir=/var/lib/minidlna
# Path to the directory that should hold the log file.
#log_dir=/var/log
# Minimum level of importance of messages to be logged.
# Must be one of "off", "fatal", "error", "warn", "info" or "debug".
# "off" turns of logging entirely, "fatal" is the highest level of importance
# and "debug" the lowest.
#log_level=warn
# Use a different container as the root of the directory tree presented to
# clients. The possible values are:
# * "." - standard container
# * "B" - "Browse Directory"
# * "M" - "Music"
# * "P" - "Pictures"
# * "V" - "Video"
# if you specify "B" and client device is audio-only then "Music/Folders" will be used as root
#root_container=.
# Network interface(s) to bind to (e.g. eth0), comma delimited.
#network_interface=
# IPv4 address to listen on (e.g. 192.0.2.1).
#listening_ip=
# Port number for HTTP traffic (descriptions, SOAP, media transfer).
port=8200
# URL presented to clients.
# The default is the IP address of the server on port 80.
#presentation_url=http://example.com:80
# Name that the DLNA server presents to clients.
#friendly_name=
# Serial number the server reports to clients.
serial=12345678
# Model name the server reports to clients.
#model_name=Windows Media Connect compatible (MiniDLNA)
# Model number the server reports to clients.
model_number=1
# Automatic discovery of new files in the media_dir directory.
#inotify=yes
# List of file names to look for when searching for album art. Names should be
# delimited with a forward slash ("/").
album_art_names=Cover.jpg/cover.jpg/AlbumArtSmall.jpg/albumartsmall.jpg/AlbumArt.jpg/albumart.jpg/Album.jpg/album.jpg/Folder.jpg/folder.jpg/Thumb.jpg/thumb.jpg
# Strictly adhere to DLNA standards.
# This allows server-side downscaling of very large JPEG images, which may
# decrease JPEG serving performance on (at least) Sony DLNA products.
#strict_dlna=no
# Support for streaming .jpg and .mp3 files to a TiVo supporting HMO.
#enable_tivo=no
# Notify interval, in seconds.
#notify_interval=895
# Path to the MiniSSDPd socket, for MiniSSDPd support.
#minissdpdsocket=/run/minissdpd.sock
You can grab the file itself here. The only thing that’s different is where I put the media directories. The rest of the instructions are still in the .conf, it’s much simpler than mediatomb for example.
Here’s a video of the whole thing working on 3 devices!
Once those are installed you can start trying to play videos off of the usb drive (eventually the thumb drive will be replaced with a large hdd
I’ll be using filezilla to transfer to and from the pi.
Check out the video below to see omxplayer in action:
Edit: The upload failed and I forgot to check on it. Come back soon for the video.
I’m choosing to debug my Pi over composite. The reason being is because I’m going to be working a lot with video outputs, and I don’t have a second hdmi ready monitor. I’m going to be using the EasyCAP usb video capture card. This will also give me the ability to record what i’m seeing as well. This 100% better / much more efficient than buying a second monitor and filming on it. Of course when this is ready for implementation, it will be plugged into a television with HDMI so the picture will be amazing.