### Use Remote Switch Bricklet

Example on how to Home Automate using Tinkerforge Bricklets and B4J.
Project
Switching devices remote using the Tinkerforge Remote Switch Bricklet controlled by a Raspberry Pi (RPi) Server running a B4J WebApp.
Description
Background
TinkerForge (TF) is an open source hardware platform of stackable microcontroller building blocks (Bricks)
that can control different modules (Bricklets).
The hardware can be controlled by external programs written in various languages, like C, Delphi, Java etc.
Visit their homepage http://www.tinkerforge.com .
Idea
To use a RPi B4J Webserver to control Tinkerforge Bricklets.
At this point in time (May 2014), I have not found ready available solutions on how to control TF Bricklets.
There might be a variety of solutions - but has to be explored.
I started a solution based upon the TF JavaScript API.
See http://www.tinkerforge.com/en/doc/Software/API_Bindings_JavaScript.html for information.
The Remote Switch Bricklet to switch two lamps on/off is used.
See http://www.tinkerforge.com/en/doc/Hardware/Bricklets/Remote_Switch.html for information.
See http://www.tinkerforge.com/en/doc/Software/Bricklets/RemoteSwitch_Bricklet_JavaScript.html#remote-switch-bricklet-javascript-api for API description.
This is a starting point to build a Home Automation Solution.
How it works?
The TF Bricklets are connected to the TinkerForge Master Brick, which is connected via USB to a RPi Model B or B+.
The RPi functions as a Home Server (RPi Home Server) , which can be accessed from the Internet using Websockets and ssh.
The Home Router uses Port Forwarding for DuchDNS dynamic hosting (duckdns.org).
The TF Brick Daemon (brickd) runs as a service on the RasPi Home Server, listening to its Websocket port = waiting for the B4J Webserver requests.
The B4J Webserver runs also on the RPi Home Server, listening to its Websocket Port = waiting for client requests.
The Webclient (Browser has to support Websockets) accesses via DOMAIN:PORT the B4J Webserver running on the RasPi Home Server. Based on the selected function, the
respective HTML page is loaded with Tinkerforge HOST, PORT, Bricklet UID parameter set.
These parameter are used to access the Tinkerforge Brick Daemon (brickd), which then feeds the relevant Bricklet.
The TF BrickViewer can be used to verify if the Master Brick and the Brickets are working properly.
It is required to obtain the unique Bricket UID as required to control.
Configuration
RPi:
The TF Software to be configured on the RPi server according http://www.tinkerforge.com/en/doc/Embedded/Raspberry_Pi.html .
Set the Brick Daemon (brickd) Websocket port to 4280 in .etc/brickd.conf (use nano to edit).
Then restart brickd.
Note:
For security reasons it is advised to set the authentication also (see brickd.conf for information).
Build the B4J Webserver
Read tfcontrol.b4j, tfcontrol.bas and www/index.html for detailed information.
The solution is build upon the Tinkerforge JavaScript API (tinkerforge.js), which is stored in www/tinkerforge.
The Brickets are accessed by using JavaScript functions from tinkerforge.js.
The main HTML file is index.html located in the www folder.
jQuery Mobile (jQM) Interface is used to enable access from any device via browser.
The jQM files are stored in www subfolders
Buttons defined:
• About: Information about this WebApp.
• Flip Switch A and Flip Switch B to remote control two devices - for tests lamps used.
Install the B4J Webserver
To install the B4J Webserver on the RPi Home Server, login as Pi and create a folder tfcontrol with subfolder www. Recommended is /home/pi/b4j/tfcontrol.
Copy the tfcontrol.jar and the www folder from your develop machine to the RasPi Home Server (Hint: use WinSCP) in the recommended folder /home/pi/b4j/tfcontrol.
Start tfcontrol using a terminal on the RPi server: java -jar tfcontrol.jar from /home/pi/b4j/tfcontrol.
To start tfcontrol as a daemon at RPi boot, read the readme.txt in the www\sh folder.
Access the B4J Server
Start the Browser (which has to support Websockets) and call URL YOURDOMAIN.duckdns.org:YOURPORT.
This WebApp has been tested from several devices, like iPhone, Android Smartphone & Tablet), PC, Raspberray Pi.
Tools used
• B4J to develop the Webserver tfcontrol.jar.
• Putty to remote control the Webserver tfcontrol running on the RasPi server (start / stop tfcontrol.jar during tests, as user Pi).
• WinSCP to copy files (jar, www folder) from the development pc to the raspi server (as user Pi).
• UltraEdit to edit the html, css files etc..
Additional Notes
• This WebApp requires B4J v2.2 or higher, Additional Libraries: jServer.
• Installing on a webserver ensure to copy the www folder content. This folder contains all html related files including jQM.
• Logging is done extensively using Sub AppLog; set flag CAPPLOG (teem.b4j) to false to turn logging off.
• Ensure case sensitivity. Best to keep all file & folder names to lowercase!
• To learn more, read the readme and source files. These are well commented.
• It is adviced to use additional authentication like login screen and set TinkerForge Deamon Authentication.
Installation
Unpack the zip file and load the b4j file in B4J.

Changed: 2018-06-07 09:24, Category: TinkerForge, File: T6C264M1969.htm, ID: 1969