fogbound.net




Sun, 14 Jul 2019

Sensor Problems

— SjG @ 9:28 am

I’m working on a circuit based on a 34685-MP chip bought at Marlin P Jones. The chip is supposed to be sensitive up to 7m away, but I’m not getting very reliable detection. My first thought was that, being next to a bunch of high-frequency stuff (a Teensy 3.2 with the audio shield), maybe it was interference.

The not-yet-functioning Annoy-o-Tron

I tried taking its output to the base of a transitor and lighting a LED to see if that was better. I got similar results.

I’m not sure what’s going on yet. If I do figure it out, I’ll post here.

Update: the Interwebs deliver! Following the recommendation I found in the comments on this article, I put the 34685-MP on a cable, and moved it away from the breadboard. Voilà! It now works as I had hoped!


Tue, 7 May 2019

The Maccabeam™

— SjG @ 5:43 pm

Back in 2017, I laser-cut a menorah out of poplar. When the family showed up for Hannukah, I mentioned my “laser menorah.” My nephew’s eyes lit up with excitement, but I could see his disappointment upon presentation of the actual product.

Subsequently, I’ve been building something closer to what he probably envisioned in the first place.

I’m slow at building things, and have lots of other commitments taking up time. I spend maybe an hour or two a week on the project, and tend to forget a lot of important details between sessions. There is a whole lot of learning and re-learning. However, I thought that documenting the various processes here would be good for me (my external memory), and may be of interest to others.

The first step of any project, of course, is to give it a good name and maybe a logo. Since it’s a laser menorah, I’m calling it the Maccabeam™, and my initial version of the logo looks like this:

The Official Maccabeam™ logo

So there are a lot of things to talk about here. I’ll post a lot of circuit design ideas, physical design ideas, and details on the software that drives it. I’ll also probably post some ambivalent thoughts on the whole holiday of Hannukah1. But for now, I’ll start with the list the requirements I’ve been using for the project:

  1. Instead of candles, I’ll be using lasers!
  2. The lasers will probably be illuminating vials of olive oil rather than shining on the ceiling.
  3. There will be more lights, too. Color LEDs! NeoPixels!
  4. The whole thing will be driven by an embedded controller I can program. Since I like the Teensy and Paul & Robin seem like the kind of people I want to support, I’ll go with a Teensy LC.
  5. Since I have a microcontroller, it should take advantage of the smarts, and not just rely on an on/off switch.
  6. Hey, if it’s gonna be smart, it should use a GPS receiver to figure out the location and date, and automatically run itself on Hannukah.
  7. It will need some kind of display so you can tell what it’s doing, what time it is, how long until Hannukah, etc.
  8. It’ll be cool if it could play some music too.
  9. OK, maybe I don’t want an on/off switch, but I do need a switch to trigger a simulation mode. That way, I can to show it off to people at any time of year.
  10. [update 13 May 2019] Oh, I forgot an important one. The Maccabeam™ wants to be stand-alone. It doesn’t want any dependencies on the Interet, wireless networks, or the like. It should only depend on a source of electricity and a constellation of 20-some-odd highly sophisticated satellites and their groumd support network.

So with that set of requirements, I got started. I hope to write something here about each of those requirements as I complete the build.

1 I mean, it’s celebrating the victory of a family of intolerant religious fanatics over both their foreign imperial enemies and their more moderate coreligionists. Their victory established the shaky Hasmonean dynasty whose infighting and collapse resulted in Herod’s rise to power in the region, etc.


Thu, 28 Mar 2019

Using openssl for AES-CBC-PKCS5Padding rather than mcrypt in PHP

— SjG @ 2:34 pm

Yeah, that’s quite an acronym soup.

Background: the mcrypt library for PHP has been deprecated for a long time now. However, in PHP we still have to process lots encrypted strings coming from a format like MCRYPT_RIJNDAEL_128 or stuff coming from Java (Android, I’m looking at you!), that was encrypted with a AES with Cipher Blocker Chaining and PKCS5Padding. These cipher algorithms are not explicitly included in openssl as such, although you can find stray references all over the web pointing you in the general direction.

I frequently have to integrate with third-party sites or services that are written in Java, and which provide sample PHP code for implementing my end. Because the ciphers in mcrypt are easier to identify, this provided source usually uses the deprecated library rather than openssl.

So, to save some time, here’s the equivalent openssl encryption/decryption commands:

openssl_encrypt($plaintext,'aes-128-cbc',$key,0,$iv)
openssl_decrypt($encrypted, 'aes-128-cbc',$key, 0, $iv)

For a more verbose proof-of-concept, a longer test program is included below. But before you look at that, consider the following warnings:

DO NOT USE A FIXED INITIALIZATION VECTOR!
DO NOT USE STUPID PASSWORDS!
DO NOT USE THIS CODE IN PRODUCTION!

<?php
// sooper-secret message
$src = array('don' => 'sleeper agent', 'mike' => 'coverup');
// ultra-seekrit key
$key = '1234567890123456';
// hard-coded initialization vector to prove we really know our stuff
$iv = '6543210987654321';

$original = json_encode($src);

// encrypt with mcrypt
$size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$pad = $size - (strlen($original) % $size);
$plain = $original . str_repeat(chr($pad), $pad);
$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
mcrypt_generic_init($module, $key, $iv);
$data = mcrypt_generic($module, $plain);
mcrypt_generic_deinit($module);
mcrypt_module_close($module);
$mcrypted = base64_encode($data);

// encrypt with openssl
$ocrypted = openssl_encrypt($original, 'aes-128-cbc', $key, 0, $iv);

if (strcmp($mcrypted, $ocrypted))
{
echo "Uh-oh. Encrypted strings don't match up.\n";
echo "mcrypt encrypted string:\n$mcrypted\n";
echo "openssl encrypted string:\n$ocrypted\n";
}
else
{
// decrypt using mcrypt
$m_from_o_decrypt_padded = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, base64_decode($ocrypted), MCRYPT_MODE_CBC, $iv);
$dec_s = strlen($m_from_o_decrypt_padded);
$padding = ord($m_from_o_decrypt_padded[$dec_s - 1]);
$m_from_o_decrypt = substr($m_from_o_decrypt_padded, 0, -$padding);

// decrypt using openssl
$o_from_m_decrypt = openssl_decrypt($mcrypted, 'aes-128-cbc', $key, 0, $iv);

if (strcmp($o_from_m_decrypt, $m_from_o_decrypt))
{
echo "Uh-oh. Decrypted JSON strings don't match up.\n";
echo "openssl decrypting mcrypt encrypted string:\n$o_from_m_decrypt\n";
echo "mcrypt decrypting openssl encrypted string:\n$m_from_o_decrypt\n";
}
else
{
echo "Encrypted strings were the same, and each library decrypted the other's encrypted data\n.";
}
}

Wed, 13 Mar 2019

Replacing the battery in a mid-2012 Retina MBP

— SjG @ 8:17 pm

I bought my MBP back in September of 2012, and it’s been the best machine I’ve ever owned. Named Apotheosis, it’s quiet, powerful, good battery life, and all that. It’s been through four or five iterations of MacOS, and still runs fast. It’s got old-school USB 3 ports, an SD card reader, and the mag-safe connector. In short, the only thing I could hope to improve it would be more storage.

Of late, however, it’s been exhibiting power issues. It will log itself out, or spontaneously go to sleep and not wake up without external power. I reset the System Management Controller (SMC) by the arcane ritual of holding down Shift, Control, and Option on the left side of the keyboard, then holding down the power button for 10 seconds. No luck.

Then, there’s some minor issues with the screen; small areas that look almost like fungus in the display. From what I’ve read, this has affected a lot of older Retina displays, but in my case it’s more an annoyance than a serious problem. Still, given these two issues, I thought perhaps it’s time to upgrade. After all, the machine’s over six years old.

Looking at the new MacBooks and MacBook Pros, though, I can’t find anything that would be satisfactory. Certainly nothing at a price-point that I feel like paying. I’m not a big fan of the new keyboards, and if you want to put a lot of storage (i.e., 2TB) in a machine, Apple really makes you pay. Resolved, then, try to get more years out of Apotheosis. Apple no longer services this model, and the indy/Authorized dealers wanted $500 to replace the battery. That seemed high to me.

Other World Computing has a replacement battery kit for $85. I ordered it, and it arrived overnight! In big red letters, they warn that “Professional Installation Highly Recommended” but people like me don’t pay any attention to such things.

Back cover removed

The kit comes complete with Torx screwdrivers specifically for the MBP, including the (in)famous “pentalobe” driver.

They also supply a step-by-step video for the process. This is a really outstanding instructional video. It shows everything in perfect detail. Things which sound simple in words (e.g., “unclip the connector”) are often not so clear when staring at the physical object. But watching each step makes it very simple.

That being said, they estimated two hours to do the process, and it took me more like three. Part of that was my obsessive disassembly process which I’ve perfected over the years. It involves lots of post-it notes, with little drawings and sticky tape that I use to make sure I can reassemble things correctly. In this case, the video would have been sufficient, but old habits die hard. And, frankly, this is a good habit.

Even thought OWC makes this a straightforward process, Apple certainly didn’t intend it to be. You have to remove the speakers to really get at the battery. To get the speakers out, you basically have to remove all the guts:

Ready for battery removal
All The Innards… haruspices take note

The kit includes a gnarly solvent that helps dissove the adhesive holding the battery in place. This is the worst part of the process, although they provide gloves and eye protection to make it a safer process.

Once the machine was back together, I went through a full charge/discharge cycle, and it’s seemed quite stable. There are some weird minor discrepancies. For example, while writing this post and doing some other odd chores, I’ve been unplugged. The menu bar battery gauge tells me I’m at 81%, while CoconutBattery tells me I’m at 77.6%.

With any luck, this repair will help me keep Apotheosis up and running for a few more years!


Wed, 2 Jan 2019

Using Makefiles for Teensy Development on Mac OS

— SjG @ 9:24 am

Even if you don’t want to use the Arduino IDE for building your application, or want to do fancy things that only the evil, convoluted syntax of Makefiles can provide, you can do Teensy development on the Mac from the command line. While there are a lot of guides out there on how to do it, I hadn’t seen a complete step-by-step set of instructions.

So with no further ado, here are the steps for building Teensy applications on Mac OS using the Makefiles and the mighty command line.

  1. Download the Arduino IDE from https://www.arduino.cc/en/main/software and install it.
  2. Download Teensyduino from https://www.pjrc.com/teensy/td_download.html and install it.
  3. Download a “basic project template” for Teensy. The original is at https://github.com/apmorton/teensy-template but I’ve been using the fork at https://github.com/a-j-f/teensy-template which has been updated more recently and uses the Teensy CLI Loader for uploading to the device.
  4. Update the Makefile to set your Teensy model
  5. Type make to build, or make upload to install on your Teensy

If you’re wanting to monitor the serial console of your Teensy over USB, install a terminal program like CoolTerm. Alternatively, if you use Homebrew or MacPorts you can use a console-based terminal program like minicom. Once the USB connector is plugged in, your Teensy will show up as something like /dev/cu.usbmodem45504901. The communications settings will be 115200 baud, 8 bit, no parity, 1 stop bit.