Dejan Levec

Compiling OpenSSL and OpenSSL.Net for Windows

If you need fast way to hash passwords with MD5/SHA1, or to encrypt and decrypt data with AES I recommend OpenSSL library. This library supports more than 25 algorithms for ciphering, hashing and public-key cryptography. It’s open source and supports most platforms, including Linux, OS X and Windows. To use OpenSSL from C# you need to use a wrapper. I hadn’t had much problems with OpenSSL.Net and it’s supposed to be cross-platform.

Building OpenSSL on Windows

What you need?

You can build library with assembly optimizations or without them. Optimizations provide faster code execution, according to some benchmarks they can speed up execution by up to 4 times, depending on selected algorithm. To successfully build them you need Visual Studio Professional or better, because VS Express doesn’t offer assembly compiler NASM.

To build x64 version start Visual Studio Command Prompt (x64) and run:

1
2
3
4
perl Configure VC-WIN64A --prefix=C:\openssl-x64-build
ms\do_win64a
nmake -f ms\nt.mak
nmake -f ms\nt.mak install

To build x32 version start 32bit Visual Studio CMD and run:

1
2
3
4
perl Configure VC-WIN32 --prefix=C:\openssl-x32-build
ms\do_ms
nmake -f ms\nt.mak
nmake -f ms\nt.mak install

If you don’t have assembly compiler add no-asm to perl Configure call (e.g. perl Configure VC-WIN64A no-asm –prefix=C:\openssl-x64-build).

If everything went without a hitch you should find built library in folder specified with –prefix.

 

Building OpenSSL.Net

What you need?

Download wrapper library and open it with Visual Studio. Choose x32 or x64 platform and press F5. Solution also includes tests (you need NUnit to run them) and example Console application.

The only problem you might have is hard coded version of supported OpenSSL library. Wrapper specifies which version it supports in Core/Native.cs on line 228:

1
public const uint Wrapper = 0x00908000;

If you have different version of library you need to modify this value and hope that all calls in library stayed the same.

Alternatively, you can disable consistency check by commenting it in Core/Native.cs on lines 160-166:

1
2
3
4
5
6
Version lib = Version.Library;
Version wrapper = Version.Wrapper;
uint mmf = lib.Raw & 0xfffff000;
// if (mmf != wrapper.Raw)
// throw new Exception(string.Format("Invalid version of {0}, expecting {1}, got: {2}",
// DLLNAME, wrapper, lib));

After that you shouldn’t have any problems.

Downloads

  • OpenSSL 32bit library (built with debug-VC-WIN32 no-asm): openssl.zip
  • OpenSSL 64bit library (built with VC-WIN64A and assembly optimizations): openssl64.zip
  • OpenSSL.Net 64bit with appropriate OpenSSL library: OpenSSL.Net

Monitoring electricity consumption

I was always interest in knowing how much energy am I using and have been experimenting with different approaches to measure consumption. My favorite way to measure electricity consumption is the use of current transformers/clamps around wires coming into the house. Sadly, I don’t have any space left in electrical box to p ut them in.

About a year ago I found a solution, which is to direct web camera to electricity meter, watch blinking LED and calculate consumption. I made a simple prototype in the form of gluing webcam to outside electrical box (where we have electricity meter) and simple Forms application made in C# with bindings to OpenCV library.

The problem was that this prototype consumed almost whole core on older dual core AMD Turion laptop, which consumes more than 40 Wh. That is not a cheap solution and I also need that laptop for other purposes.

After a week or two of testing, I finally removed laptop and kinda forget about everything this idea. Later, I tried two different ways to capture blinking LED but neither worked as it should.

Two days ago I remembered about it and wanted to port everything to Raspberry PI, and in the future maybe even to a small router.

Over the last two days I tried two different approaches:

1.) Capture frames using v4l2 (in my opinion this was the best idea, and it should be as much as possible lightweight)

Capturing frames with v4l2 seemed like a hard thing to do, and it would actually be really hard, should I not use code from mjpeg-streamer.  I went through the source of mjpeg-streamer, quickly found most interesting module – input_uvc and searched for usable source code.

After I successfully got frames, I needed to process them in figure out when the LED blinked. For debugging purposes I also added code to measure performance (frames per second) and to save captured frames.

Project is available at: https://github.com/deanrock/electricity-monitoring

Application runs in background (it’s a console application), watches captured frames for change of color intensity in specified area of interest, and makes http request to server to save results.

How it works?

  • Main function gets raw frames in YUYV format from web cam.
  • Function do_my_thing uses two for loops to iterate over received frame and get the average intensity of red color in specified area of interest.
  • If current intensity level is larger than average intensity level of previous 10 frames + 10 levels (to discard noise) then we mark it as blink of an LED.
  • We wait for a frame that has intensity level lower than the one marked as blink, and then make a http request to web server. (Single blink can be captured on multiple frames)
  • Simple PHP script running on web server saves data to database.

I should also mention my genius idea of how to keep intensity levels of last 10 frames – circular buffer. When it gets full it replaces oldest position with a new intensity level – that way I always have last 10 intensity levels and don’t need to move any data around.

I need to test this solution in the following week to see if there are any problems with capturing LED, but only improvement I could see right now is to add threads. One thread  for frame capturing and processing, and another for sending http requests. Currently http request blocks capturing new frames and this may introduce a slight delay or lower fps.

Here is a picture of my current “GUI”:

MySQL Workbench running a query which counts reported blinks in the last minute. Now I need to make a web app that will show statistics and current consumption of electricity.

2.) Capturing and processing frames done with OpenCV library (should be the easiest)

I also tried this approach, and it really reduces the number of lines of code written, but Raspberry PI captures about 1 frame per second via OpenCV library, and about 15-20 with v4l2 API.

Ubuntu 13.04

Recently I have been working on some personal projects where I need to use Linux – from building OpenWRT packages to programming apps with libraries that are Linux only. Sometimes I just work on Linux because it’s easier to set up any kind of libraries, doesn’t matter if it’s for C/C++ or Python development, in both cases nothing can beat apt-get install libwhatever-dev and additional flag when invoking a compiler.

( Few days ago I spent more than half an hour setting up paths for OpenGL project in Visual Studio to use glfw and similar libraries. On Linux that can be achieved in less than 1 minute by adding additional flags. )

However, I still prefer Windows environment for everything but programming, because it’s hardware support is supsrb, GUI (even Metro) is cool and most of the work I do on Linux can be done in virtual machine.

Sadly, my latest project needs v4l2 library to communicate with webcam and every time I tried connecting USB webcam through VirtualBox VM it would stop responding. Only viable solution was to use native installation on Linux. Since I removed my Crunchbang installation about a month ago, I needed to install new OS. I promptly went to Google and searched for Ubuntu download and downloaded 13.04 thinking that it was still in beta.

After installation, which went smoothly, I saw a lot of visual as well as technical improvements. I didn’t really follow development of this version so I was really surprised. A quick visit to OMG Ubuntu quickly helped me realize that I actually installed final version and not beta as I thought.

Why was I so pleasantly surprised?

  1. Very nice looking design changes. :-)
  2. Graphics support out of the box. What what?? It actually worked for the first time (for me) without any tearing. Connecting and disconnecting external monitor correctly resized my desktop.
  3. Waking from suspend without any problems? Yeah.

I just might use native Linux a bit more in the following months than I used to.

 

Pan & Tilt Servo bracket

Recently I bought Pan & Tilt Servo bracket set that looks like this:

Servo motors are not robust as I am used to them, but they will be good enough to hold a light web cam.

Servos are controlled by PWM (pulse width modulation) and they need special hardware support. Thankfully, ordinary Arduino supports 6 PWM channels which should be enough for most projects.

Arduino already has a library called Servo to support these motors and there are a lot of articles explaining how it works, so I won’t go into details.

In my case I want to control it from a computer (or in the future from Raspberry Pi or some router running OpenWRT) and therefore need a way to communicate with Arduino.

Servos can be set to a fixed angle where they will stay no mater what (if you don’t put too much force to them) and this angle is from 0 to 180 degrees – half a circle.

Communication between Arduino and computer is usually in the form of data transfer through USB emulated serial port. Serial port communication is intended for transferring text so you must send/receive chars – which are represented by bytes. Byte consists of 8 bits and therefore can represent 128 different states or numbers.

Since 180 degrees is a bit bigger than 128 we need to find another way. I chose to separate data about each angle into two bytes.

First byte holds number from 0 to 127, and second one holds the reminder to 180. For example 160 degrees angle is sent as 127 and 33 – together they represent number 160.

Final format looks like this:

[first motor 0-127] [first motor 0-53] [second motor 0-127] [second motor 0-53]

The only remaining question is how do we know when byte from first motor starts? What if communication is interrupted before sending every byte? We need a unique identifier identifying the start of this sequence.

I decided to firstly post 127 and after that our sequence. However, we need to change the first byte for both motors from 0-127 to 0-126, so that 127 is never send anywhere else except at start.

Final Arduino code looks like this:

#include <Servo.h>

Servo s1,s2;

void setup() {
s1.attach(2);
s2.attach(3);
Serial.begin(9600);
}

int angle0=90, angle1=90;

void loop() {
s1.write(angle0);
s2.write(angle1);

if(Serial.available()>4 && Serial.read()==127) {
int ss = Serial.read()+Serial.read();
int ss2 = Serial.read()+Serial.read();
angle0=ss;
angle1=ss2;
}
}

At the start motors are at 90 degrees, and then we wait for 5 available bytes with first of them equal to 127. After that we simply add bytes and change global variables which will be latter sent to Servo library.

Example C# code with serialPort and two trackBars on form looks like this:

private void Form1_Load(object sender, EventArgs e)
{
serialPort1.Open();
}

private void Send()
{
int x1 = trackBar1.Value,x2=0;

if (x1 > 126)
{
x2 = x1 – 126;
x1 = 126;
}

int y1 = trackBar2.Value, y2 = 0;

if (y1 > 126)
{
y2 = y1 – 126;
y1 = 126;
}

byte[] buf = new byte[5];
buf[0] = 127;
buf[1] = (byte)x1;
buf[2] = (byte)x2;
buf[3] = (byte)y1;
buf[4] = (byte)y2;

string a = “”;

for (int i = 0; i < 5; i++)
{
a += Convert.ToString((int)buf[i]);
a += “,”;
}
label1.Text = a;
serialPort1.Write(buf, 0, buf.Length);
}

private void trackBar1_Scroll(object sender, EventArgs e)
{
Send();
}

private void trackBar2_Scroll(object sender, EventArgs e)
{
Send();
}

Now I might use this for a webcam controlled by Wii Nunchuck or via web page.

DS18S20 and MSP430

Couple of weeks ago, when I was thinking about what temperature sensors to use with my heating controller, I remembered DS18S20 and instantly order samples from Maxim.

Today I finished programming software for main controller – MSP430 chip and I want to show you, how I got those sensors working by bitbanging data pin. Sensors are connected in parasitic mode (which may not be such a good idea since turning oil burner/water heater on and off causes some unwanted interference, and it certainly doesn’t help that data & ground wires are near AC cables). I also found out, that chip’s internal pull-up isn’t powerful enough to drive temperature sensor while it’s converting temperature.

How to talk to one-wire DS18S20 / DS1820 sensor?

  1. Reset
  2. Check if there is a device present
  3. Send skip ROM command
  4. Send convert command
  5. Reset
  6. Send skip ROM command
  7. Send read scratchpad command
  8. Read byte from the bus

Firstly, we need to reset the device, then it’s necessary to sample bus to see if device responded and after that we send skip command, because we only have one sensor connected to the line and don’t care about it’s unique ID and then we request from sensor to convert (take temperature).

After that we do another reset, then skip ROM and read scratchpad commands, and after that we read received data – scratchpad from the device.

I talked more about specific commands and which data scratchpad contains previously on this blog, but I will nonetheless paste code which converts temperature to human readable float number:

int16_t temp = (((int16_t)scratchpad[1]) << 8) | scratchpad[0];
float temp2 = temp*0.5 – 0.25+((scratchpad[7]-scratchpad[6])/scratchpad[7]);

 

How to reset the bus?

P2OUT &= ~bi; //drive bus low
delay_us(480); //delay 480 us

P2OUT |= bi; //release bus
delay_us(70); //delay 70 us

//Presence
P2DIR &= ~bi; //input

We drive bus low for 480 us, and the release it and change pin from output to input. (bi is data pin bit)

After that we can check if device is present on the bus by sampling the bus. Device pulls data line high after reset for about 410 us.

 

How to send a byte?

Well, the better question is how to send a bit. Every byte contains 8 bits and when dealing with that low level of controlling the device with big banging you will need to send bits instead of bytes.

Bit can be presented as logical high ’1′ or logical low ’0′. Logical low can be send with the following code:

P2DIR |= b;//output
P2OUT &= ~b; //drive bus low
delay_us(60);

P2OUT |= b; //release bus
delay_us(10);

You need to drive bus low for 60 us and then release it (drive it high) for 10 us.

On the other hand, logical high can be represented as driving bus low for 6 us and then high for 64 us:

P2DIR |= b;//output
P2OUT &= ~b; //drive bus low
delay_us(6);

P2OUT |= b; //release bus
delay_us(64);

With the help of those two functions we can write bytes to the bus like this:

for(int i = 0;i<8;i++) {
if(b&0×01) {
write_1(bi);
}else{
write_0(bi);
}

b>>=1;
}

If I understand this right (I mostly overslept class about bytes, bits, bitwise operators and other low level stuff about computers), you write byte to the bus by shifting bits to right and comparing them to 0×01 to decide if we want to write logical high or logical low.

Let’s for example take hexadecimal number 0xCC which indicates temperature sensor to skip ROM check:
0xCC is 204 in decimal and 1100 1100 in binary format.

Firsly, we check if this is true: 0xCC & 0×01. Ampersand (&) represents bitwine AND operator which checks every single bit of both numbers and results in 1 if bits in same place in both numbers are 1. Otherwise it results in 0.

For example:
0010 1011 &
0100 0010 equals:
0000 0010
because only second bit from the right side is true in both numbers.

0xCC & 0×01 equals:
1100 1100 & 0000 0001 which returns 0000 0000 because there the last bit in first number is  not positive. Because 0000 0000 equals logical false, the condition of if statement is false and we use function to write 0 on the one-wire bus.

Secondly, we right shift bits for one place and get 0110 0110.

This repeats eight times, to write all bits of the byte to the bus. This type of bit shifting means that device receives byte from least significant bit (LSB) or right-most bit to the most significant bit.

Now we have taken care of resetting and writing bytes to the device. Only thing left to do is to read received byte to actually read the temperature from the sensor.

 

Reading byte from device

Again, we need to read byte from device bit by bit and here is the code for reading a single bit:

P2DIR |= bi;//output
P2OUT &= ~bi; //drive bus low
delay_us(6);
P2OUT |= bi; //release bus
delay_us(9);

P2DIR &= ~bi;//input
boolean x = (P2IN & bi);
delay_us(55);

We make pin an output, drive bus low for 6 us and release it for 9 us. Then we change direction of the pin to input and read bit’s state. After that we need to wait 55 us before reading another one.

I like to complicate things so here is my code for reading a whole byte (I’m quite certain that there is a better way to doing this, but this is what I came up with):

byte j=0×01;
byte r=0×00;
for(int i = 0;i<8;i++) {

if(read_bit(bi)) {
r=r| j;
}else{
r=r& ~j;
}

j<<=1;
}

So, what we do here is to define two variables, first equals 0×01 or 0000 0001 and the second is 0. After that we check is we read logical true or false as current bit and modify variable r accordingly with the help of variable j, which is shifted left after every turn.

Again, we are dealing with byte represented from the least significant bit forward.

r = r | j represents bitwise OR which compares each bit of both bytes and results in 1 if bit of either one of bytes is 1 or true.

If, for example, we are trying to read 0xAC (1010 1100) we start with the rightmost bit/LSB and go to the left:

1.) First bit is 0, so we execute  r & ~j which means:
0000 0000 & ~0000 0001 and ~ (tilde) inverses all bits, so we get:
0000 0000 & 1111 1110 which leaves alone every bit except the rightmost bit, which is set to 0.

2.)After that we shift j to left by 1, which gives us: 0000 0010.

Second bit is 0, so we again clear that bit from the final byte by using 0000 0000 & 1111 1101 to get: 0000 0000.

3.) Third bit is 1 and by using 0000 0000 | 0000 0100 we get: 0000 0100.

4.) Fourth bit is 1: 0000 0100 | 0000 1000 equals 0000 1100

5.) Fifth bit is 0: 0000 1100 & 1110 1111 leaves byte unchanged

6.) Sixth bit is 1: 0000 1100 | 0010 0000 equals 0010 1100

7.) Seventh bit is 0, byte stays the same

8.) Eight and the last bit is 1 which means that: 0010 1100 | 1000 000 equals 1010 1100 and we get our final number 0xAC.

 

That is all you need to know to communicate to a single one-wire temperature sensor from DS1820 family. It actually seems a lot easier than it seemed when I read this from different datasheets.

Here is the link to code containing all functions needed to get temperature data from this sensor: ds18s02

You need to call function get_temp to get the temperature and it’s only argument is bit from second port of the micro controller. Code was written and tested using Energia IDE (which has the same crappy IDE as Arduino, but libraries are excellent) so it should work fine with msp430 gcc compiler.

Old Epson “Printer”

Today I was searching for something at home and stumbled upon old printing calculator, that hasn’t been in use for a few years.

Firstly, I plugged it in the wall socket and tried pressing different keys to see what it can do. Since I don’t really know how to use it, only thing I got were some random numbers printed on paper.

Secondly, I started thinking what I can do with it … well, I can at least open it to see what’s inside.

Picture below shows how it looks inside. On the upper right there is a mains transformer which has two outputs, first one is probably for micro controller and logic circuit and outputs about 6V, and the second one has output of approximately 22V and might be used to drive printer and display.

On the upper left there is a numeric printed which is surprisingly made by Epson.

On the middle/bottom part there is a logic board with the screen and a second board which holds keys.

 


Logic board has Mitsubishi 8-bit micro controller MS50754-125SP which has (according to datasheet) 6144 bytes on ROM and 160 bytes of RAM and apart from supply circuit it also features three Mitsubishi Darlington transistor arrays M54531P which are used to control numeric printer.

I don’t really have any use of screen and keyboard, but printer looks quite interesting. I don’t really know how much time will I have in the near future, but I would really like to control this printer from micro controller/computer.

Click here for more pictures of the printer

Heating controller – Triac board

Hi,

I successfully designed, etched and build triac board for my central & water heating controller.
Board is divided into two parts:

  1. Triac & optocoupler part
  2. Power supply part

 

Board features four BTA12 triacs that are driven by MOC3063 optocouplers and have snubber circuit. Optocouplers input will be controlled via MSP430 microcontroller.

Power supply part supplies 3.3V and 5V voltages to micro controller and WR703N Linux router. I decided to use low dropout regulators so that I can drive it from 6V transformer.

Central & water heating controller

About a year ago I decided to replace analog heating controller at home with something a bit more advanced. I have designed circuit and PCB at that time, but haven’t made until recently. It consisted of ATmega328 microcontroller, 4 relays and ENC28J60 breakout board for network connection. When I was writing source code, I thought about noisy relays and my distrust to using ENC28J60 (I previous had problems with AVR hanging after too much requests).

I quickly scrapped the plan and started thinking about more reliable approach, something like separating controller logic from network access. That day (or maybe day before) I got 3 MSP430 Launchpad boards I ordered online a few weeks ago. I heard about those boards quite frequently before, but did’t want to jump on that bandwagon = frankly, I didn’t have a good reason for buying it. But when I saw ad or something about it, I said what the heck and bought it. So I instantly saw an opportunity to use it for heating controller in combination with WR703N wireless router.

So my plan is like this:
- WR703N provides web interface, logs statistics in combination with USB flash drive, and fetches temperature from Ethernet temperature module
- MSP430 controls optocoupler-triac board according to temperature sensors, and reports data about current state of sensors/triacs back to router (it should also be possible to put it in a manual mode and circumvent control logic)
- optocoupler-triac board featuring 4 BTA12 triacs controlled by MOC3063 optocoupler

Cost of this setup:
- controller box and PCB: 10€
- WR703N: 20€
- MSP430: 3€
- DS18s20 temperature sensors: 0€ (samples from Maxim IC)
- triacs, optocouplers and similar stuff: < 10€

So for about 40€ I will have a really powerful controller with data logging capabilities.

I need to control 4 devices:
- electric boiler for heating water in summer
- oil burner for heating water boiler and central heating
- pump for heating water boiler
- pump for circulation of water through radiators

Electric boiler and oil burner have additional temperature sensors to prevent overheating of water, so even if my system screws up, it won’t cause any unwanted behavior except a bit warmer water and rooms in the house.

STM32F4 Discovery board

I bought STM32F4 Discovery board from Farnell two months ago, but I got to it only two days ago. After testing some examples and trying to interface with ENC28J60 Ethernet Controller, I quiclky found out, that free versions of development tools cannot build executables bigger than 32KB. So I decided to install toolchain on Ubuntu 12.04.

I found installation procedure for 11.04 at http://cu.rious.org/make/stm32f4-discovery-board-with-linux/, but some things have changed since than, so here is installation guide for latest Ubuntu version:

$ sudo apt-get install flex bison libgmp-dev libmpfr-dev libncurses5-dev libmpc-dev autoconf texinfo build-essential libftdi-dev zlibc libmpc-dev
$ git clone https://github.com/esden/summon-arm-toolchain.git
$ cd summon-arm-toolchain
$ sudo ./summon-arm-toolchain
$ cd ..
$ sudo apt-get install libsgutils2-dev libusb-1.0-0-dev
$ git clone https://github.com/texane/stlink.git
$ cd stlink
$ ./autogen.sh
$ ./configure
$ make

More info about using ChibiOS on it:
http://recursive-labs.com/blog/2012/05/07/stm32f4discovery-chibios-linux/
http://www.chibios.org/dokuwiki/doku.php?id=chibios:articles:stm32f4_discovery

My first project? Internet radio player. More about it soon.

WiFi Car

I have been planning to make Wi-Fi enabled car for quite a while (from last October/November), and I have actually done something to get to that goal. The point where I stopped was when I was trying to use my old iPhone as car controller. I tried for a while, and I got to the point where I could control motor via iPhone from my PC and Android phone. Sadly, I couldn’t transmit video from iPhone’s camera to my PC and at that point I gave up.

Every one in a while I would remember this project and thing about it for an hour or so, but that was it. However, about two weeks ago I read about 3G router (TP-Link TL-WR703N) sold in China, that costs about 15€ and supports OpenWrt. It seemed cool and I ordered one, and I finally received it a few days ago.

I have been playing with it for the last two days and I got OpenWrt, USB, lighttpd and mjpg_streaming working. Now I can stream video from connected webcam, and control peripherals connected via Arduino.

This router seems like a good platform with OpenWrt support, 32MB of RAM, WiFi, USB and serial port for all sorts of projects from robotics to home automation. You couldn’t want more for such a low price; it’s cheaper than Arduino Uno.

It has some downsides, like only 4MB of internal EEPROM memory which means it can hold only a minimal number of applications, and you will need to install others to external memory like flash drive. Like every memory, it has limited number of write operations, so you need to be careful and use it as little as possible.

Today I added USB hub and 8GB flash drive. Hub is connected directly to router’s USB port pins, and it currently has two (out of four available) used ports. First is connected directly to flash drive, hidden below hub’s circuit, and second is wired to USB socket for webcam.

I plan to use third port for AVR micro controller connected via USB using V-USB library. Micro controller will currently only be used for control of car motors, but in the future I might use it for something more.

Previous Posts