App My Pi – AppMyProduct Heat Control Demo on Your Raspberry Pi

Have you ever wanted to connect and remote control an embedded device in real-time, like for example your Raspberry Pi? Are you concerned about the security of your communication, but still want to quickly develop high-quality apps? With our IoT application platform AppMyProduct we provide the solution for you!

To give you an impression, we created a Heat Control Demo (app+device source code available on GitHub). This article describes all necessary steps to install and run the demo application on your Raspberry Pi and control it with a smartphone app for iOS/Android – and with a little experience (or some documentation digging) with both HTML5 and Cordova you can extend the demo to whatever RPI remote control purpose you can think of.

rpi-control

Also check out the demo running on a STM32 with FreeRTOS or low-cost ESP8266 WiFi module.

Setup Raspberry Pi

If you already have a Raspberry Pi with working Internet connection, you can skip this step.

Get the Raspbian operating system (lite or with PIXEL) from the official website. Copy the downloaded image to a SD card as described in the documentation.

Use an Ethernet cable to connect your Raspberry Pi to the network, or follow this guide to setup wireless networking.

Install the Heat Control Demo

The device software of our AppMyProduct Heat Control Demo is available on GitHub. The following steps walk you through installing it on your Raspberry Pi.

Build

This step requires git and cmake. I you haven’t installed them already, you should do that now:

sudo apt-get install -y git cmake

To get the source files, clone our GitHub repository and enter the directory:

git clone --recursive https://github.com/nabto/appmyproduct-device-stub.git
cd appmyproduct-device-stub

Create a build folder and enter it:

mkdir build
cd build

Build the demo application:

cmake ..
make -j3

Move the built executable to your home directory:

mv amp_device_stub ~/

Finally, go back to your home directory:

cd

Create Run Script

The demo application takes a couple of parameters, most importantly the unique device id and a licence key. You can obtain both from portal.appmyproduct.com.
You can try to run the demo with the following command (use your parameters):

sudo ./amp_device_stub -d 7pbugghs.smtth.appmyproduct.com -k 985ef2a3de0fe5328cf7c1923b13cbef -N 'RPI' -P 'Raspeberry Pi'

Abort the demo with Ctrl-C.

For a more convenient usage, we create a startup script:

nano run_amp.sh

Copy the following code into the editor (use your parameters):

#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
$DIR/amp_device_stub -d 7pbugghs.smtth.appmyproduct.com -k 985ef2a3de0fe5328cf7c1923b13cbef -N 'RPI' -P 'Raspeberry Pi'

Save and close nano with Ctrl-X and confirm with Y and Return.

Make the script executable with:

chmod +x run_amp.sh

Try to run the demo application again using the startup script:

sudo ./run_amp.sh

Run on Startup

If you want the demo to run automatically whenever the Raspberry Pi starts up, open /etc/rc.local with nano:

sudo nano /etc/rc.local

Insert the following line above “exit 0”:

stdbuf -oL /home/pi/run_amp.sh > /tmp/amp.log 2>&1 &

This runs the startup script in background and logs all outputs to /tmp/amp.log.

To test it, reboot you Raspberry Pi using:

sudo reboot

After the reboot, you should be able to see the amp_device_stub process running in the background. Check with:

ps | grep amp_device_stub

You can also see the log file using:

cat /tmp/amp.log

Example log:

11:12:19:331 unabto_common_main.c(127) Device id: '7pbugghs.smtth.appmyproduct.com'
11:12:19:331 unabto_common_main.c(128) Program Release 123.456
11:12:19:331 unabto_app_adapter.c(698) Application event framework using SYNC model
11:12:19:331 unabto_context.c(55) SECURE ATTACH: 1, DATA: 1
11:12:19:331 unabto_context.c(63) NONCE_SIZE: 32, CLEAR_TEXT: 0
11:12:19:331 unabto_common_main.c(206) Nabto was successfully initialized
11:12:19:331 unabto_main.c(85) AppMyProduct demo stub [7pbugghs.smtth.appmyproduct.com] running!
11:12:19:331 unabto_context.c(55) SECURE ATTACH: 1, DATA: 1
11:12:19:331 unabto_context.c(63) NONCE_SIZE: 32, CLEAR_TEXT: 0
11:12:19:331 unabto_attach.c(792) State change from IDLE to WAIT_DNS
11:12:19:331 unabto_attach.c(793) Resolving dns: 7pbugghs.smtth.appmyproduct.com
11:12:19:341 unabto_attach.c(814) State change from WAIT_DNS to WAIT_BS
11:12:19:402 unabto_attach.c(479) State change from WAIT_BS to WAIT_GSP
11:12:19:412 unabto_attach.c(266) ######## U_INVITE with LARGE nonce sent, version: - URL: -
11:12:19:453 unabto_attach.c(580) State change from WAIT_GSP to ATTACHED

Use it!

First, install the AppMyProduct Heat Control Demo on your smartphone (Apple App Store / Google Play / Android APK / Source).

Search for devices in your network. You should see your Raspberry Pi:

rpi-discover

After pairing, you can control the simulated heat pump:

rpi-control

We added a little gimmick for the Raspberry Pi: The device application controls the green on-board LED! Its blink frequency reflects the currently set target temperature. If you deactivate the heat pump, the LED is turned off. (On some Raspberry Pi versions the LED is inverted, hence the LED is permanently turned on in that case.)

Factory Reset

The Heat Control Demo application saves all settings and paired devices in a file called persistence.bin located in the same directory (your home directory in this case). In order to reset these to default, delete the file with

sudo rm persistence.bin

and restart the demo, e.g. by rebooting the Raspberry Pi:

sudo reboot

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s