Send and receive

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

The last days I’ve been playing around with my JeeLink and a JeeNode including three sensors. I made a JeePlug with the TMP36, an LDR and a TSOP infrared receiver, which I can plug into my JeeNode. So I don’t have to plug everything in a breadboard and connect the wires, just place the JeePlug on top of my JeeNode in ports 1 and 4. I use port 1 for the temperature (TMP36) on the analog pin and I use the analog pin of port 4 for the LDR. The TSOP is connected to the digital pin of port 4 which leave the digital pin of port 1 empty to be used in the future.

The goal is to create a WSN based on several JeeNode and a Jeelink as central node. The JeeLink/JeeNode have a RF12Demo sketch with which it is possible to easily create a mesh network. The JeeLink is connected to a computer through USB. JeeNodes can communicate wirelessm using the on board RF12 chipset. All you have to do is configure the two units s that they can ‘find’ each other. See POF03 @ JeeLabs for an instruction on how to do that.

After I made the setup of my nodes and got the sketch started I saw data being displayed in my serial monitor. The JeeNode (sensor node) displayed information about the light and temperature on screen and sent the data to the JeeLink. I saw valid messages in the serial monitor of the JeeLink. The data shown by the JeeLink was something like:

OK 33 103 113 175 65

This line contains the measured temperature sent by the JeeNode. I knew that the value measured and sent was related to 21.93 degrees Centigrade. So somewhere in the sequence could be found. When you take a look at the Arduino Reference page about float (floating-point variables) you find that a float is represented by 4 bytes. So 4 of the 5 numbers in the message will together form the floating point number.

I wanted to know what 4 bytes were sent by the JeeNode so I did put in some extra Serial.print statements to display not only the measured temperature of (in this case) 21.93 degrees but I also the seperate bytes. To do this I had to ‘convert’ the float into an array of bytes. The lines below show you how:

float temperature;
byte  *ArrayOfFourBytes;

temperature = 21.93;

ArrayOfFourBytes = (byte*)&temperature;

Serial.println(temperature); // print the temperature

Serial.println(ArrayOfFourBytes[0],BIN); // print the 'first' byte in binary notation
Serial.println(ArrayOfFourBytes[1],BIN); // print the 'first' byte in binary notation
Serial.println(ArrayOfFourBytes[2],BIN); // print the 'first' byte in binary notation
Serial.println(ArrayOfFourBytes[3],BIN); // print the 'first' byte in binary notation

The printed binary numbers were:

1100111
1110001
10101111
1000001

In decimal notation equal to 103, 113, 165 and 65 which are the last 4 numbers of the received message. This still doesn’t look like 21.93 but we’re getting there.

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

A float is represented by 4 bytes which are equal to 32 bits. The printed binaries above are not all 8-bits but three of them are 7-bits. This is because the preceding zero’s are left out and you have to place them yourself in front of the 7-bits.
Al together these 32-bits give you three pieces of the puzzle; a sign, a exponent and a mantisse.

The formula for a IEEE 754 float is:  sign * 2exponent * mantissa  , where sign is +1 or -1

There are many of pages that explain more about floating-point numbers and their representation in bits, together with formulas. You can also find websites with converters that let you enter a number in the different representations and show that number in a binary, hexadecimal, float or decimal representation. I used this website.

I entered 21.93 in the converter and got 01000001 10101111 01110000 10100100 (in bytes this is equal to 65 175 112 164). This isn’t equal to 103, 113, 165 and 65; except for the 65 which might be a coincidence or not.
When I entered the original bit sequence with preceding zeros 01100111 01110001 10101111 01000001 I got the float number 1.1413232E24 and not 21.93.

I know that there are more possibilities of ordering and combining bytes to get e.g. a float. You have big-endian and little-endian (see Wikipedia about Endianness). This has a relation with the hardware platform you are using.

So I tried the sequence: 01000001 10101111 01110001 01100111 (which takes the ‘last’ byte first, then the third, second and first byte). The converter now gave me the floating-point number  21.930372 which is not exactly 21.93 but close enough to continue.

The explanation for the difference between 21.93 and 21.930372 can be found on the Arduino Reference page about Serial.print. Where the second example shows: Serial.print(1.23456) gives “1.23”, in other words the float is rounded or capped to two decimals.

This difference also explains the difference in bit sequence returned by the converter.

Knowing this I can continue sending, receiving and translating data to build my own WSN solution with central reporting functionality.

 

Serial communication between PC and Arduino

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

Using sensors together with the Arduino or JeeNode is cool. Using the serial monitor of the Arduino IDE lets you see the values. The data is sent from the Arduino over (in my case) a USB cable to my PC. With the serial monitor of the IDE I can receive and also send data.

To make a domotica solution I need something more than the serial monitor of the IDE. I want to be able to send and receive data from my own application and website.

I’m want to use Java to develop the application that’s going to send and receive data. Although I might also try C/C++ in a while, but for now I’ll focus on Java. The reason I want to use Java is that I’m using a Windows computer to develop, but I might want to use a Linux based device to let my application run and do its job. Hopefully it will be easier to switch between the two operating systems with an application written in Java.
(The platform I’m thinking of is the Raspberry Pi; hopefully soon to be ordered)

Searching for a good way to start writing code to communicate with an Arduino I found this page on the Arduino website and also another interesting website. (I did find this site about C and Arduino which might be useful in the future; and this one is I think about Processing and Arduino)

It turns out that by using the RXTX Java library it is possible to use serial communication to send and receive data between a PC and an Arduino. The page shows a code example of a Java app which is able to receive data from a serial port (COM port; in PC terms).

I chose Eclipse as my Java IDE (the SpringSource version to be exact). I had to download the 2.2pre version of the RXTX Library, because my Windows is a 65-bit version. In the 2.2pre package a 64-bit version of the rxtxSerial.dll is included. The 32-bit version will not work on a 64-bit OS, as I found out by trial-and-error. (I didn’t read through the entire page on Arduino.cc otherwise I would have seen the instruction)  Beware that when you use the 2.2pre .dll file you also need to use the 2.2pre RXTXcomm.jar file in your classpath. If you don’t a version mismatch error will be shown.

The steps I took for my basic serial communication test application were:

  1. copy the rxtxSerial.dll to c:\windows\sysWOW64
  2. add the c:\windows\sysWOW64 to your path
  3. added the location of the RXTXcomm.jar file to the classpath in Eclipse
  4. created a new Java project;
  5. added a new file to the \src of the project
  6. named it SerialTest.java
  7. copied the code snippet from the Arduino page (file is attached to this post)
  8. change the COM port to COM5
  9.  changed the bit-rate in the Java code (I needed 57600 for my JeeLink with RF12Demo)
  10. build en run

I saw the expected text from the RF12Demo in the console window of Eclipse.

The first step is done, data is transferred from JeeLink (Arduino) to a ‘self made’ application. The next step is, of course, to send data to an Arduino. I’ll let  you know whta my findings are.

The first of many steps to my own Java Domotica-bridge application.