Skip to content

Commit

Permalink
Merge pull request #73 from NUTFes/feat/nose/receive-sensor
Browse files Browse the repository at this point in the history
センサーの出力を受け取る
  • Loading branch information
RyokiOnodera authored Aug 22, 2024
2 parents 1db4d9d + 39e0227 commit 8e02024
Show file tree
Hide file tree
Showing 30 changed files with 3,978 additions and 0 deletions.
184 changes: 184 additions & 0 deletions raspi/sensor/ESP_BLE/ESP_BLE.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>

//---------------------------------------------------------
// Constants
//---------------------------------------------------------
#define SERVICE_UUID "55725ac1-066c-48b5-8700-2d9fb3603c5e"
#define CHARACTERISTIC_UUID "69ddb59c-d601-4ea4-ba83-44f679a670ba"
#define BLE_DEVICE_NAME "MyBLEDevice"
#define LED_PIN 22
#define BUTTON_PIN 23
#define echoPin 16 // Echo Pin
#define trigPin 17 // Trigger Pin

//---------------------------------------------------------
// Variables
//---------------------------------------------------------
BLEServer *pServer = NULL;
BLECharacteristic *pCharacteristic = NULL;
bool deviceConnected = false;
bool oldDeviceConnected = false;
std::string rxValue;
std::string txValue;
bool bleOn = false;
bool buttonPressed = false;
int carFlag = 0;

double Duration = 0; //受信した間隔
double Distance = 0; //距離
double thresholdDistance = 50; //検出閾値[cm]

//---------------------------------------------------------
// Callbacks
//---------------------------------------------------------
class MyServerCallbacks : public BLEServerCallbacks {
void onConnect(BLEServer *pServer) {
deviceConnected = true;
Serial.println("onConnect");
};
void onDisconnect(BLEServer *pServer) {
deviceConnected = false;
Serial.println("onDisconnect");
}
};

class MyCharacteristicCallbacks : public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
Serial.println("onWrite");
std::string rxValue = pCharacteristic->getValue();
if (rxValue.length() > 0) {
bleOn = rxValue[0] != 0;
Serial.print("Received Value: ");
for (int i = 0; i < rxValue.length(); i++) {
Serial.print(rxValue[i], HEX);
}
Serial.println();
}
}
};

//---------------------------------------------------------
void setup() {
//
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);

pinMode( echoPin, INPUT );
pinMode( trigPin, OUTPUT );

Serial.begin(115200);
//
BLEDevice::init(BLE_DEVICE_NAME);
// Server
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Service
BLEService *pService = pServer->createService(SERVICE_UUID);
// Characteristic
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY);
pCharacteristic->setCallbacks(new MyCharacteristicCallbacks());
pCharacteristic->addDescriptor(new BLE2902());
//
pService->start();
// Advertising
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(false);
pAdvertising->setMinPreferred(0x0);
BLEDevice::startAdvertising();
Serial.println("startAdvertising");
}

//---------------------------------------------------------
void loop() {
// disconnecting
if (!deviceConnected && oldDeviceConnected) {
delay(500); // give the bluetooth stack the chance to get things ready
pServer->startAdvertising();
Serial.println("restartAdvertising");
oldDeviceConnected = deviceConnected;
}
// connecting
if (deviceConnected && !oldDeviceConnected) {
oldDeviceConnected = deviceConnected;
}
// LED
digitalWrite(LED_PIN, bleOn ? HIGH : LOW);

//超音波センサ
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite( trigPin, HIGH ); //超音波を出力
delayMicroseconds( 10 ); //
digitalWrite( trigPin, LOW );
Duration = pulseIn( echoPin, HIGH ); //センサからの入力

if (Duration > 0) {
Duration = Duration/2; //往復距離を半分にする
Distance = Duration*340*100/1000000; // 音速を340m/sに設定
//Distance[cm]
if (Distance >= thresholdDistance) {
carFlag = 0;
String str = "CarFlag:" + String(carFlag);
Serial.println(str);
// Notify
if (deviceConnected) {
txValue = str.c_str();
pCharacteristic->setValue(txValue);
pCharacteristic->notify();
}
// buttonPressed = true;
delay(50);
}
else {
carFlag = 1;
String str = "CarFlag:" + String(carFlag);
Serial.println(str);
// Notify
if (deviceConnected) {
txValue = str.c_str();
pCharacteristic->setValue(txValue);
pCharacteristic->notify();
}
// buttonPressed = true;
delay(50);
}
}
else{
carFlag = 0;
String str = "CarFlag:" + String(carFlag);
Serial.println(str);
// Notify
if (!deviceConnected) {
txValue = str.c_str();
pCharacteristic->setValue(txValue);
pCharacteristic->notify();
}
// buttonPressed = false;
delay(50);
}
// Serial.println(Distance);
// if (_hoge = 1) {

// }

// _hoge = 1;


// Serial.println("hoge");
// carFlag = 2;
// String str = "CarFlag:" + String(carFlag);
// Serial.println(str);
// // Notify
// if (deviceConnected) {
// txValue = str.c_str();
// pCharacteristic->setValue(txValue);
// pCharacteristic->notify();
// }
delay(1000);
}
21 changes: 21 additions & 0 deletions raspi/sensor/handson-sample/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 sakura.io

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
83 changes: 83 additions & 0 deletions raspi/sensor/handson-sample/arduino/IBMcloud-part2-LED.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// ヘッダファイル指定 Including header files
#include <SakuraIO.h>

// LEDの定義 Definition of LED
#define LED_1 7
#define LED_2 6
#define LED_3 5

// 変数の定義 Definition of variables
SakuraIO_I2C sakuraio;
uint32_t cnt = 0;

// 起動時に1回だけ実行 Run once at startup
void setup() {
Serial.begin(9600);
Serial.print("Waiting to come online");
for (;;) {
if ( (sakuraio.getConnectionStatus() & 0x80) == 0x80 ) break;
Serial.print(".");
delay(1000);
}
Serial.println("");
pinMode(LED_1, OUTPUT);
pinMode(LED_2, OUTPUT);
pinMode(LED_3, OUTPUT);
}

// 以下ループ実行 Loop execution
void loop() {

// cnt値のカウントアップ Count up cnt value
cnt++;
Serial.println(cnt);

// データの即時送信 Immediate transmission of data
if (sakuraio.sendImmediately(0, cnt) != CMD_ERROR_NONE) {
Serial.println("[ERR] enqueue error");
}
digitalWrite(LED_3, HIGH);
delay(500);
digitalWrite(LED_3, LOW);
delay(500);

// 利用可能な領域(Available)とデータが格納された領域(Queued)の取得 Get Available and Queued
uint8_t available;
uint8_t queued;
if (sakuraio.getTxQueueLength(&available, &queued) != CMD_ERROR_NONE) {
Serial.println("[ERR] get tx queue length error");
}
Serial.print("Available :");
Serial.print(available);
Serial.print(" Queued :");
Serial.println(queued);

// sakura.ioからの受信データに応じたLED点灯(Digital7,6) Lights the LED according to received data
available = 0;
if (sakuraio.getRxQueueLength(&available, &queued) != CMD_ERROR_NONE) {
Serial.println("[ERR] get rx queue length error");
}
if (available > 0)
{
for (int i = 0; i < available; i++)
{
uint8_t ch, type, value[8];
int64_t offset;
sakuraio.dequeueRx(&ch, &type, value, &offset);
if (ch == 0) {
if (value[0] == 1) {
digitalWrite(LED_1, HIGH);
} else {
digitalWrite(LED_1, LOW);
}
} else if (ch == 1) {
if (value[0] == 1) {
digitalWrite(LED_2, HIGH);
} else {
digitalWrite(LED_2, LOW);
}
}
}
sakuraio.clearRx();
}
}
Loading

0 comments on commit 8e02024

Please sign in to comment.