Categories
armbian Raspberry Pi Software

The fastest way to clone an SD card on macOS

If you have a raspberry pi or other single board computer and would like to make a backup of it, or even clone it to another SD card, then it can take a long time. Your first thought is to probably use the built in “Disk Utility”. Unfortunately this has issues reading linux partitions (well in my experience) and is often slow. This simple command line trick will have you copying or cloning a full disk image of your SD card in record time!

WARNING: Be very careful when running any command with sudo dd in it. If you type any of the parameters incorrectly you may accidently erase or overwrite important data.

Requirements:

  • macOS running a recent version (this guide was tested on macOS Catalina).
  • basic knowledge of command line operations.
  • Make sure you’ve got homebrew installed. You can visit this link to find out how to download and install homebrew if you haven’t already got it.
  • After you’ve installed homebrew, you’ll need to install a package called core-utils. Do so by running brew install coreutils in your terminal. It should take a few minutes to run.

Identify your sd card:

You’ll need to find out which disk your SD card represents. You can run diskutil list and should see an output like below:

/dev/disk1 (synthesized):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      APFS Container Scheme -                      +500.0 GB   disk1
                                 Physical Store disk0s2
   1:                APFS Volume Macintosh HD — Data     396.0 GB   disk1s1
   2:                APFS Volume Preboot                 81.9 MB    disk1s2
   3:                APFS Volume Recovery                528.5 MB   disk1s3
   4:                APFS Volume VM                      4.3 GB     disk1s4
   5:                APFS Volume Macintosh HD            11.0 GB    disk1s5

/dev/disk4 (external, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:     FDisk_partition_scheme                        *31.9 GB    disk4
   1:             Windows_FAT_32 boot                    268.4 MB   disk4s1
   2:                      Linux                         31.6 GB    disk4s2

From that output we can see that our SD card must be /dev/disk4 as our card is 32GB in size and has a fat32 and linux partition (standard for most raspberry pi images). You should add an r in front of disk4 so it looks like this /dev/rdisk4. The r means when we’re copying, it will use the “raw” disk. For an operation like this, it is much more efficient.

Copy the SD card as a disk image (dmg)

Now you should run the following command, replacing 4 with whatever number you identified as your sd card:

sudo gdd if=/dev/rdisk4 of=sd_backup.dmg status=progress bs=16M

Tip: you can experiment with different numbers for the block size by replacing bs=16M with larger or smaller numbers to see if it makes a difference to the speed. I’ve found 16M the best for my hardware.

You should see some progress feedback telling you the transfer speed. If you’d like to experiment with different block sizes, just type ctrl + c to cancel the command, then you can run it again.

Once the command has finished running, you’ll end up with a file in your home directory called sd_backup.dmg. If you’d like to backup multiple SD cards (or keep multiple backups!) simply replace sd_backup.dmg with a different file name. This will contain a complete disk image of your SD card. If you’d like to restore it, or clone it to another SD card, read on.

Copy the disk image (dmg) to your SD card

You’ll first need to unmount your SD card. Do not click the eject button in finder, but run this command, replacing 4 with whatever number you identified as your sd card sudo diskutil unmountDisk /dev/disk4.

Then to copy the image, run the following command:

sudo gdd of=/dev/rdisk4 if=sd_backup.dmg status=progress bs=16M

Tip: you can experiment with different numbers for the block size by replacing bs=16M with larger or smaller numbers to see if it makes a difference to the speed. I’ve found 16M the best for my hardware.

You should see some progress feedback telling you the transfer speed. If you’d like to experiment with different block sizes, just type ctrl + c to cancel the command, then you can run it again.

Once the command has finished running, your SD card should be an exact copy of the disk image you specified.

Categories
Uncategorised

Untangling the mess that is USB Type C and video outputs

USB Type C is meant to be the answer to all of our problems and be this magic, universal port right? Well in terms of charging things it’s pretty good. We’ve got the USB Type C PD (power delivery) spec that means my Apple charger will work on my MacBook Pro, my Samsung S9+, Samsung Gear Icon X, Nintendo switch, and most things with a Type C port on it. In general I’ve had a good experience with USB C being a truly universal solution for charging devices. However, getting a video signal out of a USB Type C port is another story.

I recently purchased a 2018 MacBook Pro (MBP) 15″ and I’ve been trying to work out how to setup my desk. I started investigating different docking stations, USB Type C adapters and cables, etc. I quickly learned that the world of USB Type C/Thunderbolt 3 docks and video adapters is complex and full of confusion. What’s the difference between Thunderbolt 3, USB3/3.1, “Thunderbolt 3 compatible” devices? Why do some only support mirroring on macOS but extended displays on windows? What is USB Type C alternate mode “alt mode”, etc.

I found myself asking so many questions. As a result I quickly fell into a rabbit hole of trying to understand all the different options that are available on the market. I’m going to attempt to summarise everything I’ve learnt, so that you don’t have to go through the same pain.

Thunderbolt 3 vs USB 3/3.1 vs Thunderbolt 3 “Compatible”

Source: thewirecutter.com

I quickly discovered that there are two main types of docks, proper Thunderbolt 3 ones, USB 3/3.1 ones and Thunderbolt 3 compatible adapters/docking stations. The Thunderbolt 3 options seemed far more expensive than their USB 3.1 and “compatible” alternatives. So what gives? The main difference is the way they communicate with your device, whether that’s a laptop like my MBP or a phone like my S9+.

Thunderbolt 3

Thunderbolt 3 is a standard that’s been developed by Intel to allow you to connect high bandwidth peripherals such as displays and storage devices. However, because of the high amounts of available bandwidth, it’s also used in many docks or “port replicators”. In fact, with the 40Gbps of bandwidth it has, you can drive two 4k displays at 60hz and still have room leftover for other peripherals.

USB 3/3.1

USB 3/3.1 on other other hand, is just the latest revision of the USB (Universal Serial Bus) protocol that has been around for a long time. Thunderbolt 3 “compatible” devices seem to be just a marketing ploy to get people to think they support Thunderbolt 3. Really, they just use the normal USB protocol that Thunderbolt 3 automatically falls back to. USB 3.1 only has 10Gbps of bandwidth compared to Thunderbolt 3’s 40Gbps which means it doesn’t event have enough for a single 4k 60hz display signal. However, USB 3.1 over Type C has a nice trick up its sleeve which I’ll explain later.

Thunderbolt 3 ports are often accompanied by a small lightning icon to signify the fact. However, my MBP and some other devices don’t always do this. Thunderbolt 3 ports will normally fallback to USB3/3.1 if that’s the only protocol the device (such as a dock or adapter) supports.

USB Type C Display Output Methods

There are many different ways that USB Type C devices (laptops and docks etc.) output and interpret display signals. I’ll explain the common ones below.

USB 3/3.1 Over Type C With DisplayLink Chip

USB Type 3/3.1 over Type C docks normally rely on a chip manufactured by a company called DisplayLink (or something similar). These chips use software to encode, compress and send a display signal over the lower bandwidth USB 3/3.1 protocol. However, these chips are software driven so they don’t perform well in demanding applications such as gaming or video editing. They might even struggle with playing some videos. Anything besides general office use is asking for trouble.

DisplayPort/HDMI Over Type C With Alternate Mode

Most cheap USB Type C dongles/adapters rely on on a neat trick called USB C alternative mode. Basically, a dongle/adapter/dock can ask a compatible device like a laptop or smartphone to output a non USB signal at the same time over some unused wires. Some examples of these non USB signals include HDMI and DisplayPort. Yep, the standard protocol that a HDMI or DisplayPort cable carries can also be carried by the humble USB Type C port.

The way this works is the dongle/dock will ask the output device if it’s able to support HDMI/DisplayPort etc. via alternative mode. If it can, the device starts to output a native HDMI/DisplayPort signal straight from the GPU – no software to get in the way like a DisplayLink chip. These cheap adapters are completely passive, basically just joining the correct wires from the Type C connector to the right places ono the HDMI/DisplayPort connector. They don’t manipulate or process the signal.

DisplayPort MST

Part of the DisplayPort standard includes MST – Multi Stream Transport. This handy feature allows you to daisy chain displays, use multiple outputs to drive a high res/refresh rate display, or carry multiple signals to different monitors as a “splitter” from a hub. A lot of docking stations and adapters that support more than one display out rely on MST, which is fine for the most part. However, Apple does not properly support MST in macOS. The only part of MST that’s supported is driving one larger screen from two DisplayPort outputs.

Unfortunately this means a lot of docking stations that work flawlessly in Windows or Linux show a “mirrored” image on both outputs instead of separate images for each. There’s nothing that can be done as a workaround as the problem is macOS fundamentally not supporting it. What this practically means is that some docking stations with multiple display outputs will only show up as a single one in macOS and output the same image on each one.

A Mixture of the Above

You’d think that adapters and dongles would probably pick one of the above methods and stick with it. However, from what I’ve seen most docks that advertise 2 or more outputs rely on some crazy combination of the methods above. Some will have one DisplayPort driven via USB Type C alt mode, and another two with a DisplayLink chip, or two with DisplayPort and MST via USB C Alt mode. This crazy mishmash of implementations and lack of information on product data sheets means it’s difficult for even a tech savvy consumer to work out if something is compatible with their device.

For example, I found this great looking Dell dock for the reasonable price of $200. I was about to buy it when I saw a review saying it only supports one display output on macOS. After looking into this I figured out it was due to the lack of MST support in macOS. I then found a more expensive one for $300 from Lenovo, and thought sweet, this is it. Apparently it uses DisplayPort via alt mode for one connector and a DisplayLink chip over USB for the other two. This means you get one output with “good” performance and the other two are severely restricted in comparison with CPU rendering.

Source: slashgear.com

Passive Dongles/Adapters and Cables

I didn’t spend too much time researching this, but there are still a few problems here. Whilst not ideal, someone should be able to plug a USB C to HDMI into a HDMI to DisplayPort adapter then use it with their screen, right? Well not quite, because of the way USB C video outputs are so varied and inconsistent, it’s unlikely you’ll be able to find the right combination of adapters that will work. It ends up just being easier to buy a new USB Type C adapter for every single type of output you need rather then chaining old ones onto a single Type C to HDMI adapter.

Source: samsung.com

You’d also think that all USB Type C cables are the same right? Well, only certain cables support Thunderbolt 3, and only some cables are rated for higher amounts of power. How do you know? It’s impossible to tell. USB Type C enabled devices are developing into an ecosystem where you have to plug something in and cross your fingers that it all works. This isn’t the way it was meant to be.

Conclusion

Most manufacturers don’t tell you what ungodly mess they’ve got going on inside their products. Because of this complete mishmash, some display outputs will be severely limited in their performance, while the one next to it might be fine. Some docks and adapters may work fine with windows machines but not with macOS. On top of that, sometimes you can’t tell if a USB Type C port, cable or device, is USB3/3.1, Thunderbolt 3, DisplayPort/HDMI over alt mode compatible, etc. It used to be if a cable fit, the device and cable were compatible, but that’s no longer the case.

Consumers shouldn’t need to spend hours researching how an adapter or dock is implemented to work out if it’s going to be compatible with their use case and performance needs. This inconsistency and lack of information from manufactures is a massive problem and is dragging down an otherwise great standard that should be universal and consistent.

P.s. if I’ve left anything out or made any mistakes please let me know in the comments. My head is still spinning from the huge amount of information I’ve processed over the last day while trying to write this.

Categories
Home Automation Software

Control your Avocent PDU from python

I got my hands on an Avocent PDU (Model: PM3012V). This thing is pretty cool, it has 20 outlets on it and each one can be remotely switched on or off via it’s control interface. Just plug it into a spare network port and you’d think you’ve got a 20 channel home automation relay bank, well not quite. There is not proper API for this thing meaning you can’t setup your voice assistant (google home etc) or automation software to easily control it. That’s where I come in!

I spent an evening with burp suite, firefox and the awful Avocent web interface. I went through every single network request to and from the PDU the whole way from logging in to commanding an outlet to switch on or off. I replicated these requests in python and culled all the unnecessary ones. End result is the  avocentpdu  module. (super original name right?)

You can check out more information and the documentation on my GitHub repository right here. I’ll post an update once I’ve finished writing my custom home assistant component.

Categories
Home Automation MQTT Uncategorised

MQTT Status Codes (Connack Return Codes)

If you’ve ever played with MQTT, then you’ve probably had issues connecting to your broker. Whether it’s one you’ve setup or you’re using a 3rd party provider like AWS, they should all follow the MQTT protocol. This is mainly for my reference because I can never find it, but below is a list of the standard connack codes that could be returned when you try to connect.

Note these have been directly copied from the official specification. You can see the original by clicking here.

Table 3.1 – Connect Return code values
Value Return Code Response Description
0 0x00 Connection Accepted Connection accepted
1 0x01 Connection Refused, unacceptable protocol version The Server does not support the level of the MQTT protocol requested by the Client
2 0x02 Connection Refused, identifier rejected The Client identifier is correct UTF-8 but not allowed by the Server
3 0x03 Connection Refused, Server unavailable The Network Connection has been made but the MQTT service is unavailable
4 0x04 Connection Refused, bad user name or password The data in the user name or password is malformed
5 0x05 Connection Refused, not authorized The Client is not authorized to connect
6-255 Reserved for future use
Categories
Uncategorised

Fix for slow tab auto completion on ubuntu (bash)

For days I’ve struggled with this new linux install on a virtual machine on my local network.  The SSH has been super unreliable and everytime I typed tab for an auto completion the whole thing seemed to lock up for ~30 seconds.  Turns out the autocompletion problem was the simplest fix ever!  After scouring the internet for ages I found this command.

sudo updatedb

It’s simple, all it does is update the auto completion database. (according to the forum I found it on) What was probably happening is the database got really big and was taking ages to scan through.  It beats me why a fresh ubuntu install had this problem, but at least it’s solved, for now.

Categories
Uncategorised

3DR Solo DSLR & 1/4″ Tripod Mount Adaptor

If you’ve seen my 3DR Solo xtra large leg extenders post, you might be wondering what I used to attach my Sony a5100 to my Solo. Well I used the “pretty” face plate thing that comes with solo.  (for use without a gimbal)  It has a hard mounted GoPro adaptor on it and for now this will suffice. It’s basically a little right angle GoPro to 1/4″ tripod mount adaptor with an offset 1/4″ mount to roughly centre the a5100 and ensure it’s as small as possible.

You can download my STL file for the print via the link at the end of this post.  You can see this mount in action in the photo below.  I highly recommending that you take it slow and easy.  The “rubber stoppers” to help combat gello/vibrations are designed to take a ~85g GoPro, not a ~400g compact DSLR.  I highly recommend tethering the Sony to the Solo just in case the mount fails.

I recommend using leg extenders like these for some extra clearance!

It’s important to print the mount so that when you look down on the print bed from above you see an L shape.  This ensures the layers aren’t parallel to the camera body.  It’s extremely weak when printed this way.

Download the 3DR Solo 1/4″ Tripod Mount Adaptor STL file

Categories
Drones

3DR Solo Leg Extenders (XLarge!)

So I’ve just bought a Sony a5100 (awesome little camera) and am in the process of fitting it to my 3DR Solo drone. At the moment I have it attached via an adaptor to the hard mounted GoPro mount. It points straight down. However, with the stock legs the lens is dangerously close to the ground; this is not ideal.

Dangerously little clearance with stock leg extenders!

There are a few leg extenders floating around but most were either to small or unnecessarily complicated to print. I decided to just design my own. I call it, Simple Solo Leg Extender – super original name right? They don’t need any support and will print on basically any printer. Did I mention they’re also extra large? They add roughly 5cm of additional ground clearance to Solo. My a5100 is much happier now it’s less likely to get a scratched up lens.

And Fin !

Download the STL file here

 

Categories
Drones

3DR Solo GPS Spacer (“Cardboard Mod”)

I finally got around to 3D printing a GPS spacer for my 3DR solo.  This is also known as the “cardboard mod” around the forums/Facebook groups.  What this spacer does is puts some space between the copper foil and the GPS PCB.  3DR made a mistake when designing the GPS module and spaced it to close to the PCB, meaning it’s not as effective.

Note:  DO NOT use a “Revision A” GPS.  These have a known design flaw and are not reliable.  You can check what you have by the writing on the GPS PCB.

The first step is to head over to thingiverse and download the model designed by @Tunames.  The thingiverse page is here.  Just print it out and begin taking apart your solo.  I’ll briefly cover the installation procedure below.  It should go without saying, every time you take Solo apart no battery should be connected! (unless you know what you’re doing)  It’s also not a bad idea to take the GoPro off too.

Start by undoing all 4 screws underneath where the battery normally sits.  Then get a small flat-head screwdriver (ideally a rubber spudger) and gently pry off the GPS cover.  Underneath the GPS cover you should find 3 screws, undo all 3 and slowly lift up the battery tray.  Be careful of the GPS cable, unclip it as soon as you can.

Now flip the battery tray over and you should see a black tape looking cover.  Peel it off, being careful not to destroy it.  It’s copper shielding that’s really important for keeping out interference.  Undo all three screws holding the GPS in and sit them aside.  Place the 3D printed spacer with the flat side out.

Tip: If the tape loses it’s stickiness use a generous amount of glue stick to hold it in place.  You do not want the tape coming loose!

Now, put everything back together in the reverse order.  When you turn Solo back on you may not notice an immediate difference.  You might get a slightly faster lock, but in general the GPS should be more reliable.

Categories
Drones Electronics

DIY 3DR Solo Charger (also car charger)

So I bought a 3DR Solo “Smart Drone” and some spare batteries.  To charge them all faster I thought I’d make my own charger.  I’ve also made a little adaptor so I can charge them in my car.  (run another cable off the car battery or turn the current down when using a car)

First of all you must use a voltage and current limited power supply.  If you don’t you’ll damage the battery or worse, start a fire.  These may be “smart batteries” but they will happily accept more voltage and current than what’s good for them.

I’ve made lots of assumptions in this build log/tutorial.  if you can’t do some of the stuff it assumes then this project probably isn’t for you.

Before starting you should be comfortable with:

  • Using a multimeter for measuring voltage and current
  • Soldering
  • Reading basic circuit diagrams
  • Having a power supply of 12v at ~7A per charger

Parts:

Boost Converter ~$7 (link)

I used one of these boost converters off eBay.  They’re cheap and can be used for many things as you can set the exact output voltage and apply a current limit.  I ordered a couple to keep on hand.

Input Connector – XT60 (you can use anything)

I love the yellow XT60 connectors, they come on most LiPo batteries I buy and can handle 60 amps.  They are easy to solder and plug/unplug.  Anything I have that uses/supplies around 12v has an XT60 on it making lots of things interchangeable.  (like my glue gun that can be used on batteries, desk power supply and car) You can get these online or from HobbyKing.

Output Connectors – Copper Clad Board (link for Australia)

The output connectors are a bit tricky.  They don’t use a readily available connector.  I just found some copper clad board that fit snugly and cut out the correct size.  My calipers broke so I can’t measure it properly but I’d guess it’s the standard 1.5mm thick stuff.  I’m currently working on a 3D printed connector, at the moment you have to manually plug the +/- connectors in.  If you get the polarity wrong, you will blow up your boost converter and probably the battery controller.

Hint: shorten one side so it’s harder to short.

Optional Case/Fan

I also bought a plastic case for about $3 and used a random 12v fan I had lying around.  This makes for a clean setup that stays cool.

How to assemble

1) The Case/Fan

If you’re putting the boost converter inside a case like I did you need some form of active cooling.  I drilled a bunch of holes in the tops roughly the shape of where air blows from the fan.  I drilled about 1/3 of the holes on the side to force the air into the case then out the side.  I then use a rubber band to hold the fan in place.  I only put it over half so more of the blades were over the holes and so I can attach another charger.

2) Wiring

The wiring is pretty straight forward.  I’ve attached a diagram to help out.  Make sure you use cables capable of carrying 16.8v @ 5A on the output and 12v @ 7A on the input.

3) Setting Voltage/Current

If you got the same boost converter I did (or one that looks similar) you can probably use the diagram below for reference.  Turn the current limit all the way down to the minimum.  (you should hear clicking when the trim pot hits the min) Next, get your multimeter and check the voltage.  Keep twisting the voltage trim pot until it gets to exactly 16.8v.  If you go any higher you risk damaging the battery.

Now set up your multimeter to measure the amps going into the battery from the charger.  Hook it all up, start charging the battery and measure the current.  Adjust the current up to a maximum of 5 amps.  Any more than 5 amps will damage the battery and be a fire/explosion risk as it can’t safely charge that fast.  I recommend doing this on a half charged battery for optimal results.

 

4) Fin.

Congratulations, you just made a $10-$15 solo battery charger.  Also at 5A, this charges approximately 50% faster than the original charger.

Categories
armbian

Connect to armbian (orange pi) without the IP

There are many reasons why you may not have a static IP configured for your orange pi/armbian installation.  Maybe it needs to be portable to different networks or your network doesn’t allow static IPs.  Whatever the reason it is a pain to find the IP, often you have to use a scanning tool like nmap.

We’re going to install a popular Linux implementation of zerconf.  These steps should work on any flavour of Ubuntu but I specifically used armbian on an orange pi zero.  It’s simple to install, just follow these steps:

Change your hostname from default

While not mandatory, it’s generally a good idea.  If another device running raspbian for example pops onto the network you’ll have two devices with the same hostname!  Edit /etc/hostname using the vim text editor (if you don’t know vim install it then type “vimtutor”):

sudo vim /etc/hostname

In that file you’ll see the current hostname, simply remove it and replace it with the new one.  Now edit the hosts file.  Make sure you replace every instance of the old hostname with the new one to make sure it resolves properly

sudo vim /etc/hosts

Now reboot your system to apply all of the changes.

sudo reboot

Install the Avahi Daemon

Something to note is most full versions of Ubuntu come with avahi-daemon installed out of the box, so give hostname.local a try first.  Simply type the following command and it’ll install avahi-daemon.  You shouldn’t need to restart but if it doesn’t work right away you know what to do.

sudo apt-get install avahi-daemon

In the case of my orange pi, after setting the hostname to “homeassistant” and installing the avahi daemon, it’s now accessible via homeassistant.local – much more convenient than an IP.