AdSense

Tuesday, 28 May 2013

PHP - Save password as hash and not as plain text

(Deutsche Version) Currently, I am working on a text based online Game. For the login sequence, it is necessary to save the user passwords. The basic idea is simple: The password is saved into a file on the server which is not accessible from the internet. The problem is: What would be if a user was using his password for everything, e.g. for banking accounts? I would be able to look at the password. Therefore, passwords should not be saved as plain text.


The solution for this problem is the usage of a hash-function (http://en.wikipedia.org/wiki/Hash_function). The implementation in PHP is the following:

$passwort = hash('ripemd160', $passwort);

hash returns a combination of numbers and letters. This combination is (with a relatively high propability) unique. In the PHP code the password is directly converted into the hash value, all operations which need the password are performed with the hast value (e.g. password saving, comparison if the password is valid). From this hash value, it is not possible to generate the password, because the hash value would be converted again if someone (evil) would type in the hash value which would result in a totally different value. It is impossible to find out the password or crack it because it is not saved anywhere.

Sunday, 26 May 2013

ATMega - Hardware SPI

(Deutsche Version) SPI communication can be very useful (e.g. to expand an Raspberry PI with ATMegas). To use the built in SPI Interface of an ATMega8, I hardly found any information. Here is the code which I finally found after a long time of researching:

void SPI_SlaveInit(void)
{
  DDRB |= (1<<PORTB4);//Set MISO to output
  SPCR = (1<<SPE);
}

char SPI_SlaveReceive(void)
{
  char data;
  while(!(SPSR & (1<<SPIF)))
  {}
  data = SPDR;
  SPDR = 0;
  return data;
}

void SPI_SlaveSend(char data)
{
  SPDR = data;
  while(!(SPSR & (1<<SPIF)))
  {}
  SPDR = 0;
}

Saturday, 25 May 2013

ATMega - Control an ESC

(Deutsche Version) I recently bought an brushless motor and an electronic speed controller. In this post, I want to explain what kind of signal has to be applied to the ESC to move the motor.

An ESC is constructed like this: 3 cables for the motor, 2 cables the battery and 3 cables for the PWM signal. The motor cables can be connected to any motor cable, if the motor spins into the wrong direction, simply switch two cables.

PWM is the shortcut for pulse width modulation. This means, for a several amount of time, there is a high signal and then a low signal. This is often used to set the brightness of LEDs. At an ATMega, a counter counts to 255. If i set my PWM channel to some value, e.g. 150, the output will be high from 1 to 150 and then low until 255.

The three cables of the esc are black (connected to ground), red (not connected) and white (connected to the PWM output). To calibrate the motor, a sequence has to be executed:
a) Maximum power signal (PWM channel set to  250)
b) Minimum power signal (PWM channel set to 150)
Now the motor speed will be linear dependant on the length of the PWM, 150 equals no power and 250 equals maximum power. Here is the code for an ATMega8:


#include <avr/io.h>
#define F_CPU 16000000
#include <util/delay.h>

int main( void )
{
  DDRB=0xFF; //Define all PORTB-channels as output
  OCR2=250; //250 of 255, nearly all time high
  TCCR2=0x6D; //Frequency set to a bit more than 50 Hz

  _delay_ms(10000);//Wait until the motor starts beeping
  OCR2 = 150;((//150 of 255, this is the no-power signal
  _delay_ms(10000);
//wait until the motor is calibrated

  //Now the ESC is calibrated and can be used:
  //OCR2 = 150: Minimum power
  //OCR2 = 250: Maximum power

  OCR2 = 200;//Set to 50% power
  while (1)
  {}
}

Tuesday, 21 May 2013

Plant propagation of pinguicula and drosera

(Deutsche Version) Here is an image of my five pinguiculas:
In the upper left corner, there is the mother plant, in the upper right corner another half of this plant (it split up some time ago). The three large plants were grown by me via leaf cuttlings. The basic principle is simple: Tear out a leaf near the root and put in on humid earth (and wait).

Two important things are rarely mentioned:
a) The plant needs humid air. I therefore covered the whole plant with a plastic bag.
b) Light. Since I started to use a energy-saving lamp (6400 K, 5 W), the plants grow much faster.

In the other pots there is drosera. The propagation of drosera is similar to the propagation of pinguicula.

Monday, 20 May 2013

ATMega - Temperature measurement with the DS18S20

(Deutsche Version) To measure the temperature in my room, I use a DS18S20, this is a 3-pin temperature sensor (looks like a transistor) and an ATMega 8 to read the temperature digitally. The precision is ± 0.5 °C. The sensor has the following pins: Vcc(+5V), Gnd and the data pin. Further information about the communication is provided in the data sheet. In my application, PORTD0 is an output and PORTD2 is an input. Both pins are connected to the data pin of the sensor. Here is the code i use to read the temperature:

void on()
{
  PORTD |= (1 << PORTD0);
}

void off()
{
  PORTD &= ~ (1 << PORTD0);
}

void write(int hilow)
{
  if (hilow == 0)
  {
   off();
   _delay_us(80);
   on();
   _delay_us(20);
  }
  else
  {
   off();
   _delay_us(5);
   on();
   _delay_us(95);
  }
}

void read()
{
  off();
  _delay_us(2);
  on();
  _delay_us(98);
}

double read0()
{
  double retVal = 0;
  off();
  _delay_us(2);
  on();
  _delay_us(1);
  if (PIND & (1 << PIND2)) //HIGH
  {
    retVal = 1;
  }
  else
  {
    retVal = 0;
  }
  _delay_us(97);
  return retVal;
}


double measureTemp()
{
  double temp = 0;
  double fac = 0.5;
  double neg = 0;
  double value = 0;

  off();
  _delay_us(500);
  on();
  _delay_us(1000);

  write(0);
  write(0);
  write(1);
  write(1);
  write(0);
  write(0);
  write(1);
  write(1);

  _delay_us(100);
  //Start temperature conversion
  write(0);
  write(0);
  write(1);
  write(0);
  write(0);
  write(0);
  write(1);
  write(0);

  _delay_us(1000);

  off();
  _delay_us(500);
  on();
  _delay_us(1000);

  write(0);
  write(0);
  write(1);
  write(1);
  write(0);
  write(0);
  write(1);
  write(1);

  _delay_us(100);
  //read Temperature
  write(0);
  write(1);
  write(1);
  write(1);
  write(1);
  write(1);
  write(0);
  write(1);

  value = read0();

  temp += fac * value; fac = fac*2; 
  bits[0] = value;

  value = read0();
  temp += fac * value; fac = fac*2;
  bits[1] = value;

  value = read0();
  temp += fac * value; fac = fac*2;
  bits[2] = value;

  value = read0();
  temp += fac * value; fac = fac*2;
  bits[3] = value;

  value = read0();
  temp += fac * value; fac = fac*2;
  bits[4] = value;

  value = read0();
  temp += fac * value; fac = fac*2; 
  bits[5] = value;

  value = read0();
  temp += fac * value; fac = fac*2;
  bits[6] = value;

  value = read0(); 
  temp += fac * value;  bits[7] = value;
  //The last bit is irrelevant if count_remain is involved.

  neg = read0();
  read();
  read(); 
  read();
  read();
  read(); 
  read();
  read();

  //TH 
  read(); 
  read(); 
  read(); 
  read();
  read(); 
  read();
  read();
  read();

  //TL 
  read(); 
  read();
  read();
  read(); 
  read();
  read(); 
  read(); 
  read();

  //Reserved 
  read(); 
  read(); 
  read(); 
  read(); 
  read(); 
  read(); 
  read(); 
  read();

  //Reserved
  read();
  read(); 
  read(); 
  read(); 
  read(); 
  read(); 
  read(); 
  read();

  //COUNT REMAIN 
  count_remain[0] = read0(); 
  count_remain[1] = read0();
  count_remain[2] = read0();
  count_remain[3] = read0();
  count_remain[4] = read0(); 
  count_remain[5] = read0(); 
  count_remain[6] = read0(); 
  count_remain[7] = read0();

  //COUNT PER °C 
  count_per_c[0] = read0(); 
  count_per_c[1] = read0();
  count_per_c[2] = read0(); 
  count_per_c[3] = read0(); 
  count_per_c[4] = read0(); 
  count_per_c[5] = read0(); 
  count_per_c[6] = read0(); 
  count_per_c[7] = read0(); 

  //CRC 
  read(); 
  read();
  read();
  read();
  read(); 
  read(); 
  read(); 
  read();

  //You can increase the precision if you use count_remain

  return temp;

}


C# Windows Forms - Rotate image

(Deutsche Version) For Panzerkampf it is necessary to rotate an image. The solution which I use is:

private Image rotateImage(Image b, double angle)
{
  //create a new empty bitmap to hold rotated image
  Bitmap returnBitmap = new Bitmap(2*b.Width, 2*b.Height);
  //make a graphics object from the empty bitmap
  Graphics g = Graphics.FromImage(returnBitmap);
  //move rotation point to center of image
  g.TranslateTransform(center_x, center_y);
  //rotate
  g.RotateTransform(-(float)(angle*180.0/Math.PI));
  //move image back
  g.TranslateTransform(-center_x, -center_y);
  //draw passed in image onto graphics object
  g.DrawImage(b, new Point(0, 0));
  return returnBitmap;
}

Panzerkampf

(Deutsche Version) Some years ago, I programmed a game for Linux called Panzerkampf (translated: Tank fight). The basic idea was to navigate a tank via arrows/WASD through the environment and try to kill the other player. Sadly this project was limited by many factors: A single keyboard cannot handle enough keys so it could happen that one player could not shoot anymore.

About two weeks ago, I started to program the game again. The game is written in C# and can be played via internet or local area network. The network communication is running with sockets, here the basic code snippet:

Client:

System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
clientSocket.Connect("mein.server.de", port);
NetworkStream serverStream = clientSocket.GetStream();
byte[] outStream = System.Text.Encoding.ASCII.GetBytes("message to server");
//Nachricht an Server senden
serverStream.Write(outStream, 0, outStream.Length);
serverStream.Flush();
byte[] inStream = new byte[70000];
//inStream anpassen (größe an clientSocket.ReceiveBufferSize anpassen)
Array.Resize(ref inStream, clientSocket.ReceiveBufferSize);
//Nachricht vom Server empfangen
serverStream.Read(inStream, 0, clientSocket.ReceiveBufferSize);
string dataFromServer = System.Text.Encoding.ASCII.GetString(inStream);
clientSocket.Close();


Server:

TcpListener serverSocket = new TcpListener(port);
TcpClient clientSocket = default(TcpClient);
serverSocket.Start();
clientSocket = serverSocket.AcceptTcpClient();
NetworkStream networkStream = clientSocket.GetStream();
byte[] bytesFrom = new byte[10025];
Array.Resize(ref bytesFrom, clientSocket.ReceiveBufferSize);
networkStream.Read(bytesFrom, 0, clientSocket.ReceiveBufferSize);
string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
Byte[] sendBytes = Encoding.ASCII.GetBytes("message to Client");
networkStream.Write(sendBytes, 0, sendBytes.Length);
networkStream.Flush();
clientSocket.Close();


For the sounds I used sounds from freesound.org which are under the Creative Commons 0 license, I do not have to refer to the creators but I am not allowed to say that the sounds would belong to me.

The graphics are made with windows forms.

The download for the client is here: http://physudo.bplaced.net/Panzerkampf/publish.htm
And for the server here: http://physudo.bplaced.net/PanzerkampfServer/publish.htm

If anyone plays Panzerkampf, I would be glad to receive a small feedback (maybe as comment to this post)

LED-cube

(Deutsche Version) I recently built an LED-Cube. The Size is 4x4x4 and it consists of green LEDs.
 The cuve is controlled via an ATMega16A, this ATMega (contrary to the ATMega8) has enough output channels. PORTA and PORTC are used for the columns and PORTD3..6 for the layers.
Here you can see the ATMega. On the left, there are 4 NPN-transistors which are able to supply a whole layer with power (16 LED/layer at 20 mA = 320 mA). All LEDs above each other are connected via their anodes (+) and then are connected to one of the 16 output channels of the ATMega (PORTA + PORTC). The cathodes (-) are connected to each other within a layer and every layer is connected to the collector of one transistor. The base of the transistor is connected via a resistor to one of the PORTD-outputs. The basic programming concept is relatively simpel: Each layer has got a variable which saves the state of every LED (from 0 to FFFF). The layers are lighted after each other for 100 µs, this results in a repetition rate of 2.5 kHz which should be much higher than any flickering the human eye can detect.

Every LED has a piece of opaque white on the top, otherwise, every LED would light the LED above and it seems that both LEDs are switched on.

The next step should be some kind of implementation of SPI for being able to control the cube via a Raspberry PI.