Black Swift board – connecting to WiFi network using UART

Hi! Today I’m showing you how to connect to a wireless network using serial console (or SSH over wired LAN, for that matter) in OpenWRT.
bsb_serial_login

Disclaimer: this tutorial is mainly focused on Black Swift boards, which are nice boards running OpenWRT, but I believe it should apply to most of the OpenWRT boards.

Connecting to WiFi using serial connection might be necessary because you’ve got WiFi connection as the only other way to configure your board but you need to connect to the Internet somehow, for example, to download updates. Not to mention it is kinda is hard to change settings on a connection you’re using to change them, one wrong move/glitch and you’re cut off, needing to reboot the board and restart =( Say, you’ve got a Black Swift Pro board with USB-UART embedded, or you have connected an external USB-UART to your BSB Basic, doesn’t matter. What matters is that you’ve got a terminal at your service, and that ain’t going anywhere – even if WiFi goes down. … I like serial consoles. They’re reliable.

By the way, I’m overriding the UCI configuration. It’s temporary, so not gonna get saved anywhere. For UCI configuration there are many other guides, which I need not repeat here – these are plain old command line tools. If this guide gets outdated at some point, do not hesitate to contact me using comments or e-mail.

Say, your network interface is wlan0 – one can never be too sure, what if you were using an USB-WiFi dongle? If in doubt and things go wrong, check.
Now you need to issue a couple of commands to connect to your WiFi network of choice. I suggest you follow this guide, it has info about using different network encryption types with iw utility. For me, I had to use wpa_supplicant because my network is WPA2 protected. For this, I needed a file with network’s PSK, and that file is usually generated by wpa_passphrase command-line utility. BSB’s firmware provides no wpa_passphrase tool in the default image, however, you can run it on any Linux machine and then just copy the output over.

If you don’t have a Linux PC with this tool nearby, there’s also this option:
1. Take this configuration:

network={
    ssid="MYSSID"
    #psk="passphrase" #network password goes here, but this is not necessary
    psk=59e0d07fa4c7741797a4e394f38a5c321e3bed51d54ad5fcbd3f84bc7415d73d
}

2. Generate the PSK part on this website
3. Fill in the fields according to the field names and save this in a file.

Then, you have to start wpa_supplicant like this:
wpa_supplicant -iwlan0 -cwpa.conf, where wpa.conf is the file you’ve just generated.

Hit Ctrl^Z when you can see it has connected to the right AP to free the terminal, then send wpa_supplicant to background with bg command.
You can use iw dev wlan0 link to verify you’ve connected to the wireless network, if you’ll be unsure about that.

Check if DNSMASQ is running – it’s most likely serving DHCP addresses on the wireless network you just connected to, you’d want to avoid that.
ps |grep dnsmasq
Once you know the PID, just kill it.

Okay, now getting the address by DHCP.
udhcpc -i wlan0

We should have basic connection. Check network connectivity now:
ping 8.8.8.8

However, we don’t have DNS working, the reason is that DNSMASQ is used as a caching DNS server and therefore it made itself a system-wide DNS server, and we just had to shut it down. We need to edit /etc/resolv.conf and replace “127.0.0.1” in there with any DNS server, be it your router, your provider’s DNS or one of public DNS addresses. I chose 8.8.8.8. When done, test it:
nslookup google.com
If the results are reasonable, you’re good to go. Now, set up whatever you need to set up. For me, I finally could do opkg update – I’ll tell you about that one later =)


 

Bonus: connecting USB-UART to the Black Swift board

(Basic, as the Pro boards have USB-UART on the board)
Requirements:

  1. A USB-UART dongle with suitable logic levels – 3.3V max. If in doubt, plug it in and check RXD and TXD lines’ levels from the ground. None should be above approx. 3.3V.
  2. A couple of sufficiently thin wires (I used male-male breadboarding wires, as you can see on the pictures below). If you don’t have headers attached to your BSB, you can solder those wires right into the connection holes on the BSB.

Diagram:

BlackSwiftToUART

You can safely swap the RXD/TXD lines, and you might need to because different manufacturers label them differently. If you plug in the board but see nothing in the terminal, most likely that’s what you need to do.

UART parameters: 115200, 8n1

20160104_023652

Tip – if you don’t have 5V line on your connector, you most definitely won’t burn your GPIO by connecting wires to it accidentally 😉

20160104_031908

I was surprised that my BSB breakout had GPIO columns swapped. I soldered UART wires incorrectly the first time before my gut feeling warned me I should check the pinouts before plugging it all in.

20160104_014908

The wires – beware, they are plugged in wrong holes on this picture!

20160104_032726

End result, up and running great.

Oh, and if you will have problems running a counterfeit USB-UART adapter based on PL2032, like the one I used, I highly recommend visiting this site for drivers. Worked like a charm on Windows 8 x64.

Advertisements

Making luakit play YouTube videos

Luakit, even though it’s a quite awesome (and fast!) browser, doesn’t play YouTube videos out-of-the-box. At least, when installing it from Raspberry Pi Debian repos, it doesn’t pull the dependencies need and doesn’t even list the packages as ‘recommended’. Nowhere I could find it, but one Arch Linux forum page had a hint, which I decrypted:
Install gstreamer1.0-libav and gstreamer1.0-alsa – the latter being responsible for sound in YouTube videos.
That’s it. Hope it helps =) If you find it’s still not working for you – comment below, I’ll try to debug the issue.

Compiling the latest Arduino IDE for Raspberry Pi

I now have started to use Raspberry Pi 2 with Raspbian Jessie as my desktop PC, and it does all the things I need it to do pretty well. Except… Well, Raspbian is stuck with Arduino IDE 1.0.5, even Jessie. Honestly, it sucks – the newest is 1.6.4 (arduino.cc, not arduino.org version. Screw arduino.org.) Did you know we’ve gotten a sketch autosave feature somewhere around 1.5? If I’d have known, I could have gotten some of my sketches saved when Arduino IDE crashed. Oh, BTW, 1.0.5 also crashes. Why are we stuck with it is incomprehensible.

Even more incomprehensible given that Arduino IDE happily compiles on Raspberry Pi 2 with just a build.xml file (ant build system file) slightly modified to add a new architecture, as well as some pre-compiled files replaced by the ones compiled for ARM. I won’t go into details much, since I hope that Arduino maintainers accept my build.xml modifications  and do what’s necessary to support ARM architecture. If they won’t, I’ll post complete build instructions myself. I’ll describe what needs to be done though.

  1. First, dependencies. Honestly, I have installed a lot of packages while trying many different ways to run Arduino IDE (including getting Linux x32 tarball and trying to replace libs one by one, which was probably stupid), so I just don’t know which from what I’ve installed is necessary and which isn’t. If you absolutely need it before it’s supported, you can always use tools like apt-file to determine necessary packages by using their filenames. I guess that ‘apt-get build-dep arduino’ will bring most of them.
  2. Then, there’s libastylej.so . You’ll need to install libastylej-jni package – it’s something that Arduino IDE sources have to download from arduino.cc website. I have modified build.xml so that it’ll copy the installed libastylej.so instead of unzipping the bundled version. You’ll also have to install avr-gcc and avrdude, as well as libusb-dev.
  3. Now’s building. You can use the official tools and instructions for building Arduino IDE, except that, as for now, the working build.xml is in my Arduino repository copy. It’s forked from version 1.6.5, but I’m sure it’ll work with the next versions as well. As for now, you might just clone my  repo instead of official:
    git clone https://github.com/CRImier/Arduino.git
    You should be able to just run ‘ant’ and relax while it compiles. Then use ‘ant run’ to run the thing, at least for the first time (haven’t researched if it does something first run-specific). After first run, at least, it should be perfectly safe to just  run it as ‘linux/work/arduino‘ (from ‘build’ directory) or even move the ‘linux/work’ directory somewhere and name it ‘arduino-1.6.5’.
  4. To have the “Compile” and “Upload” functions working, you need to replace all the files in ‘build/linux/work/hardware/tools/avr/bin‘ by their symlinks in /usr/bin.  You’ll also want the ‘build/linux/work/hardware/tools/avr/etc/avrdude.conf‘ to point to ‘/etc/avrdude.conf‘. Here’s an one-liner that you have to execute in the bin directory to make symlinks:

    for i in *; do echo $i; rm $i; ln -s /usr/bin/$i $i; done

If you try this approach and it doesn’t work, there’s a possibility I have forgotten something. Do post an error message in the comments – I’ll try to solve it =) There might be some problems because I’m using Raspbian Jessie, when most of you will use Raspbian Wheezy, so I figure some packages might need to be grabbed from testing repositories in case there’s a problem with them in Wheezy.

One more problem solved, and Raspberry Pi is one step closer to being a suitable work PC replacement for a guy like me. I’d say web browsers are still a major problem though. On Raspbian Jessie, as for now, both Epiphany, Iceweasel and Chromium crash randomly from time to time. A browser that crashes randomly isn’t a good browser for me. If you’re searching for a browser too, try Luakit. It’s Webkit-based, fast, never crashed since. You can see the latest HTML5 test of it here.

Remaking MSR206 RS232 magnetic card reader to USB

As you might have noticed, sometimes I work as freelancer in electronics =) It applies not only to fixing PC electronics and mobile phones, but also to repairing, modifying and designing my own solutions in consumer electronics. One of the projects I worked on yesterday is modifying MSR-206 magnetic card reader to replace RS232 port with USB. Let me tell you about that…

IMG_0830

This was of high urgency, so I’ve had to suspend my usual work activities and concentrate on this project yesterday. MSR-206 is a pretty impressive piece of technology, incomparable to those cheap eBay magnetic card readers. It even includes 24V 2.2A DC power supply, which is quite heavy and makes you question those cheap USB 2.5W card readers – like, are they really OK for most applications when somebody released such a powerful thing just to accomplish the same task? It’s also kind of state-of-art inside… Well, maybe it’s just me being used to see a lot of disassembled Chinese stuff =)
Unfortunately, it also is quite old – it has only RS232 port in 8P8C (Ethernet cable) form-factor, not to mention non-standard wiring. My acquaintance working as the IT guy in one hotel had to replace the receptionist PC with a newer one. He didn’t think of need of having a COM port, though. Also, the original COM-8P8C cable was lost, and, of course, cheap COM-8P8C cable for connecting to network equipment didn’t work. So – there’s a hotel that needs their equipment to be in working state ASAP…Which meant replacing RS232 with USB. RS232… I instantly sensed MAX232 chip inside.

So – I disassembled this card reader. Looks great, doesn’t it?

IMG_0831

OK, the top part doesn’t mean much to us. Let’s pull out the PCB:

IMG_0833

On the left side, you can see something related to a power supply. Logic runs on 5V anyway – so I’m not supposed to look closer and examine =)

IMG_0836

L293D? OK, maybe there is a motor or some kind of motor-like thing inside =) Wasn’t exactly my job to figure it out.. 3 of them is something, though. I also didn’t want to see what’s the chip with a sticker on top – must be a MCU, and I knew it didn’t matter to me because I wouldn’t understand it anyway =) Those old MCUs are a PITA when you have to somehow change contents of the ROM or something like that.

IMG_0837

Some kind of logic elements… OK, that’s not what I’m searching for. Let’s see the bottom side of PCB.

IMG_0838

A lone transistor/regulator… Doesn’t matter much.

IMG_0840

Some more logic elements, I guess I’ll have to examine them in case somebody got high and implemented UART-RS232 using buffered logic gates.

IMG_0841

Woohoo! The object of my search. As you know, MAX232 has 2 RS232->TTL (R-T) elements and 2 TTL->RS232 (T-R) elements inside. One pair of R-T&T-R is RX and TX. Other pair is usually unused… Usually. Let’s desolder the chip to see what’s actually used…
IMG_0843
Sorry, sticker, you just were at the wrong place at the wrong time.
IMG_0844
Oh. All 4 elements are used. That’d mean trial and error =( That’s the moment when I realised that I should give Google a try. What I’ve got? A programmer’s manual! Pinouts, commands and so on! Excellent!

First of all, manual doesn’t mention 2 of 4 lines coming from MAX232 to the 8P8C connector. I guess they aren’t that significant then. Other two lines are TX and RX and wiring for MAX232 is exactly the same as on this circuit diagram:

So I soldered 3 wires to GND, TTL RX and TTL TX. What you’d need a chip for then you can interface USB to TTL directly? That’s when a USB-UART converter appears on the stage.
IMG_0850
It’s based on ARK3116 chip, which works on Linux and Windows. I guess now it’s time to say – what a shame that Windows drivers aren’t open-source! This chip works with both x32 and x64 Linux versions, but doesn’t work with Windows x64 – exactly the OS that hotel uses. They’ve installed x32 OS on reception, though – PC wasn’t configured yet and it wasn’t that hard. This board is so small because it was inside of a mobile phone interfacing cable.

So – I soldered headers to the cable that carried TTL signals (the one I soldered instead of MA232). First, I’ve tested them using a FTDI breakout board – it was guaranteed to work good. You know, I’vehad some problems with USB-UART converters in the past =) I issued a command that turns all 3 indicator LEDs on the board… It didn’t work. I swapped TX and RX and – voila!

IMG_0848
Photo (sorry, not screenshot) of my monitor – programmer’s manual on the left side, cutecom terminal on another:
IMG_0849
BTW, Cutecom is a great serial terminal with a GUI =)
Now it’s time for ARK3116 with a USB-B socket soldered to it. That’s when things went wrong – I had wiring problems, soldering problems, adapter problems, software problems and so on. After an hour or so, it finally was OK:
IMG_0851IMG_0852


“[ 587.976696] usb 6-2: ark3116: don’t know how to do software flow control

Sorry, converter, neither do I.

It worked… The only problem was that case wouldn’t close. Turned out that I needed to reposition the converter somewhere else. Like this:
IMG_0853
Cover with a bit of masking tape:
IMG_0854IMG_0857
*puts sunglasses on* Case closed. Now testing with original Windows-only card reading/writing software:
IMG_0858
Woohoo! Ready to be given back to the hotel.

IMG_0855

One more thing for my portfolio in freelance electronics 😉 That sure was fun to make and I hope that’ll inspire somebody. Now – back to writing software for my Arduino payphone project.
BTW, my part-time job now is software testing. It seems that the job changes me already – I’ve found a bug in Tetris:

IMG_0859

tetris-bsd from bsdgames package

Payphone project – screen problem solved & parts arriving

I’ve got some updates for the payphone project. First of all, a photo of the actual payphone I’m modifying. It’s a GPT Sapphire payphone, and there were a lot of payphones like this in Latvia just some years before. Now there are no payphones like this on the streets =(

Now, something about the HD44780 screen. First, this screen compared with the cheapest chinese HD44780-compatible screen:
IMG_0817

As you can see, they differ not only in sizes, but also that the header is not at the right place. What a shame =(

As I’ve told you, I’ve ordered replacement comtroller ICs for the display. They’ve arrived:
IMG_0746
A closeup shot:
IMG_0757
So – I’ve desoldered original ICs and soldered the new ones on the board.

IMG_0750IMG_0751IMG_0755IMG_0753IMG_0754IMG_0762

Still didn’t work! Why? Amused, I connected the display to the payphone… Only to see it working.

IMG_0792

What was it? I’ve had a guess, and I guessed it right.

IMG_0793

Seems that my HD44780 screen needs negative contrast voltage in order to display something. Couldn’t have known that earlier – there isn’t much information on this on the Web, and I can see why – it’s an extended temperature range screen, they aren’t that popular. I’ll need to make a negative charge pump in order to power it… BTW, would be a nice idea to see just how much current it needs on the contrast line – to understand which charge pump is needed. If it’s under 0,5 A, I can use a well-known 7660 IC. If not, well, that’s sad – that’d mean I’d need somebody to help.

Some info about this phenomena:
First clue I’ve found that helped me understand;
Actual screen modification that provides some info on how and why;
Just a bunch of photos of different displays =)

Also, I’ve received DTMF generators (datasheet) and decoders (datasheet)… But I haven’t got the crystals for them yet – they’re to arrive soon =( I’ve made breakout boards for both, but, as you can see, they lack some passive components and crystals.
IMG_0818IMG_0818

(Left – a ready board, center – DTMF decoder board w/o crystal and passive elements, right – DTMF generator board w/o crystal)
In order to understand my breakout circuit, just look at the datasheets, there’s a section with a sample usage. The breakout board with a crystal and IC is MT8870 DTMF decoder board – it’s basically the same IC, just older and from another manufacturer, but pinout and circuit are the same. I’ve made this board around 3-4 years before =) That might be one of the reasons it doesn’t work so well – my soldering wasn’t so good back then.
I also plan on making breakouts in Eagle CAD, just after I test these hand-made boards. I’ll share them here – along with some more useful breakouts I hope I’ll finally get an opportunity to test.

I’ve received a I2C-to-HD44780 interface converter, or, as they’re called, I2C LCD backpack.

IMG_0816

It’s also a Chinese version – so it’s not amusing it doesn’t work with the popular I2C LCD backpack Arduino libraries. Why? Seems that pin mapping between I2C-GPIO IC that’s on the backpack board and LCD header on the same board just isn’t the same as in popular versions of this kind of backpack. We’re left to guess just why they’d manufacture a backpack like that, yet it isn’t the prettiest thing to do. At least they’ve supplied Arduino libraries and example code… Which doesn’t compile in my Arduino IDE, and I don’t even want to know why ’cause I sure don’t want to translate code comments from Chinese. I suppose modifying some library that is already working just to have the bits of data sent in different order won’t be any hard. Also, it has a backlight control pin connected to the I2C-GPIO chip, so that’s a bonus =)

I plan on doing the next update when I’ll receive crystals and complete my breakout boards. I’ll probably have Eagle-made breakout boards and libraries for DTMF chips to share by then, too =) Not to forget Arduino libraries.

Debian Jessie, VirtualBox – driver compilation failing because kernel sources cannot be found

I installed the Debian Jessie (testing) system from an around 3-month-old CD. Then I installed virtualbox and while installing it has complained that kernel sources are not found. I’ve installed linux-headers package and all the linux-headers packages that I could have found, but still no luck. I kept getting the following message:


The VirtualBox Linux kernel driver (vboxdrv) is either not loaded or there is a permission problem with /dev/vboxdrv. Please reinstall the kernel module by executing

‘/etc/init.d/vboxdrv setup’

as root. If it is available in your distribution, you should install the DKMS package first. This package keeps track of Linux kernel changes and recompiles the vboxdrv kernel module if necessary.


/etc/init.d/vboxdrv didn’t even exist! As I knew VirtualBox used DKMS, I did aptitude reinstall virtualbox-dkms, and it still outputted “Module build for the currently running kernel was skipped since the kernel source for this kernel does not seem to be installed.” I even installed linux-source – it didn’t help. So what was it?

My distribution had an older kernel, as it was on the CD I installed everything from. Since then, things changed – 3.13 was replaced by 3.14. The repository only had linux headers for 3.14 – and when I installed linux-headers metapackage, I installed headers for 3.14, even though system used the 3.13 kernel. So – when a vboxdrv can’t be build, check if you really have headers for the kernel version you are using =)

Now I just need to learn how to read properly. And to reboot my PC =)

Minimizing Eagle-made boards

Disclaimer: I haven’t yet tested any of these projects as currently I haven’t got a possibility to print PCBs even using the toner transfer method. So use them at your own risk – as you should do with any other project on the Internet =)

From time to time I find cool PCB projects on the Web that are just too big for my use or otherwise unsuitable. Sometimes, those are proprietary and you cannot do anything about it. Other times, source files are available and I use my Eagle CAD knowledge and just minimize the PCB dimensions, replacing components and throwing out useless parts =) The power of open-source hardware, hell yeah. Anyway, here are three Eagle CAD projects that I’ve minimized and would like to share.

1) Tiny USB LIRC-compatible receiver

I was searching for a really tiny USB IR receiver for my laptop. I’ve been searching for a long time but it seemed that every solution wasn’t fitting and had one of three problems: 1) not tiny,2) not USB, 3) not for laptops. Finally, I’ve found a solution on this page that is also full of research on the topic… Still not small enough, but the page includes Eagle CAD files! So I’ve got them and opened them, and noticed that connectors could be replaced in favor of smaller ones. (USB-B for MiniUSB) I did that, rearranged the components on the board and it looked nice. That was the first version – the second has 4 holes for wire instead of connector, which makes it even smaller and embeddable – I plan to embed one in my laptop, one in my Raspberry Pi project and keep some assembled receivers in the drawer just for other projects =)

  • First version – photo (some traces are interrupted but it’s OK – you have to use “Ratsnest” tool, so you get a nice ground fill polygon):
  • Initial version – Eagle files are on the same page where I’ve found the circuit
  • Second version – photo (you can compare boards by looking at the FTDI IC):
  • IR receiver
  • Second version – Eagle files

2) FTDI SIM card reader

This is a quite nice thing that Adafruit used to sell as kits for assembly. It included FTDI and RS232 interfacing circuit – I’d call the second part useless in our days, since nobody’d use COM port when there’s FTDI header available. I took the schematics, threw out everything that was related to RS232 and made the board as little as possible. Almost all the remaining components were replaced with their SMD variants, so this board now is as wide as SIM socket and just a little bit longer =) I guess I’ll order 5 PCBs of this after a while – just after I figure out why the heck I’d need them 😉 I’ll definitely have to extend the capabilities of the SIM card reading software that Adafruit provides (I guess it’s not actually made by them, but I’ll find the owner and fork his repository 😉 ).Also, I’ve made an eBay collection with needed parts in bulk, so that when time comes I can easily order everything – and so can everyone.

By the way, before making this board, I’ve assembled a deadbug-style prototype that actually is even smaller and works with USB-FTDI converter without any problems. Here’s a photo for you:

3) 12V-220V power inverter

This Arduino project  by techmind.dk was featured at Hackaday some time ago and I got interested in it, having searched for something like that for a long time. It’s an inverter that takes 12VDC (or whatever here is) and converts it to the 220VAC. It’s controlled by AtMega 168P in DIP package, and there were no SMD components in the original circuit at all. I decided to fix that and asked the author about original .sch and .brd files – fortunately, he shared them. You know, many people just do not share their original files – and I guess that’s one of the biggest mistakes a person can make when designing something that wouldn’t be much of a problem to open-source. See, when all that’s
between a slightly useful project and quite useful project is uploading some files to a hosting, I prefer the latter.
Anyway, I opened those files, replaced through-hole components with SMD and rearranged the elements… I also needed to retrace the whole board, I’d say it was quite challenging =) I don’t know if it works as good as the previous version, though – haven’t tested yet, but you’d definitely need to change some pin assignments in firmware – I mean, LEDs and maybe something else I’ve forgot about, just compare the schematics and you’ll see – I’ll probably do this make a follow-up as soon as I’ll make this board myself. I think I’ll replace temperature sensor with DS18B20, but that’s just an idea.

I’ve already got more minimized projects that I’d like to tell you about, but I guess it will be later – after I check if they’re, in fact, working =)