Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

UNIVERSITY OF CAGLIARI

DIEE - Department of Electrical and Electronic Engineering

Blynk platform

Claudio Marche
claudio.marche@outlook.it
Claudio Marche
Blynk platform
1/34
http://mclab.diee.unica.it
Summary
• The Blynk IoT platform

• First app + PWM

• Virtual pins

• Sending data to app

• Receiving data from app


Blynk platform
Claudio Marche 2/34
http://mclab.diee.unica.it
Blynk platform (1)
• Founded in 2014, Blynk is a private IoT Company that
offers an IoT Platform-as-a-Service
• It is possible to use his own server freely with some
limitation (limited “energy”) or build up your own
Blynk server with no/configurable limitations
-> Help and tutorials: https://docs.blynk.cc/

Blynk platform
Claudio Marche 3/34
http://mclab.diee.unica.it
Blynk platform (2)
To work with Blynk, you need:

A smartphone An iOT Hardware Internet


enabled (Arduino board / Connection
(iOS – Android) ESP8266, etc) (wifi, serial, ETH…)
Blynk platform
Claudio Marche 4/34
http://mclab.diee.unica.it
Blynk platform – Mobile (1)
With the Blynk app it is possible to build
visual interfaces without coding!
• Download the Blynk App form
iOS/Android market and launch it

• If there is no account created, tap on


“Create New Account” otherwise tap on
“Login”
Blynk platform
Claudio Marche 5/34
http://mclab.diee.unica.it
Blynk platform – Mobile (2)
• Fill the “Email” field with a valid email (it
will be your login username)
• Fill the “Password” field
• Tap on “Next” button

Blynk platform
Claudio Marche 6/34
http://mclab.diee.unica.it
Blynk platform – Mobile (3)
Done! Ready to build the first interface!
• Logout button
• Scan for a prebuilt shared interface

• Tap on “New Project”

Blynk platform
Claudio Marche 7/34
http://mclab.diee.unica.it
Blynk platform – Mobile (4)
• Give a project name
• Choose “NodeMCU” in the
“CHOOSE DEVICE” field
• Choose “WiFi” in the
“CONNECTION TYPE” field
• Tap on “Create Project”
Blynk platform
Claudio Marche 8/34
http://mclab.diee.unica.it
Blynk platform – Mobile (5)
• “Back to projects selection” button
• “Project settings” button
• “Run the interface” button
• Interface canvas
Tap over the interface canvas to show the
“Widget Box”
Blynk platform
Claudio Marche 9/34
http://mclab.diee.unica.it
Blynk platform – Mobile (6)
• Energy meter balance
• Widgets list – scroll to show more
• Widget
• Widget energy
Tap on the “Button” widget to add it on the
interface canvas
Blynk platform
Claudio Marche 10/34
http://mclab.diee.unica.it
Blynk platform – Mobile (7)
• Use the handlers to resize the widget
• Press and drag the widget to move it
• Move it in the upper left corner to
duplicate the widget
• Move it in the upper right corner to
delete (and recycle energy) the widget
Blynk platform
Claudio Marche 11/34
http://mclab.diee.unica.it
Blynk platform – Mobile (8)
• Tap on the Button widget to show the
Button Settings
• Name of the widget
• Color
• Connected pin
• OFF / ON value
• Mode
• ON / OFF labels

Blynk platform
Claudio Marche 12/34
http://mclab.diee.unica.it
Blynk platform – Mobile (9)
• Tap on PIN field to open pin selection
• There are two possibilities:
• Digital (physical pins)
• Virtual (virtual pins)
• Choose “Digital”
• Choose “D4” (ESP8266 builtin LED)
• Tap OK to confirm and go back
• In the “Button Settings” screen tap OK
Blynk platform
Claudio Marche 13/34
http://mclab.diee.unica.it
Blynk platform – Mobile (10)
• Resize the Button widget
using the handlers
• Tap on Project Settings
button
• Write down (or email it) the
Authorization Token
• Tap ok to go back
KEEP the Authorization Token PRIVATE!!
Blynk platform
Claudio Marche 14/34
http://mclab.diee.unica.it
Blynk platform – NodeMCU (1)
Add a new library named “Blynk”
• “Sketch” – “Include Library” – “Manage Libraries…”
• Search for Blynk an install the library

• Reload the Arduino IDE


Blynk platform
Claudio Marche 15/34
http://mclab.diee.unica.it
Blynk platform – NodeMCU (2)
• Open the Arduino IDE and select Select “NodeMCU” as
board (Menu “Tools” – Board: NodeMCU 1.0)
• Check the COM Port (Menu “Tools” – Port)
• From the “File” menu, choose:
• Examples > Blynk > Boards_WiFi > NodeMCU
• A new window will appear. Save it as a new sketch:
• “File” – “Save as…” – give a name
Blynk platform
Claudio Marche 16/34
http://mclab.diee.unica.it
Blynk platform – NodeMCU (3)
#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "YourAuthToken"; // YOUR Blynk Token

// YOUR WiFi credentials


char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";

void setup() {
// Debug console
Serial.begin(115200);

Blynk.begin(auth, ssid, pass);


// You can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
}

void loop() {
Blynk.run();
}

Blynk platform
Claudio Marche 17/34
http://mclab.diee.unica.it
Blynk platform – NodeMCU (4)
Opening port

• Compile and upload the Sketch.


Port open
[25772] Connected to WiFi
[25772] IP: 192.168.1.42
[25772]
___ __ __

You should see this message on / _ )/ /_ _____ / /__


/ _ / / // / _ \/ '_/
/____/_/\_, /_//_/_/\_\
/___/ v0.6.1 on NodeMCU

the Serial Monitor [25778] Connecting to blynk-cloud.com:80


[25971] Ready (ping: 60ms).

• On the app, tap the “Play” button


• When tapping on the Button
widget, the builtin ESP8266 LED
will blink accordly
Blynk platform
Claudio Marche 18/34
http://mclab.diee.unica.it
Blynk – PWM (1)
• Stop the project by tapping on the
“Stop” button
• Open the “Widget Box” by tapping
inside the interface canvas and add a
“Slider” widget

Blynk platform
Claudio Marche 19/34
http://mclab.diee.unica.it
Blynk – PWM (2)
• Use the handlers to
• resize the slider widget

• Tap on the slider widget to open the


Slider Settings
• Tap here to enable the PWM output

Blynk platform
Claudio Marche 20/34
http://mclab.diee.unica.it
Blynk – PWM (3)
• Set OUTPUT as “D4”
Note the “BUSY” label: it warns the user
that the D4 pin is shared with another
widget (the button widget)!
• Turn off the switch “Send values on
release only”
• Tap ok “OK” button
Blynk platform
Claudio Marche 21/34
http://mclab.diee.unica.it
Blynk – PWM (4)
• Tap on the “Play” button and move the
slider cursor to dim the ESP8266 LED

Until now, we have sent values directly to physical PINs.

Blynk platform
Claudio Marche 22/34
http://mclab.diee.unica.it
Blynk – Virtual Pins (1)
• A virtual pin is a pin that is not directly connected to a
physical pin.
• They are used to exchange data between the app and
the microcontroller/board
• Virtual pins are named Vx where x = 1, 2, …

Blynk platform
Claudio Marche 23/34
http://mclab.diee.unica.it
Blynk – Virtual Pins (2)
• Sending data from the board to the app:

Blynk.virtualWrite(VirtualPin, data)

• Sending data from the app to the board (callback):

BLYNK_WRITE(VirtualPin) {
...
}
Blynk platform
Claudio Marche 24/34
http://mclab.diee.unica.it
Read temperature data (1)
To do
• Blynk app:
• Add Value Display or Labeled Value widget
• Eading Rate: PUSH
• Connect to V5 virtual pin
• NodeMCU:
• Read temperature data form a DHT11 sensor
• Send the data using V5
Blynk platform
Claudio Marche 25/34
http://mclab.diee.unica.it
Read temperature data (2)

DHT11 NodeMCU
VCC (+) 3V3
GND (-) GND
S D4

Install
- DHT sensor library

Blynk platform
Claudio Marche 26/34
http://mclab.diee.unica.it
Read temperature data (3)
#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include "DHT.h”

#define DHTPIN 5 // Digital pin (D4) connected to the DHT sensor


#define DHTTYPE DHT11 // DHT 11

// Initialize DHT sensor.


DHT dht(DHTPIN, DHTTYPE);
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = ”YOUR TOKEN";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = ”SSID";
char pass[] = ”password";

BlynkTimer timer;

// Announcing the timer


void myTimerEvent(){
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
Blynk.virtualWrite(V5, t);
}

Blynk platform
Claudio Marche 27/34
http://mclab.diee.unica.it
Read temperature data (3)
void setup(){

// Debug console
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);

// You can also specify server:


//Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

dht.begin();
timer.setInterval(1000L, myTimerEvent);

void loop(){

Blynk.run();
timer.run(); // running timer every second
}

Blynk platform
Claudio Marche 28/34
http://mclab.diee.unica.it
MEMs sensors
• Micro-Electro-Mechanical systems is the technology of
microscopic devices with moving parts.
• MEMS are made up of components between 1 and
100 micrometers in size
• Examples:
• Accelerometers
• Gyros
• Microphones
• BioSensors
Blynk platform
Claudio Marche 29/34
http://mclab.diee.unica.it
Read accelerometers data (1)
Library: SparkFunLSM9DS1.h
MPU9250 member functions
9DoF NodeMCU
Constructor(Wire, address) VCC 3V3
int begin() GND GND
Float ax() SCL D1
SDA D2
float ay()
float az()

Blynk platform
Claudio Marche 30/34
http://mclab.diee.unica.it
Read accelerometers data (2)
• Add three “Level H” widgets to canvas
and resize it
• Modify the “Level H” widgets Settings as
follow:
Name Pin Range
X V1 -2…+2
Y V2 -2..+2
Z V3 -2..+2

• Reading Rate: 3 Seconds


Blynk platform
Claudio Marche 31/34
http://mclab.diee.unica.it
Read accelerometers data (3)
// The SFE_LSM9DS1 library requires both Wire and SPI be
// included BEFORE including the 9DS1 library.
#include <Wire.h>
#include <SPI.h>
#include <SparkFunLSM9DS1.h>

#define LSM9DS1_M 0x1E

Add these lines of code at the


#define LSM9DS1_AG 0x6B

// Use the LSM9DS1 class to create an object. [imu] can be


// named anything, we'll refer to that throught the sketch.

beginning of the sketch


LSM9DS1 imu;

BLYNK_READ(V1)
{
imu.readAccel();
Blynk.virtualWrite(V1, imu.calcAccel(imu.ax)); //sending to Blynk
}

BLYNK_READ(V2)
{
imu.readAccel();
Blynk.virtualWrite(V2, imu.calcAccel(imu.ay)); //sending to Blynk
}

BLYNK_READ(V3)
{
imu.readAccel();
Blynk.virtualWrite(V3, imu.calcAccel(imu.az)); //sending to Blynk
}

Blynk platform
Claudio Marche 32/34
http://mclab.diee.unica.it
Read accelerometers data (4)
// Before initializing the IMU, there are a few settings
// we may need to adjust. Use the settings struct to set
// the device's communication mode and addresses:
imu.settings.device.commInterface = IMU_MODE_I2C;
imu.settings.device.mAddress = LSM9DS1_M;
imu.settings.device.agAddress = LSM9DS1_AG;

if (!imu.begin())
{
Serial.println("Failed to communicate with LSM9DS1.");
while (1)
;
}
Add these lines of code in the
setup() function

Blynk platform
Claudio Marche 33/34
http://mclab.diee.unica.it
To Do
To do (for the next lesson)

• Install Python 3
• Install Pycharm Professional

• Read data from MPU9250


• Read data from BME/BMP280

Blynk platform
Claudio Marche 34/34
http://mclab.diee.unica.it

You might also like