Skip to content

Commit 74e9de2

Browse files
Merge pull request #26 from GoWired/dev
Dev
2 parents 84a660b + 1ac3e8e commit 74e9de2

File tree

25 files changed

+1052
-375
lines changed

25 files changed

+1052
-375
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
Images/image.png

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ This is a repository for **GoWired**, RS485 & MySensors based, open source home
1414
## Wiki
1515
To learn more, have a look at our [wiki](https://github.com/feanor-anglin/GetWired-Project/wiki).
1616

17-
## 2021-07 Changelog
18-
- Reorganized folders, add descriptions
17+
## 2022-04 Changelog
18+
- Reorganize folders, add descriptions
19+
- Change the way software handles IOs
20+
- Add software for 8RelayDin Shield
21+
- Relocate Multiprotocol Gateway to separate [repository](https://github.com/GoWired/Multiprotocol-Gateway)
22+
- Improvements to dimmer operation (new function for simultaneous change of brightness and colours)
1923

2024
##
2125
The code in master branch should always be tested & working. Dev branch may not be tested, use on your own responsibility.
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
*
3+
* All definitions in one file
4+
*
5+
*/
6+
7+
8+
#ifndef Configuration_h
9+
#define Configuration_h
10+
11+
/* *******************************************************************************************
12+
* MySensors Definitions
13+
* *******************************************************************************************/
14+
// Identification
15+
#define MY_NODE_ID 1
16+
#define SN "RS485 8RelayDin 1"
17+
#define SV "1.0"
18+
19+
// Selecting transmission settings
20+
#define MY_RS485 // Enable RS485 transport layer
21+
#define MY_RS485_DE_PIN 7 // DE Pin definition
22+
#define MY_RS485_BAUD_RATE 57600 // Set RS485 baud rate to use
23+
#define MY_RS485_HWSERIAL Serial // Enable for Hardware Serial
24+
#define MY_RS485_SOH_COUNT 3 // Collision avoidance
25+
26+
// FOTA Feature
27+
#define MY_OTA_FIRMWARE_FEATURE
28+
29+
// Other
30+
#define MY_TRANSPORT_WAIT_READY_MS 60000 // Time to wait for gateway to respond at startup
31+
32+
/* *******************************************************************************************
33+
* General Definitions
34+
* *******************************************************************************************/
35+
// Relay states
36+
#define RELAY_ON LOW
37+
#define RELAY_OFF HIGH
38+
39+
#define INTERVAL 300000 // Interval value (ms) for reporting readings of the sensors: temperature, power usage (default 300000)
40+
#define INIT_DELAY 200 // A value (ms) to be multiplied by node ID value to obtain the time to wait during the initialization process
41+
#define PRESENTATION_DELAY 10 // Time (ms) to wait between subsequent presentation messages (default 10)
42+
#define LOOP_TIME 100 // Main loop wait time (ms); (default 100)
43+
44+
/* *******************************************************************************************
45+
* IO Config
46+
* *******************************************************************************************/
47+
#define FIRST_OUTPUT_ID 0 // default 0; should not be altered (expander pins for outputs: 0-7)
48+
#define TOTAL_NUMBER_OF_OUTPUTS 8 // Total number of outputs; value from 0-8 (default 8)
49+
50+
// Inputs not bound to any outputs (expander pins for inputs: 8-15)
51+
#define INDEPENDENT_IO 0 // Number of independent inputs and outputs; value from 0 to 8 (default 0)
52+
#define INPUT_TYPE 0 // Define input type: 0 - INPUT_PULLUP, 1 - INPUT, 3 - Button
53+
54+
#define NUMBER_OF_OUTPUTS TOTAL_NUMBER_OF_OUTPUTS-INDEPENDENT_IO
55+
56+
#define SPECIAL_BUTTON // Enables long press functionality for all buttons
57+
58+
/* *******************************************************************************************
59+
* MCU Pin Definitions
60+
* *******************************************************************************************/
61+
// OUTPUT [RELAY / RGBW]
62+
#define OUTPUT_PIN_1 5
63+
#define OUTPUT_PIN_2 9
64+
#define OUTPUT_PIN_3 6
65+
#define OUTPUT_PIN_4 10
66+
67+
// INPUT [BUTTON / SENSOR]
68+
// General input
69+
#define INPUT_PIN_1 2
70+
#define INPUT_PIN_2 3
71+
#define INPUT_PIN_3 4
72+
#define INPUT_PIN_4 A3
73+
74+
// Analog input
75+
#define INPUT_PIN_5 A1
76+
#define INPUT_PIN_6 A2
77+
#define INPUT_PIN_7 A6
78+
#define INPUT_PIN_8 A7
79+
80+
// Protocols
81+
// 1-wire
82+
#define ONE_WIRE_PIN A0
83+
84+
// I2C
85+
#define I2C_PIN_1 A4
86+
#define I2C_PIN_2 A5
87+
88+
/* *******************************************************************************************
89+
* ERROR REPORTING
90+
* *******************************************************************************************/
91+
//#define ERROR_REPORTING
92+
#ifdef ERROR_REPORTING
93+
#ifdef POWER_SENSOR
94+
#define ES_ID HYSTERESIS_ID+1
95+
#endif
96+
#ifdef INTERNAL_TEMP
97+
#define TS_ID ES_ID+1
98+
#endif
99+
#ifdef EXTERNAL_TEMP
100+
#define ETS_ID TS_ID+1
101+
#endif
102+
#endif
103+
104+
//#define RS485_DEBUG
105+
#ifdef RS485_DEBUG
106+
#define DEBUG_ID ETS_ID+1
107+
#endif
108+
109+
#endif
110+
/*
111+
*
112+
* EOF
113+
*
114+
*/
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/*
2+
* PinShift - variable corresponding to arythmetical difference
3+
* between a sensor and its pin
4+
*/
5+
6+
#include "ExpanderIO.h"
7+
8+
PCF8575 Expander;
9+
10+
/* *******************************************************************************************
11+
* Constructor
12+
* *******************************************************************************************/
13+
ExpanderIO::ExpanderIO() {
14+
15+
NewState = 0;
16+
State = 0;
17+
}
18+
19+
/* *******************************************************************************************
20+
* Expander Initialization
21+
* *******************************************************************************************/
22+
void ExpanderIO::ExpanderInit(uint8_t Address) {
23+
24+
Expander.begin(Address);
25+
}
26+
27+
/* *******************************************************************************************
28+
* Set Values
29+
* *******************************************************************************************/
30+
void ExpanderIO::SetValues(bool RelayOFF, uint8_t Type, uint8_t Pin1, uint8_t Pin2) {
31+
32+
_RelayOFF = RelayOFF;
33+
34+
SensorType = Type;
35+
switch(SensorType) {
36+
// Door/window sensor
37+
case 0:
38+
_SensorPin = Pin1;
39+
Expander.pinMode(_SensorPin, INPUT_PULLUP);
40+
break;
41+
// Motion sensor // not used
42+
case 1:
43+
_SensorPin = Pin1;
44+
Expander.pinMode(_SensorPin, INPUT);
45+
break;
46+
// Relay output
47+
case 2:
48+
_RelayPin = Pin1;
49+
Expander.pinMode(_RelayPin, OUTPUT);
50+
Expander.digitalWrite(_RelayPin, _RelayOFF);
51+
break;
52+
// Button input
53+
case 3:
54+
_SensorPin = Pin1;
55+
Expander.pinMode(_SensorPin, INPUT_PULLUP);
56+
break;
57+
// Button + Relay
58+
case 4:
59+
_SensorPin = Pin1;
60+
_RelayPin = Pin2;
61+
Expander.pinMode(_SensorPin, INPUT_PULLUP);
62+
Expander.pinMode(_RelayPin, OUTPUT);
63+
Expander.digitalWrite(_RelayPin, _RelayOFF);
64+
break;
65+
default:
66+
break;
67+
}
68+
}
69+
70+
/* *******************************************************************************************
71+
* Input Check
72+
* *******************************************************************************************/
73+
void ExpanderIO::CheckInput() {
74+
75+
bool Reading;
76+
bool Shortpress = false;
77+
uint32_t StartTime = millis();
78+
79+
do {
80+
if(SensorType == 0 || SensorType == 3 || SensorType == 4) {
81+
// Hardcoded DebounceValue = 50
82+
Reading = ReadDigital(50, false);
83+
}
84+
else {
85+
Reading = ReadDigital(50, true);
86+
}
87+
88+
switch(SensorType) {
89+
case 0:
90+
case 1:
91+
if(Reading == true) {
92+
NewState = 1;
93+
}
94+
else {
95+
NewState = 0;
96+
}
97+
break;
98+
case 3:
99+
case 4:
100+
if(!Shortpress && Reading) {
101+
NewState = !State;
102+
Shortpress = true;
103+
}
104+
105+
// Hardcoded LongpressDuration = 1000
106+
if(millis() - StartTime > 1000) {
107+
NewState = 2;
108+
break;
109+
}
110+
111+
if(millis() < StartTime) {
112+
StartTime = millis();
113+
}
114+
break;
115+
default:
116+
break;
117+
}
118+
} while(Reading);
119+
}
120+
121+
// Read digital input
122+
bool ExpanderIO::ReadDigital(uint8_t DebounceValue, bool Invert) {
123+
124+
bool DigitalReading;
125+
bool PreviousReading = false;
126+
bool InputState = false;
127+
uint32_t Timeout = millis();
128+
uint32_t StartTime = Timeout;
129+
130+
do {
131+
DigitalReading = (Invert ? Expander.digitalRead(_SensorPin) : !Expander.digitalRead(_SensorPin));
132+
133+
if(DigitalReading && !PreviousReading) {
134+
StartTime = millis();
135+
}
136+
137+
if(millis() - StartTime > DebounceValue) {
138+
if(DigitalReading) {
139+
InputState = true;
140+
}
141+
}
142+
143+
if(millis() - Timeout > 255 || millis() < StartTime) {
144+
break;
145+
}
146+
147+
PreviousReading = DigitalReading;
148+
} while(DigitalReading);
149+
150+
return InputState;
151+
}
152+
153+
/* *******************************************************************************************
154+
* Set Relay
155+
* *******************************************************************************************/
156+
void ExpanderIO::SetRelay() {
157+
158+
bool RelayValue = NewState == 1 ? !_RelayOFF : _RelayOFF;
159+
Expander.digitalWrite(_RelayPin, RelayValue);
160+
State = NewState;
161+
}
162+
/*
163+
*
164+
* EOF
165+
*
166+
*/
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* This is a code written for easy management of digital input/output sensors such as door/window/pir security sensors,
3+
* buttons and relays. It menages easy sensor initialization, holds sensor states, changes relays states and provides a quite smart
4+
* function for reading inputs (including special button functionality which change its state after one second of holding a button).
5+
*
6+
*
7+
* SensorType:
8+
* 0 - INPUT_PULLUP sensor
9+
* 1 - INPUT sensor
10+
* 2 - Relay output
11+
* 3 - Button input
12+
* 4 - Button input + Relay output
13+
*
14+
*/
15+
16+
#ifndef ExpanderIO_h
17+
#define ExpanderIO_h
18+
19+
#include "Arduino.h"
20+
#include <Wire.h>
21+
#include <PCF8575.h>
22+
23+
class ExpanderIO
24+
{
25+
public:
26+
ExpanderIO();
27+
28+
uint8_t SensorType;
29+
uint8_t NewState;
30+
uint8_t State;
31+
32+
void ExpanderInit(uint8_t Address=0x20);
33+
void SetValues(bool RelayOFF, uint8_t Type, uint8_t Pin1, uint8_t Pin2=0);
34+
void CheckInput();
35+
void SetRelay();
36+
bool ReadDigital(uint8_t DebounceValue, bool Invert);
37+
38+
private:
39+
uint8_t _RelayPin;
40+
uint8_t _SensorPin;
41+
bool _LowStateDetection;
42+
bool _HighStateDetection;
43+
bool _Condition;
44+
bool _RelayOFF;
45+
46+
};
47+
48+
#endif

0 commit comments

Comments
 (0)