Merge pull request #321 from Mickaelh51/development
Add Oregon decoder library
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
/**
|
||||
* The MySensors Arduino library handles the wireless radio link and protocol
|
||||
* between your home built sensors/actuators and HA controller of choice.
|
||||
* The sensors forms a self healing radio network with optional repeaters. Each
|
||||
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
|
||||
* network topology allowing messages to be routed to nodes.
|
||||
*
|
||||
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
|
||||
* Copyright (C) 2013-2015 Sensnology AB
|
||||
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
|
||||
*
|
||||
* Documentation: http://www.mysensors.org
|
||||
* Support Forum: http://forum.mysensors.org
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* version 2 as published by the Free Software Foundation.
|
||||
*
|
||||
*******************************
|
||||
*
|
||||
* REVISION HISTORY
|
||||
* WARNING: I use MySensors V1.6.0 (dev branch) (https://github.com/mysensors/Arduino/tree/development/libraries)
|
||||
*
|
||||
* Version 1.0 - Hubert Mickael <mickael@winlux.fr> (https://github.com/Mickaelh51)
|
||||
* - Clean ino code
|
||||
* - Add MY_DEBUG mode in library
|
||||
* Version 0.2 (Beta 2) - Hubert Mickael <mickael@winlux.fr> (https://github.com/Mickaelh51)
|
||||
* - Auto detect Oregon 433Mhz
|
||||
* - Add battery level
|
||||
* - etc ...
|
||||
* Version 0.1 (Beta 1) - Hubert Mickael <mickael@winlux.fr> (https://github.com/Mickaelh51)
|
||||
*
|
||||
*******************************
|
||||
* DESCRIPTION
|
||||
* This sketch provides an example how to implement a humidity/temperature from Oregon sensor.
|
||||
* - Oregon sensor's battery level
|
||||
* - Oregon sensor's id
|
||||
* - Oregon sensor's type
|
||||
* - Oregon sensor's channel
|
||||
* - Oregon sensor's temperature
|
||||
* - Oregon sensor's humidity
|
||||
*
|
||||
* MySensors gateway <=======> Arduino UNO <-- (PIN 2) --> 433Mhz receiver <=============> Oregon sensors
|
||||
*/
|
||||
|
||||
// Enable debug prints
|
||||
#define MY_DEBUG
|
||||
|
||||
#define MY_NODE_ID 10
|
||||
|
||||
// Enable and select radio type attached
|
||||
#define MY_RADIO_NRF24
|
||||
//#define MY_RADIO_RFM69
|
||||
|
||||
#include <SPI.h>
|
||||
#include <MySensor.h>
|
||||
#include <Oregon.h>
|
||||
|
||||
//Define pin where is 433Mhz receiver (here, pin 2)
|
||||
#define MHZ_RECEIVER_PIN 2
|
||||
//Define maximum Oregon sensors (here, 3 differents sensors)
|
||||
#define COUNT_OREGON_SENSORS 3
|
||||
|
||||
#define CHILD_ID_HUM 0
|
||||
#define CHILD_ID_TEMP 1
|
||||
#define CHILD_ID_BAT 2
|
||||
|
||||
MyMessage msgHum(CHILD_ID_HUM, V_HUM);
|
||||
MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
|
||||
MyMessage msgBat(CHILD_ID_BAT, V_VAR1);
|
||||
|
||||
void setup ()
|
||||
{
|
||||
|
||||
Serial.println("Setup started");
|
||||
|
||||
//Setup received data
|
||||
attachInterrupt(digitalPinToInterrupt(MHZ_RECEIVER_PIN), ext_int_1, CHANGE);
|
||||
|
||||
Serial.println("Setup completed");
|
||||
}
|
||||
|
||||
void presentation()
|
||||
{
|
||||
// Send the Sketch Version Information to the Gateway
|
||||
sendSketchInfo("Oregon Sensor", "1.0");
|
||||
|
||||
// Present all sensors to controller
|
||||
for (int i=0; i<COUNT_OREGON_SENSORS; i++) {
|
||||
present(i, S_TEMP);
|
||||
present(i, S_HUM);
|
||||
present(i, S_CUSTOM); //battery level
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void loop () {
|
||||
//------------------------------------------
|
||||
//Start process new data from Oregon sensors
|
||||
//------------------------------------------
|
||||
cli();
|
||||
word p = pulse;
|
||||
pulse = 0;
|
||||
sei();
|
||||
if (p != 0)
|
||||
{
|
||||
if (orscV2.nextPulse(p))
|
||||
{
|
||||
//Decode Hex Data once
|
||||
const byte* DataDecoded = DataToDecoder(orscV2);
|
||||
//Find or save Oregon sensors's ID
|
||||
int SensorID = FindSensor(id(DataDecoded),COUNT_OREGON_SENSORS);
|
||||
|
||||
// just for DEBUG
|
||||
OregonType(DataDecoded);
|
||||
channel(DataDecoded);
|
||||
|
||||
//Send messages to MySenors Gateway
|
||||
send(msgTemp.setSensor(SensorID).set(temperature(DataDecoded), 1));
|
||||
send(msgHum.setSensor(SensorID).set(humidity(DataDecoded), 1));
|
||||
send(msgBat.setSensor(SensorID).set(battery(DataDecoded), 1));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
With git:
|
||||
1) git clone https://github.com/Mickaelh51/Arduino-Oregon-Library.git Oregon
|
||||
2) Put the "Oregon" folder in your Arduino "libraries" folder.
|
||||
|
||||
Direct download:
|
||||
1) Download https://github.com/Mickaelh51/Arduino-Oregon-Library/archive/master.zip
|
||||
2) Extract master.zip
|
||||
3) rename folder: mv Arduino-Oregon-Library-master Oregon
|
||||
4) Put the "Oregon" folder in your Arduino "libraries" folder.
|
||||
@@ -0,0 +1,9 @@
|
||||
##The MIT License (MIT)
|
||||
|
||||
####Copyright (c) 2015: Mickael Hubert, Dominique Pierre, Olivier Lebrun
|
||||
|
||||
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.
|
||||
@@ -0,0 +1,65 @@
|
||||
# Arduino Oregon Library:
|
||||
##Tested Hardware:
|
||||
- Arduino UNO
|
||||
- 433Mhz receiver (connected on digital pin 2)
|
||||
- Oregon sensors THGR228N
|
||||
|
||||
## Example without MySensors:
|
||||
Serial output:
|
||||
```
|
||||
Brute Hexadecimal data from sensor:
|
||||
1A2D10E2711860043CDA
|
||||
Oregon ID: 226 Hexadecimal: E2
|
||||
Sensor ID: 226 has been find in position EEPROM: 0
|
||||
Oregon model: THGR228N
|
||||
Oregon channel: 1
|
||||
Oregon temperature: 18.70
|
||||
Oregon humidity: 46
|
||||
Oregon battery level: 90
|
||||
|
||||
----
|
||||
|
||||
Brute Hexadecimal data from sensor:
|
||||
1A2D10B4201340052F43
|
||||
Oregon ID: 180 Hexadecimal: B4
|
||||
Sensor ID: 180 has been find in position EEPROM: 1
|
||||
Oregon model: THGR228N
|
||||
Oregon channel: 1
|
||||
Oregon temperature: 13.20
|
||||
Oregon humidity: 54
|
||||
Oregon battery level: 90
|
||||
```
|
||||
|
||||
## Example with MySensors library (www.mysensors.org):
|
||||
Serial output:
|
||||
```
|
||||
Brute Hexadecimal data from sensor:
|
||||
1A2D10E2711860043CDA
|
||||
Oregon ID: 226 Hexadecimal: E2
|
||||
Sensor ID: 226 has been find in position EEPROM: 0
|
||||
Oregon model: THGR228N
|
||||
Oregon channel: 1
|
||||
Oregon temperature: 18.70
|
||||
send: 10-10-0-0 s=0,c=1,t=0,pt=7,l=5,sg=0,st=ok:18.7
|
||||
Oregon humidity: 46
|
||||
send: 10-10-0-0 s=0,c=1,t=1,pt=7,l=5,sg=0,st=ok:46.0
|
||||
Oregon battery level: 90
|
||||
send: 10-10-0-0 s=0,c=1,t=24,pt=7,l=5,sg=0,st=ok:90.0
|
||||
|
||||
----
|
||||
|
||||
Brute Hexadecimal data from sensor:
|
||||
1A2D10B4201340052F43
|
||||
Oregon ID: 180 Hexadecimal: B4
|
||||
Sensor ID: 180 has been find in position EEPROM: 1
|
||||
Oregon model: THGR228N
|
||||
Oregon channel: 1
|
||||
Oregon temperature: 13.20
|
||||
send: 10-10-0-0 s=1,c=1,t=0,pt=7,l=5,sg=0,st=ok:13.2
|
||||
Oregon humidity: 54
|
||||
send: 10-10-0-0 s=1,c=1,t=1,pt=7,l=5,sg=0,st=ok:54.0
|
||||
Oregon battery level: 90
|
||||
send: 10-10-0-0 s=1,c=1,t=24,pt=7,l=5,sg=0,st=ok:90.0
|
||||
```
|
||||
## Result on OpenHAB controller (With MySensors)
|
||||

|
||||
@@ -0,0 +1,126 @@
|
||||
/**
|
||||
* The MySensors Arduino library handles the wireless radio link and protocol
|
||||
* between your home built sensors/actuators and HA controller of choice.
|
||||
* The sensors forms a self healing radio network with optional repeaters. Each
|
||||
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
|
||||
* network topology allowing messages to be routed to nodes.
|
||||
*
|
||||
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
|
||||
* Copyright (C) 2013-2015 Sensnology AB
|
||||
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
|
||||
*
|
||||
* Documentation: http://www.mysensors.org
|
||||
* Support Forum: http://forum.mysensors.org
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* version 2 as published by the Free Software Foundation.
|
||||
*
|
||||
*******************************
|
||||
*
|
||||
* REVISION HISTORY
|
||||
* WARNING: I use MySensors V1.6.0 (dev branch) (https://github.com/mysensors/Arduino/tree/development/libraries)
|
||||
*
|
||||
* Version 1.0 - Hubert Mickael <mickael@winlux.fr> (https://github.com/Mickaelh51)
|
||||
* - Clean ino code
|
||||
* - Add MY_DEBUG mode in library
|
||||
* Version 0.2 (Beta 2) - Hubert Mickael <mickael@winlux.fr> (https://github.com/Mickaelh51)
|
||||
* - Auto detect Oregon 433Mhz
|
||||
* - Add battery level
|
||||
* - etc ...
|
||||
* Version 0.1 (Beta 1) - Hubert Mickael <mickael@winlux.fr> (https://github.com/Mickaelh51)
|
||||
*
|
||||
*******************************
|
||||
* DESCRIPTION
|
||||
* This sketch provides an example how to implement a humidity/temperature from Oregon sensor.
|
||||
* - Oregon sensor's battery level
|
||||
* - Oregon sensor's id
|
||||
* - Oregon sensor's type
|
||||
* - Oregon sensor's channel
|
||||
* - Oregon sensor's temperature
|
||||
* - Oregon sensor's humidity
|
||||
*
|
||||
* MySensors gateway <=======> Arduino UNO <-- (PIN 2) --> 433Mhz receiver <=============> Oregon sensors
|
||||
*/
|
||||
|
||||
// Enable debug prints
|
||||
#define MY_DEBUG
|
||||
|
||||
#define MY_NODE_ID 10
|
||||
|
||||
// Enable and select radio type attached
|
||||
#define MY_RADIO_NRF24
|
||||
//#define MY_RADIO_RFM69
|
||||
|
||||
#include <SPI.h>
|
||||
#include <MySensor.h>
|
||||
#include <Oregon.h>
|
||||
|
||||
//Define pin where is 433Mhz receiver (here, pin 2)
|
||||
#define MHZ_RECEIVER_PIN 2
|
||||
//Define maximum Oregon sensors (here, 3 differents sensors)
|
||||
#define COUNT_OREGON_SENSORS 3
|
||||
|
||||
#define CHILD_ID_HUM 0
|
||||
#define CHILD_ID_TEMP 1
|
||||
#define CHILD_ID_BAT 2
|
||||
|
||||
MyMessage msgHum(CHILD_ID_HUM, V_HUM);
|
||||
MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
|
||||
MyMessage msgBat(CHILD_ID_BAT, V_VAR1);
|
||||
|
||||
void setup ()
|
||||
{
|
||||
|
||||
Serial.println("Setup started");
|
||||
|
||||
//Setup received data
|
||||
attachInterrupt(digitalPinToInterrupt(MHZ_RECEIVER_PIN), ext_int_1, CHANGE);
|
||||
|
||||
Serial.println("Setup completed");
|
||||
}
|
||||
|
||||
void presentation()
|
||||
{
|
||||
// Send the Sketch Version Information to the Gateway
|
||||
sendSketchInfo("Oregon Sensor", "1.0");
|
||||
|
||||
// Present all sensors to controller
|
||||
for (int i=0; i<COUNT_OREGON_SENSORS; i++) {
|
||||
present(i, S_TEMP);
|
||||
present(i, S_HUM);
|
||||
present(i, S_CUSTOM); //battery level
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void loop () {
|
||||
//------------------------------------------
|
||||
//Start process new data from Oregon sensors
|
||||
//------------------------------------------
|
||||
cli();
|
||||
word p = pulse;
|
||||
pulse = 0;
|
||||
sei();
|
||||
if (p != 0)
|
||||
{
|
||||
if (orscV2.nextPulse(p))
|
||||
{
|
||||
//Decode Hex Data once
|
||||
const byte* DataDecoded = DataToDecoder(orscV2);
|
||||
//Find or save Oregon sensors's ID
|
||||
int SensorID = FindSensor(id(DataDecoded),COUNT_OREGON_SENSORS);
|
||||
|
||||
// just for DEBUG
|
||||
OregonType(DataDecoded);
|
||||
channel(DataDecoded);
|
||||
|
||||
//Send messages to MySenors Gateway
|
||||
send(msgTemp.setSensor(SensorID).set(temperature(DataDecoded), 1));
|
||||
send(msgHum.setSensor(SensorID).set(humidity(DataDecoded), 1));
|
||||
send(msgBat.setSensor(SensorID).set(battery(DataDecoded), 1));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
*******************************
|
||||
*
|
||||
* Version 1.0 - Hubert Mickael <mickael@winlux.fr> (https://github.com/Mickaelh51)
|
||||
* - Clean ino code
|
||||
* - Add MY_DEBUG mode in library
|
||||
* Version 0.2 (Beta 2) - Hubert Mickael <mickael@winlux.fr> (https://github.com/Mickaelh51)
|
||||
* - Auto detect Oregon 433Mhz
|
||||
* - Add battery level
|
||||
* - etc ...
|
||||
* Version 0.1 (Beta 1) - Hubert Mickael <mickael@winlux.fr> (https://github.com/Mickaelh51)
|
||||
*
|
||||
*******************************
|
||||
* DESCRIPTION
|
||||
* This sketch provides an example how to implement a humidity/temperature from Oregon sensor.
|
||||
* - Oregon sensor's battery level
|
||||
* - Oregon sensor's id
|
||||
* - Oregon sensor's type
|
||||
* - Oregon sensor's channel
|
||||
* - Oregon sensor's temperature
|
||||
* - Oregon sensor's humidity
|
||||
*
|
||||
* Arduino UNO <-- (PIN 2) --> 433Mhz receiver <=============> Oregon sensors
|
||||
*/
|
||||
|
||||
// Enable debug prints
|
||||
#define MY_DEBUG
|
||||
|
||||
#include <SPI.h>
|
||||
#include <EEPROM.h>
|
||||
#include <Oregon.h>
|
||||
|
||||
//Define pin where is 433Mhz receiver (here, pin 2)
|
||||
#define MHZ_RECEIVER_PIN 2
|
||||
//Define maximum Oregon sensors (here, 3 differents sensors)
|
||||
#define COUNT_OREGON_SENSORS 3
|
||||
|
||||
void setup ()
|
||||
{
|
||||
|
||||
Serial.println("Setup started");
|
||||
|
||||
//Setup received data
|
||||
attachInterrupt(digitalPinToInterrupt(MHZ_RECEIVER_PIN), ext_int_1, CHANGE);
|
||||
|
||||
Serial.println("Setup completed");
|
||||
}
|
||||
|
||||
|
||||
void loop () {
|
||||
//------------------------------------------
|
||||
//Start process new data from Oregon sensors
|
||||
//------------------------------------------
|
||||
cli();
|
||||
word p = pulse;
|
||||
pulse = 0;
|
||||
sei();
|
||||
if (p != 0)
|
||||
{
|
||||
if (orscV2.nextPulse(p))
|
||||
{
|
||||
//Decode Hex Data once
|
||||
const byte* DataDecoded = DataToDecoder(orscV2);
|
||||
//Find or save Oregon sensors's ID
|
||||
int SensorID = FindSensor(id(DataDecoded),COUNT_OREGON_SENSORS);
|
||||
|
||||
// just for DEBUG
|
||||
OregonType(DataDecoded);
|
||||
channel(DataDecoded);
|
||||
temperature(DataDecoded);
|
||||
humidity(DataDecoded);
|
||||
battery(DataDecoded);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
#######################################
|
||||
# Syntax Coloring Map For Oregon
|
||||
# Created by Mickael Hubert
|
||||
#######################################
|
||||
|
||||
#######################################
|
||||
# Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
|
||||
DecodeOOK KEYWORD1
|
||||
OregonDecoderV2 KEYWORD1
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
nextPulse KEYWORD2
|
||||
isDone KEYWORD2
|
||||
getData KEYWORD2
|
||||
resetDecoder KEYWORD2
|
||||
gotBit KEYWORD2
|
||||
manchester KEYWORD2
|
||||
alignTail KEYWORD2
|
||||
reverseBits KEYWORD2
|
||||
reverseNibbles KEYWORD2
|
||||
done KEYWORD2
|
||||
decode KEYWORD2
|
||||
ext_int_1 KEYWORD2
|
||||
temperature KEYWORD2
|
||||
humidity KEYWORD2
|
||||
battery KEYWORD2
|
||||
id KEYWORD2
|
||||
channel KEYWORD2
|
||||
OregonType KEYWORD2
|
||||
DataToDecoder KEYWORD2
|
||||
FindSensor KEYWORD2
|
||||
|
||||
#######################################
|
||||
# Instances (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
|
||||
#######################################
|
||||
# Constants (LITERAL1)
|
||||
#######################################
|
||||
@@ -0,0 +1,33 @@
|
||||
{
|
||||
"name": "Oregon",
|
||||
"keywords": "Oregon, 433Mhz, temperature, humidity, battery, THN132N, THGR228N, MySensors",
|
||||
"description": "This sketch provides an example how to implement a humidity/temperature from Oregon sensor.",
|
||||
"repository":
|
||||
{
|
||||
"type": "git",
|
||||
"url": "https://github.com/Mickaelh51/Arduino-Oregon-Library.git"
|
||||
},
|
||||
"authors":
|
||||
[
|
||||
{
|
||||
"name": "Mickael Hubert",
|
||||
"email": "github@winlux.fr",
|
||||
"url": "https://github.com/Mickaelh51",
|
||||
"maintainer": true
|
||||
},
|
||||
{
|
||||
"name": "Olivier Lebrun"
|
||||
},
|
||||
{
|
||||
"name": "Dominique Pierre"
|
||||
}
|
||||
],
|
||||
"dependencies":
|
||||
{
|
||||
"name": "MySensor",
|
||||
"authors": "Henrik Ekblad",
|
||||
"frameworks": "arduino"
|
||||
},
|
||||
"frameworks": "arduino",
|
||||
"platforms": "atmelavr"
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
name=Oregon
|
||||
version=1.0.0
|
||||
author=Mickael Hubert, Dominique Pierre, Olivier Lebrun
|
||||
maintainer=Mickael Hubert <github@winlux.fr>
|
||||
sentence=Decode data from Oregon sensors.
|
||||
paragraph=This sketch provides an example how to implement a humidity/temperature from Oregon sensor (433Mhz). <br /> Ex: Arduino UNO -- (PIN 2) --> 433Mhz receiver ====> Oregon sensors <br />
|
||||
category=Sensors
|
||||
url=https://github.com/Mickaelh51/Arduino-Oregon-Library
|
||||
architectures=*
|
||||
@@ -0,0 +1,347 @@
|
||||
// Oregon V2 decoder modified for MySensors compatibility - Mickael Hubert <github@winlux.fr>
|
||||
// Oregon V2 decoder modified - Olivier Lebrun
|
||||
// Oregon V2 decoder added - Dominique Pierre
|
||||
// New code to decode OOK signals from weather sensors, etc.
|
||||
// 2010-04-11 <jcw@equi4.com> http://opensource.org/licenses/mit-license.php
|
||||
// $Id: ookDecoder.pde 5331 2010-04-17 10:45:17Z jcw $
|
||||
|
||||
#ifndef Oregon_h
|
||||
#define Oregon_h
|
||||
#include "Arduino.h"
|
||||
|
||||
/*--------------------------
|
||||
Class DecodeOOK
|
||||
--------------------------*/
|
||||
class DecodeOOK
|
||||
{
|
||||
protected:
|
||||
byte total_bits, bits, flip, state, pos, data[25];
|
||||
|
||||
virtual char decode (word width) =0;
|
||||
|
||||
public:
|
||||
|
||||
enum { UNKNOWN, T0, T1, T2, T3, OK, DONE };
|
||||
|
||||
DecodeOOK () { resetDecoder(); }
|
||||
|
||||
bool nextPulse (word width) {
|
||||
if (state != DONE)
|
||||
|
||||
switch (decode(width)) {
|
||||
case -1: resetDecoder(); break;
|
||||
case 1: done(); break;
|
||||
}
|
||||
return isDone();
|
||||
}
|
||||
|
||||
bool isDone () const { return state == DONE; }
|
||||
|
||||
const byte* getData (byte& count) const {
|
||||
count = pos;
|
||||
return data;
|
||||
}
|
||||
|
||||
void resetDecoder () {
|
||||
total_bits = bits = pos = flip = 0;
|
||||
state = UNKNOWN;
|
||||
}
|
||||
|
||||
// add one bit to the packet data buffer
|
||||
|
||||
virtual void gotBit (char value) {
|
||||
total_bits++;
|
||||
byte *ptr = data + pos;
|
||||
*ptr = (*ptr >> 1) | (value << 7);
|
||||
|
||||
if (++bits >= 8) {
|
||||
bits = 0;
|
||||
if (++pos >= sizeof data) {
|
||||
resetDecoder();
|
||||
return;
|
||||
}
|
||||
}
|
||||
state = OK;
|
||||
}
|
||||
|
||||
// store a bit using Manchester encoding
|
||||
void manchester (char value) {
|
||||
flip ^= value; // manchester code, long pulse flips the bit
|
||||
gotBit(flip);
|
||||
}
|
||||
|
||||
// move bits to the front so that all the bits are aligned to the end
|
||||
void alignTail (byte max =0) {
|
||||
// align bits
|
||||
if (bits != 0) {
|
||||
data[pos] >>= 8 - bits;
|
||||
for (byte i = 0; i < pos; ++i)
|
||||
data[i] = (data[i] >> bits) | (data[i+1] << (8 - bits));
|
||||
bits = 0;
|
||||
}
|
||||
// optionally shift bytes down if there are too many of 'em
|
||||
if (max > 0 && pos > max) {
|
||||
byte n = pos - max;
|
||||
pos = max;
|
||||
for (byte i = 0; i < pos; ++i)
|
||||
data[i] = data[i+n];
|
||||
}
|
||||
}
|
||||
|
||||
void reverseBits () {
|
||||
for (byte i = 0; i < pos; ++i) {
|
||||
byte b = data[i];
|
||||
for (byte j = 0; j < 8; ++j) {
|
||||
data[i] = (data[i] << 1) | (b & 1);
|
||||
b >>= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void reverseNibbles () {
|
||||
for (byte i = 0; i < pos; ++i)
|
||||
data[i] = (data[i] << 4) | (data[i] >> 4);
|
||||
}
|
||||
|
||||
void done () {
|
||||
while (bits)
|
||||
gotBit(0); // padding
|
||||
state = DONE;
|
||||
}
|
||||
};
|
||||
|
||||
/*--------------------------
|
||||
Class OregonDecoderV2
|
||||
--------------------------*/
|
||||
class OregonDecoderV2 : public DecodeOOK {
|
||||
public:
|
||||
|
||||
OregonDecoderV2() {}
|
||||
|
||||
// add one bit to the packet data buffer
|
||||
virtual void gotBit (char value) {
|
||||
if(!(total_bits & 0x01))
|
||||
{
|
||||
data[pos] = (data[pos] >> 1) | (value ? 0x80 : 00);
|
||||
}
|
||||
total_bits++;
|
||||
pos = total_bits >> 4;
|
||||
if (pos >= sizeof data) {
|
||||
//Serial.println("sizeof data");
|
||||
resetDecoder();
|
||||
return;
|
||||
}
|
||||
state = OK;
|
||||
}
|
||||
|
||||
virtual char decode (word width) {
|
||||
if (200 <= width && width < 1200) {
|
||||
//Serial.println(width);
|
||||
byte w = width >= 700;
|
||||
|
||||
switch (state) {
|
||||
case UNKNOWN:
|
||||
if (w != 0) {
|
||||
// Long pulse
|
||||
++flip;
|
||||
} else if (w == 0 && 24 <= flip) {
|
||||
// Short pulse, start bit
|
||||
flip = 0;
|
||||
state = T0;
|
||||
} else {
|
||||
// Reset decoder
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
case OK:
|
||||
if (w == 0) {
|
||||
// Short pulse
|
||||
state = T0;
|
||||
} else {
|
||||
// Long pulse
|
||||
manchester(1);
|
||||
}
|
||||
break;
|
||||
case T0:
|
||||
if (w == 0) {
|
||||
// Second short pulse
|
||||
manchester(0);
|
||||
} else {
|
||||
// Reset decoder
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
} else if (width >= 2500 && pos >= 8) {
|
||||
return 1;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
/*--------------------------
|
||||
Manipulation functions
|
||||
--------------------------*/
|
||||
OregonDecoderV2 orscV2;
|
||||
|
||||
volatile word pulse;
|
||||
|
||||
void ext_int_1(void)
|
||||
{
|
||||
static word last;
|
||||
// determine the pulse length in microseconds, for either polarity
|
||||
pulse = micros() - last;
|
||||
last += pulse;
|
||||
}
|
||||
|
||||
float temperature(const byte* data)
|
||||
{
|
||||
int sign = (data[6]&0x8) ? -1 : 1;
|
||||
float temp = ((data[5]&0xF0) >> 4)*10 + (data[5]&0xF) + (float)(((data[4]&0xF0) >> 4) / 10.0);
|
||||
float result = sign * temp;
|
||||
#ifdef MY_DEBUG
|
||||
Serial.println("Oregon temperature: " + String(result));
|
||||
#endif
|
||||
return sign * temp;
|
||||
}
|
||||
|
||||
|
||||
byte humidity(const byte* data)
|
||||
{
|
||||
byte humidity = (data[7]&0xF) * 10 + ((data[6]&0xF0) >> 4);
|
||||
#ifdef MY_DEBUG
|
||||
Serial.println("Oregon humidity: " + String(humidity));
|
||||
#endif
|
||||
return (humidity);
|
||||
}
|
||||
|
||||
//10 => battery lovel is LOW
|
||||
//90 => battery level is HIGH
|
||||
byte battery(const byte* data)
|
||||
{
|
||||
byte BatteryLevel = (data[4] & 0x4) ? 10 : 90;
|
||||
#ifdef MY_DEBUG
|
||||
Serial.println("Oregon battery level: " + String(BatteryLevel));
|
||||
#endif
|
||||
return BatteryLevel;
|
||||
}
|
||||
|
||||
//Find id from Oregon Sensors
|
||||
byte id(const byte* data)
|
||||
{
|
||||
#ifdef MY_DEBUG
|
||||
Serial.print("Oregon ID: " + String(data[3]) + " Hexadecimal: ");
|
||||
Serial.println(data[3],HEX);
|
||||
#endif
|
||||
return (data[3]);
|
||||
}
|
||||
|
||||
//Find Channel from Oregon Sensors
|
||||
byte channel(const byte* data)
|
||||
{
|
||||
byte channel;
|
||||
switch (data[2])
|
||||
{
|
||||
case 0x10:
|
||||
channel = 1;
|
||||
break;
|
||||
case 0x20:
|
||||
channel = 2;
|
||||
break;
|
||||
case 0x40:
|
||||
channel = 3;
|
||||
break;
|
||||
default:
|
||||
channel = 0;
|
||||
break;
|
||||
}
|
||||
#ifdef MY_DEBUG
|
||||
Serial.println("Oregon channel: " + String(channel));
|
||||
#endif
|
||||
return channel;
|
||||
}
|
||||
|
||||
// Detect type of sensor module
|
||||
const char* OregonType (const byte* data)
|
||||
{
|
||||
const char* Model;
|
||||
if(data[0] == 0xEA && data[1] == 0x4C)
|
||||
{
|
||||
Model = "THN132N";
|
||||
}
|
||||
else if(data[0] == 0x1A && data[1] == 0x2D)
|
||||
{
|
||||
Model = "THGR228N";
|
||||
}
|
||||
else
|
||||
{
|
||||
Model = "UNKNOWN";
|
||||
}
|
||||
#ifdef MY_DEBUG
|
||||
Serial.println("Oregon model: " + String(Model));
|
||||
#endif
|
||||
|
||||
return Model;
|
||||
}
|
||||
|
||||
// Decode data once
|
||||
const byte* DataToDecoder (class DecodeOOK& decoder)
|
||||
{
|
||||
byte pos;
|
||||
const byte* data = decoder.getData(pos);
|
||||
#ifdef MY_DEBUG
|
||||
Serial.println("Brute Hexadecimal data from sensor: ");
|
||||
for (byte i = 0; i < pos; ++i) {
|
||||
Serial.print(data[i] >> 4, HEX);
|
||||
Serial.print(data[i] & 0x0F, HEX);
|
||||
}
|
||||
Serial.println(' ');
|
||||
#endif
|
||||
decoder.resetDecoder();
|
||||
return data;
|
||||
}
|
||||
|
||||
int FindSensor (const int id, int maxsensor)
|
||||
{
|
||||
int EepromId=0;
|
||||
for (int i=0; i<maxsensor; i++){
|
||||
#ifdef MySensor_h
|
||||
int SensorID = loadState(i);
|
||||
#else
|
||||
int SensorID = EEPROM.read(i);
|
||||
#endif
|
||||
if(SensorID == 255)
|
||||
{
|
||||
#ifdef MySensor_h
|
||||
saveState(i,id);
|
||||
#else
|
||||
EEPROM.write(i,id);
|
||||
#endif
|
||||
|
||||
#ifdef MY_DEBUG
|
||||
Serial.print("Sensor ID: ");
|
||||
Serial.print(SensorID);
|
||||
Serial.print(" has been saved in position EEPROM: ");
|
||||
Serial.println(i);
|
||||
#endif
|
||||
EepromId = i;
|
||||
break;
|
||||
}
|
||||
|
||||
if(id == SensorID){
|
||||
#ifdef MY_DEBUG
|
||||
Serial.print("Sensor ID: ");
|
||||
Serial.print(SensorID);
|
||||
Serial.print(" has been find in position EEPROM: ");
|
||||
Serial.println(i);
|
||||
#endif
|
||||
EepromId = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return EepromId;
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user