Follow our illustrated blog on Embedded Software Architecture

How to build a hybrid solar/wind energy harvester?

Green4 serial test protocol and GUI

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)


For most embedded projects it is good practice to have basic tooling for testing I/Os. I/O primitives such as ‘read gpio’, ‘set pwm duty cycle’, ‘read adc’, not only allow engineers to verify hardware and software drivers, it might also be very helpful to service and support people.

Serial protocol

In our project we use a simple serial protocol which brings all I/Os to the end-user. Its main usage is testing but it could be used by a controlling application as well (more on this later). To give you an idea, here are a few protocol commands:

  • h: help
  • c <0-3> pwm,<0-100>: set pwm duty cycle of channel.
  • c <0-3>: get channel mode/setting
  • f pwm,<0-100>: set fan duty cycle
  • l <0-3> <on,off>: set led on or off

The green4 will answer whether the command was successful or not e.g. c:0 3 pwm,80 (i.e. channel 3 was successfully set to pwm 80).

The protocol being ascii-based is a deliberate choice: ascii is human readable so the user only needs a serial terminal to communicate with the device. No additional tooling is required. A binary protocol would be more efficient though: less bytes to transfer and less parsing on target (ideally, binary data can be mapped 1-to-1 in a memory structure – beware of endianness!). As a consequence, cpu power and bandwidth is better utilized.

At this moment communication speed is configured as 9600 baud which is slow but reliable (less chance of hardware errors).

We are planning to extend the serial commands with an optional CRC.


But of course, it is nicer and more fun to work with a GUI. Here is a screenshot:

green4 power controller gui

green4 power controller gui

All peripherals are ‘exposed’ to the user:

  • 4 ‘green’ channels for switching renewable energy including volts and amps measurements
  • 3 supplementary channels e.g. for fan, external device etc.
  • a collection of inputs configurable as gpio or adc/input capture
  • board input voltage
  • some leds

Also, it is possible to connect channels to each other by making groups (in the picture, channel 3 and 4 are each other’s complement).

Not too many features: the goal is to test hardware and basic software.

Choice of technology

The Green4 gui runs multi-platform on Windows and Linux (and probably OSX as well). In order to achieve this, the choice of GUI framework (or toolkit) is important. There are several possibilities such as Qt, Fox toolkit, wxWidgets, juce… but we choose FLTK because it is simple, lightweight and long-lasting (we know the code will still compile 20 years from now without much effort).

The windows executable is cross-compiled from Linux with mingw.

Scripting possibilities

The linux version has several scripting possibilities and integrates well in the unix/linux environment. Some examples:

  • ./green4: start the gui and do your thing there :-) No scripting.
  • ./green4 -s /dev/ttyUSB0: start gui and connect to serial
  • ./green4 configuration.txt: start gui with certain config (connection and all I/Os can be configured)
  • cat configuration.txt | ./green4 –stdin config1.txt config2.txt: read configuration from stdin and from 2 files
  • ./green4 -s/dev/ttyUSB0 –stdout –nogui | grep ‘something': do not start gui but dump everything on stdout and grep a certain IO

Remote telemetry is an advanced feature we support as well (with the same tool!). This will be explained in another article.

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)

The red-black tree and hash table

red-black tree The even more advanced container we look at now is the red-black tree, which is a type of self-balancing binary search tree. Especially, interesting is the time complexity in big O notation: Action/Subject       Average          … [Continue reading]

3D printing of enclosures

printed in translucent white ABS

Having the correct and proper enclosures, is a mechanical (and also a marketing) problem many electronics projects suffer from. It is also related to anticipated sales numbers. For big numbers, a custom enclosure which is produced with an injection … [Continue reading]

Vector, list and tree

Choice of containers Information technology, even embedded devices, is about information gathering, processing or calculation, and control. Input and data needs to be juggled around and maybe sorted. In this article, we want to point out some … [Continue reading]

Priority inversion

I found the inspiration for this article while working on a consultancy job: Priority inversion. In computer science, priority inversion is a (potential) problematic scenario in scheduling in which a high priority task is indirectly preempted by a … [Continue reading]

Progress update

Time for another update. The Green4 project is progressing steadily. In the last few months, we have been busy preparing the Green4 prototypes. Test software has been developed which is already very usable, but it is still in a state of flux as we … [Continue reading]

Embedding a Basic interpreter

The green4 stm32 cortex m3 microprocessor we selected is - according to today's standards - a rather small microprocessor in Flash (128KB) and RAM (8KB). why an interpreter The Green4 device is running control software on top of FreeRTOS, with a … [Continue reading]

Green4 charge controller: wiring diagrams part 2

Wiring diagram: wind turbine to dump load In contrast to solar panels, a wind turbine can't be disconnected in case it generates too much power. We need to brake it by attaching a load to it. However, wind power can't always flow through the charge … [Continue reading]

Green4 charge controller: wiring diagrams part 1

charge controller wiring batteries to external device

Introduction Instead of endlessly listing requirements and specifications, we prefer to demonstrate the architecture of our Green4 charge controller by means of wiring diagrams and other illustrations. The Green4 is designed to be a versatile … [Continue reading]

Open source implementations of malloc

On (very) small embedded systems without a full OS (e.g. because there is only a scheduler), one can (and should) usually live without dynamic memory allocation. But sometimes, it is really needed or it is just more convenient. It is possible to … [Continue reading]