ESP32 touch switch with voice control

I am building a light switch that an the ESP32 to turn on and off a relay via an onboard touch sensor and voice input(Arest and IFTTT). The problem I am having is making the two inputs turn on the same relay without getting in the way of eachother. I decided to make the touch switch with some code I wrote that makes each touch on the sensor a 1 or a 0 consecutively. I wasn’t sure how to display the data coming from Arest, all I know how to do is to set a pin to HIGH or LOW. To combat this I just jumped the output pin that the Arest server turns on to an input pin that I had set up, this gives me a 1 or 0 for the voice control part. Now I have it so that the voice control and touch switch work well for turning the relay on/off when used separately. When used together I get mixed results and due to the pin I am using for the voice control not sharing power with the touch switch it can’t turn off the switch with voice if I have turned the switch on via the touch switch.

I realize that is probably clear as mud and I don’t have many hours of coding under my belt so the code may not be very pretty. Maybe I am making it more complicated than it is? Here is the code I am using. I also wrote it a different way, I’ll explain below this code. Someone save me.

#include <WiFi.h>
#include <PubSubClient.h>
#include <aREST.h>

WiFiClient espClient;
PubSubClient client(espClient);

aREST rest = aREST(client);

char* device_id = “*****”;

const char* ssid = “";
const char* password = "
”;

//aRest input and touch modulation
int previousState = 0;
int inPin = 4;
int state;
int light2 = 2;
//aRest input and touch modulation

// touchCounter
int threshold = 40;
bool touch1detected = false;
int count =0;
int light =23;
void gotTouch1(){
touch1detected = true;
}
// touchCounter

//wifi control
void callback(char* topic, byte* payload, unsigned int length);
//wifi control

void setup()
{

Serial.begin(115200);

//wifi connect
client.setCallback(callback);

rest.set_id(device_id);
rest.set_name(“esp32”);

WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println(“WiFi connected”);
//wifi connect

//touch switch
touchAttachInterrupt(T4, gotTouch1, threshold);
//touch switch

//I/O setup
pinMode(light, OUTPUT);
pinMode (light2, OUTPUT);
pinMode(inPin, INPUT);
//I/O setup
}

void loop() {
//aRest
rest.handle(client);
//aRest

//touch switch
delay(800); //800
if(touch1detected){
touch1detected = false;
Serial.println(“Touch 1 detected”);
count++;

if (count > 1){
count=0;
}
Serial.println(count);
}
//touch switch
//touch switch + voice input input pin is connected to output pin that is connected to Arest server
state=digitalRead(inPin);
Serial.println(state);

if (previousState != state ){
count++;
previousState=state;}
//touch switch + voice input

//touch switch
if (count == 1 ){
digitalWrite(light,HIGH);
delay(300); // Bounce control 300
}
else {digitalWrite(light,LOW);
delay(300); // Bounce control 300
}
//touch switch

}
//wifi control
void callback(char* topic, byte* payload, unsigned int length) {

rest.handle_callback(client, topic, payload, length);
//wifi control
}

My second idea was to jump output to input on both the touch switch and the voice control and then use the 1s and 0s that came from the two inputs to control a separate output that was attached to the relay. The code is as follows.

#include <WiFi.h>
#include <PubSubClient.h>
#include <aREST.h>

WiFiClient espClient;
PubSubClient client(espClient);

aREST rest = aREST(client);

char* device_id = “***”;

const char* ssid = “";
const char* password = "
”;

//aRest input and touch modulation
//int previousState = 0;
int inPin = 4;
int voice;
int previousVoice =0;
int light2 = 2;
//aRest input and touch modulation
//new
int inPin2 = 22;
int touch;
int light3 = 12;

// touchCounter
int threshold = 40;
bool touch1detected = false;
int count =0;
int light =23;
void gotTouch1(){
touch1detected = true;
}
// touchCounter

//wifi control
void callback(char* topic, byte* payload, unsigned int length);
//wifi control

void setup()
{

Serial.begin(115200);

//wifi connect
client.setCallback(callback);

rest.set_id(device_id);
rest.set_name(“esp32”);

WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println(“WiFi connected”);
//wifi connect

//touch switch
touchAttachInterrupt(T4, gotTouch1, threshold);
//touch switch

//I/O setup
pinMode(light, OUTPUT);
pinMode (light2, OUTPUT);
pinMode(inPin, INPUT);
//I/O setup
//new
pinMode(inPin2, INPUT);
pinMode(light3, OUTPUT);

}

void loop() {
//aRest
rest.handle(client);
//aRest

//touch switch
delay(2000); //800
if(touch1detected){
touch1detected = false;
Serial.println(“Touch 1 detected”);
count++;

if (count > 1){
count=0;
}
Serial.println(count);
}

if (previousVoice != voice) {
count++;
previousVoice=voice;}

if (count == 1 ){
digitalWrite(light,HIGH);
delay(300); // Bounce control 300
}
else {digitalWrite(light,LOW);
delay(300); // Bounce control 300
}
voice=digitalRead(inPin);
Serial.print("Voice ");
Serial.println(voice);
touch=digitalRead(inPin2);
Serial.print("Touch ");
Serial.println(touch);

if (voice==1 && touch==1) {
digitalWrite(light3,HIGH);
}
else if (voice==1 && touch==0){
digitalWrite(light3,HIGH);
}
else if (voice==0 && touch==1){
digitalWrite(light3,HIGH);
}

else{digitalWrite(light3,LOW);
}

}
//wifi control
void callback(char* topic, byte* payload, unsigned int length) {

rest.handle_callback(client, topic, payload, length);
//wifi control
}

esp32_room_light_beta.ino (2.13 KB)

2nd_times_a_charm.ino (2.43 KB)

You just need one variable that controls the state of the relay. If a touch occurs, toggle it. If the input pin changes state toggle it.

#include <WiFi.h>
#include <PubSubClient.h>
#include <aREST.h>

WiFiClient espClient;
PubSubClient client(espClient);

aREST rest = aREST(client);

char* device_id = "*****";

const char* ssid = "*****";
const char* password = "*****";

//aRest input and touch modulation
int previousState = 0;
const int inPin = 4;
//int state;
//int light2 = 2;
//aRest input and touch modulation

vool isLightOn = false;

// touchCounter
const int threshold = 40;
volatile bool touch1detected = false;
//int count = 0;
const int light = 23;

void gotTouch1() {
  touch1detected = true;
}
// touchCounter

//wifi control
void callback(char* topic, byte* payload, unsigned int length) {
  rest.handle_callback(client, topic, payload, length);
  //wifi control
}

void setup()
{
  Serial.begin(115200);

  //wifi connect
  client.setCallback(callback);
  rest.set_id(device_id);
  rest.set_name("esp32");

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  //wifi connect

  //touch switch
  touchAttachInterrupt(T4, gotTouch1, threshold);
  //touch switch

  //I/O setup
  pinMode(light, OUTPUT);
  pinMode (light2, OUTPUT);
  pinMode(inPin, INPUT);
  //I/O setup
}

void loop() {
  //aRest
  rest.handle(client);
  //aRest

  //touch switch
  delay(800); //800
  if (touch1detected) {
    touch1detected = false;
    Serial.println("Touch 1 detected");
    isRelayOn = !isRelayOn; // toggle relay
  }
  //touch switch

  //touch switch + voice input         input pin is connected to output pin that is connected to Arest server
  int state = digitalRead(inPin);
  Serial.println(state);

  if (previousState != state ) {
    isRelayOn = !isRelayOn; // toggle relay
  }
  previousState = state;
  //touch switch + voice input

  //touch switch
  if (isRelayOn == true ) {
    digitalWrite(light, HIGH);
  }
  else {
    digitalWrite(light, LOW);
  }
  delay(300); // Bounce control 300
  //touch switch
}

blh64 - Thanks so much for the reply, that code runs much smoother and is 100% better than mine was. I am still left with the issue of conflicting inputs. What exactly I mean by that is when the touch switch is toggled to on the voice control input has to be on to toggle it off. With voice commands that is a bit of an issue due to having to say "light on" to turn off the light, and vice versa. Not the biggest issue in the world, just wondering if you or one of the other experts had a good solution?

I have no experience with the aRest library and how it works but rather than "lights on" or "lights off" can you say "toggle lights" so it is moreintuitive that you want to change from the current state to the other state?