ESP32 touch switch with voice control problem (aREST help?)
Posted: 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.
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]