All posts by Bastiaan

PlatformIO part 2

I hope you guys enjoyed my first posting about the PlatformIO IDE. Given the complexity of the product, I thought I’d better split stuff up and give some insights on the more advanced features. In this post I’d like to give some clues away about the usage of the integrated Terminal. This is, on any given platform, a very nice thing to know about.

If you open up a project or create a new one, you can add Terminals to the user interface. I’m gonna show you some things using the “platformio” command, which is the basic thing to know about.

Via the PlatformIO menu you can add more terminals to your screen:

It will add a terminal to your editing environment on the bottom part:

This terminal works like any old terminal would on the OS layer. This basically means that if you are on Windows (which is why I’m showing this on the Mac, haha) you can’t use tab-completion. There are a couple of buttons around the edges, as you might have noticed. They can take you full screen, change the coding table and so on, it’s all very self-explanatory.

Any of the actions you can do with pointing and clicking can also be achieved with this terminal. Sometimes it’s faster, sometimes it’s not. I’ve noticed a difference on Windows and Mac as far as shortcuts go, they tend to be better on the Mac. For example Command-Shift-B builds your project, but in Windows you have to find the menu entry and click it.

Anyway, all this boils down to the use of the command “platformio”. If given without any parameters it will display a list of possible options:

There are lots of options here to use and I’ll show you a couple. Let’s start with the “lib” command. This handles anything to do with the libraries. Again, if given without a parameter (e.g. platformio lib) it will display a list of possible options:

The library option/command can have different flags. For one, the -g is good to keep in mind. It shows you the global value of the command you give. So if you want to check which libraries are installed for global usage, you’d type in “platformio lib -g list”. This will list all globally stored libraries and where they are stored:

 

As you can see I have no global libraries because I’m using my “old” Arduino path with existing libraries. I’ve set it up that way so I always have access to my most important libraries. The reason I’m not using the PIO directory is that I have my libraries on my Google Drive.

Anyway, we could, if we want to, install a library from the repository into the global library path. But first, we need to find the library. “platformio lib search <libraryname>” can sort that for us! We are gonna install the VirtualWire library into the global storage location, so, we find it first:

So, now we know it exists in the repository (at this point I have no idea where this repository comes from, but I suspect it’s from the Arduino Library Manager). On Windows this screen will look a bit different by the way. You can see a short description here and the ID of the library. Each library has a unique ID (really strange, I know … ). It also lists the compatibility with the platforms and framework(s).

There are two ways to install the library. You can install it by name or by ID. This comes in handy when there are libraries which have similar names (try and search for “DHT” … ). “platformio lib -g install 251” will install the library in the global storage location, so all your projects can access it. If you omit the “-g” part it will install the library in a folder called “.piolibdeps” in the current project directory. Make a mental note of this, because with this terminal you can do the same as with a regular terminal. You can cd and so on.

In my example you see I’m working on a project called “ClockLamp_v5”. So, I will install the library in the global location:

You can see it downloaded the library and added it to the global storage location. I can now use it in any project I like.

You can see it doesn’t list the library when you omit the “-g” and vice versa:

It may be a bit confusing in the beginning, but it definitely has it’s merits.

So, how does all this interact? That’s the big question. In part 1 of my tutorials I’ve showed you how to use the an environment variable to point to the Arduino library (since I’m on two platforms, the libraries will always be in sync that way). For the purpose of this tutorial I’ve created several copies of the TFT library and changed the version numbers, so we can see what happens when we put a library in a certain storage location.

This part is, by the way, all done with a default platformio.ini, nothing fancy, just a description of the used board:

First off, I’ve created/copied the libraries to (and changed their version numbers):

  1. My Arduino library directory on Google Drive (1.0.5)
  2. The general storage location (<homedir>/.platformio/lib, 1.0.2)
  3. The deps directory of the project (project/.piolibdeps, 1.0.4)
  4. The lib directory of the project (project/lib,  1.0.3)
  5. Lastly, there is a built-in version (1.0.6, most recent) somewhere…

Because I have the environment variable that overrides all cases, at compile time I will see it uses the “1.0.5” version from my Google Drive:

You’ll notice it also gives a nice tree of dependencies from other libraries. So, let’s start messing about! I removed the environment variable from my system and now it compiles to the “1.0.3” version from the lib directory!

Ok, but suppose we removed it from the lib directory and still with a default platformio.ini? Ok, let’s see what happens:

Ok, now it compiles the library from the (hidden) .piolibdeps directory. We are getting somewhere… I remove that too and re-compile:

Going to the “1.0.2” version now, or as we know it, the global storage location (as per the PIO default). We remove that too now:

And the runner-up is the built-in loction for libraries (like we know from the Arduino environment). Awesome! Now we know in which order the library locations are processed and we also know that the PLATFORMIO_LIB_EXTRA_DIRS overrules them all. With this knowledge we can do very good tests to see how libraries interact with each other. This is somewhat of an advanced feature, but nonetheless a very important one.

Of course it depends a bit on what sort of developer you are. Do you want to be bleeding edge or just make some stable stuff? PIO gives you all those possibilities. I hope this little write-up makes it a bit more clear how the libraries interact and in the mean time I’ve shown you a couple of terminal commands too!

It’s way too much to handle everything here (and I’m only just a beginner too). The PIO library system is well developed and offers a wide range of library management possibilities. The one thing I’ve mentioned in the first tutorial (to access all your Arduino libraries with the environment variable) lead to somewhat of a discussion on Twitter with PIO. I like to keep it KISS, hence, that is why I used the lib_ldf_mode = deep and the environment variable. WIth those basic settings you can access the PIO projects from all of your machines. Wether it be Linux, macOs or Windows.

But as we’ve seen here there is much more too it to setup the PIO to yor prefered method of working. Well, enjoy and see you next time with something else (I’m not sure what yet, haha, so much to explore!).

For more information you can always visit the very good documentation pages at  http://docs.platformio.org/page/librarymanager/ldf.html

Using PlatformIO for your Arduino projects

Lately the default Arduino IDE has been bugging me more and more. So I decided it was time for an alternative. A couple of months ago I tried PlatformIO. It wasn’t the best back then. I think it was in it’s early stages of supporting the Arduino hardware and so on. So recently I decided to go nuts and install it again to see what has changed…

I was very surprised to see it actually made vast improvements over the last version (where I couldn’t even get a library integrated in a sketch…). My goal at this point is to have a cross-platform development solution where I don’t have to fiddle with too much libraries and such things and it needs to look fancy. Well, mission accomplished!

So, here are a few pointers for configuring the PlatforIO editor (based on Atom, b.t.w., incredibly powerful text editor) to get you started without too much of a hassle and thinking about libraries and such.

I assume you all know how to install software, so we’ll skip that. After you opened up Atom for the first time there will probably be a few updates (check the bottom right corner). If you install all the updates it will require several restarts of Atom, so don’t go updating in the middle of coding.

It may ask you to install a couple of side dishes like Clang, Python and maybe some more. You need to install those too! But every time it needs something it gives you a link to the download page, very user-friendly! Also, some are absolutely essential (like Python) and some (like Clang) are there for neat code completion and so on.

Anyway, after all updates you’ll be confronted with the start screen, it should look somewhat like this:

So, there you have it! There are a couple of key differences between the Arduino IDE and the PlatformIO IDE (actually, it’s a plugin for Atom). First of all, it’s so much more flexible in the way you setup your projects. You can have a different set of libraries for each project or global ones. This way you can easily test new versions of libraries.

Now, when you click “New Project” it has a couple of questions for you:

Now comes the really good stuff. You can choose several boards here! With this feature, you can cross compile for different platforms at once. If you are into serious Atmel programming (or ESP’s) this may come in handy. What’s more, you can specify different libraries for each board in the same project (more about that later).

So, we choose a board and a directory where to store it. The directory structure will be a bit different from what we are used to, but you’ll adapt (as the Borg say):

After you click “Process” it will download some files for the avr platform and you will see the created project on the left:

You’ll notice a couple of things, there is lib directory, a src directory and a couple files. Of course, this plugin has support for Git but I don’t know anything about that, so we’ll skip that for now ;-).

One of the nicest things about this is the “platformio.ini” file. This file stores all the settings for your project. If you click on it, it will open the file and you can see there is already something configured for you:

It’s the basic settings for configuring the avr compiler to actually know what to compile for. You can add more environments here (which would’ve been the case if we had selected more than one board), but for the purpose of this post it’s not really relevant.

Now, we all know the Arduino has a couple of default libraries on board like Wire. The PlatformIO IDE has those too, so we don’t need to worry about those. What we do need to worry about are the regular libraries we use for all our projects. What is also important to know, the PlatformIO IDE has a bad way of dealing with nested libraries (or maybe the developers of the libraries are bad, I don’t know, either way, we are gonna solve it).

There are several ways to go about this library thing:

  1. Add each library for only your current project
  2. Use the Arduino (or any other) library folder for all the projects
  3. Mix them up all together

Because I personally work on two platforms (Mac and Windows) I’ve decided to put all my Arduino libraries on my Google Drive and link them from there. The only way to achieve this is with environment variables. PlatformIO has a bunch of environment variables it listens too. You can use those to setup the whole thing as you like.

For the libraries, we only need to worry about one: PLATFORMIO_LIB_EXTRA_DIRS. This one will be pointed to the location of our libraries on, in this case, my Google Drive. In Windows it looks like this:

And on the Mac, well, it’s Linux, export etc.

After this has been done any old code with the libraries should be able to be build. So, we are gonna add a source file in the, how’d you guess, src folder. Right-click the src folder and choose New File. You can name it any way you like. If you want to retain compatibility with the Arduino IDE I’d suggest you just name it with the extension INO, like so:

Now press enter, ét voilá, a new file has been born! Now you can go about and produce your brilliant code for the thing you are trying to do. So, now we have some brilliant piece of code, something like this (mind you, D1 as an alias for pin Digital 1 doesn’t exist, so it’s just 1, A0 does exist of course):

Now you go to the platformio Menu and click “Build”. This compiles the code for you, but nothing more. You will notice in the bottom part some text will fly by, just like the Arduino IDE, with warnings and such. If you choose “Upload” it will upload it to the Board, if it’s attached to your computer. In most cases, it can decide for you on which port the Board is, but if not, don’t worry, you can change that in the platformio.ini file, like so:

Note though, that the names of the ports differ on Mac and Windows, so I guess I’m stuck on Automatic choosing, but so far it works great. If you need more libraries for this project, but just this one, you can add another line:

The lib_extra_dirs directive can be placed here too. So now it will know to look there too.

In my endeavors to make it cross platform, I ran into another library thing. If you deep link libraries (from one to the other, with #include and such) it’s possible that the compiler cannot find all the libraries it needs because the dependencies are “missing”. What it actually is, the compiler stops looking after it runs into one link. To fix this, change the platformio.ini as follows:

Unfortunately, there is no environment variable for this (lib_ldf_mode), so I think I’ll make it a habit to just add this line to whatever project I’m doing since creators of libraries often don’t care about this problem.

One more thing to know about environment variables versus the platformio.ini directives. The environment variables take precedence over the ini files!

Well, I do hope this has been informational and that you will give this a chance. It’s looking way better than the Arduino IDE and the support for the boards is also very advanced at this moment. Enjoy!

WS2812 CrossFader

Based on Clay Shirky’s work for the Arduino, I extended it a little bit to use it with the WS2812 LED’s. Enjoy 🙂

-editorial-

That’s what happens when you code without trying, it doesn’t work at all, haha. Below is the revised code including some randomness, setup for 3 LED’s. Next step will be to remember the given values for each LED, it’s still a bit jumpy now.

 

Nextion 3.5″ touch display

After somewhat disappointing results with the 3D printer (XYZPrinting makes crap printers, fyi), I’ve had a display gathering dust. Namely the 3.5″ touch display from Nextion. This display has a builtin touch functionality and comes with a piece of software to upload images, buttons, gauges and whatnot to the screen. The screen than handles all the clicking and dragging and you get some clean out put to your Arduino or other serial device, neat!!

Too bad the software is, how to put it nicely, the biggest piece of crap you have ever seen. To proces things in the builtin controller of the screen there is something of a “programming” language available without much documentation. You’d suspect there would be something like a for-loop huh? Well, no … they can handle not too complex IF-statements but that’s about it.

But after messing around a bit, it started to make sense. Global variables are not global, and post/pre-actions don’t make sense, spaces between filling variables are of course leading to compilation errors and much more misery.

So, lots of work left there … the hardware seems really nice though. In the end I managed to setup a little LCARS interface with buttons who actually remember their settings and it even has a fade-in/fade-out sequence with a timeout! Don’t ask how I made the timers do the fading though. Normally you would load a little library in the Arduino and control the screen from there, but I want to put as much functions in the screen itself as possible so I can have the most juice in my Arduino or ESP to handle important things, like running my coffeemaker.

Anyway, here is a little piece of footage to let you see what I made. It’s not too fancy, but it shows a little of what this neat screen can do (if you stop thinking about everything you learned when programming …). In the end it makes it worth.

The next step is getting things to work with the Arduino and my favorite Arduino App, Blynk. I’ll probably be using  https://github.com/bborncr/nextion this library to read/write to the screen. Very light and simple, I like that 🙂

Slic3r and da Vinci 1.0 Pro

Sup folks,

Here is a little snippet I wrote in Perl to use as post-processing script in Slic3r. It will pick up the Slic3r .gcode output file and transform it in to a .3w file to use with XYZware. This is for those who, like, don’t really like the XYZware software and are too cheap to buy Simplify3D 😉

This script is quite possibly not written in the best way, but it’s the least I could do in about two hours. It works fine on my Mac,  but I’m guessing Windows users could be a bit troubled because of Perl not working OK. Basically this script was based on https://gamium.org/3d-printing/printing-on-the-da-vinci-xyz-printer/12/2219 this article, but since I can’t run Notepad++ without Wine, I thought I’d eliminate this step.

One more word of caution, I did not test the script yet, but it looks fine in XYZware. The description of the file is NOT accurate though because I manually insert a heading which is just rubbish, so I suggest trying this with a simple print first. I’m too lazy to get all parameters from Slic3r, though it should be possible to insert those into the modified header, maybe later …

-edit- Machine name ID should be daVinciAW10 apparently for the 1.0 Pro …

Lego train revisited

General idea for a new train controller!

The idea is to have each train equiped with the following:

Arduino Nano

NRF204 Wireless transmitter

Infrared Emitter

The IR emitter will be put on the engine part and taped of so signals only get send to the local train. Lights are an option too of course. This way we can equip each train with it’s own unique NRF address and forget the whole Lego Power Functions channels and colors because the IR will only reach the one train.

On the Nano there will the NRF library, I like the RF24 library because of its ease of use, and the Lego Power Functions library.

One Arduino (also with an NRF will be in control of everything via Blynk app and the local Java server so minimum latency is guaranteed. Because the NRF also supports some sort of broadcast-to-all it’s easy to build in emergency stopping functionality, just in case. In the end this will probably be a Mega, but for now an UNO will probably be enough. This one will be connected via Ethernet (ENC28J60 SPI module).

Detection of the trains can be with reed contacts. This probably is the cheapest solution hardware-wise but you need to write a lot of software for that. Currently I’m investigating color-sensors. They have a tiny CCD with just a couple pixels for color detection. The huge advantage of this method is you can label each train at the bottom with unique colored bricks and have separate colors for different types of cars. You can measure length and direction of the train probably really easy:

ICE-LOC <> WAGON1 <> WAGON2 <> ICE-Close

(green) (blue) (blue) (red)

FREIGHT <> WAGON1 <> WAGON2 <> WAGON3

(purple) (yellow) (yellow) (red)

In this example we can differentiate between the freight train and passenger train and the start and end of the train. We can do all sorts of good stuff with this setup. For example have the passenger train get a higher priority, slow down the freight train at the station, read the train length etc.

Anyway, so far for the theory, we should put this in practice now, hopefully I’ll be able to show you some progress soon-ish. 🙂

ENC28J60 stability

In my quest for the exploration of the IOT I came across a piece of hardware for connecting the Arduino to ethernet, namely this thing: ENC28J60. It’s a very cheap alternative for the Arduino shields and works, almost as good as them.

I combined this port with a couple of LDR’s (see my LDR debounce posts) to measure light and graph it in the wonderful app of Blynk which now features history graphing and raw data logging. This is an end to a means of automating lights somehow.

Anyway, the ethernet library I use is the UIPethernet ethernet. This is the most practical in use and works very good with this adapter. However, I do noticed some stability issues. The board it self is based on the SPI protocol and therefor is really easy to attach to your device.

I modified the ENC28J60Network.cpp file according to some source on the internet als follows. There is a procedure in there called “sendPacket()” with the following piece of  code in it:

Somehow that doesn’t do the trick and my ethernet disconnects after a day or two, sometimes sooner. The “Internet” suggested I change this like so:

And that’s it, for now it runs smoothly so I have good hopes it will stay running so I can record my light levels in the living room, normalize them and attach a relay or LED lamp to it!

LDR debouncing

Hey all,

Back for some more fiddling with Light Depended Resistors. I’ve been writing a little piece of Arduino code to debounce an LDR (or more than one). I’ve noticed that LDR’s tend to behave irregular. No idea why, but I guess that’s the way it is. To get a good reading you need a couple readings and average them out. In the code snippet below I added an RBG LED on PWM pins 11, 10 and 9 and an LDR with a pullup resistor on A0.

What this code does:
– Take, in this case 19, readings and put them in an array with a pause of 1/4s between them (you can adjust that with the delay function in the first for-loop)
– Take out the highest and lowest value to correct for weird readings on the analog port
– Average everything out and adjust PWM accordingly.

Bear in mind that the adjusting of the PWM pins has some caveats:
– Map() the analog values which come out of the port to 0-255, because that’s what analogWrite can handle
– Because LED’s, especially blue/white ones can be really bright I added a correction factor of 150. In other words, if we measure 200, the PWM pin actually sends 50 as output value. Of course you can use the outcome just as it as, a value from 0-1023 for other purposes
– After that I added a constrain() to ensure the values make sense for PWM. I added three different constrains for each color because the human eye perceives different base colors in different ways.

As you can see at the bottom I constrain everything with a minimum value of 0. If you set this to 1 our LED will always be on.

 

Brain fart

Brain farts? We all have them, don’t worry. Usually though it doesn’t go that fast from idea to exectution. In this case, it did. Worse, the brain fart was much longer than the time I needed to build it. Check this out!

I made a very simple Etch-A-Sketch (without the shaking, but with the knobs) using an Arduino, two variable resistors and the Adafruit GFX library. The wiring took about two minutes because I misplaced some wires, so I had to search and the code was about 46s.

Surprise, digital Etch-A-Sketch was born! Somebody probably allready did it, but I thought it was cool to demonstrate the ease with which you can prototype on this smart lil’ platform.

Lego Trains again

Hello folks.

I’ve made a little project and video of my next step in Lego-train control land. You can check it out over here on YouTube

It’s basically simple detection with reed-contacts. I’ve placed a little magnet on the front of the train and the display let’s you look at where it is. Also this is my first go at the display, it works neat!

There are many, many, things to do. The next step will be detecting which way the train is going and using two magnets. On the front and on the back of the train. After that I’m thinking of running more trains on one track. The idea is to have one train automated and the other at manual control. So if the manual train blocks anything like a station or a section, the automated train reacts by stopping or whatnot.

Anyway, short text, but nifty progress. The reed magnets work perfect! I’ve just soldered them onto a little print which is made to fit between the Lego rails, like so:

IMG_3460-300x225 IMG_3461-300x225

Not the most compicated thing, but it runs really well. Much better than using physical buttons or photoresistors.