Remote Hardware Control via Mobile App.
Blynk was designed for the Internet of Things. It can control hardware remotely, it can display sensor data, it can store and visualise data.

In this lab you will:
You will need the Blynk app on an Android or IoS device such as a Smart Phone or Tablet. If you don't have a device available to do this, you can emulate Android on your desktop.
Install the Blynk App for your Smartphone (or Tablet) from here.
Use the App to do the following:
Raspberry Pi 3 as the board and the type of network connection you use with the RPi (either Wifi or Ethernet).
We will use Node Blynk library to control the RPi.
(BTW: If node is already installed and you don't want to remove it, you can try to proceed to the next step and see if it works...)
sudo apt-get purge node nodejs node.js -y
sudo apt-get autoremove
curl -sL "https://deb.nodesource.com/setup_6.x" | sudo -E bash -
sudo apt-get install build-essential nodejs -y
sudo apt-get install npm
mkdir blynk-lab
npm init
You will be asked a few questions. Fill them in as you see fit - if you are unsure of any lust hit enter to use the default value.
sudo npm install blynk-library --save
sudo npm install onoff ---save
blynk-lab directory, create a new file called index.js with the following content:var Blynk = require("blynk-library");
var AUTH = 'YOUR-AUTH-TOKEN';
var blynk = new Blynk.Blynk(AUTH);
var v1 = new blynk.VirtualPin(1);
v1.on('write', function(param) {
console.log('V1:', param[0]);
});
The above script creates a Virtual Pin on your Raspberry Pi. You can use this to interface, display and send data with your Blynk app on your phone.
node index.js at the command line. You should see the following output:
Leave the app running on the RPi for the following section.
So now lets send commands from your phone to the Raspberry Pi. Open Blynk-lab1 project on your mobile device and do the following
+ icon in the menu to open the Widget box
Button and it should appear on your projectLightPIN and change it to Virtual V1Switch
Now hit the back arrow to return to the Project.
Click the Run Button to start the app on the phone.
Light button a few times, it will toggle between on and off.
You're now controlling tshe virtual pin from your phone from anywhere via the internet. To confirm this, you can turn off the Wifi on your phone and use the 3G/4G data from your phone network - it should still work.
Next let's hook up the SenseHat to the virtual pin.
ctrl-c at the command line.node-sense-hat module to your projectnpm install node-sense-hat --save
index.js to the following:var Blynk = require("blynk-library");
var sense = require("node-sense-hat");
var AUTH = 'YOUR-AUTH-CODE';
var blynk = new Blynk.Blynk(AUTH);
var v1 = new blynk.VirtualPin(1);
var white = [255, 255, 255];
sense.Leds.clear();
// v1 write call back
v1.on('write', function(param) {
console.log('V1:', param[0]);
if (param[0]==1){
sense.Leds.clear(white)
}else{
sense.Leds.clear();
}
});
The updated code now includes the following; initialises the SenseHat with sense.Leds.clear() and sets the LEDs to white when the virtual pin, v1, is set to 1. Every time the button is pressed in the phone app results in the callback function for the write event to execute. Compare this to how you handled Python callbacks in the MQTT lab.
Lets replace the button with a more interactive way of conrolling the LED matrix.
Delete option.Widget Box and add the zeRGBra widget. Your app should look like this:

index.js on the RPi, replace the v1.on('write'... call back with the following code:// v1 write call back
v1.on('write', function(param) {
var colour = param.map(Number);
sense.Leds.clear(colour);
});
The above write callback converts the array of rgb values written by the zeRGBra widget to numeric(integers) and passes the values to the sensehat leds.
Stop the app on your phone by clicking on the Stop icon button in the menu bar.
Click on the + icon in the menu to open the Widget box and add a Value Display widget.
Double tap on the Value Display widget and configure the widget to use virtual pin V2 and read the temp value every 5 seconds as follows:

index.js with the following code:var Blynk = require("blynk-library");
var sense = require("node-sense-hat");
var imu = sense.Imu;
var IMU = new imu.IMU();
var AUTH = 'YOUR-AUTH-CODE';
var blynk = new Blynk.Blynk(AUTH);
var v1 = new blynk.VirtualPin(1);
var v2 = new blynk.VirtualPin(2);
var white = [255, 255, 255];
sense.Leds.clear();
// v1 write call back
v1.on('write', function(param) {
var colour = param.map(Number);
sense.Leds.clear(colour);
});
v2.on('read', function() {
IMU.getValue(function (e, data) {
v2.write(data.temperature);
})
});
The updated code "listens" for reads events on virtual pin V2, gets the SenseHAT temperature value, and returns the temperature value to pin V2.
node index.js.Blynk-lab1 app on your phone again by tapping the Run icon. It will now read the value of the virtual pin V2 every 5 seconds and display the returned value.NOTE: The following sections were tested using an Android Device (Alcatel A3). Blynk is designed to be cross platform but some variations in operation, particularly when integrating with phone sensors, may exist on different phone models. You can try this section on an emulated smartphone.
Blynk has lots of widgets for various uses. The phone app also allow you to access the phones sensors such a light sensor, accelerometer. The next example will use use a webhook to push light sensor data to Thingspeak for analysis.
Light Sensor to your app (you'll need to scroll down to find it).Double-tap on the light sensor widget to open the settings and configure as follows:
Run the Blynk app on your phone, you should see lux (light measurement) values for light intensity in the widget.
index.js:v3 variable and a variable to record the last button statevar v3 = new blynk.VirtualPin(3);
V3 at the end of index.js to handle Virtual Pin 3 (i.e. the one that's linked to the light sensor)v3.on('write', function(param) {
//check if it's too dark!
if (param[0]<50){console.log("It's a bit dark")}
//You could do something interesting here like turn on lights!
});
The Blynk Webhook widget allows you to communicate with 3rd party services. With Webhook widget you can send HTTP(S) requests to any 3rd party service or devices that has HTTP(S) API (e.g. other smart devices with a Web API, Thingspeak, Smart Lights).
Go to Thingspeak and, if you haven't already, create a new channel for Temperature data. You can use one from previous labs if you want. (See last weeks lab/tutorial)
Retrieve your Write API Key for the channel you wish to use.
Webhook widget to your app (you'll need to scroll down to find it).For this part, we will want to monitor virtual pin V2 and send the data to Thingspeak.
Double tap the Webhook widget and configure as shown below:

The URL field should be filled in as follows:
https://api.thingspeak.com/update?api_key=YOUR_API_WRITE_KEY&field1=/pin/
Now, every time there is a “write” command to V2 pin on the Raspberry Pi the Webhook will be triggered and write the value (in this case the temp) into the URL using the /pin/ placeholder.
See here for more details

In this section we will use Blynk's GPS streaming feature to push the phone location to Wia. As we're working with Javascript, you'll need to install the Wia node module. Run the following command in your project directory.
npm install wia --save
light sensor widget. This will give you enough energy for the GPS widget!Widget Box abd add the GPS widgetV4 as the output.
+ symbol and the Add New Device wuindow will appear.Enter 'Location Device' as the name and click Add Device.

Click on View device to go to the device's overview page. Take a note of the device's secret key, you'll need this later.
If running, Stop the RPi Node app.
Open index.js in an editor and add the following line of code to the top of the file. Replace the device-secret-key on with your device's secret key.
var wia = require('wia')('YOUR-DEVICE-SECRET-KEY');
Wia using MQTT.v4.on('write', function(param) {
console.log("v4: lat. " + param[0])
wia.locations.publish({
latitude: param[0],
longitude: param[1]
});
});
wia.stream.connect();
Now run both the RPi Node app and the Phone App. Make sure the location/GPS is switched on.
Open Wia in a browser and go to the Locations tab for your device. You should start to see your location appear in the Wia platform.

Overview tab in add a Map widget by clicking the Add a Widget button. 
