Week 7 Techniques – BLE, NFC and Wifi

Week 7 Slides: Here

Github Repo: Here

Class Notes

Get your wearables talk to the internet

  • Serial Communication
  • Bluetooth LE
  • Wifi
  • NFC

Serial Communication

Bluetooth LE

Bluetooth Low Energy Overview

  • Bluetooth LE, BLE, Bluetooth Smart, Bluetooth 4.0
  • Reduced power consumption
  • Similar communication range
  • Less time cost for connection
  • Multiple devices (no pairing)

Central & Peripheral

  • Central: Uses services on a peripheral
  • Peripheral: Provides services

Peripherals, services and characteristics

Steps:

  1. Central device scan available peripherals
  2. Central device connected to a peripheral
  3. Get a list of services
  4. For each service, get a list of characteristics
  5. For each characteristic, get values
  6. Read or write to characteristic, or
  7. Save handle to read or write later
  8. Subscribe to be notified on changes

Advertising

Example:
MAC Address: AA:BB:CC:DD:EE:FF
Services: [ 0x180A, 0xFF10, 0XFF20, …]
Manufacturer Data: <00007465 6d70>
Connectable: True
TX Power Level: 4
RSSI: -80
Name: TI Sensor Tag

Example:
MAC Address: AA:BB:CC:DD:EE:FF
Services: [ 0x180A, 0xFF10, 0XFF20, …]
Manufacturer Data: <00007465 6d70>
Connectable: True
TX Power Level: 4
RSSI: -80
Name: TI Sensor Tag

UUID: Universally Unique Identifier

Peripheral UUID:
Service UUID:
Characteristics UUID:

Characteristic Properties

Read
Write
Read & Write

Subscribe and Notification

Subscribe
Notify

BLE Compatible Microcontrollers

Flora Bluetooth LE Module

LilyPad Simblee

Retired

Beetles

Arduino 101

Intel Curie
32 bit
3.3V (5V protected)
14 digital input/output pins, 4 PWM
6 analog input
SPI/USB UART/I2C
6-axis accelerometer/gyro
Bluetooth LE

Bluefruit LE

Not a microcontroller, only a bluetooth module

Feather Bluefruit LE

ATmega32u4 (Leonardo)
8 bit
3.3V
20 digital I/O pins
8 PWM pins
10 analog inputs
SPI/1 UART/I2C
LiPo charger

Blend Micro

ATmega32u4 & nRF8001
Similar to feather bluefruit LE, no LiPo charger

BLE Nano

nRF51822 & Cortex M0
32 bit
3.3V
9 digital I/O pins
3 analog inputs
SPI/1 UART/I2C
Bluetooth LE

LightBlue Bean

Retired
3.3V coin cell battery
8 digital I/O pins
2 analog inputs
SPI/I2C
Wireless programming
Bluetooth LE
Accelerometer/Thermometer
RGB LED
Protoboard

Sensor Tag

Temperature, humidity, gyroscope, accelerometer, magnetometer, pressure, IR temperature, vattery sensor
2 buttons
Coin cell battery
Bluetooth LE

RFDuino

3-3.6V
7 digital I/O pins
1 analog input
Bluetooth LE
I2C/1 UART/SPI

Wifi + BLE Microcontrollers

RedBear Duo

Cortex-M3
18 digital I/O pins
Bluetooth LE + Wifi

ESP32

2.2V-3.6V
32 digital I/O all PWM
3 SPI/3 UART/2 I2C
Bluetooth LE + Wifi

MKR 1010

3.3V
SAMD21 Cortex-M0+ 32 bit
U-BLOX NINA-W10

Wireless Prototyping Basics

Process

  1. Build circuit, make it work
  2. Build connection, make them talk
  3. Set up protocols
  4. Build app/interface

Hardwares and Softwares

  1. Laptop
  2. Cable
  3. Bluetooth LE device
  4. Mobile phone [optional]

 

  1. Node.js installed [optional]
  2. Text editor

Different Central devices

Computer:
Noble library for node.js
Web Bluetooth for frontend javascript
P5 BLE Library for p5.js

Mobile phone:
Cordova ble central plugin

Web Bluetooth API

https://googlechrome.github.io/samples/web-bluetooth/

P5 BLE Library

Read Button Example: https://itpnyu.github.io/p5ble-website/docs/read-one-char-callback

Apps for BLE prototyping

LightBlue App

Adafruit Bluefruit App

Getting Started with Flora Bluefruit LE Module

Install Library

Arduino:
Sketch -> Include Library -> Manage Libraries
Install Adafruit BluefruitLE nRF51

Example for Circuit Playground

File -> Examples -> cplay_neopixel_picker

Wiring:

  • Circuit Playground 3.3V to Flora Bluefruit LE 3.3V
  • Circuit Playground GND to Flora Bluefruit GND
  • Circuit Playground serial TX to Flora Bluefruit serial RX. Double check you connect TX to RX and not TX to TX!
  • Circuit Playground serial RX to Flora Bluefruit serial TX. Again double check you connect RX to TX and not RX to RX!
  • Circuit Playground #12 to Flora Bluefruit MODE

Download mobile app

Search Bluefruit in App store

  • Connect CPlay_BLE
  • Select Controller
  • Use Color Picker to pick color
  • Use Control Pad to control which NeoPixel lights up

Example for Circuit Playground & Flora

File -> Examples -> bleuart_data

Wiring:

  • Circuit Playground / Flora 3.3V to Flora Bluefruit LE 3.3V
  • Circuit Playground / Flora GND to Flora Bluefruit GND
  • Circuit Playground / Flora serial TX to Flora Bluefruit serial RX. Double check you connect TX to RX and not TX to TX!
  • Circuit Playground / Flora serial RX to Flora Bluefruit serial TX. Again double check you connect RX to TX and not RX to RX!
  • Circuit Playground / Flora #12 to Flora Bluefruit MODE

Developing mobile apps for Bluetooth LE

Hardwares and Softwares

  1. laptop
  2. mobile phone
  3. cable
  4. bluetooth le device

 

  1. Xcode
  2. node

Setup device

  1. Generate a signing certificate https://developer.apple.com/account/ios/certificate/certificateList.action
    IOS App Development >> upload csr file from keychain Access >> approve
  2. IOS certificates >> download your name >> double click to add to keychain Access
  3. IOS certificates >> download “anyone” >> open Xcode >> drag certificate to Xcode app icon

Intro to Cordova

Phone Gap

npm install -g cordova
cordova create myApp
cordova platform add ios

Folders

config.xml — config of the entire project
www — js, html, css files
platforms — IOS, Android, etc
plugins — plugins used in this project

Deploy

cordova run
platforms >> ios >> ProjectName.xcodeproj

BLE Central plugin for Cordova

cordova plugin add cordova-plugin-ble-central

How to connect

ble.scan(services, seconds, success, failure);
ble.connect(device_id, connectSuccess, connectFailure);
ble.disconnect(device_id, [success], [failure]);

How to read

ble.read(device_id, service_uuid, characteristic_uuid, success, failure);
or
ble.startNotification(device_id, service_uuid, characteristic_uuid, success, failure);

How to control

ble.write(device_id, service_uuid, characteristic_uuid, success, failure);
or
ble.writeWithoutResponse(device_id, service_uuid, characteristic_uuid, success, failure);

How to debug

  • Alert at beginning
  • Safari Developer Tools: Safari >> Preferences >> Show develop menu
  • Test with LightBlue

Keep Questioning: hardware or software?

Learn More

Don’s Bluetooth LE Cordova plugin
https://github.com/don/cordova-plugin-ble-central

Demo

Flora Bluefruit LE Data mode

P5 BLE Library

Initiate BLE Device

let myBLE = new p5ble();

let myOtherBLE = new p5ble();

When connected

myBLE.connect(service, gotCharacteristics)

  • Service: Service UUID(e.g. 0x1234, 0x12345678, 99999999-0000-1000-8000-00805f9b34fb)
  • gotCharacteristics(optional): A callback function

Read, Write, StartNotification

myBLE.read(characteristic, dataType?, gotDataCallback);
myBLE.write(characteristic, data)
myBLE.startNotifications(myCharacteristic, handleNotificationsCallback);

Demo

  • MKR1010 Start Notification Button
  • MKR1010 Start Notification Potentiometer
  • MKR1010 LED Switch
  • MKR1010 LED Dimming
  • MKR1010 Multiple Notifications
    Flora FSR

Wifi

Components of an IoT Application

System

  • Physical device (microcontroller+sensor/actuator)
  • Server
  • Other devices (computer, mobile phone)

Wifi Compatible Microcontrollers

MKR1000

Cortex M0+
32 bit
3.3V
8 digital I/O pins, 4PWM
7 analog inputs
SPI/1 UART/I2C
Wifi
LiPo charger

Feather Huzzah

ESP8266
32 bit
3.3V w/ 500mA peak current output
9 digital I/O pins
1 analog inputs
SPI/USB UART/I2C
LiPo charger

Huzzah

ESP8266

Particle Photon

Hardware + Cloud Service

Demo: Arduino MKR 1010 as Wifi Scanner

File >> Examples >> WifiNINA >> ScanNetworkAdvanced

SSID (Service Set Identifier): name of a Wifi network

Mac Address (Media Access Control address): device’s unique identifier

Encryption types:

  • WPA2 (Wi-Fi Protected Access)
  • WPA2 Enterprise

Wifi – Getting Started with ESP8266

Install USB to UART driver

https://www.silabs.com/products/mcu/Pages/USBtoUARTBridgeVCPDrivers.aspx

Install ESP8266 Board Package

  1. Arduino >> Preference >> Additional Boards Manager URLs
    Enter: http://arduino.esp8266.com/stable/package_esp8266com_index.json
  2. Tools >> Board >> Boards Manager
    Search esp8266, install

Test with blink example

  1. File >> Examples >> 01.Basics >> Blink
    Change pin 13 to pin 0
  2. Tools >> Board >> Adafruit Huzzah ESP8266
  3. Tools >> Port >> /dev/cu.SLAB_USBtoUART (MAC)
  4. Upload

Red LED on pin0 and Blue LED on pin2, auto blinks when uploading

Test with WifiScan Example

  1. File >> Examples >> ESP8266Wifi >> WifiScan
  2. Tools >> Board >> Adafruit Huzzah ESP8266
  3. Tools >> Port >> /dev/cu.SLAB_USBtoUART (MAC)
  4. Upload

Wifi – Connect to server as a client

ITP Networks

NYU: enterprise network, WPA2-Enterprise authentication
ITPSandbox: devices on itpsandbox can be both servers and clients

Register ESP8266 on ITPSandbox

  1. Connect to ITPSandbox, register your own computer
  2. Find the media access control address (MAC address) of your ESP8266
    Github Repo: https://github.com/ZhuJingwen/esp8266-examples
    Upload ESP8266_MAC, open serial monitor
  3. Register the device on http://computer.registration.nyu.edu

ESP8266 as Client Test

Github Repo: https://github.com/ZhuJingwen/esp8266-examples
ESP8266_ClientTest
ssid: itpsandbox
Success connection means: connected to wifi and can act as a client

Wifi – Build your own server

ESP8266 as websocket client

Github Repo: https://github.com/ZhuJingwen/esp8266-examples

  1. Connect your laptop to itpsandbox
  2. Run the server: websocket_server
    npm install express
    npm install ws
    node server.js
  3. Open System Preference >> Network >> Copy IP Address
  4. Open: ESP8266_WebSocket
  5. Change host: your-ip-address ssid: itpsandbox password:
  6. Install library ArduinoJson, ArduinoHttpClient
  7. Upload

ESP8266 as client for HTTP GET request

Github Repo: https://github.com/ZhuJingwen/esp8266-examples

  1. Connect your laptop to itpsandbox
  2. Run the server: http_server
    npm install express
    node server.js
  3. Open System Preference >> Network >> Copy IP Address
  4. Open: ESP8266_HttpGet
  5. Change host: your-ip-address ssid: itpsandbox password:
  6. Upload

ESP8266 as server

Github Repo: https://github.com/ZhuJingwen/esp8266-examples

  1. Open: ESP8266_Server
  2. Upload
  3. In Serial Monitor, copy IP address, paste in browser

Routes: /inline, /led

ESP8266 as access point

Github Repo: https://github.com/ZhuJingwen/esp8266-examples

  1. Open: ESP8266_AccessPoint
  2. Change
  3. Connect to the access point like connect a new wifi
  4. Go to http://192.168.4.1

Routes: /led

More Libraries

Wifi Manager

NFC(Near Field Communication)

Electromagnetic fields: https://www.androidauthority.com/what-is-nfc-270730/

NFC Devices

Passive NFC devices:
Tags, cards, stickers

Active NFC devices:
Smart phones

Passive NFC System

  • NFC tag (contains static information)
  • NFC reader
  • Other devices (computer, mobile phone)

Active NFC System

  • NFC device (microcontroller + sensor/actuator + battery)
  • NFC reader
  • Other devices (computer, mobile phone)

Usage

  • Identifier (Active & Passive)
  • Information exchange (Active & Passive)
    • NDEF: NFC Data Exchange Format, e.g. URIs, plain text, etc
  • Data exchange (Active Only)

Demo https://learn.adafruit.com/adafruit-pn532-rfid-nfc/overview

Cellular Data

Fona

Cellular
Send and receive SMS messages, voice call, GPRS data, FM radio, PWM vibration motor control
Headphone jack
LiPo charge
Also a feather version

Particle Electron

Hardware + Cloud Service

No on-board wireless communications

Feather M0

Cortex M0 (Arduino Zero)
32 bit
3.3V w/ 3.7V regulator
20 digital I/O all PWM
6 analog inputs
1 analog ouput (DAC)
Skip out on a breadboard
Another version: Adalogger M0
LiPo charger

Teensy

Logic: 3.3V
Sound projects

Flora

Atmega32u4
3.5-5V
SPI/1 UART/I2C
No LiPo Charger (no fire on body!)
Power switch
External bluetooth le module

Gemma

Attiny85
8 bit
3.3V
Digital I/O pins 3, 2 PWM
1 analog input
Power switch
No UART

Arduino Gemma

Adafruit Gemma

Gemma M0

ATSAMD21E18
32-bit
3.3V
Built in RGB DotStar LED
3 Analog input/digital IO
3 hardware capacitive touch sensors
1 UART
1 I2C

Circuit Playground
Classic vs Express

ATmega32u4 VS ATSAMD21
Express with Infrared receiver and transmitter

In-class practice: p5 BLE + MKR 1010

Arduino MKR 1010 Update Firmware 1

  1. Download and install the hourly Arduino IDE build. https://www.arduino.cc/en/Main/Software#hourly
  2. Launch the hourly IDE release and use the IDE’s library manager (Sketch -> Include Library -> Manage Libraries …) to install/update the following libraries: WiFiNINA, ArduinoBLE
  3. Plug in the MKR WiFi 1010 board into your PC with a USB cable.
  4. Select MKR WiFi 1010 as the board and it’s serial port in the Tools menu of the IDE.
  5. Open the WiFiNINA Firmware Updater example sketch: File -> Examples -> WiFiNINA -> Tools -> FirmwareUpdater
  6. Upload the sketch onto your board.
  7. If the Serial Monitor is open, close it.
  8. Launch the “WiFi 101 / NINA Firmware Updater” from the Tools menu of the IDE
  9. Select the Serial Port for your board. The latest version of firmware for the NINA will be automatically selected.
  10. Click the “Update Firmware” button, and wait for the update to complete.
  11. Close the Updater.

Generate Your Service UUID

https://www.uuidgenerator.net/

Load Arduino Example

Open Week7_Example_BLE_Write >> arduino-sketches >> ArduinoBLE_SwitchLED
Use Your UUID to replace ledService UUID
Select Board Arduino Wifi MKR 1010
Select Port
Upload

Load p5 Example

Open Week7_Example_BLE_Write >> p5-sketch with text editor
Use Your UUID to replace serviceUuid UUID
Save
Double click index.html

LED Dimming example

Open Week7_Example_BLE_Write >> arduino-sketches >> ArduinoBLE_DimLED
Use Your UUID to replace ledService UUID
Select Board Arduino Wifi MKR 1010
Select Port
Upload
Open index.html

Power with LiPo Battery

Comment out while (!Serial);

Summary

Networks

Cellular
Wifi 160’
Bluetooth Classic 328’
Bluetooth Low Energy (BLE) 328’/1,312’
ZigBee and ZigWave 328’
RFID (Radio Frequency Identification) 3’
NFC (Near Field Communication) 3’
Other Proprietary Radio Frequencies

Architectures

Dedicated gateway
Smartphone as gateway
Direct internet connection
Device to device connections
Service to service connections

How to choose?

Distance
Communication: To phone, to laptop, to gateway?
Portable?Plug into wall?
Power consumption?
How many devices?

Compatibility

Sharing the same network protocol, any devices can communicate to any apps/servers

Data Type

Character: e.g. ‘r’ for rainbow animation, ‘b’ for all blue
Int: e.g. 255, 100, 230 for r, g, b color
Byte
Byte arrays

APIs

Application Programming Interface

  • Data
    • Weather
    • Twitter
    • Email
  • Applications
    • IFTTT

Potentials & Limitations

  • Wearables vs Things vs Screens
  • Smart?
  • Stability of network
  • Security & privacy

Assignment

Reading: http://www.elasticspace.com/2013/03/no-to-no-ui
Turn one of the previous wearable projects into a wireless one