Is it winter or not rather fall right now ? We do not know for sure right now . Weather is always an important topic for conversation. So we are going to be a professional weatherman – with Java, JavaFX and Tinkerforge.
To be able to make a statement about the weather we do not need a poor frog that is confined in a glass and climbs up and down a ladder every once in a while. We can also build a frog-friendly weather station.
The components
==============
For our weather station we need the following ingredients today: a temperature-, a light- and a barometer sensor. This way we are able to measure the air pressure, the barometric height and the luminosity (in Lux).
The temperature-(Link) and barometer sensor(Link) was already part of the previous articles. New for us today is the light sensor. Additionally we are using an 20×4-LCD-display.
The Light Sensor
========================
The light sensor (Ambient Light Sensor) is able to measure the brightness of the environment and provides the value in Lux. The resolution (0 Lux-900 Lux) is 0,1 which equals 12 bit. With approximately 2 g the light sensor is one of the lightest and uses only 1 mAt.
The Weatherstation
==================
As always (see previous articles) all elements are plugged into the master and shortly after they appear on the display of the BrickViewer. Note: Do not forget to update your sensor software if a newer version is available.
All sensors can be used with the Java-Interface which provides the usual ActionListener concept. Additionally every sensor can be requested directly. But keep in mind that this method leads to an unnecessary high bandwidth. The sensorsin this example are programmed without JavaFX because this time no visualization with LineChart is shown. As an example in listing 1: the barometer. Instead of the JavaFX-Elements this time we write directly into the 20×4-LCD display. So far nothing special.
The 20×4-LDC-Bricklet
=========================
The LCD brick let contains 20×4 dot matrix inclusively a blue background lightning. Additionally there are four buttons at the lower edge of the board. The API enables us to directly write individual characters or whole rows up to a length of 20 characters. The background lightning can be switched on and off and also the state of the buttons can be read. With this LCD bricklet the data of the connected sensors are displayed. Every sensor value is assigned an own row. In our example the following rows are assigned as follows:
- Temperature in degree Celsius
- Air pressure in Millibar
- Barometric height in meters
- Luminosity in Lux
The advantage with the implementation of the 20×4-LCD display is that it is Thread-safe (italic). The brick let can be provided with data competitively by different threads without having to expect problems. The dispribution of the rows simplifies the programming a lot since only the particular sensor is responsible for the clear display of its own row. Since here for reasons of simplification per sensor unit only one instance of the LCD brick let is generated also the initialization is scrolled through multiple times. For our example this is sufficient for now.
In order to show the requested characters on the LCD-bricklet they first have to be converted in a bit pattern. For this purpose the method String utf16ToKS0066U(String utf16) (also contained in the exemplary source code) can be extracted from the TinkerForge documentation. After the coding the text of max. 20 characters per row can be displayed with the method writeLine() (Listing 2)
Listing 3 shows the complete implementation of the class LCD20x4 (italic).
The usage
——————————
The implementing happens in the main-method of the class WeatherStation (Listing 4). All sensors are started in a separate thread. In order for the program to not be terminated ahead of time we wait on the command line as long as the user types in an „O“ followed by ENTER.
Summary
————————
In this version the weather station is still connected to the computer via USB. In one of the next articles I am going to show how the weather station can be decoupled from the PC. It then will be possible to run the weather station e.g. outside in a dry place with power supply and still get the evaluation comfortably at the computer in JavaFX. For ideas, suggestions or questions please just contact me on Twitter: @SvenRuppert
Stay tuned. Happy coding!
The source code to this article can be found under
https://bitbucket.org/rapidpm/jaxenter.de-0012-iot-tinkerforge .
For people who are interested in more complex examples I recommend the following
https://bitbucket.org/rapidpm/modules .
Blog author
Sven Ruppert
Do you still have questions? Just send me a message.
Do you still have questions? Just send me a message.