This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def send2ambient(dataRow): # データーから温度、湿度、気圧を取り出し、Ambientに送る | |
| (temp, humid, press) = struct.unpack('<hhh', bytes.fromhex(dataRow)) | |
| ret = am.send({'d1': temp / 100, 'd2': humid / 100, 'd3': press / 10}) | |
| def handleDiscovery(self, dev, isNewDev, isNewData): | |
| if isNewDev or isNewData: | |
| for (adtype, desc, value) in dev.getScanData(): # スキャンデーターを調べる | |
| if desc == 'Manufacturer' and value[0:4] == companyID: # 対象を見つけたら | |
| send2ambient(value[6:]) # データーをAmbientに送る |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def handleDiscovery(self, dev, isNewDev, isNewData): | |
| if isNewDev: | |
| for (adtype, desc, value) in dev.getScanData(): # スキャンデーターを調べる | |
| if desc == 'Complete Local Name' and value == 'AmbientEnv-01': # 対象を見つけたら | |
| if dev.addr in scannedDevs.keys(): # すでに見つけていたらスキップ | |
| return | |
| devThread = EnvSensor(dev) # EnvSensorクラスのインスタンスを生成 | |
| scannedDevs[dev.addr] = devThread | |
| devThread.start() # スレッドを起動 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class EnvSensor(Thread, Peripheral): | |
| def __init__(self, dev): | |
| # 初期化処理 | |
| def run(self): | |
| while True: | |
| while self.isConnected == False: # つながるまでconnectする | |
| self.connect(self.dev) | |
| self.isConnected = True | |
| latestDataRow = self.getCharacteristics(uuid=uuid)[0] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void setup() { | |
| M5.begin(); // M5Stackの初期化 | |
| Serial.begin(115200); // シリアルの初期化 | |
| BLEDevice::init(""); // BLEモジュールの初期化 | |
| pBLEScan = BLEDevice::getScan(); // スキャンオブジェクトを取得 | |
| pBLEScan->setActiveScan(false); // パッシブスキャンに設定 | |
| WiFi.begin(ssid, password); // Wi-Fi APに接続 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void loop() { | |
| bool found = false; | |
| float temp, humid, press; | |
| BLEScanResults foundDevices = pBLEScan->start(3); // 3秒間スキャンする | |
| int count = foundDevices.getCount(); // 受信したデバイス数を取得 | |
| for (int i = 0; i < count; i++) { // 受信したデーターに対して | |
| BLEAdvertisedDevice d = foundDevices.getDevice(i); | |
| if (d.haveManufacturerData()) { // Manufacturer dataがあって | |
| std::string data = d.getManufacturerData(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def handleDiscovery(self, dev, isNewDev, isNewData): | |
| if isNewDev: | |
| for (adtype, desc, value) in dev.getScanData(): # スキャンデーターを調べる | |
| if desc == 'Complete Local Name' and value.startswith('BBC micro:bit'): # 対象を見つけたら | |
| if dev.addr in scannedDevs.keys(): # すでに見つけていたらスキップ | |
| return | |
| devThread = EnvSensor(dev) # EnvSensorクラスのインスタンスを生成 | |
| scannedDevs[dev.addr] = devThread | |
| devThread.start() # スレッドを起動 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def handleDiscovery(self, dev, isNewDev, isNewData): | |
| if isNewDev or isNewData: | |
| for (adtype, desc, value) in dev.getScanData(): | |
| if desc == '16b Service Data' and value[0:4] == 'aafe' and value[8:28] == '000000000000616d6269': | |
| seq = (int(value[32:33], 16) & 0xC) >> 2 | |
| if seq != self.lastseq: # シーケンス番号が前回と違っていたら(新しいデーター) | |
| self.lastseq = seq | |
| temp = int(value[38:40], 16) | |
| sendWithRetry({'d1': temp}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def handleDiscovery(self, dev, isNewDev, isNewData): | |
| if isNewDev or isNewData: | |
| for (adtype, desc, value) in dev.getScanData(): | |
| if desc == '16b Service Data' and value[0:4] == 'aafe' and value[8:28] == '000000000000616d6269': | |
| seq = (int(value[32:33], 16) & 0xC) >> 2 | |
| if seq != self.lastseq: # シーケンス番号が前回と違っていたら(新しいデーター) | |
| self.lastseq = seq | |
| temp = (int(value[37:40], 16) & 0x3FF) / 10 | |
| humid = ((int(value[35:38], 16) & 0xFFC) >> 2) / 10 | |
| press = (int(value[32:35], 16) & 0x3FF) + 400 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <M5Stack.h> | |
| #define PERIOD 10 // 測定周期(秒) | |
| void setup() { | |
| M5.begin(); | |
| M5.Speaker.write(0); // スピーカーをオフする | |
| Serial.begin(115200); | |
| pinMode(35, INPUT); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <M5Stack.h> | |
| #include "Ambient.h" | |
| #define PERIOD 10 // 測定周期(秒) | |
| WiFiClient client; | |
| Ambient ambient; | |
| const char* ssid = "ssid"; | |
| const char* password = "password"; |