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 == 'Short Local Name' and value == 'Env': # 「2JCIE-BL01」を見つけ | |
| if dev.addr in devs.keys(): | |
| return | |
| MSG('New %s %s' % (value, dev.addr)) | |
| devThread = EnvSensor(dev) # EnvSensorオブジェクトを生成し | |
| devs[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): | |
| Peripheral.__init__(self) | |
| Thread.__init__(self) | |
| self.setDaemon(True) | |
| self.dev = dev | |
| self.am = ambient.Ambient(channelID, writeKey) |
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 run(self): | |
| while True: | |
| self.connect(self.dev) # デバイスにコネクトする | |
| latestDataRow = self.getCharacteristics(uuid=_OMRON_UUID(0x3001))[0] | |
| dataRow = latestDataRow.read() | |
| send2ambient(self.am, dataRow) | |
| time.sleep(300.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
| def send2ambient(am, dataRow): | |
| (seq, temp, humid, light, uv, press, noise, discom, heat, batt) = struct.unpack('<BhhhhhhhhH', dataRow) | |
| am.send({'d1': temp / 100, 'd2': humid / 100, 'd3': press / 10, 'd4': batt / 1000, 'd5': light, 'd6': noise / 100}) |
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 == 'Manufacturer' and value[0:4] == 'd502': | |
| if value[4:6] != self.lastseq: | |
| self.lastseq = value[4:6] | |
| send2ambient(value[6:]) |
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): | |
| (temp, humid, light, uv, press, noise, accelX, accelY, accelZ, batt) = struct.unpack('<hhhhhhhhhB', bytes.fromhex(dataRow)) | |
| am.send({'d1': temp / 100, 'd2': humid / 100, 'd3': press / 10, 'd4': (batt + 100) / 100, 'd5': light, 'd6': noise / 100}) |
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
| BLEDevice::init("AmbientEnv-01"); // デバイスを初期化 | |
| BLEServer *pServer = BLEDevice::createServer(); // サーバーを生成 | |
| pServer->setCallbacks(new MyServerCallbacks()); // コールバック関数を設定 | |
| BLEService *pService = pServer->createService(SENSOR_UUID); // サービスを生成 | |
| // キャラクタリスティクスを生成 | |
| pService->createCharacteristic(LATESTDATA_UUID, BLECharacteristic::PROPERTY_READ) | |
| ->setCallbacks(new dataCb()); // コールバック関数を設定 | |
| pService->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
| void onRead(BLECharacteristic *pChar) { | |
| uint8_t buf[19]; | |
| bme280.get_sensor_data(&data); // センサーを読む | |
| Serial.printf("temp: %f, humid: %f, press: %f\r\n", data.temperature, data.humidity, data.pressure / 100); | |
| uint16_t temp = (uint16_t)(data.temperature * 100); | |
| uint16_t humid = (uint16_t)(data.humidity * 100); | |
| uint16_t press = (uint16_t)(data.pressure / 10); | |
| memset(buf, 0, sizeof buf); // バッファーを0クリア | |
| buf[0] = seq++; // シーケンス番号をバッファーにセット |
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
| BLEDevice::init("AmbientEnv-02"); // デバイスを初期化 | |
| BLEServer *pServer = BLEDevice::createServer(); // サーバーを生成 | |
| BLEAdvertising *pAdvertising = pServer->getAdvertising(); // アドバタイズオブジェクトを取得 | |
| setAdvData(pAdvertising); // アドバタイジングデーターをセット | |
| pAdvertising->start(); // アドバタイズ起動 | |
| Serial.println("Advertizing started..."); | |
| delay(T_PERIOD * 1000); // T_PERIOD秒アドバタイズする | |
| pAdvertising->stop(); // アドバタイズ停止 |
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 BLEServerCallbacks { | |
| public: | |
| virtual ~BLEServerCallbacks() {}; | |
| virtual void onConnect(BLEServer* pServer); | |
| virtual void onDisconnect(BLEServer* pServer); | |
| }; // BLEServerCallbacks | |
| // 修正版 |