Mark Easley
Created December 31, 2016

BLE IoT Workshop for TI LaunchPad and Sensors BoosterPack

Get started with some IoT Projects. Learn how to use BLE wireless communication with the TI LaunchPad.

BeginnerFull instructions provided1.5 hours824
BLE IoT Workshop for TI LaunchPad and Sensors BoosterPack

Things used in this project

Story

Read more

Schematics

SlidesWiFiIoT

Slides

SlidesPDFWiFiIOT

PresentationSlidesAugust2017

PresentationSlidesPDFAugust2017

File missing, please reupload.

PresentationSlidesPDFJanuary2018

Workshop slides in PDF

PresentationSlidesJanuary2018

Workshop slides

Code

CustomEduBPTest.ino

Arduino
/*  
CustomEduBPTest for MSP432 
This example is based off the EduBP_mkii_Production_Test code  
for Energia IDE. Checks various functions of the BoosterPack. 
Hardware Required:  
* LaunchPad  
* Educational BoosterPack MKII 
This example code is in the public domain.  
*/ 
#include <itoa.h> 
#include <Wire.h> 
#include <Adafruit_TMP006.h> 
//#include "OPT3001.h" 
#define USE_USCI_B1  
#define USING_MSP430F5529_LAUNCHPAD 
//#define USING_TIVA_C_LAUNCHPAD 
// Core library for code-sense 
#if defined(WIRING) // Wiring specific 
#include "Wiring.h" 
#elif defined(MAPLE_IDE) // Maple specific 
#include "WProgram.h" 
#elif defined(MPIDE) // chipKIT specific 
#include "WProgram.h" 
#elif defined(DIGISPARK) // Digispark specific 
#include "Arduino.h" 
#elif defined(ENERGIA) // LaunchPad MSP430, Stellaris and Tiva, Experimeter Board FR5739 specific 
#include "Energia.h" 
#elif defined(CORE_TEENSY) // Teensy specific 
#include "WProgram.h" 
#elif defined(ARDUINO) && (ARDUINO >= 100) // Arduino 1.0 and 1.5 specific 
#include "Arduino.h" 
#elif defined(ARDUINO) && (ARDUINO < 100) // Arduino 23 specific 
#include "WProgram.h" 
#else // error 
#error Platform not defined 
#endif 
// Include application, user and local libraries 
#include "SPI.h" 
// Screen selection 
#define HX8353E // HX8353E K35 HI32 W32 ILI9225B HY28A ST7735 PicasoSPE PicasoSGC 
#include "Screen_HX8353E.h" 
Screen_HX8353E myScreen; 
// Define variables and constants 
//uint32_t chrono; 
/// 
/// @brief      protocolSquare 
/// @details    measure time to draw a square with side=pixels 
/// @param      pixels number of pixels of one side 
/// 
void protocolSquare(uint16_t pixels) 
{ 
   if ((pixels < myScreen.screenSizeX()) && (pixels < myScreen.screenSizeY()) && (pixels > 48)) { 
       myScreen.setOrientation(0); 
       myScreen.setPenSolid(false); 
       uint16_t x100 = (myScreen.screenSizeX()-pixels)/2; 
       uint16_t y100 = (myScreen.screenSizeY()-pixels)/2; 
       myScreen.dRectangle(x100-1, y100-1, pixels+2, pixels+2, whiteColour); 
       myScreen.setPenSolid(true); 
       uint32_t chrono = millis(); 
       myScreen.dRectangle(x100, y100, pixels, pixels, grayColour); 
       chrono = millis() - chrono; 
       myScreen.setFontSize(myScreen.fontMax()); 
       myScreen.gText(x100 +2, y100 +2, "(" + i32toa(pixels) + ")"); 
       myScreen.gText(x100 +2, y100 +pixels -myScreen.fontSizeY() -2, i32toa(chrono) + " ms"); 
       Serial.print("Square("); 
       Serial.print(pixels, DEC); 
       Serial.print(")\t"); 
       Serial.println(chrono, DEC); 
   } 
} 
/// 
/// @brief      protocolCopyPaste 
/// @details    measure time to copy-paste a 64x64 area 
/// @param      orientation default=1 
/// 
void protocolCopyPaste(uint8_t orientation = 1) 
{ 
   uint32_t chrono; 
   myScreen.clear(); 
   myScreen.setOrientation(orientation); 
   // Image 
   chrono = millis(); 
   for (uint8_t i=0; i<64; i++) { 
       for (uint8_t j=0; j<64; j++) { 
           myScreen.point(i, j, myScreen.calculateColour(4*i, 4*j, 254-2*i-2*j)); 
       } 
   } 
   myScreen.setPenSolid(false); 
   myScreen.rectangle(1, 1, 62, 62, blackColour); 
   myScreen.line(0,   0, 63, 63, whiteColour); 
   myScreen.line(32,  0, 63, 63, whiteColour); 
   myScreen.line(0,  32, 63, 63, whiteColour); 
   myScreen.dRectangle(0, 0, 64, 64, whiteColour); 
   chrono = millis() - chrono; 
   myScreen.setFontSize(0); 
   myScreen.gText(0, 66, "0: Original"); 
   myScreen.setFontSize(1); 
   myScreen.gText(0, 76, i32toa(chrono) + " ms"); 
   Serial.print("Original(="); 
   Serial.print(orientation, DEC); 
   Serial.print(")\t"); 
   Serial.println(chrono, DEC); 
   // Method 1 
   chrono = millis(); 
   for (uint16_t i=0; i<64; i++) { 
       for (uint16_t j=0; j<64; j++) { 
           myScreen.point(myScreen.screenSizeX()/2+i, j, myScreen.readPixel(i, j)); 
       } 
   } 
   chrono = millis() - chrono; 
   myScreen.setFontSize(0); 
   myScreen.gText(myScreen.screenSizeX()/2, 66, "1. point readPixel"); 
   myScreen.setFontSize(1); 
   myScreen.gText(myScreen.screenSizeX()/2, 76, i32toa(chrono) + " ms"); 
   Serial.print("point(readPixel())\t"); 
   Serial.println(chrono, DEC); 
   // Method 2 
   chrono = millis(); 
   myScreen.copyPaste(0, 0, 0, myScreen.screenSizeY()/2, 64, 64); 
   chrono = millis() - chrono; 
   myScreen.setFontSize(0); 
   myScreen.gText(0, myScreen.screenSizeY()/2 +66, "2. copyPaste"); 
   myScreen.setFontSize(1); 
   myScreen.gText(0, myScreen.screenSizeY()/2 +76, i32toa(chrono) + " ms"); 
   Serial.print("copyPaste()\t"); 
   Serial.println(chrono, DEC); 
   // Method 3 
   if (myScreen.isStorage()) { 
       uint32_t address; 
       chrono = millis(); 
       address = 100; 
       myScreen.copyArea(0, 0, 64, 64, address); 
       address = 100; 
       myScreen.pasteArea(myScreen.screenSizeX()/2, myScreen.screenSizeY()/2, 64, 64, address); 
       chrono = millis() - chrono; 
       myScreen.setFontSize(0); 
       myScreen.gText(myScreen.screenSizeX()/2, myScreen.screenSizeY()/2 +66, "3. copy-paste SRAM"); 
       myScreen.setFontSize(1); 
       myScreen.gText(myScreen.screenSizeX()/2, myScreen.screenSizeY()/2 +76, i32toa(chrono) + " ms"); 
       Serial.print("copyArea()+pasteArea() SRAM\t"); 
       Serial.println(chrono, DEC); 
   } 
} 
/// 
/// @brief      protocolText 
/// @details    measure time to draw text in 3 fonts, 4 orientations, 10x 
/// 
void protocolText() 
{ 
   uint32_t chrono1, chrono2; 
   uint16_t colour; 
   uint8_t k = 0; 
   // Serial.print("fast gText... "); 
   myScreen.clear(grayColour); 
   myScreen.setFontSolid(true); 
   chrono1 = millis(); 
   for (uint8_t j=0; j<10; j++) { 
       for (uint8_t i=0; i<4; i++) { 
           if (k==1) colour = redColour; 
           else if (k==2) colour = yellowColour; 
           else if (k==3) colour = greenColour; 
           else if (k==4) colour = cyanColour; 
           else if (k==5) colour = blueColour; 
           else  colour = violetColour; 
           k++; 
           k %= 7; 
           myScreen.setPenSolid(false); 
           myScreen.dRectangle(0, 0, myScreen.screenSizeX(), myScreen.screenSizeY(), colour); 
           myScreen.setOrientation(i); 
           myScreen.setFontSize(0); 
           myScreen.gText(4, 4, "font 0 on " + String(i), colour); 
           myScreen.setFontSize(1); 
           myScreen.gText(4, 14, "font 1 on " + String(i), colour); 
           myScreen.setFontSize(2); 
           myScreen.gText(4, 34, "font 2 on " + String(i), colour); 
       } 
   } 
   chrono1 = millis()-chrono1; 
   Serial.print("10xFontSolid(true)\t"); 
   Serial.println(chrono1, DEC); 
   // Serial.print("slow gText... "); 
   myScreen.clear(grayColour); 
   myScreen.setFontSolid(false); 
   chrono2 = millis(); 
   for (uint8_t j=0; j<10; j++) { 
       for (uint8_t i=0; i<4; i++) { 
           if (k==1) colour = redColour; 
           else if (k==2) colour = yellowColour; 
           else if (k==3) colour = greenColour; 
           else if (k==4) colour = cyanColour; 
           else if (k==5) colour = blueColour; 
           else  colour = violetColour; 
           k++; 
           k %= 7; 
           myScreen.setPenSolid(false); 
           myScreen.dRectangle(0, 0, myScreen.screenSizeX(), myScreen.screenSizeY(), colour); 
           myScreen.setOrientation(i); 
           myScreen.setFontSize(0); 
           myScreen.gText(4, 4, "font 0 on " + String(i), colour); 
           myScreen.setFontSize(1); 
           myScreen.gText(4, 14, "font 1 on " + String(i), colour); 
           myScreen.setFontSize(2); 
           myScreen.gText(4, 34, "font 2 on " + String(i), colour); 
       } 
   } 
   chrono2 = millis()-chrono2; 
   Serial.print("10xFontSolid(false)\t"); 
   Serial.println(chrono2, DEC); 
   Serial.print("Ratio%\t"); 
   Serial.println((uint32_t)((uint64_t)(chrono1*100)/chrono2), DEC); 
} 
const int JOY_X = 2; 
const int JOY_Y = 26; 
const int SEL = 5; 
//MIC 
const int MIC = 6; 
//ACCELEROMETER 
const int ACC_X = 23; 
const int ACC_Y = 24; 
const int ACC_Z = 25; 
//SWITCHES 
const int SW1 = 33; 
const int SW2 = 32; 
//BUZZER 
const int BUZZ = 40; 
//RGB LED 
const int RGB_RED = 39; 
const int RGB_GRN = 38; 
const int RGB_BLU = 37; 
//TMP006 
float tempReading = 0; 
// notes in the melody: 
#define NOTE_C4_1 260 
#define NOTE_B0  31 
#define NOTE_C1  33 
#define NOTE_CS1 35 
#define NOTE_D1  37 
#define NOTE_DS1 39 
#define NOTE_E1  41 
#define NOTE_F1  44 
#define NOTE_FS1 46 
#define NOTE_G1  49 
#define NOTE_GS1 52 
#define NOTE_A1  55 
#define NOTE_AS1 58 
#define NOTE_B1  62 
#define NOTE_C2  65 
#define NOTE_CS2 69 
#define NOTE_D2  73 
#define NOTE_DS2 78 
#define NOTE_E2  82 
#define NOTE_F2  87 
#define NOTE_FS2 93 
#define NOTE_G2  98 
#define NOTE_GS2 104 
#define NOTE_A2  110 
#define NOTE_AS2 117 
#define NOTE_B2  123 
#define NOTE_C3  131 
#define NOTE_CS3 139 
#define NOTE_D3  147 
#define NOTE_DS3 156 
#define NOTE_E3  165 
#define NOTE_F3  175 
#define NOTE_FS3 185 
#define NOTE_G3  196 
#define NOTE_GS3 208 
#define NOTE_A3  220 
#define NOTE_AS3 233 
#define NOTE_B3  247 
#define NOTE_C4  262 
#define NOTE_CS4 277 
#define NOTE_D4  294 
#define NOTE_DS4 311 
#define NOTE_E4  330 
#define NOTE_F4  349 
#define NOTE_FS4 370 
#define NOTE_G4  392 
#define NOTE_GS4 415 
#define NOTE_A4  440 
#define NOTE_AS4 466 
#define NOTE_B4  494 
#define NOTE_C5  523 
#define NOTE_CS5 554 
#define NOTE_D5  587 
#define NOTE_DS5 622 
#define NOTE_E5  659 
#define NOTE_F5  698 
#define NOTE_FS5 740 
#define NOTE_G5  784 
#define NOTE_GS5 831 
#define NOTE_A5  880 
#define NOTE_AS5 932 
#define NOTE_B5  988 
#define NOTE_C6  1047 
#define NOTE_CS6 1109 
#define NOTE_D6  1175 
#define NOTE_DS6 1245 
#define NOTE_E6  1319 
#define NOTE_F6  1397 
#define NOTE_FS6 1480 
#define NOTE_G6  1568 
#define NOTE_GS6 1661 
#define NOTE_A6  1760 
#define NOTE_AS6 1865 
#define NOTE_B6  1976 
#define NOTE_C7  2093 
#define NOTE_CS7 2217 
#define NOTE_D7  2349 
#define NOTE_DS7 2489 
#define NOTE_E7  2637 
#define NOTE_F7  2794 
#define NOTE_FS7 2960 
#define NOTE_G7  3136 
#define NOTE_GS7 3322 
#define NOTE_A7  3520 
#define NOTE_AS7 3729 
#define NOTE_B7  3951 
#define NOTE_C8  4186 
#define NOTE_CS8 4435 
#define NOTE_D8  4699 
#define NOTE_DS8 4978 
// OPT3001 
//opt3001 opt3001; 
Adafruit_TMP006 tmp006; 
unsigned long readings = 0; 
int melody[] = { 
  NOTE_C4_1,NOTE_C4, NOTE_D4, NOTE_C4,NOTE_F4,NOTE_E4, 
  NOTE_C4_1,NOTE_C4,NOTE_D4,NOTE_C4,NOTE_G4,NOTE_F4, 
  NOTE_C4_1,NOTE_C4,NOTE_C5,NOTE_A4,NOTE_F4,NOTE_F4, NOTE_E4,NOTE_D4, 
  NOTE_AS4,NOTE_AS4,NOTE_A4,NOTE_F4,NOTE_G4,NOTE_F4}; 
// note durations: 4 = quarter note, 8 = eighth note, etc.: 
int noteDurations[] = { 
 4, 4, 2, 2,2,1, 
 4, 4, 2, 2,2,1, 
 4, 4, 2, 2,4,4,2,1,  
 4, 4, 2, 2,2,1}; 
void setup() 
{ 
 // put your setup code here, to run once: 
 Serial.begin(9600); 
 delay(300); 
 analogReadResolution(12);
 pinMode(SEL, INPUT); 
 pinMode(SW1, INPUT); 
 pinMode(SW2, INPUT); 
 pinMode(BUZZ, OUTPUT); 
 Serial.println("Welcome to the Educational BoosterPack MKII Custom Test:"); 
 tmp006.begin(TMP006_CFG_8SAMPLE);  // Takes 8 averaged samples for measurement 
//  opt3001.begin();  
} 
char *ftoa(char *a, float f, int precision) 
{ 
 long p[] = {0,10,100,1000,10000,100000,1000000,10000000,100000000}; 
 char *ret = a; 
 long heiltal = (long)f; 
 itoa(heiltal, a, 10); 
 while (*a != '\0') a++; 
 *a++ = '.'; 
 long desimal = abs((long)((f - heiltal) * p[precision])); 
 itoa(desimal, a, 10); 
 return ret; 
} 
void loop() 
{ 
 int16_t i; 
 uint16_t colour; 
   Serial.println("*** LCD_screen test ****"); 
   Serial.println("(All times in ms)"); 
   myScreen.begin(); 
   Serial.println(myScreen.WhoAmI()); 
   Serial.print(myScreen.screenSizeX(), DEC); 
   Serial.print("x"); 
   Serial.println(myScreen.screenSizeY(), DEC); 
   myScreen.setFontSize(myScreen.fontMax()); 
   myScreen.clear(darkGrayColour); 
   protocolSquare(300); 
   protocolSquare(200); 
   protocolSquare(100); 
   protocolSquare(50); 
   delay(2000); 
   protocolText(); 
   delay(2000); 
   if (myScreen.isReadable()) { 
       protocolCopyPaste(1); 
       delay(2000); 
   } 
   myScreen.clear(); 
   Serial.println("-END OF SCREEN TEST--"); 
   Serial.println(); 
   myScreen.clear(grayColour); 
   myScreen.setFontSolid(false); 
   colour = redColour; 
	myScreen.setOrientation(i); 
	myScreen.setFontSize(2); 
 //Test out the joystick 
 myScreen.gText(0, 0, "  JoyStick Test", blueColour); 
 myScreen.gText(0, 15, "  Press middle btn", blueColour); 
 myScreen.gText(0, 30, "  to exit ", blueColour); 
 while(digitalRead(SEL)==0){ 
   if(analogRead(JOY_X)<900){ 
     myScreen.gText(0, 45, "Right", colour); 
   } 
   else if(analogRead(JOY_X)>0){ 
     myScreen.gText(0, 45, "Left ", colour); 
   } 
   else if(analogRead(JOY_Y)<4094){ 
     myScreen.gText(0, 45, "Up", colour); 
   } 
   else if(analogRead(JOY_Y)>0){ 
     myScreen.gText(0, 45, "Down", colour); 
   } 
   else { 
     myScreen.gText(0,75, "Center", colour); 
   } 
 } 
 delay(400); 
 //Test out the microphone 
 myScreen.clear(grayColour); 
 myScreen.gText(0,0, "   Tap  Mic", blueColour); 
 while(analogRead(MIC) > 100); 
 myScreen.gText(50,10, " Passed!", colour); 
 myScreen.gText(0,20, "  Button Tests", blueColour); 
 //Test out button 1 
 myScreen.gText(0,40, "Press S1", colour); 
 while(digitalRead(SW1) == 1); 
 myScreen.gText(50,40, " > Passed!", colour); 
 //Test out button 2 
 myScreen.gText(0,55, "Press S2", colour); 
 while(digitalRead(SW2) == 1); 
 myScreen.gText(50,55, " > Passed!", colour); 
 myScreen.gText(0,70, "  Buzzer Test", blueColour); 
 myScreen.gText(0,85, "hold S1 to skip", blueColour); 
 //Test out buzzer 
 delay(2000); 
 if(digitalRead(SW1) == 1){ 
   for (int thisNote = 0; thisNote < 26; thisNote++) { 
     // to calculate the note duration, take one second  
     // divided by the note type. 
     //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc. 
     int noteDuration = 1000/noteDurations[thisNote]; 
     tone(BUZZ, melody[thisNote],noteDuration); 
     int pauseBetweenNotes = noteDuration + 50;      //delay between pulse 
     delay(pauseBetweenNotes>>2); 
     noTone(BUZZ);                // stop the tone playing 
   } 
 myScreen.gText(0,85, "S1&S2 if sound", colour); 
 while(digitalRead(SW2) == 1 || digitalRead(SW1) == 1); 
 myScreen.gText(0,100, " PASSED!!", greenColour); 
 delay(400); 
 } 
 //Test out 3-axis analog accelerometer 
 // 1200 ~ -1G / 2000 ~ 0G / 2800 ~ +1G 
 myScreen.clear(grayColour); 
 myScreen.gText(0,0, " Accelerometer", blueColour); 
 myScreen.gText(0,15, "Flat", colour); 
 while(analogRead(ACC_X) < 900 || analogRead(ACC_X) > 900 || analogRead(ACC_Y) > 900 || analogRead(ACC_Y) < 900 || analogRead(ACC_Z) < 900); 
 myScreen.gText(50,15, " > Passed!", colour); 
 myScreen.gText(0,30, "Left", colour); 
 while(analogRead(ACC_X) > 900 || analogRead(ACC_Y) > 900 || analogRead(ACC_Y) < 900 || analogRead(ACC_Z) > 900 || analogRead(ACC_Z) < 900); 
 myScreen.gText(50,30, " > Passed!", colour); 
 myScreen.gText(0,45, "Right", colour); 
 while(analogRead(ACC_X) < 900 || analogRead(ACC_Y) > 900 || analogRead(ACC_Y) < 900 || analogRead(ACC_Z) > 900 || analogRead(ACC_Z) < 900); 
 myScreen.gText(50,45, " > Passed!", colour); 
 myScreen.gText(0,60, "Away", colour); 
 while(analogRead(ACC_X) < 900 || analogRead(ACC_X) > 900 || analogRead(ACC_Y) < 900 || analogRead(ACC_Z) < 900 || analogRead(ACC_Z) > 900); 
 myScreen.gText(50,60, " > Passed!", colour); 
 myScreen.gText(0,75, "To you", colour); 
 while(analogRead(ACC_X) < 900 || analogRead(ACC_X) > 900 || analogRead(ACC_Y) > 900 || analogRead(ACC_Z) < 900 || analogRead(ACC_Z) > 900); 
 myScreen.gText(50,75, " > Passed!", colour); 
 myScreen.gText(0,90, "Upside Down", colour); 
 while(analogRead(ACC_X) < 900 || analogRead(ACC_X) > 900 || analogRead(ACC_Y) > 900 || analogRead(ACC_Y) < 900 || analogRead(ACC_Z) > 900); 
 myScreen.gText(0,105, "   PASSED!!", greenColour); 
 delay(400); 
 myScreen.clear(grayColour); 
 //TEST TMP006 TEMP SENSOR 
 float temp = tmp006.readObjTempC(); 
 char* tempText; 
 char ii; 
 tempText = (char*) malloc(10); 
 for (ii=0;ii<10;ii++) 
 tempText[ii] = 0; 
 tempText =  ftoa(tempText, temp, 4); 
 myScreen.gText(0,0, "   TMP006 Test", blueColour); 
 myScreen.gText(0,20, (char*)tempText , colour); 
 myScreen.gText(60,20, "*C", colour); 
 myScreen.gText(0,40, "   RGB LED Test", blueColour); 
 //TEST OPT3001 LIGHT SENSOR 
 //To be enabled once OPT3001 is added to future Edu BP MK II Rev. 
 // Serial.println("Cover the light sensor to test the OPT3001."); 
 // while(opt3001.readResult() > 30); 
 // Serial.println("Shine flashlight onto the light sensor "); 
 // while(opt3001.readResult() < 1000); 
 // Serial.println("OPT3001 testing successful!"); 
 //TEST RGB LED 
 i = 0; 
 int brightness = 0; 
 int fadeAmount = 5; 
 for(brightness = 0; brightness<255; brightness+=fadeAmount){ 
   analogWrite(RGB_RED, brightness);     
   // wait for 30 milliseconds to see the dimming effect     
   delay(10);  
 } 
 for(brightness = 255; brightness>=0; brightness-=fadeAmount){ 
   analogWrite(RGB_RED, brightness);     
   // wait for 30 milliseconds to see the dimming effect     
   delay(10);  
 }   
 for(brightness = 0; brightness<255; brightness+=fadeAmount){ 
   analogWrite(RGB_GRN, brightness);     
   // wait for 30 milliseconds to see the dimming effect     
   delay(10);  
 } 
 for(brightness = 255; brightness>=0; brightness-=fadeAmount){ 
   analogWrite(RGB_GRN, brightness);     
   // wait for 30 milliseconds to see the dimming effect     
   delay(10);  
 }   
 for(brightness = 0; brightness<255; brightness+=fadeAmount){ 
   analogWrite(RGB_BLU, brightness);     
   // wait for 30 milliseconds to see the dimming effect     
   delay(10);  
 } 
 for(brightness = 255; brightness>=0; brightness-=fadeAmount){ 
   analogWrite(RGB_BLU, brightness);     
   // wait for 30 milliseconds to see the dimming effect     
   delay(10);  
 } 
 myScreen.gText(0,70, "S1 to Finish", colour); 
 while(digitalRead(SW1) == 1); 
 myScreen.gText(0,90, "ALL TESTS PASSED", greenColour); 
 while(1); 
} 

Energia_BLE_Test.ino

Arduino
/************************************************************** 
* Blynk is a platform with iOS and Android apps to control 
* Arduino, Raspberry Pi and the likes over the Internet. 
* You can easily build graphic interfaces for all your 
* projects by simply dragging and dropping widgets. 
* 
*   Downloads, docs, tutorials: http://www.blynk.cc 
/**************************************************************
 * Blynk is a platform with iOS and Android apps to control
 * Arduino, Raspberry Pi and the likes over the Internet.
 * You can easily build graphic interfaces for all your
 * projects by simply dragging and dropping widgets.
 *
 *   Downloads, docs, tutorials: http://www.blynk.cc
 *   Blynk community:            http://community.blynk.cc
 *   Social networks:            http://www.fb.com/blynkapp
 *                               http://twitter.com/blynk_app
 *
 * Blynk library is licensed under MIT license
 * This example code is in public domain.
 *
 **************************************************************
 *
 * This example shows how to use Energia BLE
 * to connect your project to Blynk.
 *
 * NOTE: BLE support is in beta!
 *
 **************************************************************/

//#define BLYNK_DEBUG
#define BLYNK_PRINT Serial
#define BLYNK_RUN_YIELD() { ble.handleEvents(); }

#include <BLE.h>
#include <BlynkSimpleSerialBLE.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";

WidgetLED led1(V4);

// This function tells board what to do if there is a Widget
// which is requesting data for Virtual Pin (4)
BLYNK_READ(V4)
{
  if( digitalRead(PUSH2) == 0){
    Blynk.virtualWrite(V4, 1);
  }
  else {
    Blynk.virtualWrite(V4, 0);
  }
}


// This function will be called every time LED
// in Blynk app writes values to the Virtual Pin 5
BLYNK_WRITE(V5)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  // You can also use:
  // String i = param.asStr();
  // double d = param.asDouble();
  Serial.print("V5 value is: ");
  Serial.println(pinValue);
  if(pinValue == 1){
    digitalWrite(GREEN_LED, HIGH);
    led1.on();
  }
  else if(pinValue == 0){
    digitalWrite(GREEN_LED, LOW);
    led1.off();
  }
}

void setup() {
  Serial.begin(115200);

  ble.setLogLevel(BLE_LOG_ERRORS);
  ble.begin();
  ble.serial();
  ble.setAdvertName("Blynk BLE MSP432");
  ble.startAdvert();

  Blynk.begin(ble, auth);
  pinMode(GREEN_LED, OUTPUT);
  pinMode(PUSH2, INPUT_PULLUP);
}

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

BLEInputOutput.ino

C/C++
//BLEInputOutput.ino
#include <BLE.h>

/* State variables for LED chars */
uint8_t led1Value = 0;
uint8_t led2RedValue = 0;
uint8_t led2GreenValue = 0;
uint8_t led2BlueValue = 0;

/* State Variables for Button Chars */
// Buttons are active low so initial state is 1
uint8_t button1State = 1;
uint8_t button2State = 1;

/* Pin number variables for LEDs on MSP432P401R LaunchPad */
uint8_t led1Pin = 78;
uint8_t led2RedPin = 75;
uint8_t led2GreenPin = 76;
uint8_t led2BluePin = 77;

/* Pin number variables for Buttons on MSP432P401R LaunchPad */
uint8_t button1Pin = 73;
uint8_t button2Pin = 74;

/* Declare Simple LED Service Characteristics here */

// Char 1 requires authentication, this means you will need to
// pair with your phone before being able to write/read from it
BLE_Char led1Char =
{
  {0xF1, 0xFF},                               /* 16 bit BLE UUID = 0xFFF1 */
  BLE_AUTHEN | BLE_WRITABLE| BLE_READABLE,    /* BLE Properties           */
  "LED 1"                                     /* User Description         */
};

BLE_Char led2RedChar =
{
  {0xF2, 0xFF},
  BLE_WRITABLE| BLE_READABLE,
  "LED 2: Red"
};

BLE_Char led2GreenChar =
{
  {0xF3, 0xFF},
  BLE_WRITABLE| BLE_READABLE,
  "LED 2: Green"
};

BLE_Char led2BlueChar =
{
  {0xF4, 0xFF},
  BLE_WRITABLE| BLE_READABLE,
  "LED 2: Blue"
};

/* BLE LED Service is made up of LED Chars */
BLE_Char *simpleLEDServiceChars[] = {&led1Char, &led2RedChar, &led2GreenChar, &led2BlueChar};

/* LED Service Declaration */
BLE_Service simpleLEDService =
{
  {0xF0, 0xFF},
  4, simpleLEDServiceChars
};

BLE_Char button1Char =
{
  {0x01, 0xFF},
  BLE_NOTIFIABLE,
  "Button 1 State"
};

BLE_Char button2Char =
{
  {0x02, 0xFF},
  BLE_NOTIFIABLE,
  "Button 2 State"
};

/* BLE LED Service is made up of LED Chars */
BLE_Char *simpleButtonServiceChars[] = {&button1Char, &button2Char};

/* LED Service Declaration */
BLE_Service simpleButtonService =
{
  {0x00, 0xFF},
  2, simpleButtonServiceChars
};

void setup() {
  Serial.begin(115200);
  ble.setLogLevel(BLE_LOG_ERRORS);
  ble.begin();

  // Initalize I/O
  pinMode(led1Pin, OUTPUT);
  pinMode(led2RedPin, OUTPUT);
  pinMode(led2GreenPin, OUTPUT);
  pinMode(led2BluePin, OUTPUT);

  // Note that the switches on the MSP432P401R LP need pullups
  pinMode(button1Pin, INPUT_PULLUP);
  pinMode(button2Pin, INPUT_PULLUP);

  button1State = digitalRead(button1Pin);
  button2State = digitalRead(button2Pin);

  /* Add and intialize LED service */
  ble.addService(&simpleLEDService);
  ble.writeValue(&led1Char, led1Value);
  ble.writeValue(&led2RedChar, led2RedValue);
  ble.writeValue(&led2GreenChar, led2GreenValue);
  ble.writeValue(&led2BlueChar, led2BlueValue);

  /* Add and initialize Button Service */
  ble.addService(&simpleButtonService);
  ble.writeValue(&button1Char, button1State);
  ble.writeValue(&button2Char, button2State);

  /* Start Advertising */
  ble.setAdvertName("BLE I/O Demo");
  ble.startAdvert();

  /* Setup security params */
  ble.setPairingMode(BLE_SECURITY_WAIT_FOR_REQUEST);
  ble.setIoCapabilities(BLE_DISPLAY_YES_NO);
  ble.useBonding(true);

  /* Print a message to the console */
  Serial.println(" BLE Energia Buttons/LED Demo");
}

// the loop routine runs over and over again forever as a task.
void loop() {
  ble.handleEvents();

  if(ble.isConnected())
  {
    // Read the state of both buttons and send a notification
    // if either is pressed, note that your app will
    // need to register for notifications by writing 01:00
    // to the button char's CCCD
    uint8_t newButton1State = digitalRead(button1Pin);
    uint8_t newButton2State = digitalRead(button2Pin);

    if(button1State != newButton1State)
    {
      button1State = newButton1State;
      Serial.print("Button 1 Val Changed=");Serial.println(button1State);
      ble.writeValue(&button1Char, button1State);
    }

    if(button2State != newButton2State)
    {
      button2State = newButton2State;
      Serial.print("Button 2 Val Changed=");Serial.println(button2State);
      ble.writeValue(&button2Char, button2State);
    }

    // Poll the values of the characteristics in SimpleProfile
    // Print char value if it changed
    if(led1Value != ble.readValue_char(&led1Char))
    {
      led1Value = ble.readValue_char(&led1Char);
      Serial.print("LED 1 Val Changed=");Serial.println(led1Value);
    }

    if(led2RedValue != ble.readValue_char(&led2RedChar))
    {
      led2RedValue = ble.readValue_char(&led2RedChar);
      Serial.print("LED 2 Red Val Changed=");Serial.println(led2RedValue);
    }

    if(led2GreenValue != ble.readValue_char(&led2GreenChar))
    {
      led2GreenValue = ble.readValue_char(&led2GreenChar);
      Serial.print("LED 2 Green Val Changed=");Serial.println(led2GreenValue);
    }

    if(led2BlueValue != ble.readValue_char(&led2BlueChar))
    {
      led2BlueValue = ble.readValue_char(&led2BlueChar);
      Serial.print("LED 2 Blue Val Changed=");Serial.println(led2BlueValue);
    }

    // Set LEDs based on new characteristic values
    // LED1 can only be on or off, mask out all other bits
    digitalWrite(led1Pin, (led1Value & 0x01));
    // Tri color LED can be PWM'd to create different colors
    // Values between 0-255 are acceptable
    analogWrite(led2RedPin, led2RedValue);
    analogWrite(led2GreenPin, led2GreenValue);
    analogWrite(led2BluePin, led2BlueValue);
  }
}

SerialOverBLE.ino

C/C++
//SerialOverBLE.ino
#include <BLE.h>
#include <String.h>

/* Pin number variables for LEDs on MSP432P401R LaunchPad */
uint8_t led1Pin = 78;
uint8_t led2RedPin = 75;
uint8_t led2GreenPin = 76;
uint8_t led2BluePin = 77;

/* +1 for null-terminator */
int numBytes = 0;
char serialData[BLE_SERIAL_BUFFER_SIZE + 1];


void setup() {
  Serial.begin(115200);
  ble.setLogLevel(BLE_LOG_ERRORS);
  // Initalize I/O
  pinMode(led1Pin, OUTPUT);
  pinMode(led2RedPin, OUTPUT);
  pinMode(led2GreenPin, OUTPUT);
  pinMode(led2BluePin, OUTPUT);
  ble.begin();
  ble.serial();
  ble.setAdvertName("Energia Serial");
  ble.startAdvert();
}

void parseSerialString(String dataString)
{
  // Check if we found Adafruit app msg start char
  if('!' == dataString.charAt(0))
  {
    switch (dataString.charAt(1))
    {
      case 'C':
      {
        // RGB color packet is of the form "[!],[C], [R byte], [G Byte], [B byte]"
        Serial.println("Received Adafruit RGB LED Color Packet");
        char redVal = dataString.charAt(2);
        char greenVal = dataString.charAt(3);
        char blueValue = dataString.charAt(4);

        // Use PWM to write the color to the RGB LED
        analogWrite(led2RedPin, redVal);
        analogWrite(led2GreenPin, greenVal);
        analogWrite(led2BluePin, blueValue);
        break;
      }
      case 'B':
      {
        // Buttons packet is of the form: "[!], [B], [Button #] [1 (pressed)/0 (released)]
        String state = (('1' == dataString.charAt(3)) ? " Pressed" : "  Released");
        String result = "Received Adafruit Button Packet: Button ";
        result += dataString.charAt(2) + state;
        Serial.println(result);
      }
      default:
        break;
    }
  }
}

void loop() {
  ble.handleEvents();
  /* Forward Energia serial monitor to BLE serial. */
  if (Serial.available())
  {
    while ((numBytes = Serial.available()))
    {
      Serial.readBytes(serialData, numBytes);
      serialData[numBytes] = '\0';
      Serial.print("Sending via serial:");
      Serial.println(serialData);
      ble.print(serialData);
    }
  }
  if (ble.available())
  {
    /* All the Serial functions are available with BLE serial. */
    String leData = ble.readString();
    parseSerialString(leData);
    Serial.print("Received via BLE:");
    Serial.println(leData);
  }
}

ble-ti-energia.zip

JavaScript
No preview (download only).

Credits

Mark Easley

Mark Easley

65 projects • 137 followers
Texas Instruments LaunchPad SW Engineer

Comments