스마트 폰 앱과 Genuino 101 블루투스를 사용한 L 치카로 점등 상태를 sakura.io에서도 수신
18356 단어 벚꽃. 이오Arduino블루투스Genuino101
여기에서는 Genuino 101 × sakura.io Arduino 실드의 LED를 스마트 폰 앱으로 ON/OFF시키고 LED의 상태를 sakura.io로 보냅니다.
전회의 투고 「 sakura.io를 Genuino 101로 시작했다(L치카~sakura.io 접속 테스트) 」의 환경이 구축되고 있는 것을 전제에 가 갑시다.
참고로 한 페이지
절차
블루투스로 L 치카 템플릿 열기
File→Examples→CurieBLE→Peripheral→LED를 엽니다.
필요한 라이브러리 추가
Sketch→Include Library→Wire와 Sketch→Include Library→SakuraIO에서 라이브러리를 추가하고 SakuraIO_I2C를 sakuraio로 로드합니다.
#include <Wire.h>
#include <SakuraIO.h>
SakuraIO_I2C sakuraio;
sakura.io I2C 연결 및 연결 대기 처리 추가
void setup() {
Serial.begin(9600);
Wire.begin(); //<1>
Serial.print("Waiting to come online"); //<2>
for(;;){
if( (sakuraio.getConnectionStatus() & 0x80) == 0x80 ) break;
Serial.print(".");
delay(1000);
}
Serial.println("");
}
// set LED pin to output mode
pinMode(ledPin, OUTPUT);
// begin initialization
BLE.begin();
<1> Serial.begin()
함수로 시리얼 통신을 개시한 후, Wire.begin()
함수로 I2C 통신을 개시
<2> 사쿠라 통신 모듈의 연결 상태가 연결될 때까지 대기
LED 상태 값 전송
loop()
함수 앞에 추가
uint8_t SAKURAIO_CH_LED_STATE = 0; //<1>
uint32_t LED_STATE_OFF = 0; //<2>
uint32_t LED_STATE_ON = 1; //<3>
void loop() {
<1> LED 상태를 확인하기 위한 채널로 sakura.io의 채널 0을 상수로 등록
<2> LED 상태가 OFF인 경우
<3> LED 상태가 ON일 때
if (switchCharacteristic.written()) {
if (switchCharacteristic.value()) { // any value other than 0
Serial.println("LED on");
digitalWrite(ledPin, HIGH); // will turn the LED on
sakuraio.enqueueTx(SAKURAIO_CH_LED_STATE, LED_STATE_ON); //<1>
} else { // a 0 value
Serial.println(F("LED off"));
digitalWrite(ledPin, LOW); // will turn the LED off
sakuraio.enqueueTx(SAKURAIO_CH_LED_STATE, LED_STATE_OFF); //<1>
}
sakuraio.send(); //<2>
}
<1> LED 상태가 변경되면 사쿠라 통신 모듈의 송신 큐에 상태 값을 모은다.
<2> sakura.io 플랫폼에 데이터 보내기
프로그램 완성
Arduino IDE로 쓰자.
led_sakuraio.ino/*
* Copyright (c) 2016 Intel Corporation. All rights reserved.
* See the bottom of this file for the license terms.
*/
/*
* Sketch: led.ino
*
* Description:
* This is a Peripheral sketch that works with a connected Central.
* It allows the Central to write a value and set/reset the led
* accordingly.
*/
#include <Wire.h>
#include <SakuraIO.h>
SakuraIO_I2C sakuraio;
#include <CurieBLE.h>
BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service
// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEUnsignedCharCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
const int ledPin = 13; // pin to use for the LED
void setup() {
Serial.begin(9600);
Wire.begin();
Serial.print("Waiting to come online");
for(;;){
if( (sakuraio.getConnectionStatus() & 0x80) == 0x80 ) break;
Serial.print(".");
delay(1000);
}
Serial.println("");
}
// set LED pin to output mode
pinMode(ledPin, OUTPUT);
// begin initialization
BLE.begin();
// set advertised local name and service UUID:
BLE.setLocalName("LED");
BLE.setAdvertisedService(ledService);
// add the characteristic to the service
ledService.addCharacteristic(switchCharacteristic);
// add service
BLE.addService(ledService);
// set the initial value for the characeristic:
switchCharacteristic.setValue(0);
// start advertising
BLE.advertise();
Serial.println("BLE LED Peripheral");
}
uint8_t SAKURAIO_CH_LED_STATE = 0;
uint32_t LED_STATE_OFF = 0;
uint32_t LED_STATE_ON = 1;
void loop() {
// listen for BLE peripherals to connect:
BLEDevice central = BLE.central();
// if a central is connected to peripheral:
if (central) {
Serial.print("Connected to central: ");
// print the central's MAC address:
Serial.println(central.address());
// while the central is still connected to peripheral:
while (central.connected()) {
// if the remote device wrote to the characteristic,
// use the value to control the LED:
if (switchCharacteristic.written()) {
if (switchCharacteristic.value()) { // any value other than 0
Serial.println("LED on");
digitalWrite(ledPin, HIGH); // will turn the LED on
sakuraio.enqueueTx(SAKURAIO_CH_LED_STATE, LED_STATE_ON);
} else { // a 0 value
Serial.println(F("LED off"));
digitalWrite(ledPin, LOW); // will turn the LED off
sakuraio.enqueueTx(SAKURAIO_CH_LED_STATE, LED_STATE_OFF);
}
sakuraio.send();
}
}
// when the central disconnects, print it out:
Serial.print(F("Disconnected from central: "));
Serial.println(central.address());
}
}
/*
Copyright (c) 2016 Intel Corporation. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
스마트폰과 연결하여 LED를 ON/OFF, sakura.io 플랫폼에서 LED의 상태를 확인
그리고는 Arduino/Genuino 101 CurieBLE LED 을 참고로, 스마트폰 앱의 nRF Connect로 LED라고 표시된 디바이스에 접속해, ON/OFF를 송신해 LED를 점등 소등시키면, sakura.io 플랫폼측에 LED의 스테이트의 값이 보내집니다.
Reference
이 문제에 관하여(스마트 폰 앱과 Genuino 101 블루투스를 사용한 L 치카로 점등 상태를 sakura.io에서도 수신), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/YYouzhen/items/8b6cf7f1dd3a789f53cd
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
#include <Wire.h>
#include <SakuraIO.h>
SakuraIO_I2C sakuraio;
void setup() {
Serial.begin(9600);
Wire.begin(); //<1>
Serial.print("Waiting to come online"); //<2>
for(;;){
if( (sakuraio.getConnectionStatus() & 0x80) == 0x80 ) break;
Serial.print(".");
delay(1000);
}
Serial.println("");
}
// set LED pin to output mode
pinMode(ledPin, OUTPUT);
// begin initialization
BLE.begin();
uint8_t SAKURAIO_CH_LED_STATE = 0; //<1>
uint32_t LED_STATE_OFF = 0; //<2>
uint32_t LED_STATE_ON = 1; //<3>
void loop() {
if (switchCharacteristic.written()) {
if (switchCharacteristic.value()) { // any value other than 0
Serial.println("LED on");
digitalWrite(ledPin, HIGH); // will turn the LED on
sakuraio.enqueueTx(SAKURAIO_CH_LED_STATE, LED_STATE_ON); //<1>
} else { // a 0 value
Serial.println(F("LED off"));
digitalWrite(ledPin, LOW); // will turn the LED off
sakuraio.enqueueTx(SAKURAIO_CH_LED_STATE, LED_STATE_OFF); //<1>
}
sakuraio.send(); //<2>
}
/*
* Copyright (c) 2016 Intel Corporation. All rights reserved.
* See the bottom of this file for the license terms.
*/
/*
* Sketch: led.ino
*
* Description:
* This is a Peripheral sketch that works with a connected Central.
* It allows the Central to write a value and set/reset the led
* accordingly.
*/
#include <Wire.h>
#include <SakuraIO.h>
SakuraIO_I2C sakuraio;
#include <CurieBLE.h>
BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service
// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEUnsignedCharCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
const int ledPin = 13; // pin to use for the LED
void setup() {
Serial.begin(9600);
Wire.begin();
Serial.print("Waiting to come online");
for(;;){
if( (sakuraio.getConnectionStatus() & 0x80) == 0x80 ) break;
Serial.print(".");
delay(1000);
}
Serial.println("");
}
// set LED pin to output mode
pinMode(ledPin, OUTPUT);
// begin initialization
BLE.begin();
// set advertised local name and service UUID:
BLE.setLocalName("LED");
BLE.setAdvertisedService(ledService);
// add the characteristic to the service
ledService.addCharacteristic(switchCharacteristic);
// add service
BLE.addService(ledService);
// set the initial value for the characeristic:
switchCharacteristic.setValue(0);
// start advertising
BLE.advertise();
Serial.println("BLE LED Peripheral");
}
uint8_t SAKURAIO_CH_LED_STATE = 0;
uint32_t LED_STATE_OFF = 0;
uint32_t LED_STATE_ON = 1;
void loop() {
// listen for BLE peripherals to connect:
BLEDevice central = BLE.central();
// if a central is connected to peripheral:
if (central) {
Serial.print("Connected to central: ");
// print the central's MAC address:
Serial.println(central.address());
// while the central is still connected to peripheral:
while (central.connected()) {
// if the remote device wrote to the characteristic,
// use the value to control the LED:
if (switchCharacteristic.written()) {
if (switchCharacteristic.value()) { // any value other than 0
Serial.println("LED on");
digitalWrite(ledPin, HIGH); // will turn the LED on
sakuraio.enqueueTx(SAKURAIO_CH_LED_STATE, LED_STATE_ON);
} else { // a 0 value
Serial.println(F("LED off"));
digitalWrite(ledPin, LOW); // will turn the LED off
sakuraio.enqueueTx(SAKURAIO_CH_LED_STATE, LED_STATE_OFF);
}
sakuraio.send();
}
}
// when the central disconnects, print it out:
Serial.print(F("Disconnected from central: "));
Serial.println(central.address());
}
}
/*
Copyright (c) 2016 Intel Corporation. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
Reference
이 문제에 관하여(스마트 폰 앱과 Genuino 101 블루투스를 사용한 L 치카로 점등 상태를 sakura.io에서도 수신), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/YYouzhen/items/8b6cf7f1dd3a789f53cd텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)