Practical] I made a gadget to prevent people with chronic illnesses from forgetting to take their medicine.

capsule drag

photo credit: incurable_hippie via photopin cc

Good evening, this is Bono.

A relative of mine has a certain disease, and he has to take his medicine before every meal, but he often forgets to do so.

If it is after every meal, it seems to be okay because of the trigger of rice, but certainly if it is before every meal, it is usually difficult to remember unless you have to remember it yourself.

So, I made and presented a gadget to prevent such forgetting to take medicine, and here is a summary of what I did.

finished product

This time, I packaged it inside a toy Lodi. (I bought it as a toy, but my child didn't play with it much, so I decided to put it to good use this way.)

NewImage
content

(technical) specification

The main specifications are written below.

  • Sounds at fixed times of 6:00, 11:00, and 17:00 every day.
  • Two types of sounds are available: music and voice.
  • The music was "A scent of cyclamen" by Akira Fuse, a favorite of my relatives.
  • The voice is Fuse Akira's voice, "Are you forgetting something?" and speaks to you.
  • Surprise a child's toy that was already in the house with a gadget.

What you need

The following parts/materials are used here.

  • Raspberry Pi TypeB
  • SD card: 8GB
  • Speaker: ELECOM's ASP-SMP100L
  • Real Time Clock (RTC): DS3231
  • Stuffed animals (anything that can be packaged): Lodi dolls for toys
  • power (button on TV, etc.)
  • Phonematerials (described below)
  • Switch: 2-stage ON/OFF switching type
  • Breadboard or universal board
  • Electric wire: male-female is useful to connect the board to the Raspberry Pi
  • Tupperware: 100 yen uniform is sufficient

way of making

Prepare Raspberry Pi

Follow the previous posts to install the OS on the Raspberry Pi and perform the initial setup.

Raspberry Pi] Summary of settings from buying a Raspberry Pi to connecting to a PC in the network via SSH | Makepresso

Install pygame

Install the library pygame for music playback. We referred to the following site for the installation and its configuration.

You can play, stop, fade out, adjust volume, etc., so with a little effort you can even create a jukebox.

Playing mp3 in Python - Programming Log

music - Pygame Documentation

Speaker installation

Raspberry Pi by itself cannot produce sound, so we purchased speakers.

This time I wanted to mount it all on the Tupperware, so I searched Amazon for speakers and chose the smallest possible. (which ended up sticking out a bit)

The volume is sufficient for use in the house, the sound quality is not bad, it is rechargeable and can be used many times, resulting in a good purchase. I recommend it.

Lithium type compact speaker for smartphones - ASP-SMP100L series

small speaker

Prepare sound material

Since he is a Fuse Akira fan, we attempted to prepare two types of voices: a hint of cyclamen, his signature song, and his voice.

As a result, we decided to forego voice this time and use only music.

Although pygame can play sounds in several formats, we use the familiar mp3 format here.

music

Download Cyclamen no Kahori on iTunes.

I then converted it to mp3 format on iTunes according to the following site. This is OK.

Converting to MP3 with iTunes [How to use iTunes even beginners can understand].

voice

It would be best if we had audio of Fuse Akira himself, but since we have no connections or anything, we will not do such a high hurdle.

In such a case, it is all about posting on koebu! (I used it for the first time at a hackathon I competed in the past and found it so useful that I've been saving up to use it somewhere else for a long time.)

Therefore, we have solicited mimicry voices with the following conditions.

  • in the manner of Fuse Akira
  • Dialogue: hoge, aren't you forgetting something?
  • Request: Speak softly to me.

However, after waiting two weeks, no one has submitted any audio.
Was it still too different generations? For now, we decided to give it a miss this time.

Volume control

I adjusted the volume according to the following website. With this speaker, I got a good volume at about 90%.

[crayon] sudo amixer cset numid=1 90
[/crayon]

Linux Blog : Using the Raspberry Pi - Adjusting the volume

RTC Installation

Buy

In this case, we want to have the ability to produce sound at a certain time, so it is essential to know the real time.

However, the Raspberry Pi does not have an RTC function to keep track of real time.

The method would be to either use NTP, which uses the network, or an external RTC module.

In this case, we decided not to use the network because there is not much need for it.
So we decided to install an RTC module and get real-time from it.

The purchase was made on Amazon. The price was about 1,300 yen.

DS3231 Ultra-High Precision I²C Integrated RTC/TCXO/Crystal - Maxim's

The unit price itself is cheaper at places like RS Online, so if you're buying in bulk, that's a better deal.

Writing time to RTC

Basically, we followed the following website.

RTC module with I2C connection DS3231 [E-Mail] - VICTORY SEVEN Net Sales

In addition, although it was not mentioned on this site, after the following line, the following method was used for writing.

[crayon] echo ds3231 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
[/crayon]

After this, the current time is obtained from the network using the following command.

[crayon] sudo ntpd -gq
[/crayon]

The following command was then used to write the set time to the RTC.

[crayon] sudo hwclock -w
[/crayon]

Finally, make sure it is written properly, just to be sure.

[crayon] date
[/crayon]

Installation of switch section

Switch Preparation

For versatility, the following multiple switches were installed. The switches themselves were purchased at Akihabara.

  • ON/OFF switch: Enables/disables the function
  • ON/OFF switch: select music/voice mode
  • Push switch: manual mode (toggle playback and stop)

circuit design

Determination of the number of

The GPIO only needs to be able to detect High/Low levels, so the current should be small.

Here, we used a 10kΩ resistor that we had on hand. (As a precaution, the resistor value should not be too large, since the switch has a minimum current value specification and it is recommended that a higher current be applied.)

Circuit diagram

141225 dragpy schematic

Mounted on board

Mount the device on a universal board with a soldering iron.

The universal board is only for testing.

Precautions

Due to the physical characteristics of the switch, it does not switch instantaneously between ON and OFF the moment it is pressed, but rather repeats ON and OFF several times, settling down after a certain time. (You can see this clearly by looking at it with an oscilloscope.)

This phenomenon is called chattering.

Whenever you use a switch, you must keep this phenomenon of chattering in mind, or you will have a problem somewhere.

There are several measures that can be taken.

For example, a low-pass filter can be created with a resistor and a capacitor to eliminate chattering in a hard way, or a soft way can be used to determine the state when the state has not changed for a certain period of time.

In this case, we wanted to make the circuit as small as possible, so we took care of the problem software-wise.

Attach RTC, switches, speakers, power supply, and circuitry to Raspberry Pi

Drill holes in the tupperware and attach the RTC, switch, power supply, and circuitry as shown in the photo below.

Mounted on Raspberry Pi

The switch was fixed with hot melt to prevent it from coming off. I actually tried to fix the speaker and circuit as well, but gave up due to lack of time.

Creating a program to play sound

python.

What's in the Program

[crayon] import pygame.time
import pygame.mixer
pygame.mixer.init()
pygame.mixer.music.load("/home/pi/RPi.GPIO-0.4.1a/test/Shikuramen.mp3")
GPIO.setmode(GPIO.BCM)
GPIO.setup(21, GPIO.OUT)
GPIO.setup(18, GPIO.IN)
GPIO.setup(23, GPIO.IN)
GPIO.setup(24, GPIO.IN)
playing = False
pause = False
power_input1 = GPIO.input(23)
time.sleep(0.1)
power_input2 = GPIO.input(23)
if power_input1 == power_input2: if power_input1 == power_input2: if power_input1 == power_input2
if power_input2 == True: if power_input2 == True
print("now playing")
pygame.mixer.music.play()
time.sleep(275)
pygame.mixer.music.stop()
[/crayon]

Brief description of the program

First, load a previously created mp3 file.

For power_input1 and power_input2, if the state after 0.1 second matches a certain timing for software chattering prevention, the switch state is assumed to be fixed.

and if the power switch is ON, music is played.

For a playback time of 4 minutes and 34 seconds for Cyclamen's Scent, time.sleep is set to 275 seconds.

The following site was used as a reference for setting up to use GPIOs.

Basic usage of RPi.GPIO | Mamelium

operation check

The following command successfully played a hint of cyclamen! Sound quality and volume are not bad.

[crayon] sudo python /home/pi/RPi.GPIO-0.4.1a/test/drag.py
[/crayon]

Setting up cron

Now that the music has been played above, all that remains is to set up cron to run this command at a certain time!

I thought it would be easy to set up cron, but I ran into a little bit of trouble. I thought it would be, but I got a little stumped.

I called cron with the following command, which is the usual way, and set it up to run every minute as follows.

[crayon] crontab -e
[/crayon] [crayon]
          • sudo python /home/pi/RPi.GPIO-0.4.1a/hoge.py
            [/crayon]

However, no matter how long they waited, the line was not executed.

When I type the above command manually instead of cron, it runs fine.

I found an unexpected error when I looked at the following file, which can spit out cron errors.

[crayon] vim /var/log/sylog
[/crayon] [crayon] Dec 18 22:40:03 raspberrypi /USR/SBIN/CRON [2390]: (CRON) info (No MTA installed, discarding output)
[/crayon]

As shown in the following page, others are experiencing the exact same phenomenon. Apparently, cron can only be used if a mailer like postfix is installed.

RaspberryPi - Talking about using cron with Raspberry Pi - Qiita

Since it is not included in Raspberry Pi by default, install as follows.

[crayon] $ sudo apt-get install postfix
[/crayon]

Here the configuration screen appears. In this case, I selected the lowest, LOCAL USE ONLY.

For your information, we have set the music to play every day at 6:00, 11:30, and 5:30 pm.

Completion!

Finally completed!

It took about two weeks, including the time to gather tools.

He was surprised and pleased with it, and is still using it every day. He still uses it every day.

content

supplement

How to play music on Raspberry Pi

In addition to pygame, there seems to be another software called mpg321. I tried it and was able to play it here as well.

See the following website for details.

Linux Blog : Using the Raspberry Pi - Playing mp3 files