From e24ddeff58bf5d3dcc472d9be96d4802f939e762 Mon Sep 17 00:00:00 2001 From: PSVM-J <39494832+PSVM-J@users.noreply.github.com> Date: Thu, 23 Aug 2018 22:31:04 +0300 Subject: [PATCH 1/2] new module TroykaMeteoSensor --- modules/@amperka/meteo-sensor.js | 113 +++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 modules/@amperka/meteo-sensor.js diff --git a/modules/@amperka/meteo-sensor.js b/modules/@amperka/meteo-sensor.js new file mode 100644 index 0000000..5bdd8a3 --- /dev/null +++ b/modules/@amperka/meteo-sensor.js @@ -0,0 +1,113 @@ +function TroykaMeteoSensor(opts) { + opts = opts || {}; + var _i2c = opts.i2c || PrimaryI2C; + var _address = opts.address || 0x44; + var _repeatability = opts.repeatability || 'HIGH'; + var _clockStretching = opts.clockStretching || 'ON'; + + this.setRepeatability = function(value) { + _repeatability = value; + }; + + this.clockStretchingOn = function() { + _clockStretching = "ON" + }; + + this.clockStretchingOff = function() { + _clockStretching = "OFF" + }; + + this.reset = function() { + _i2c.writeTo(_address, [0x30, 0xA2]); + }; + + this.heaterOn = function() { + _i2c.writeTo(_address, [0x30, 0x6D]); + }; + + this.heaterOff = function() { + _i2c.writeTo(_address, [0x30, 0x66]); + }; + + this.read = function(callback) { + if (_clockStretching == 'ON') { + switch (_repeatability) { + case 'HIGH': + _i2c.writeTo(_address, [0x2C, 0x06]); + break; + case 'MEDIUM': + _i2c.writeTo(_address, [0x2C, 0x0D]); + break; + case 'LOW': + _i2c.writeTo(_address, [0x2C, 0x10]); + break; + default: + _i2c.writeTo(_address, [0x2C, 0x06]); + } + } else { + switch (_repeatability) { + case 'HIGH': + _i2c.writeTo(_address, [0x24, 0x00]); + break; + case 'MEDIUM': + _i2c.writeTo(_address, [0x24, 0x0B]); + break; + case 'LOW': + _i2c.writeTo(_address, [0x24, 0x16]); + break; + default: + _i2c.writeTo(_address, [0x24, 0x00]); + } + } + var data = _i2c.readFrom(_address, 6); + if (data[2] != _checkCRC8(data, 0, 2) || data[5] != _checkCRC8(data, 3, 5)) { + callback(new Error('checksum error'), { + tempC: -1, + tempF: -1, + tempK: -1, + }) + } else { + var tmpC = ((((data[0] * 256.0) + data[1]) * 175.0) / 65535.0) - 45.0; + var tmpF = (tmpC * 9.0 / 5.0) + 32.0; + var tmpK = tmpC + 273.15; + var hum = ((((data[3] * 256.0) + data[4]) * 100.0) / 65535.0); + callback(null, { + tempC: tmpC, + tempF: tmpF, + tempK: tmpK, + humidity: hum + }) + } + }; + + var _checkCRC8 = function(data, num, len) { + var crc = 0xFF + var table = [ + 0, 49, 98, 83, 196, 245, 166, 151, 185, 136, 219, 234, 125, 76, 31, 46, + 67, 114, 33, 16, 135, 182, 229, 212, 250, 203, 152, 169, 62, 15, 92, 109, + 134, 183, 228, 213, 66, 115, 32, 17, 63, 14, 93, 108, 251, 202, 153, 168, + 197, 244, 167, 150, 1, 48, 99, 82, 124, 77, 30, 47, 184, 137, 218, 235, + 61, 12, 95, 110, 249, 200, 155, 170, 132, 181, 230, 215, 64, 113, 34, 19, + 126, 79, 28, 45, 186, 139, 216, 233, 199, 246, 165, 148, 3, 50, 97, 80, + 187, 138, 217, 232, 127, 78, 29, 44, 2, 51, 96, 81, 198, 247, 164, 149, + 248, 201, 154, 171, 60, 13, 94, 111, 65, 112, 35, 18, 133, 180, 231, 214, + 122, 75, 24, 41, 190, 143, 220, 237, 195, 242, 161, 144, 7, 54, 101, 84, + 57, 8, 91, 106, 253, 204, 159, 174, 128, 177, 226, 211, 68, 117, 38, 23, + 252, 205, 158, 175, 56, 9, 90, 107, 69, 116, 39, 22, 129, 176, 227, 210, + 191, 142, 221, 236, 123, 74, 25, 40, 6, 55, 100, 85, 194, 243, 160, 145, + 71, 118, 37, 20, 131, 178, 225, 208, 254, 207, 156, 173, 58, 11, 88, 105, + 4, 53, 102, 87, 192, 241, 162, 147, 189, 140, 223, 238, 121, 72, 27, 42, + 193, 240, 163, 146, 5, 52, 103, 86, 120, 73, 26, 43, 188, 141, 222, 239, + 130, 179, 224, 209, 70, 119, 36, 21, 59, 10, 89, 104, 255, 206, 157, 172 + ]; + + for (var i = num; i < len; i++) { + crc = table[(crc ^ data[i]) % 256] + } + return crc; + }; +} + +exports.connect = function(opts) { + return new TroykaMeteoSensor(opts); +} From 6cb48c35a861444f881f69a9cda788a95a7276f0 Mon Sep 17 00:00:00 2001 From: PSVM-J <39494832+PSVM-J@users.noreply.github.com> Date: Mon, 27 Aug 2018 14:19:03 +0300 Subject: [PATCH 2/2] =?UTF-8?q?no=20=C2=96clockStretching?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/@amperka/meteo-sensor.js | 97 +++++++++++++++----------------- 1 file changed, 45 insertions(+), 52 deletions(-) diff --git a/modules/@amperka/meteo-sensor.js b/modules/@amperka/meteo-sensor.js index 5bdd8a3..2955055 100644 --- a/modules/@amperka/meteo-sensor.js +++ b/modules/@amperka/meteo-sensor.js @@ -3,20 +3,11 @@ function TroykaMeteoSensor(opts) { var _i2c = opts.i2c || PrimaryI2C; var _address = opts.address || 0x44; var _repeatability = opts.repeatability || 'HIGH'; - var _clockStretching = opts.clockStretching || 'ON'; this.setRepeatability = function(value) { _repeatability = value; }; - this.clockStretchingOn = function() { - _clockStretching = "ON" - }; - - this.clockStretchingOff = function() { - _clockStretching = "OFF" - }; - this.reset = function() { _i2c.writeTo(_address, [0x30, 0xA2]); }; @@ -30,53 +21,55 @@ function TroykaMeteoSensor(opts) { }; this.read = function(callback) { - if (_clockStretching == 'ON') { - switch (_repeatability) { - case 'HIGH': + + switch (_repeatability) { + case 'HIGH': + try { _i2c.writeTo(_address, [0x2C, 0x06]); - break; - case 'MEDIUM': + } catch (e) { + callback(new Error('I2C write error'), undefined); + } + break; + case 'MEDIUM': + try { _i2c.writeTo(_address, [0x2C, 0x0D]); - break; - case 'LOW': + } catch (e) { + callback(new Error('I2C write error'), undefined); + } + break; + case 'LOW': + try { _i2c.writeTo(_address, [0x2C, 0x10]); - break; - default: + } catch (e) { + callback(new Error('I2C write error'), undefined); + } + break; + default: + try { _i2c.writeTo(_address, [0x2C, 0x06]); - } - } else { - switch (_repeatability) { - case 'HIGH': - _i2c.writeTo(_address, [0x24, 0x00]); - break; - case 'MEDIUM': - _i2c.writeTo(_address, [0x24, 0x0B]); - break; - case 'LOW': - _i2c.writeTo(_address, [0x24, 0x16]); - break; - default: - _i2c.writeTo(_address, [0x24, 0x00]); - } + } catch (e) { + callback(new Error('I2C write error'), undefined); + } } - var data = _i2c.readFrom(_address, 6); - if (data[2] != _checkCRC8(data, 0, 2) || data[5] != _checkCRC8(data, 3, 5)) { - callback(new Error('checksum error'), { - tempC: -1, - tempF: -1, - tempK: -1, - }) - } else { - var tmpC = ((((data[0] * 256.0) + data[1]) * 175.0) / 65535.0) - 45.0; - var tmpF = (tmpC * 9.0 / 5.0) + 32.0; - var tmpK = tmpC + 273.15; - var hum = ((((data[3] * 256.0) + data[4]) * 100.0) / 65535.0); - callback(null, { - tempC: tmpC, - tempF: tmpF, - tempK: tmpK, - humidity: hum - }) + + try { + var data = _i2c.readFrom(_address, 6); + if (data[2] != _checkCRC8(data, 0, 2) || data[5] != _checkCRC8(data, 3, 5)) { + callback(new Error('checksum error'), undefined); + } else { + var tmpC = ((((data[0] * 256.0) + data[1]) * 175.0) / 65535.0) - 45.0; + var tmpF = (tmpC * 9.0 / 5.0) + 32.0; + var tmpK = tmpC + 273.15; + var hum = ((((data[3] * 256.0) + data[4]) * 100.0) / 65535.0); + callback(null, { + tempC: tmpC, + tempF: tmpF, + tempK: tmpK, + humidity: hum + }); + } + } catch (e) { + callback(new Error('I2C read error', undefined)); } }; @@ -102,7 +95,7 @@ function TroykaMeteoSensor(opts) { ]; for (var i = num; i < len; i++) { - crc = table[(crc ^ data[i]) % 256] + crc = table[(crc ^ data[i]) % 256]; } return crc; };