Raspberry Pi with Audio

Tags

, , ,

To sound alarm when it is time to take medicine

– use cron job to run a python file that plays audio file – can be child’s voice to remind parents to take medicine.

reference link
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=32&t=31613
http://makezine.com/projects/pilarm-portable-raspberry-pi-room-alarm/
http://www.raspberrypi-spy.co.uk/2013/06/raspberry-pi-command-line-audio/
** http://raspberrypi.stackexchange.com/questions/7088/playing-audio-files-with-python
** http://www.pygame.org/docs/ref/music.html
http://www.raspberrypi-spy.co.uk/2012/06/raspberry-pi-speakers-analog-sound-test/
https://github.com/leathersole/raspberry-pi-python-sample/blob/master/sound-test/playsound.py
http://blog.whaleygeek.co.uk/playing-sounds-on-the-raspberry-pi/

Things to do

Tags

, , , , , , ,

There are a few things that I would like to do / improve on this current web app after writing the first draft of my report. (just in case i forgot what i wanted to do)

  1. Tidy up database – create table to store patient’s medicine intake
  2. Grab data from database and use it to dispense accordingly. This should be achievable by displaying the data in a table/textbox on the web app then grab the value and post it to the python function
  3. set up cron job to run a bash/python file to dispense pill at certain timing
  4. take photo when the button “dispense” is clicked

That’s all! Will add on if I thought of any.

Repeating Servo Movement at your choice

Tags

, , , ,

The next step would be allowing the user to select how many times the pills have to be dispensed. This could be useful when the same pills are dispensed for many patients. The idea was to let the user to select a number using “spinner” by jQuery UI. The code is as follow:

var spinner = $( "#spinner" ).spinner();
$("#spinner").spinner({min: 1, max: 10}); //to set min and max value for spinner
//get the value in the input
var amount = $('#spinner').val();
//post the data with the pilltype
$.post('http://172.20.194.122:8080/dispense', {pilltype:combo, amount:amount}).done(function (dispensed) {
	$('#testing2').empty().append(dispensed);
});

At the Python side, convert the data into integer and use a while loop to repeat the movements.

        amount = data.get("amount", None) 
        amount2 =int (amount) #to convert the above unicode "amount" into integer

        count = 0
        while (count < amount2):

            if pilltype == "A":
                    pigpio.set_servo_pulsewidth(servos[0], 900) 
                    time.sleep(1)
                    pigpio.set_servo_pulsewidth(servos[0], 1500) 
                    time.sleep(1)
                    print "Dispensed A!"
            count = count +1

See the demo video here:

Controlling Servos from Web App – checkbox!

Tags

, , , , ,

The idea is to let the user selects the types of pill using checkboxes. RaspiCare will then dispense the right type of pill accordingly.

So when the “Dispense” button on the web app is clicked, the jQuery function will go through each checkbox and check their “checked” status. For those that are checked, the value will be “pushed” into the array variable called “selection”. Then their values are combined as a variable “combo”. This variable will be “posted” to Python function called “dispense”. The codes are as follow:

$('#dispense').click(function(){
	var selection=[];
	$(':checkbox:checked').each(function(){
		selection.push(this.value);
	});
	var combo = selection.join('');
	
	$('#testing').empty().append('Checked value: '+combo +' x '+amount);  
	$.post('http://172.20.194.122:8080/dispense', {pilltype:combo}).done(function (dispensed) {
		$('#testing2').empty().append(dispensed);
	});							
});

At the Python side, get the data and do some if-else statements. Awesome it works. Something like this:

 @cherrypy.expose
    def dispense (self, **data):

        import pigpio
        import time

        servos=[4,7] #GPIO number
        pigpio.start()
        pilltype = data['pilltype']

            if pilltype == "A":
                    pigpio.set_servo_pulsewidth(servos[0], 900) 
                    time.sleep(1)
                    pigpio.set_servo_pulsewidth(servos[0], 1500) 
                    time.sleep(1)
                    print "Dispensed A!"
                  
            elif pilltype == "B":
                    pigpio.set_servo_pulsewidth(servos[1], 900) 
                    time.sleep(1)
                    pigpio.set_servo_pulsewidth(servos[1], 1500) 
                    time.sleep(1)
                    print "Dispensed B!"

Connection as follow:

Connecting to the servo :)

Connecting to the servo 🙂


So here is the demo video:

Turning the Servo Motor in Steps

Tags

, , , ,

I would like to control the turning of the servo motor from the web app. The servo motor has a camera mounted on it. In this way, I can monitor the surrounding environment from the camera. But it is only limited to 180 degree. It would be nice to have both tilt and pan control of the camera (may be see this: http://youtu.be/4AwF_YYbORM). That would require 2 servo motors and it’s beyond my scope of this current project. Perhaps I could explore it in times to come.

The idea was to set and keep the value for the servo PWM in a variable declared outside of the Python function. So, when the left and right button is clicked, the servo would turn by a certain amount. jQuery was used to trigger the function by posting a direction to it, as such:

$('#left_button').click(function(){
	$.post('/turnCamera', {direction:"left"}).done(function (reply) {
		$('#camerapos').empty().append(reply);
		alert("left button clicked");});					
});
$('#right_button').click(function(){
	$.post('/turnCamera', {direction:"right"}).done(function (reply) {
		$('#camerapos').empty().append(reply);
		alert("right button clicked");});
});

However the append and alert function was not seen at the main page but can be seen at :8080.
The code in the python is as follow:

    # define a variable m outside the function but inside the class
    def __init__(self):
        self.m = 1500

    @cherrypy.expose
    def turnCamera (self, **data):

        import pigpio
        import time
 
        step= 100   #set a step size of 100
        servos=[4] #GPIO number
        pigpio.start()
        key = data['direction']

        if (self.m >= 500 and self.m <= 2500): #check if it's within the limit range for the servo
            if key =="left":
                self.m +=step
                 
            elif key =="right":
                self.m -=step

            pigpio.set_servo_pulsewidth(servos[0], self.m) 
            servostat= "Position of servo: %s" % self.m
            print servostat
            time.sleep(1)

        pigpio.stop()

        return servostat

See the output here:

Showing servo's position

Showing servo’s position

Showing servo's position

Showing servo’s position


See the demo video here:

NOTE: Joo Kai (the knowledgeable tech guy in the lab) said, to reduce the buzzing noise from the servo, i should send command to it more frequently.

Better Control with the Web App – my algo works!

Tags

, , , , ,

More coding!
More testing!
More results!

Yay, this is going to be a long post as I’ve achieved quite a lot today. But my index finger got hurt while making circuit today:(

My 3D printing is done for the time being. Left with assembling the parts which I can delay it till a week before demo day.

I wrote an algorithm for my pill dispenser last midnight (this morning?) and get to test it out today. 😀 Will do 3 separate posts on it.
1. Turning the camera in steps
2. Control servos based on the checkbox checked
3. Repeat servo movement based on the number input.

And yay, I managed to get my circuit running on this Regal DC Supply! (i have been eyeing on it for very long O_O)

Regol DC Supply

Regol DC Supply – supplying 5V, 1A

Connecting the 5V and Gnd

Connecting the 5V and Gnd

Connecting to the servo :)

Connecting to the servo 🙂

Just one thing to note when connecting external power supply to the servo motors. Connect the supply ground to Raspberry Py’s ground!

The helpful nice guy in the lab told me that, to supply the power from RPi to the servo motors safely, connect a capacitor (47uF) between source and ground and connect a reverse diode near to the source to absorb the EMF and reuse it. (ok, i may remember this wrong because it has been more than 12 hours since he told me this)

3D printing Day!

Tags

, , ,

Today I printed quite a number of stuffs. Yay!
See photos!!

3 Pill storage holders with the small one being printed wrongly. haha.

3 Pill storage holders with the small one being printed wrongly. haha.

Little Chair? No it's my camera holder!

Little Chair? No it’s my camera holder!

Ugly finishing with hot glue.

Ugly finishing with hot glue.

Camera Attached to the servo with the holder!

Camera Attached to the servo with the holder!

Camera attached to servo

Camera attached to servo

Camera attached to servo

Camera attached to servo

The first generation 3D printer, no longer functioning.

The first generation 3D printer, no longer functioning.


servo holder

Extended servo holder

iphone 5 stand

An iPhone5 stand for my prof 😀

Update 18/3

Tags

, ,

I did a lot of enhancement to my web app today. I merged 2 web pages into 1 and tidied up the codes using jQuery.

Things that are working so far:
– camera app working with jQuery. Yay!
– Nicer user interface!

Things to do tomorrow:
– Print 3D stuffs
– Test servo
– Database design

Some screenshots of web app in progress:

Screenshot 2014-03-19 01.35.36

Screenshot 2014-03-19 01.48.07

Screenshot 2014-03-19 01.48.19

Screenshot 2014-03-19 01.48.27