forked from 1technophile/OpenMQTTGateway
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ZsensorGPIOKeyCode.ino
82 lines (67 loc) · 2.97 KB
/
ZsensorGPIOKeyCode.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
OpenMQTTGateway Addon - ESP8266 or Arduino program for home automation
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
Send and receiving command by MQTT
GPIO KeyCode derived from GPIO Input
This reads up to 4 gpio with latch signal (gpio line) and combines the bits as KeyKode - can be connected
to EV1527 receiver.
Copyright: (c)Grzegorz Rajtar
This file is part of OpenMQTTGateway.
OpenMQTTGateway is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenMQTTGateway is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef ZsensorGPIOKeyCode
int InputStateGPIOKeyCode = 0x0f; // Set to 3 so that it reads on startup
int lastInputStateGPIOKeyCode = 0x0f;
int lastLatchStateGPIOKeyCode = 0;
void setupGPIOKeyCode() {
pinMode(GPIOKeyCode_LATCH_PIN, INPUT_PULLUP); //
pinMode(GPIOKeyCode_D0_PIN, INPUT_PULLUP); //
pinMode(GPIOKeyCode_D1_PIN, INPUT_PULLUP); //
pinMode(GPIOKeyCode_D2_PIN, INPUT_PULLUP); //
//pinMode(GPIOKeyCode_D3_PIN, INPUT_PULLUP); //
}
void MeasureGPIOKeyCode(){
int latch = digitalRead(GPIOKeyCode_LATCH_PIN);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
{
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
#if defined(ESP8266) || defined(ESP32)
yield();
#endif
// if the Input state has changed:
if (latch > 0 && lastLatchStateGPIOKeyCode != latch) {
int reading = digitalRead(GPIOKeyCode_D0_PIN)
| (digitalRead(GPIOKeyCode_D1_PIN) << 1)
| (digitalRead(GPIOKeyCode_D2_PIN) << 2);
//| digitalRead(GPIOKeyCode_D3_PIN) << 3;
char hex[3];
InputStateGPIOKeyCode = reading;
sprintf(hex, "%02x", InputStateGPIOKeyCode);
hex[2] = 0;
Serial.printf("GPIOKeyCode %s\n", hex);
client.publish(subjectGPIOKeyCodetoMQTT,hex);
lastLatchStateGPIOKeyCode = latch;
}
if (latch != lastLatchStateGPIOKeyCode) {
lastLatchStateGPIOKeyCode = latch;
Serial.printf("GPIOKeyCode latch %d\n", latch);
if (latch == 0)
client.publish(subjectGPIOKeyCodeStatetoMQTT, "done");
}
// save the reading. Next time through the loop, it'll be the lastInputState:
lastInputStateGPIOKeyCode = InputStateGPIOKeyCode;
}
}
#endif