ESP32 touch switch with voice control problem (aREST help?)

WeenyMinotaur69
Posts: 2
Joined: Sun Jul 26, 2020 2:48 pm

ESP32 touch switch with voice control problem (aREST help?)

Postby WeenyMinotaur69 » Sun Jul 26, 2020 3:12 pm

I currently have an ESP 32 connected to a relay that controls my bedroom lights. It has voice control via aREST & IFTTT and also an on/off switch that is triggered by a touch input. It works great but has its limits. I work in a field that has nothing to do with electronics or computers and have only been messing with things like this for a few months so my experience with code/micro-controllers is sub-amateur at very best.

With some help I wrote a program that ties the voice and touch inputs together so if the relay is turned on with one input it can be turned off with the other. The voice and touch both display 1's and 0's for on/off high/low respectively . The issue I have is that when the touch switch is on (1) and the voice control is off (0) I can't tell the voice control to turn off the relay because it is already reading off, so I have to tell it to turn on the relay which triggers a state change and turns the relay off (the code may offer a better description than I can give). I was hoping I could just read every time aREST sent a new off request so I could use the off even if it was already set to off. Maybe a different server type could help me? Like i said I am a total noob so any help or advice is appreciated. I don't fully know the electrical nomenclature so if that wasn't very clear let me know and Ill try and re-phrase.

Code: Select all

[Codebox=cpp file=Untitled.cpp]
#include <WiFi.h>
#include <PubSubClient.h>
#include <aREST.h>

WiFiClient espClient;
PubSubClient client(espClient);

aREST rest = aREST(client);

char* device_id = "0000";

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

bool isLightOn = false;
bool isRelayOn;

// 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.print("ON PIN ");
  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
}
[/Codebox]

WeenyMinotaur69
Posts: 2
Joined: Sun Jul 26, 2020 2:48 pm

Re: ESP32 touch switch with voice control problem (aREST help?)

Postby WeenyMinotaur69 » Sun Jul 26, 2020 9:21 pm

Never mind, I fixed the problem by running a new output from IFTTT to a separate input so I could set it to strictly off. If anyone is interested the code is as follows. I made it a bit simpler than the original, hopefully it is as reliable.

Code: Select all

[Codebox=cpp file=Untitled.cpp]
#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 = "****";


//Voice On
int inPinVoice=4;
int voiceOut=2;
//Voice ON

//Voice Off
int offVoiceIn=19;
int offVoiceOut=18;
//Voice Off

// Relay I/O
int relay =23;
// Relay I/O

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


//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.enableSTA(true); //Might help with the crashing?
  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
 //On Pins
  pinMode(voiceOut, OUTPUT);
  pinMode(inPinVoice, INPUT);
  //On Pins
  //relay
  pinMode(relay,OUTPUT);
  //relay
  //Off Pins
  pinMode(offVoiceOut,OUTPUT);
  pinMode(offVoiceIn,INPUT);
 //Off Pins
//I/O setup

}

void loop() {
 //aRest
  rest.handle(client);
 //aRest
 
 //touch switch
  delay(800); //800
  if(touch1detected){
    touch1detected = false;
// Debug    Serial.println("Change detected");
    count++;
           
    if (count > 1){
    count=0;
    }
// Debug    Serial.println(count);
  }
 //touch switch

//voice On/Off
int voiceState;
voiceState=digitalRead(inPinVoice);
/* Debug
 // Serial.print("Voice ");
 //Serial.println(voiceState);
*/
int offState;
  offState=digitalRead(offVoiceIn);
/* Debug
  Serial.print("Voice Off ");
  Serial.println(offState);
*/  
if (voiceState == 1){
  count=1; 
  voiceState=0;
  digitalWrite(voiceOut, LOW);
  }

if (offState == 1){
  count=0; 
  offState=0;
  digitalWrite(offVoiceOut, LOW);
  }
//voice On/Off

//Relay Power
 if (count == 1) {
  digitalWrite(relay, HIGH);
  }
else {digitalWrite(relay,LOW);}
//Relay Power



  
}

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

  rest.handle_callback(client, topic, payload, length);
//wifi
}[/Codebox]

Who is online

Users browsing this forum: Bing [Bot], Majestic-12 [Bot] and 18 guests