Posted on

Light Activated MP3 player (Part 1/2)

Giftbox
Light Activated MP3 Player

Light activated MP3 player was incepted when I wanted to add something special to the present that I gave to my Mrs. So while I was learning about Arduino and have a play with the DFPlayer Mini, which is essentially an MP3 player.

Step 1. Prepare the MP3 Player

First thing to do is to connect the DF Player mini to Arduino using the following circuit.

DFplayerTest

The SD card will need to contain an MP3 file, let’s say the file is 001.mp3, this is located in the 006 folder of the SD card, i.e.: sd:\\006\001.mp3.

Once you connect the diagram above you can use the following code to test to ensure that the DF Player is playing the song you had saved. If you came from Instructables, step 4. you would want the following code to test your DF player.

// this example will play a random track from all on the sd card
// it expects the sd card to contain some mp3 files
#include <SoftwareSerial.h>
#include <DFMiniMp3.h>
// implement a notification class,
// its member methods will get called 
//
class Mp3Notify
{
public:
  static void OnError(uint16_t errorCode)
  {
    // see DfMp3_Error for code meaning
    Serial.println();
    Serial.print("Com Error ");
    Serial.println(errorCode);
  }
  static void OnPlayFinished(uint16_t track)
  {
    Serial.print("Play finished for #");
    Serial.println(track);  
  }
  static void OnCardOnline(uint16_t code)
  {
    Serial.println("Card online ");
  }
  static void OnCardInserted(uint16_t code)
  {
    Serial.println("Card inserted ");
  }
  static void OnCardRemoved(uint16_t code)
  {
    Serial.println("Card removed ");
  }
};
// instance a DFMiniMp3 object, 
// defined with the above notification class and the hardware serial class
//
//DFMiniMp3<hardwareserial, mp3notify> mp3(Serial1);
// Some arduino boards only have one hardware serial port, so a software serial port is needed instead.
// comment out the above definition and uncomment these lines
SoftwareSerial secondarySerial(10, 11); // RX, TX
DFMiniMp3<SoftwareSerial, mp3notify> mp3(secondarySerial);
int Song=1;   // 001.mp3
void setup() {
  Serial.begin(115200);
  Serial.println("initializing...");
  mp3.begin();
  mp3.reset(); 
  
  // show some properties and set the volume
  uint16_t volume = mp3.getVolume();
  Serial.print("volume ");
  Serial.println(volume);
  mp3.setVolume(20);
  
  uint16_t count = mp3.getTotalTrackCount();
  Serial.print("files ");
  Serial.println(count);
  uint16_t mode = mp3.getPlaybackMode();
  Serial.print("playback mode ");
  Serial.println(mode);
  
  Serial.println("starting...");
  
  //mp3.playRandomTrackFromAll(); // random of all folders on sd
  mp3.playFolderTrack(6, Song); // play song on sd:\\006\001.mp3
  delay(5000);
}

void loop() 
{
  // calling mp3.loop() periodically allows for notifications 
  // to be handled without interrupts
  mp3.loop();
}

 

If you are having trouble loading the “DFMiniMp3” library you can download it from the Instructables step 6, the library called “DFPlayer_Mini_MP3”.

To take it to the next level I added the LDR, which is essentially a resistor which are photosensitive. The resistance changes when there is a light shine through it, combination of the 2 makes up the Light activated MP3 Player. We will go through this in the next part.

 

Posted on

Password Protect PDF document using windows Powershell script

Password protect PDF document can be done using Windows Powershell script. I stumble across a powerful script which allow to password protect PDF document using the script. The requirement that I have is to password protect a list of batch PDF document with a specified password that are different between each document and then sending each password protected PDF document to different individual emails.

So after researching for a while I found the script that does the job, but then I come across another hurdle, how do I get the list of password and the list of pdf, so my solution is to put the list of pdf files along with the password in an excel document, and let the script open the excel find the first pdf document, then read the designated password next to it, and run the script to password protect the file and save the password protected file in a different location.

So hunting around for the powershell script to read excel proven to be fruitful, so I combine both of them into one script. Here are the step by step on what I had done and how to modify this to suit your need. This proven to speed up the manual work by a lot. Not to imagine the time and cost savings that comes along with it.

Step 1. Create the list with location of PDF document and the password that you want to protect the document with.

In picture below, we have the source of document in column B, and the target encrypted PDF file name will be located in the location specified in column C. The password for each document will be based on the information in column D. Save this Excel file in C:\temp\ location and lets call the file Book2.xlsx for this exercise.

Step 2. Open your favourite text editor and paste in the following code, you can also use the “Windows PowerShell ISE“:

[System.Reflection.Assembly]::LoadFrom("itextsharp.dll")

function PSUsing {
 param (
 [System.IDisposable] $inputObject = $(throw "The parameter -inputObject is required."),
 [ScriptBlock] $scriptBlock = $(throw "The parameter -scriptBlock is required.")
 )
 
 Try {
 &$scriptBlock
 }
 Finally {
 if ($inputObject.psbase -eq $null) {
 $inputObject.Dispose()
 } else {
 $inputObject.psbase.Dispose()
 }
 }
}

$xlCellTypeLastCell = 11 
$startRow,$col=2,1

$excel=new-object -com excel.application
$wb=$excel.workbooks.open("c:\temp\Book2.xlsx")

for ($i=1; $i -le $wb.sheets.count; $i++)
 {
 $j=0;
 $sh=$wb.Sheets.Item($i)
 $endRow=$sh.UsedRange.SpecialCells($xlCellTypeLastCell).Row
 $rangeAddress=$sh.Cells.Item($startRow+1,$col).Address() + ":" +$sh.Cells.Item($endRow+1,$col).Address()
 $sh.Range($rangeAddress).Value2 | foreach {
 $contract=$sh.Cells.Item($startRow + $j,$col).Value2
 $filesource = $sh.Cells.Item($startRow + $j,$col+1).Value2
 $filedest = $sh.Cells.Item($startRow + $j,$col+2).Value2
 $dob=$sh.Cells.Item($startRow + $j,$col+3).Value2
 
 New-Object PSObject -Property @{Contract=$contract;Dob=$dob}
 
 $file = New-Object System.IO.FileInfo $filesource
 $fileWithPassword = New-Object System.IO.FileInfo $filedest
 $password = $dob
 PSUsing ( $fileStreamIn = $file.OpenRead() ) { 
 PSUsing ( $fileStreamOut = New-Object System.IO.FileStream($fileWithPassword.FullName,[System.IO.FileMode]::Create,[System.IO.FileAccess]::Write,[System.IO.FileShare]::None) ) { 
 PSUsing ( $reader = New-Object iTextSharp.text.pdf.PdfReader $fileStreamIn ) {
 [iTextSharp.text.pdf.PdfEncryptor]::Encrypt($reader, $fileStreamOut, $true, $password, $password, [iTextSharp.text.pdf.PdfWriter]::ALLOW_PRINTING)
 }
 }
 }
 
 $j++
 }
}
$excel.Workbooks.Close()

 

Step 3. Make sure you have the itextsharp.dll library located in the same location as the PowerShell Script.

In this example it should be located in C:\Temp\. You should have the folder structure that looks similar to the one below.

Step 4. Put the source PDF files as specified in the step 1. Execute the Powershell Script by clicking on the execute button if you are using Windows PowerShell ISE.

Step 5. If everything went well, you should be able to see the execution message as per the picture below.

Step 6. The destination folders specified in excel step 1. should contain all the password protected PDF, as shown below.

Now you can tested these Password Protected PDF files by opening it using the password that you had specified in Step 1.

Please leave me a comments if you like this post. Like it if this help you to save some time in automating some of the boring task.

 

Posted on

Solving “insecure string pickle” in youtube-dl

I am trying to use youtube-dl to download something for my missus, and the error “insecure string pickle” issue is preventing the video being downloaded. After a few hours of searching the internet finally the following blog solved my problem.

The solution is quite simple:

brew install libav ffmpeg rtmpdump

Just to note my system is Hackintosh running el-capitan with homebrew installed.

Posted on

Send message to LED matrix using Bluetooth

The next iteration of the LCDMatrix in the earlier post is to add a Bluetooth module HC-05 to the Arduino. This will allow us to send message to LED matrix using bluetooth.

The bluetooth module will need to be connected to the Arduino via the following:

  • Pin 2 is connected to the RX of HC-05
  • Pin 1 is connected to the TX of HC-05

Once this is done, we need to initialise the Bluetooth module to ensure that we can connect to it. In this demo we are using Android app called Bluetooth terminal which allow us to connect to the HC-05 bluetooth module.

You can see it in action in the following clip.

If you are interested to get the source code and the detailed diagram don’t hesitate to let me know.

Posted on

Windows 10 can’t see network drive

I had a problem where my windows 10 can’t see network drive, all the mapped drive can’t be accessed. I had been pulling my hair over this issue. I got a new HUAWEI router (NBN) but somehow this muck up my Windows 10 mapped drive. I had a NAS drive that was mapped in windows 10 and every now and again the Windows 10 Home will no longer able to access the NAS drive. Strangely all the other computers, I had several Mac in the house, they are all working fine connected via the same router. Here are some instruction on how to fix Windows 10 Network file sharing, it works for me, so I hope it will save you some time.

After countless of hours of tweaking and google searching for why windows 10 can’t see network drive, I found the following Method is the most effective to resolve the issue.

 

Step 1. Go to the settings from clicking “Windows” button on your bottom left corner and select settings as shown in the picture.Windows Options

Step 2. Click on the Network & Internet icon

Windows 10 can't see network drive

Step 3. You will see the similar screen to the one below, scroll down until you find the “Sharing option” and click on it.

Network and Internet

Continue reading Windows 10 can’t see network drive

Posted on

LED Matrix Scrolling text with Arduino using a 74HC595

The extension to the previous letter guessing game, I found a font library from a post in Instructables, and this leading me to create a scrolling text message to the single LED Matrix. This can be combined to make bigger display.

Nostalgic memory remembering what one of my Uni friend’s project back 20 years ago using shift registers and pascal programming, we had come a long way to simplify the circuitry and reduce the complexity of both hardware and software.

Please contact me if you would like the source code or the copy of the fonts library.

 

Posted on

Emergency recovery shell in berryboot

Every now and again you would experiment with things in raspberry pi, in this instance I am playing around with the wall display called Dakboard, so I modify the config.txt file /boot, and because I had berryboot, it freak out and drops to “emergency recovery shell”.

In order to restore the config.txt file do the following in the command line, because berry boot doesn’t have  /boot partition by design we will have to create one:

cd /
mkdir boot
mount /dev/mmcblk0p1 /boot
vi /boot/config.txt

At this point you can edit the config back to the original or restore it from the backup (what? you didn’t make a back up?)

Once you restore it to the original, you will have to unmount the boot partition back using

umount /boot

Then reboot by using the Ctrl + Alt + Del key or power cycle the pi. This should then boot back to berryboot gui for you to perform any changes.

Posted on

TERM support in Screen command

When you get the following error message when trying to start screen

Cannot find terminfo entry for 'xterm-256color'.

you will need to find the which TERM is supported:

ls /usr/share/terminfo/x

this will give you a list of supported TERMs i.e.

xterm
xterm-xfree86

set the environment variable:

export TERM=xterm-xfree86

and then run the screen command

TERMINFO='/usr/share/terminfo/' screen
Posted on

Backup Raspberry Pi remotely

To back up your raspberry pi remotely, you will need to know the ip address, then using the following command

ssh pi@xx.x.x.xx “sudo dd if=/dev/mmcblk0 bs=1M | gzip -” | dd of=~/Desktop/pibackup.gz

Replace the xx.x.x.xx with the ip address of your pi, and the final image will be copied to your desktop.

To check from time to time the progress, press “Ctrl + T”, and you will see something like the following:

load: 0.98 cmd: dd 8967 running 0.17u 1.94s
317344+0 records in
317344+0 records out
162480128 bytes transferred in 167.835383 secs (968092 bytes/sec)

To restore the image back you need to restore to SD card

diskutil unmountDisk /dev/disk#
gzip -dc ~/Desktop/pibackup.gz | sudo dd of=/dev/rdisk# bs=1m conv=noerror,sync

 

Posted on

Fixing corrupted Time Machine backup

If you are a MAC user, the following message pop up at times.

“Time Machine completed a verification of your backups. To improve reliability, Time Machine must create a new backup for you.”

This means that:

  • Time machine will delete all your existing backups, and
  • Create a new initial backup

You will loose all your previous backups. So let’s try to fix this.

Step 1. Preparations

Time Machine will save all backups and every piece of meta information into a *.sparsebundle file on your network storage device. So this is the file we need to fix.

In order to access it, you need to connect to your networks device share that contains the backup with Finder. It will hold one or more *.sparsebundle files in its root, one for each Mac that uses this drive with Time Machine.

After that, open a Terminal window and switch to the root (=admin) user with the following command:

sudo su -

Enter your password if you’re asked to do so.

When you see the error message I talked about above for the first time, Time Machine has already flagged your backups *.sparsebundle file as bad. As a first step, we need to undo this:

chflags -R nouchg /Volumes/<name of network share>/<name of backup>.sparsebundle

Make sure to replace <name of network share> and <name of backup> accordingly and give it a while to finish.

Step 2. Mounting your backup

Next, we need to mount your backups *.sparsebundle file to enable your system to run a few checks on it:

hdiutil attach -nomount -noverify -noautofsck /Volumes/<name of network share>/<name of backup>.sparsebundle

his command will return something like this:

/dev/diskX Apple_partition_scheme
/dev/diskXs1 Apple_partition_map
/dev/diskXs2 Apple_HFS

What is important to us is the line containing Apple_HFS or Apple_HFSX(this usually is the last line) which is the device ID followed by the device type. The X will be replaced with a number which will be anywhere between 2 and 6 or even higher. So, you want to take note of the device ID that looks something like /dev/disk2s2 or /dev/disk3s2.

Step 3. Repairing the *.sparsebundle

Now we can finally let OS X do its magic of trying to repair the file. Run the following command after replacing the dummy device ID with your own:

fsck_hfs -drfy /dev/diskXs2

With this kicked of, you may want to go have a coffee or two as this may take anywhere from 15 minutes to multiple hours, depending on the size of your backup and the speed of your network connection.

After the command finished, you’ll either see »The Volume was repaired successfully« or »The Volume could not be repaired«.

Either way, you need to unmount the backup before taking any further steps:

hdiutil detach /dev/diskXs2

Step 4. Finishing touches

If the repair command from the previous step failed, there’s not much you can do about it and your backups are lost for real probably. You should just let Time Machine create a new backup for you.

If it succeeded though, there is a last step we need to take in order to convince Time Machine to keep using the existing backup.

Use the Finder to navigate to the backups *.sparsebundle file and with a right click, choose »Show package contents« to get a look inside. There you’ll find a file called com.apple.TimeMachine.MachineID.plist. Since Time Machine uses this file to mark bad backups, too, we need to modify it slightly.

Open the file with a text editor of your choice and find the line saying

<key>RecoveryBackupDeclinedDate</key>
<date>{any-date-string}</date>

and remove them completely. Next find the lines

<key>VerificationState</key>
<integer>2</integer>

and change them to

<key>VerificationState</key>
<integer>0</integer>

Done. You probably want to eject/unmount the network share from your finder and tell Time Machine to do another backup now.