ESP8266 Arduino from Command Line

I was delighted to discover (thanks to Dave of EEVBlog) that programming the ESP8266 can be as easy as programming Arduino by simply adding a package to Arduino. The instruction on the ESP8266 Arduino GitHub page are pretty clear and very simple to follow. It took me less than 5 minutes to download, setup, compile and upload the blink example – this was a huge relief after struggling with the esp-open-sdk, the latest version of which seems have issues with excessive size and cannot fit a server programme in memory.

While I have nothing but the highest respect for Arduino and the simplicity it brings to embedded programming, it does have limitations that I’m not used to as an enterprise application developer. In particular I like to split a solution into multiple source files and in multiple subdirectories, and to have the build scripted. To this end I’ve resorted to the Arduino command line which allows for some scripting options and more flexibility in source paths.

In my case the command looks something like:

/Applications/Arduino-1.8.3.app/Contents/MacOS/Arduino --upload --pref sketchbook.path=. HelloServer.ino

–upload can be replaced by –verify if you only want to compile and not upload to your ESP8266 board.

–pref sketchbook.path=. is required since the default sketchbook path will be in your home directory and setting it to dot will tell Arduino to include files from your project directory (assuming you are running this script from your project directory).

The project directory can then have source files grouped into subdirectories within a libraries subdirectory as follows:

– project directory
     – MainLoop.ino
     + libraries
         + source set 1
               – file1.h
               – file1.cpp
               – file2.h
               – file2.cpp
         + source set 2
               – file3.h
               – file3.cpp

Since the libraries are in the sketchbook path thanks to the –pref sketchbook.path=. parameter set in the call to Arduino, all these cpp and h files will now be included in the build.

Orange Pi Lite Setup

I recently bought the very cheap and proportionally powerful Orange Pi Lite from AliExpress. There’s not much info on the Orange Pi website, but for around 20 AUD delivered, including a mini camera, I thought it was worth a try. A word of warning though, they are not trivial to get up and running and the documentation is quite unclear. To make this tricker, at the time of writing, the Orange Pi Lite isn’t even listed in the model list on orangepi.org.

Setting up the SD card

There are plenty of instructions out there including on the Orange Pi website on how to setup the micro SD card with an operating system image on Windows, Linux and Mac, so I won’t reiterate here. The process is the same for all the Orange Pi models (or burning any image to SD card for that matter), just make sure you use the correct image – I downloaded the Armbian build for mine. Other images, including for other Orange Pi models, can be found here.

If you are using a Mac (which I am) you’ll need to follow the Linux instructions from the OSX Terminal. Once the SD card is setup with the image don’t be concerned when you are told that the SD card couldn’t be read, it should still work on the OPi.

Powering up for the first time

It’s recommended that you run your Orange Pi from a 2A power supply. Power is connected through a 4mm DC plug, although it can also be run through the 5V and GND pins of the GPIO, but I’m not sure if this is a good idea since the board is unlikely to be designed to handle 2A through the pins. That said, my analysis of the Lite would suggest it may need significantly less than 2A in typical use.

I purchased a cheap $2 “Apple” 2A USB charger and USB to 4mm DC charging cable on eBay. I booted from this the first time, but haven’t had a successful boot since. After a lot of playing around with multimeter and oscilloscope I discovered that the power supply couldn’t handle anywhere near 2 amps (the voltage dropped to 4V with a 1.5A draw), and the voltage ripple (peak-to-peak voltage) was around 500mV. This was nowhere near acceptable for the Orange PI. I instead tried a genuine 1A Apple USB charger and found it had around 20mV ripple and maintained the 5V during startup. The 1A rating was more than enough with bootup requiring no more than about 500mA. So the moral of the story is, don’t believe the ratings on any power supplies/adapters/chargers, test them to be sure.

Once you’ve sorted out the power supply, simply plug into an HDMI display and a USB keyboard, connect power, and be patient – it takes a while to see a picture and for the green LED on the Orange PI to light up. Also, if there is anything wrong with your SD card the LED will not turn on even though power is adequately supplied.

Setting up the wifi

If you’ve used an Orange PI Lite image such as the Armbian build then the wifi drivers should already be installed. All you have to do is follow the Debian wifi setup instructions. Since my setup is headless (no graphical user interface installed) I followed the command line setup instructions.

When configuring /etc/network/interfaces I simply set the name of the network and password:

# Wireless adapter #1
allow-hotplug wlan0
iface wlan0 inet dhcp
        wpa-ssid my-wifi-id
        wpa-psk my-wifi-password

These setting will vary somewhat depending on your wifi setup.

KiCad: Pin connected to some others pins but no pin to drive it

I got sick of the limitations of the free version of Eagle and so decided to switch to KiCad. My impressions so far are that it’s an extremely capable tool, especially considering it’s free and without restrictions.

Anyway, this post is to point out the solution to a common and annoying problem. Any net in the schematic that has power attached must be explicitly specified as a power input through the use of a power flag. This includes anything like VCC, +5V, +VBATT, and GND. If you don’t you’ll get an error like this:

Pin connected to some other pins but no pin to drive it
@(...): Pin 1 (Power input) of component #PWR001 is not driven (Net 24)

In KiCad all nets must be driven, and while you’d think that connecting a net to a power input such as +5V would drive your net, it does not. Rather you have to explicitly drive the net with  a power flag. To get rid of the errors add a power flag for each net as follows:

Screen Shot 2016-03-12 at 4.31.49 pm
KiCad: Power flag on all power input nets.

Power flags can be added by pressing ‘P’ and finding the PWR_FLAG component.

Racing Quad Parts List

I recently decided to build myself a racing quadcopter. I’ve never flown a remote controlled aircraft of any sort, so I was starting from a point of almost complete ignorance. It took quite a bit of chasing rabbits down many holes, but after considerable internet exploration I managed to find everything required to get a four-propeller bird into the air.

Following is a parts list, which I’ve just ordered, that should be complete enough to get flying. In the future I’ll no doubt extend this to include such things as FPV (First Person View) equipment, but for now I’ll just be happy if I can get off the ground.

Parts:

  • Transmitter and Receiver
  • Frame
  • Flight Controller (CC3D)
  • ESC (Electronic Speed Control)
  • Motors (brushless)
  • Propellers – 2xCW 2xCCW (one pair clockwise, one pair counter-clockwise)
  • Power Distribution Board (PDB)
  • Battery Connector
  • LiPo Battery
  • LiPo Battery Balancing Charger

Most kits available will have some of these parts, but generally not all. For example I bought all the parts from eBay and Deal Extreme, including a kit that contained all except the transmitter, receiver, and battery related items. So in most cases some knowledge of how these parts work together is required in order to know which ones to get.

Transmitter and Receiver

The transmitter (controller) typically comes with a receiver when purchased. The transmitter sends a signal over a number of channels, each channel being responsible for controlling one component of the quadcopter (e.g. controlling the throttle or pitch). So you’ll need at least 4 channels: throttle, pitch, roll, and yaw. It is often recommended to have 6 channels so that other features can be added to your quad later on. The transmitter typically sends its signal over a 2.4GHz band, the receiver receives the signal, breaks it up into the channels, and then sends each channel to the flight controller.

Flight Controller (CC3D)

The flight controller is the brains of the operation. It receives signals to move the aircraft and based on gyroscopic and acceleration sensors it decides what its orientation is and how best to implement your command by adjusting the power to each motor. The flight controller sends commands to the motors via electronic speed controllers.

ESC (Electronic Speed Control)

The ESCs drive the brushless motors by sending pulses along three lines. They receive their commands from the flight controller along a signal line (which actually has three cables – more on this in a moment), and they receive their power directly from the battery.

Each signal line is paired with two power lines (red and black). I found this confusing at first since the ESCs are powered by another two lines (also red and black) which are thicker and clearly the only ones capable of carrying enough current to drive the motors. So what are the other two, thinner power lines for? As it turns out, even though they are paired with the signal lines they are nothing to do with them and in fact provide power to the flight controller from the Battery Elimination Circuit (BEC). So then my question was, “what is the BEC?” Well, it’s a DC-DC converter/regulator and each ESC is equipped with one. I’m not sure why they call them BECs instead of voltage regulator or similar, and I’m not sure why they equip the ESCs with them rather than having them separate. One side effect of having them in the ESCs is that you end up with four when you only need one. Most instructions suggest removing the red line from three of them leaving only one to connect to the flight controller.

One more thing to consider with ESCs is the firmware. SimonK seems to be one of the most common, BLHeli too – both are open source. When purchasing ESCs they will have the firmware already flashed, so this isn’t really something I’ll want to worry about until I get somewhat more serious and significantly more skilled. I have read that the BLHeli firmware is more responsive, but I doubt I’d be able to tell the difference.

Motors (brushless)

Three phase brushless motors are driven from the three outputs of the ESC. The flight controller tells the ESCs how fast to spin each motor, the ESCs then figure out how to pulse the three magnetic poles within the motors to drive them accordingly. These little motors are very impressive and can change speed with incredible acceleration. The motors I’ve order are 2280kv. This means that they should spin at 2280RPM (revs per minute) per volt with no load – again, a theoretical value, don’t ever use the motors with any power without a load. More info here.

Propellers – 2xCW 2xCCW (one pair clockwise, one pair counter-clockwise)

Typically plastic propellers are used. Don’t be fooled, they can pack a punch. I’ve been told I should search youtube for “quad propeller injury” as a warning against carelessness. Carbon fibre props are also available, however these may not be a good idea since they are harder to break, meaning any crashed are likely to transfer any damaging force to the motors and arms.

Props are described by their radial length (length from the centre to the tip) and their pitch. The props I’m using are 5030, meaning they are 5 inches and have a pitch of 30 degrees. A larger pitch pushes more air, but also draws more current and creates more turbulence.

The last thing to know about the props is that you have two spinning clockwise and two spinning counter-clockwise. More information here.

motor-direction-propeller-direction-rotation-spin

Power Distribution Board (PDB)

Power distribution boards (typically a printed circuit board – PCB) come in many shapes and sizes. Their purpose is to make connecting the battery, ESCs, flight controller and other devices more convenient.

The power distribution board that comes with the kit I ordered is as simple as possible – it simply gives a few convenient solder points for the battery and ESC power terminals. I plan to upgrade in the near future to one that replaces one entire layer of the frame with a circuit board and includes a 12V regulator for use with FPV gear. This will significantly reduce the potential rats nest of wires required to connect everything together.

Battery Connector

There are a number of battery connectors used by LiPo batteries and you must make sure you have the right connector or adapter so that you can plug your battery into your PDB. A list of some of the more common connectors can be found here (scroll most of the way down the very long page). You’ll also have to make sure your charger has a compatible connector or adapter. My battery and kit both come with T plugs (Deans Ultra), so I’ve bough a banana plug to Deans Ultra cable for charging.

LiPo Battery

There is a lot to learn about LiPos. In particular they are scarily powerful. The maximum rate of power that can be discharged is measured as a factor of the capacity. The capacity is measured in amp hours (Ah) or milliamp hours (mAh). So for example, the batteries I’ve ordered have an 1800mAh capacity. This means that if I run them at 1.8A they will theoretically last one hour. The discharge rate of the battery is called its C rating, for the batteries I ordered they are 30C, which means they can discharge at 30 times the capacity, which is 30×1.8 = 54A. That’s a lot of current! It is a theoretical value and I don’t know how close you can actually get to this in reality, but that aside, they are still very powerful batteries and can do a lot of damage – search YouTube for LiPo battery explosions. There is also a C rating for the charge rate of the battery. This will tell you the current you can pump into your battery while charging.

Finally different LiPos have different voltages. This is because they have different numbers of cells in series and each cell is 3.7V. The batteries I ordered are 3S, meaning 3 cells in series, and are therefore 11.1V.

A very thorough explanation of LiPo batteries, connectors and chargers can be found here.

LiPo Battery Balancing Charger

You’ll need a charger that is specifically designed for charging LiPo batteries. The easiest and most common are the so called 4 button chargers. They can charge a wide range of devices. Many of them do not come with a power supply so you’ll have to acquire one separately in most cases. An old PC power supply should work fine, just make sure you check its maximum current rating for its 12V connection and don’t ask for more than it can give.

A thorough explanation of chargers can be found here.

Frame

I’m not sure what the pros and cons of the various frame sizes and configurations are, except that they are typically fibreglass or carbon fibre. Carbon fibre is only slightly more expensive and should be lighter and stronger. The frame I’ve ordered is the ZMR250, which is very similar to the popular QAV250. The 250 refers to the diagonal length (approximately) which is 250mm.

Spring MVC and Thymeleaf: Bad Request

I’ve been learning Spring MVC and Thymeleaf to produce a basic administerative web client. My starting point was this page from Spring: https://spring.io/guides/gs/serving-web-content/. After getting comfortable with basic page rendering with Thymeleaf and model and controller development with Spring MVC I ran into an issue with dropdown lists.

I was trying to select an item from a dropdown (select tag), but I received an error when trying to post the selection. The item was a custom object and a member variable of another custom object. So I wasn’t using basic object types here. Selecting from dropdown lists using Spring MVC and Thymeleaf seems pretty easy when the item is some sort of ordinal (e.g. an int or an enum – see http://itutorial.thymeleaf.org/exercise/12) but using your own objects is a little harder. The error and solution follow.

Error:

There was an unexpected error (type=Bad Request, status=400).
Validation failed for object='thing'. Error count: 2

This error is due to the members of thing that are selected from option inputs:

<select th:field="*{item}">
    <option th:each="i, iter : ${itemSet}" th:value="${i}" th:text="${i.description}"></option>
</select>

Solution:

I managed to get this working, but it seems that the select th:field must be an ordinal (e.g. an int or enum). For example:
<select th:field=”*{item.id}”>
    <option th:each=”i, ${itemSet}” th:value=”${i.id}” th:text=”${i.description}”></option>

</select>

It’s important to understand that the information required to rebuild the list of options must be stored in the view (i.e. the html page) otherwise the model can’t be rebuilt – I’m relatively new to Spring MVC, but I’m pretty sure this is the case. So rather than store all that info, it’s probably best to just use the IDs as I have here, and then the model will be rebuilt with an empty object and just the ID set. The full object can then be looked up in your controller method. For example:

@RequestMapping(params = "save", value="/items.html", method=RequestMethod.POST)
public String save(
@RequestParam Map<String,String> allRequestParams,
@ModelAttribute(value="thing") Thingthing,
Model model)
{
int id = thing.getItem().getId();
thing.setItem(loadItem(id));
...
}

Here, Thing is the parent object class and item is one of its members. The model holds the Thing but only fills in the id of the item. The ID is then used to load the full item and overwrite the otherwise empty item.

Running Tomcat in Eclipse and Linking to Native Libraries

Recently I needed to deploy a web app and then allow that app to use the native RXTX library for communication over USB. Like most things, it wasn’t too difficult once I knew how.

Adding Web Tools Platform (WTP) to an existing Eclipse install

Running Tomcat in Eclipse is pretty easy to setup through WTP and is very useful for debugging web applications. Here is a brief set of steps to set it up (more details can be found here).

  1. Open Help -> Install New Software.
  2. Select –All Available Sites– from the Work with dropdown.
  3. Select both Eclipse Java EE Developer Tools and JST Server Adapters Extensions.

Adding your Tomcat server

Here are brief instructions (more details can be found here).

  1. Open Window -> Show View -> Other…
  2. Search for and select Servers.
  3. In the Servers view click to add a new server (or right click and select New -> Server).
  4. Select your Tomcat version and click next.
  5. Select your Tomcat installation directory and click Finish.
  6. Add deployments (web projects) to the server by right clicking the server in the Servers view and selecting Add and Remove.

Linking libraries (Java and native) to your web project

The easiest way to make sure your jars get deployed with your web app and found by the class loader is to place them in the web/WEB-INF/lib directory (NOT in a sub-directory). The library can then be added to the class path through the project settings. But if the library relies on a native library, deployment requires a few more steps.

  1. Open project properties and look in Deployment Assembly.
  2. Ensure that there is a Source of /web and a Deploy Path of /.
  3. Go to Java Build Path and add your jar from web/WEB-INF/lib (click Add JARs… and select your jar).
  4. Expand the node for the added JAR and select Native library location and click Edit… and enter the location of the native library.
  5. Click OK to close the project properties.
  6. Open Run -> Run Configurations… and/or Run -> Debug Configurations…
  7. Select the Tomcat Server, and select the Arguments tab.
  8. In the VM Arguments box add -Djava.library.path=/path/to/native/library/ and click Apply.
  9. Run/Debug the web app from the Servers view.

The native library should now be loaded without any errors such as:

java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path.

Arduino: Stepping Up To The Eclipse IDE

The Arduino IDE (Integrated Development Environment) is great for getting started with programming microcontrollers, but it soon meets limitations if you’re trying to develop anything too serious, especially if you want a modular design. My background is in Java, so naturally I wanted the amazing richness of the Eclipse IDE. Fortunately a neat plugin exists for Eclipse found at www.baeyens.it/eclipse/.

I haven’t got much to say here since there’re plenty of documentation and how-tos on their site, but I did have a few issues when getting started – largely due to being a relative C/C++ ignoramus – so I wanted to put a few notes here explaining how I overcame these problems.

Setup

The setup instructions for version 2 (V2) can be found here. They’re pretty good, just make sure you follow them all. I had some strange issues such as the builder not being able to find the implementation of setup() and loop(), even though they were in the .ino file. I didn’t figure out how to solve that and in the end I uninstalled the plugin and tried again with success. Also, make sure you use the nightly build (http://www.baeyens.it/eclipse/nightly) when using Help->Install New Software... in Eclipse, since a number of things have been fixed in that. Any issues, jump on the forum which is very active.

Linking Sources

To link resources within an Arduino Eclipse project:

  1. Create a linked folder:
    1. Right click on the project,
    2. Select New->Folder,
    3. Expand the Advanced>> section,
    4. Select Link to alternative location (Linked Folder),
    5. Enter or browse to the folder,
    6. Select Finish.
  2. Open up project properties and navigate to C/C++ General->Paths and Symbols,
    1. Under the Includes tab select GNU C++ on the left,
    2. Click the add button on the right,
    3. Check the box labelled Is a workspace path,
    4. Click on the Workspace… button,
    5. Select the linked folder and click OK,
    6. Click OK again.
    7. Now select the Source Location Tab,
    8. Click Add Folder…,
    9. Select the linked folder.

You can now use #import “file.h” without having to use the full path. I think that both steps 2.1-2.6 and 2.7-2.9 are needed because one allows the C/C++ compiler/indexer to find things and stops warnings in the editor, while the other makes the Arduino verify to work. In any case, I had issues if I didn’t follow all the steps in 2.

SVN: Linking Files From Other Locations/Repositories

Sometimes it is convenient to link files or directories from external locations into your checkout. While this can be done manually using multiple checkouts it is much nicer if it can be done such that anyone else checking out the same repository gets the same externals as well, without having to go to the effort of executing the same multiple checkouts. This is possible through the svn:externals properties.

There are other tutorials and explanations out there on the web (e.g. this one using tortoise, this question on stackoverflow.com, and this page in the redbean svn book), but I’m one of those Mac users who aren’t fortunate enough to have Tortoise, and I generally prefers to use the command line anyway. So here are some instructions on how to link a file using svn:externals properties.

Set the svn:externals property:

Assume we want to check out (link to) an external file called filename from external repository directory path/to/external/repo/ into repository path path/to/repo.

  1. Change directory to to where the linked files will be checked out: cd path/to/checkout/directory
  2. Set the property: svn propset svn:externals 'path/to/external/repo/filename filename'
    You should see this if it was successful: property 'svn:externals' set on '.'
  3. Update to check out the external file: svn up
  4. You should see output something like this:
    Fetching external item into 'path/to/checkout/directory/filename':
    External at revision 7.

If you want to link more than one file you’ll want to use propedit instead of propset and put each linked file on a separate line in the property’s value. For example, use the command
svn propedit svn:externals --editor-cmd vi .

Troubleshooting:

If you see an error when executing svn up such as the following:

Fetching external item into 'filename':
svn: warning: W220000: Not authorized to open root of edit operation
At revision 6.
svn: E205011: Failure occurred processing one or more externals definitions

For some stupid reason this error is due to read permission problems. This can be worked around easily by allowing everyone read access to the repository root:

  1. Go to repository configuration directory: cd /path/to/repo/conf
  2. Edit authz file, find the section [/] and add: * = r

Thanks for this tip from this SVN forum. It doesn’t make sense to me why this is needed – it seems that the external checkout is not using the given credentials, perhaps there is a way to set this? This work around could be a security issue for some, but for me it’s acceptable in this case.

Operating a 12V Relay From a 5V Arduino Output

I thought I’d made a mistake when I bought these 8-channel, 12V relays since they operate by sinking current and both a 0V or 5V output from a microcontroller such as an Arduino will always sink. Then it occurred to me that they are actually quite well designed with complete separation between control power input and the power supply for operating the solenoids. They have two Vcc inputs which can either be tied together via a jumper or connected to separate power supplies, for example the same 12V supply you’re using to power some LEDs and a 5V supply from the Arduino you’re using to switch the LEDs on and off.

relay-8-channel-opto-chiosz-robots-4

The control logic pins can be found in the middle of the module with a ground pin at one end and a Vcc at the other. Between the two are a number of control inputs (in this case 8). The control inputs will activate their respective solenoid when set to a low voltage with respect to the control Vcc pin (see the image to the left for the control pin layout).

relay-vcc-jumper

The solenoid power supply can be found in the corner of the module where there are three pins: JD-VCC, VCC and GND. The JD-VCC pin is used to power the solenoids and is by default tied to the logic Vcc pin. To power the logic pins by a lower voltage you must remove the jumper and then connect your 12V supply to the JD-VCC pin. The ground pin is internally tied to the logic ground pin.

So in short, you connect the logic pins to your Arduino’s ground, Vcc and digital output pin(s), and connect the JD-VCC pin to you 12V power supply. Program your Arduino (or microcontroller of choice) to set the control pins low for activating the relay solenoids and high to deactivate them.

relay_bb