grovkillen wrote: ↑19 Jun 2017, 10:53
Are the other pins on the ESP available? You can use 7 GPIOs on the ESP for the keys.
yes other pins on esp is free.just i use D2 and D1 for I2C.
i used other gpios in nodemcu for input key before. but there is some problems. for example key is very slow to action. some time dont work and if nodemcu go to fail for each reason so key also dont work and other problems.
now i use arduino's gpio for key and this is sketch of arduino(PME) :
Code: Select all
#include <Wire.h>
#include <Bounce2.h>
#define I2C_MSG_IN_SIZE 4
#define I2C_MSG_OUT_SIZE 4
#define CMD_DIGITAL_WRITE 1
#define CMD_DIGITAL_READ 2
#define CMD_ANALOG_WRITE 3
#define CMD_ANALOG_READ 4
volatile uint8_t sendBuffer[I2C_MSG_OUT_SIZE];
const int buttonPinA = 10;
const int buttonPinB = 11;
const int buttonPinC = 12;
const int buttonPinD = 13;
const int buttonPinE = 14;
const int buttonPinF = 15;
const int buttonPinG = 16;
const int buttonPinH = 17;
const int relayPinA = 2;
const int relayPinB = 3;
const int relayPinC = 4;
const int relayPinD = 5;
const int relayPinE = 6;
const int relayPinF = 7;
const int relayPinG = 8;
const int relayPinH = 9;
int oldValueA = 0;
int oldValueB = 0;
int oldValueC = 0;
int oldValueD = 0;
int oldValueE = 0;
int oldValueF = 0;
int oldValueG = 0;
int oldValueH = 0;
int stateA = 0;
int stateB = 0;
int stateC = 0;
int stateD = 0;
int stateE = 0;
int stateF = 0;
int stateG = 0;
int stateH = 0;
Bounce debouncerA = Bounce();
Bounce debouncerB = Bounce();
Bounce debouncerC = Bounce();
Bounce debouncerD = Bounce();
Bounce debouncerE = Bounce();
Bounce debouncerF = Bounce();
Bounce debouncerG = Bounce();
Bounce debouncerH = Bounce();
void setup()
{
Wire.begin(0x7f);
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);
pinMode(buttonPinA, INPUT_PULLUP);
pinMode(buttonPinB, INPUT_PULLUP);
pinMode(buttonPinC, INPUT_PULLUP);
pinMode(buttonPinD, INPUT_PULLUP);
pinMode(buttonPinE, INPUT_PULLUP);
pinMode(buttonPinF, INPUT_PULLUP);
pinMode(buttonPinG, INPUT_PULLUP);
pinMode(buttonPinH, INPUT_PULLUP);
debouncerA.attach(buttonPinA);
debouncerA.interval(70);
debouncerB.attach(buttonPinB);
debouncerB.interval(70);
debouncerC.attach(buttonPinC);
debouncerC.interval(70);
debouncerD.attach(buttonPinD);
debouncerD.interval(70);
debouncerE.attach(buttonPinE);
debouncerE.interval(70);
debouncerF.attach(buttonPinF);
debouncerF.interval(70);
debouncerG.attach(buttonPinG);
debouncerG.interval(70);
debouncerH.attach(buttonPinH);
debouncerH.interval(70);
digitalWrite(relayPinA,HIGH);
digitalWrite(relayPinB,HIGH);
digitalWrite(relayPinC,HIGH);
digitalWrite(relayPinD,HIGH);
digitalWrite(relayPinE,HIGH);
digitalWrite(relayPinF,HIGH);
digitalWrite(relayPinG,HIGH);
digitalWrite(relayPinH,HIGH);
pinMode(relayPinA, OUTPUT);
pinMode(relayPinB, OUTPUT);
pinMode(relayPinC, OUTPUT);
pinMode(relayPinD, OUTPUT);
pinMode(relayPinE, OUTPUT);
pinMode(relayPinF, OUTPUT);
pinMode(relayPinG, OUTPUT);
pinMode(relayPinH, OUTPUT);
/*--------------------- Added these lines for toggle switch-------------------------*/
oldValueA = digitalRead(buttonPinA); // set oldValueA to the current status of the toggle switch
oldValueB = digitalRead(buttonPinB); // set oldValueB to the current status of the toggle switch
oldValueC = digitalRead(buttonPinC);
oldValueD = digitalRead(buttonPinD);
oldValueE = digitalRead(buttonPinE);
oldValueF = digitalRead(buttonPinF);
oldValueG = digitalRead(buttonPinG);
oldValueH = digitalRead(buttonPinH);
}
void loop() {
stateA = digitalRead(relayPinA);
debouncerA.update();
int valueA = debouncerA.read();
if (valueA != oldValueA) {
digitalWrite(relayPinA, !stateA);
oldValueA = valueA;
}
stateB = digitalRead(relayPinB);
debouncerB.update();
int valueB = debouncerB.read();
if (valueB != oldValueB) {
digitalWrite(relayPinB, !stateB);
oldValueB = valueB;
}
stateC = digitalRead(relayPinC);
debouncerC.update();
int valueC = debouncerC.read();
if (valueC != oldValueC) {
digitalWrite(relayPinC, !stateC);
oldValueC = valueC;
}
stateD = digitalRead(relayPinD);
debouncerD.update();
int valueD = debouncerD.read();
if (valueD != oldValueD) {
digitalWrite(relayPinD, !stateD);
oldValueD = valueD;
}
stateE = digitalRead(relayPinE);
debouncerE.update();
int valueE = debouncerE.read();
if (valueE != oldValueE) {
digitalWrite(relayPinE, !stateE);
oldValueE = valueE;
}
stateF = digitalRead(relayPinF);
debouncerF.update();
int valueF = debouncerF.read();
if (valueF != oldValueF) {
digitalWrite(relayPinF, !stateF);
oldValueF = valueF;
}
stateG = digitalRead(relayPinG);
debouncerG.update();
int valueG = debouncerG.read();
if (valueG != oldValueG) {
digitalWrite(relayPinG, !stateG);
oldValueG = valueG;
}
stateH = digitalRead(relayPinH);
debouncerH.update();
int valueH = debouncerH.read();
if (valueH != oldValueH) {
digitalWrite(relayPinH, !stateH);
oldValueH = valueH;
}
}
void receiveEvent(int count)
{
if (count == I2C_MSG_IN_SIZE)
{
byte cmd = Wire.read();
byte port = Wire.read();
int value = Wire.read();
value += Wire.read() * 256;
switch (cmd)
{
case CMD_DIGITAL_WRITE:
pinMode(port, OUTPUT);
digitalWrite(port, value);
break;
case CMD_DIGITAL_READ:
pinMode(port, INPUT_PULLUP);
clearSendBuffer();
sendBuffer[0] = digitalRead(port);
break;
case CMD_ANALOG_WRITE:
analogWrite(port, value);
break;
case CMD_ANALOG_READ:
clearSendBuffer();
int valueRead = analogRead(port);
sendBuffer[0] = valueRead & 0xff;
sendBuffer[1] = valueRead >> 8;
break;
}
}
}
void clearSendBuffer()
{
for (byte x = 0; x < sizeof(sendBuffer); x++)
sendBuffer[x] = 0;
}
void requestEvent()
{
Wire.write((const uint8_t*)sendBuffer, sizeof(sendBuffer));
}
so keys work locally for me . and is very fast to action.and if nodemcu go to fail , but keys will work (with arduino lonely.
i told in prev message i do a strange work. in this photo:

- LKKKKK.jpg (44.22 KiB) Viewed 19507 times
((sorry in this photo have a problem. i use D1 and D2 for I2C.))
in this photo i connect output arduino(relay ) to input nodemcu. with this work send to domoticz is ok(i create a input switch in espeasy . BUT there is one problem . i told before. when i turn on and turn off a key fast . or with domoticz turn on and off a light fast, so there will be a loop and relay run on/off again and again... for (for example ) 30 times.on/off/on/off/on...
i can not found why there is this problem. i dont want use other modules . i want fix this issue. because there is a software problem about this that we can solve.i am not a strong programmer. . .