Main Site Documentation

Need some conversion help


#1

Guys,

I was able to measure the protocol for Chacon remote power plugs. But I can’t figure out how to convert the key codes to binary. Included is a screenshot of the protocol. How can I convert the codes in the 1st column (A1, A2, …, C3, C4) to one of the colored codes. The second colored codes is what I actualy need.

The purple column is easy, 20 = On, 22 is Off.
In case of A1, how can I convert A to 000101 or 022 and convert 1 to 00010101 or 0222

I think i need to look at shifting the bits around but i can’t figure it out.

If my question is not clear enough, please say so and i’ll try to explain further.

Thanks in advance.


#2

Eric,
it looks to me like they use the the Big Endian system.
All you need to do is read the bits ( in pair) from the left to the right.

e.g.

01 read from left to right will become 10 (put the first bit to the right, according to the normal bit order)

and 10 in binary = 2 in decimal


#3

Sam, I don’t get it.

my question is how to translate codewise the following:

A => 000101
B => 010001
C => 010100
1 => 00010101
2 => 01000101
3 => 01010001
4 => 01010100

I could also hardcode this, but that doesn’t seem the right thing to do


#4

I am not sure I understand what you need :frowning:


#5

Correct me if I am wrong.

  1. You have a string “A1”, “A2”, etc
    2.You want a method that will give you a numeric value that is produced from the string using format described in the colored table.
  2. The numeric value uses 24 bit
  3. The table describes the format starting with less significant bit on the left

Is that right?


#6

hm, lemme try to explain again.

in order to turn on/off the remote power plugs i need to send a bitstream over RF.
To turn on plug A1 i need to send 00 01 01 | 00 01 01 01 | 01 01 01 | 01 01
The code is devided in 4 parts
00 01 01 is A
00 01 01 01 is 1
01 01 01 is fixed (changes never)
01 01 is Turn On (01 00 is turn Off)

I need to find a way to translate A into 00 01 01 and 1 into 00 01 01 01 (and the same for B, C and 2,3,4)

I want to be able to do something like this:


RF433 rf = new RF433(CPU.Pin.xx);
rf.transmit('A', 1, true); // Turn plug A1 On
thread.sleep(5000);
rf.transmit('A', 1, false); // Turn plug A1 Off

I’m doing something similar to this: http://www.fezzer.com/project/127/wireless-433mhz-controller-for-kaku-devices/ but the protocol is different.


#7

Maybe a lookup table? or a use a switch statement?


#8

This might be a better way to explain the chart better.

All you need to do is
Here is the psudo code

read in the byte (a pair of bits 1 and 2, 3 and 4, 5 and 6, ... etc) from left hand side.
{
        Swap place between high order to low order bit in each byte.
        {
               Convert the Binary value to Decimal 
               Place the result to the array somewhere
         }
}

Hope this help.


#9

Architect:

[quote]1. You have a string “A1”, “A2”, etc
2.You want a method that will give you a numeric value that is produced from the string using format described in the colored table.
3. The numeric value uses 24 bit
2. The table describes the format starting with less significant bit on the left
[/quote]

That’s the way I interpret the chart as well! :wink:


#10

Sam,

the lines you added to the chart makes perfectly sense and figured that out myself too.
The step i can’t figure out is from the first column (containing A1,A2,A3…C4) to the first orange and blue column.

As said before, I can hardcode these, but there has to be a neater way.

Architect, that is correct.


#11

Here it is:


        static UInt32 MakeTelegram(char Address, Int16 DeviceId, bool OnOff)
        {
            UInt32 Telegram = 0x00000154;

            if (OnOff)
                Telegram |= 0x00000001;

            int addressIndex = (67 - Address) << 1;

            int addressBits = 0x15 & ~(0x01 << addressIndex);

            int deviceIndex = (4 - DeviceId) << 1;

            int deviceBits = 0x55 & ~(0x01 << deviceIndex);

            Telegram |= (UInt32)(deviceBits << 10);
            Telegram |= (UInt32)(addressBits << 18);

            return Telegram;
        }


#12

Architect, would you be so kind to comment that code so I can also learn from it?

Thanks


#13

I’ve done it same way as in Kaku example that you have mentioned. So the on/off bit is bit 0.It wouldn’t be hard to reverse it. You can always use Kaku send method which will work as is.


#14

Here it is:


        /// <summary>
        /// 0 h 0 h 0 h 0 h  0 d 0 d 0 d 0 d  0 1 0 1 0 1  0 1 0 c
        /// </summary>
        /// <param name="Address">House code: 'A', 'B'  or 'C'</param>
        /// <param name="DeviceId">Device: 1-4</param>
        /// <param name="OnOff">true - device is on</param>
        /// <returns>32bit value. Only first 24 bits are used</returns>
        static UInt32 MakeTelegram(char Address, Int16 DeviceId, bool OnOff)
        {
            //Default value that is not changed
            //This the 'fixed' part of the table plus first 2 bits of On/Off part which are 01 always 
            UInt32 Telegram = 0x00000154;

            //Turn first bit based on the state value
            if (OnOff)
                Telegram |= 0x00000001;

            //Calculate shift value for address
            //'A'= 65 so the shift value will be 4
            int addressIndex = (67 - Address) << 1;

            //We take default value for address portion which is 0b010101=0x15
            //and turn off the bit based on address shift value:
            int addressBits = 0x15 & ~(0x01 << addressIndex);

            int deviceIndex = (4 - DeviceId) << 1;

            //Similar to address above.
            //Default value is 0b01010101=0x55
            int deviceBits = 0x55 & ~(0x01 << deviceIndex);

            //Finaly add everything together
            Telegram |= (UInt32)(deviceBits << 10);
            Telegram |= (UInt32)(addressBits << 18);

            return Telegram;
        }

Cheers,
V


#15

Architect,

it doesn’t work.

I too wrote a RF433 class (http://www.fezzer.com/project/61/rf433-mhz-/) using that class in the following way works:


rf.TransmitCode("FF0FF","0FFFF",but1State); //device C3 => 220220222 + 20 for off or 22 for off but thats handled by the but1state 


#16

All it does is produce the value according to the table you have provided.
I have tried it with ‘C’, 3, true.
I got back:
5326165

which is in binary:

010100 01010001 010101 0101
C 3 On

Cheers,
V


#17

Most probably the timing is not correct (using the send_KaKu_telegram method)


#18

Yes, probably. You will need to “adopt” it. Just remember that if protocol requires address portion first, than you have to start transmitting bits from bit 24 like Kaku does if not than bit 0 is first.


#19

Thanks for the code snippet Architect!

Now, I know why you named yourself “Architect”!! :smiley: :smiley:


#20

You are welcome!!! ;D