Linux Fu: Shell Script File Embedding

You need to package up a bunch of files, send them somewhere, and do something with them at the destination. It isn’t an uncommon scenario. The obvious answer is to create an archive — a zip or tar file, maybe — and include a shell script that you have to tell the user to run after unpacking.

That may be obvious, but it assumes a lot on the part of the remote user. They need to know how to unpack the file and they also need to know to run your magic script of commands after the unpack. However, you can easily create a shell script that contains a file — even an archive of many files — and then retrieve the file and act on it at run time. This is much simpler from the remote user’s point of view. You get one file, you execute it, and you are done.

In theory, this isn’t that hard to do, but there are a lot of details. Shell scripts are not compiled — at least, not typically — so the shell only reads what it needs to do the work. That means if your script is careful to exit, you can add as much “garbage” to the end of it as you like. The shell will never look at it, so it’s possible to store the payload there.

Continue reading “Linux Fu: Shell Script File Embedding”

Code Your Own Twitch Chat Controls For Robots — Or Just About Anything Else!

Twitch Plays Pokemon burst onto the then nascent livestreaming scene back in 2014, letting Twitch viewers take command of a Game Boy emulator running Pokemon Red via simple chat commands. Since then, the same concept has been applied to everything under the sun. Other video games, installing Linux, and even trading on the New York Stock Exchange have all been gameified through Twitch chat.

TwitchPlaysPokemon started a craze in crowdsourced control of video games, robots, and just about everything else.

You, thirsty reader, are wondering how you can get a slice of this delicious action. Fear not, for with a bit of ramshackle code, you can let Twitch chat take over pretty much anything in, on, or around your computer.

It’s Just IRC

The great thing about Twitch chat is that it runs on vanilla IRC (Internet Relay Chat). The protocol has been around forever, and libraries exist to make interfacing easy. Just like the original streamer behind Twitch Plays Pokemon, we’re going to use Python because it’s great for fun little experiments like these. With that said, any language will do fine — just apply the same techniques in the relevant syntax.

SimpleTwitchCommander, as I’ve named it on Github, assumes some familiarity with basic Python programming. The code will allow you to take commands from chat in two ways. Commands from chat can be tabulated, and only the one with the most votes executed, or every single command can be acted on directly. Actually getting this code to control your robot, video game, or pet viper is up to you. What we’re doing here is interfacing with Twitch chat and pulling out commands so you can make it do whatever you like. With that said, for this example, we’ve set up the code to parse commands for a simple wheeled robot. Let’s dive in.

Continue reading “Code Your Own Twitch Chat Controls For Robots — Or Just About Anything Else!”

Practical Sensors: The Many Ways We Measure Heat Electronically

Measuring temperature turns out to be a fundamental function for a huge number of devices. You furnace’s programmable thermostat and digital clocks are obvious examples. If you just needed to know if a certain temperature is exceeded, you could use a bimetalic coil and a microswitch (or a mercury switch as was the method with old thermostats). But these days we want precision over a range of readings, so there are thermocouples that generate a small voltage, RTDs that change resistance with temperature, thermistors that also change resistance with temperature, infrared sensors, and vibrating wire sensors. The bandgap voltage of a semiconductor junction varies with temperature and that’s predictable and measurable, too. There are probably other methods too, some of which are probably pretty creative.

Bimetalic coil by [Hustvede], CC-BY-SA 3.0.
You can often think of creative ways to do any measurement. There’s an old joke about the smart-alec student in physics class. The question was how do you find the height of a building using a barometer. One answer was to drop the barometer from the top of the building and time how long it takes to hit the ground. Another answer — doubtlessly an engineering student — wanted to find the building engineer and offer to give them the barometer in exchange for the height of the building. By the same token, you could find the temperature by monitoring a standard thermometer with a camera or even a level sensor which is a topic for another post.

The point is, there are plenty of ways to measure anything, but in every case, you are converting what you want to know (temperature) into something you know how to measure like voltage, current, or physical position. Let’s take a look at how some of the most interesting temperature sensors accomplish this.

Continue reading “Practical Sensors: The Many Ways We Measure Heat Electronically”

Linux Fu: Serial Untethered

Serial ports used to be everywhere. In a way, they still are since many things that appear to plug in as a USB device actually look like a serial port. The problem is that today, the world runs on the network. Sure, you can buy a terminal server that converts a serial port to an Ethernet port, but what fun is that? In this article, I’m going to show you how to stream serial ports over the network using some available Linux tools. It isn’t perfect, and it won’t work for every case, but when it works it works well.

Everything is a File, Until it Isn’t

At some point in the past, Unix — the progenitor of Linux — treated virtually everything as a file, and all files were created more or less equal. Programs didn’t care if a file was local, on the network, from a tape drive, or arriving over a named pipe.

But things started to change. Even though a serial port is just a file under Linux, it has some special attributes that let you set, for example, baud rates. Worse, some programs “know” too much about files and insist on certain naming conventions. So, in theory, you should be able to create a network socket, connect one end to a serial port and the other end to a program, and be done with it. In theory.

Continue reading “Linux Fu: Serial Untethered”

Using Your Phone As A Microscope On The Electronics Workbench

One aspect of working for Hackaday comes in our regular need to take good quality photographs for publication. I have a semi-decent camera that turns my inept pointing and shooting into passably good images, but sometimes the easiest and quickest way to capture something is to pull out my mobile phone.

It’s a risky step because phone camera modules and lenses are tiny compared to their higher quality cousins, and sometimes the picture that looks good on the phone screen can look awful in a web browser. You quickly learn never to zoom on a mobile phone camera because it’s inevitably a digital zoom that simply delivers grainy interpolated pictures.

That’s not to say that the zoom can’t be useful. Recently I had some unexpected inspiration when using a smartphone camera as a magnifier to read the writing on a chip. I don’t need an archival copy of the image… I just needed a quick magnifying tool. Have I been carrying a capable magnifier for soldering in my pocket or handbag for years without realising it? I decided to give it a try and it worked okay with a few caveats. While I have seen optics turn these cameras into pretty good microscopes, my setup added nothing more than a phone tripod, and will get you by in a pinch.

Continue reading “Using Your Phone As A Microscope On The Electronics Workbench”

Learn Bil Herd’s DIY Surface Mount Assembly Process

You can do your own Surface Mount Technology based PCB assembly with just a handful of tools and some patience. At the heart of my SMT process is stopping to inspect the various steps all while trying to maintain a bit of cleanliness in the process.

Surface mount or Surface Mount Technology (SMT) is the modern way to assemble Printed Circuit Boards (PCB) and is what is commonly seen when opening a modern piece of tech. It’s much smaller than the older Through-Hole (TH) technology where the component leads were inserted into holes in PCB, and act we called “stuffing” since we had to stuff the components into the holes.

A few specialized tools make this a lot easier, but resourceful hackers will be able to pull together a solder paste stencil jig, vacuum tweezers, and a modified toaster oven with a controller that can follow the reflow profile of the solder paste. Where you shouldn’t skimp is on the quality, age, and storage of the solder paste itself.

Join me after the break for my video overview of the process I use in my workshop, along with details of every step of my SMT assembly process.

Continue reading “Learn Bil Herd’s DIY Surface Mount Assembly Process”

Circuit VR: Even More Op Amps

In the last Circuit VR we looked at some basic op amp circuits in a simulator, including the non-inverting amplifier. Sometimes you want an amplifier that inverts the signal. That is a 5V input results in a -5V output (or -10V if the amplifier has a gain of 2). This corresponds to a 180 degree phase shift which can be useful in amplifiers, filters, and other circuits. Let’s take a look at an example circuit simulated with falstad.

Remember the Rules

Last time I mentioned two made up rules that are good shortcuts for analyzing op amp circuits:

  1. The inputs of the op amp don’t connect to anything internally.
  2. The output mysteriously will do what it can to make the inputs equal, as far as it is physically possible.

As a corollary to the second rule, you can easily analyze the circuit shown here by thinking of the negative (inverting) terminal as a virtual ground. It isn’t connected to ground, yet in a properly configured op amp circuit it might as well be at ground potential. Why? Because the + terminal is grounded and rule #2 says the op amp will change conditions to make sure the two terminals are the same. Since it can’t influence the + terminal, it will drive the voltage through the resistor network to ensure the – terminal is at 0V.

Continue reading “Circuit VR: Even More Op Amps”