Read and Write TTP223 sensor

Hi all,
I am trying to put up a secondary touch switch for controlling my motor using NodeMCU. The motor is run through another set of relays and switches else where and is connected to my home automation software, Openhab. When the touch switch is ON, we send this info to Openhab through mqtt and update the other switch. When the other switch is turned ON, Openhab sends that info to this secondary controller. When this info is received I want to turn ON or OFF the touch switch. How can I achieve this. Can I write to TTP223 sensor? How can I setup a read and write mode for the same pin? Following is the code.

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

// WiFi
const char *ssid = "wifissid"; // Enter your WiFi name
const char *password = "wifipassword";  // Enter WiFi password

// MQTT Broker
const char *mqtt_broker = "mybrokerip";
const char *commandtopic = "internalController/motorpower";
const char *statetopic = "internalController/motorpower/state";
const char *mqtt_username = "openhabian";
const char *mqtt_password = "mqttpassword";
int currentValue = LOW;
const int mqtt_port = 1883;
const int touchPin = 5;
WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
  // Set software serial baud to 115200;
  Serial.begin(115200);
  pinMode(touchPin, INPUT);
  // connecting to a WiFi network
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.println("Connecting to WiFi..");
  }
  Serial.println("Connected to the WiFi network");
  //connecting to a mqtt broker
  client.setServer(mqtt_broker, mqtt_port);
  client.setCallback(callback);
  while (!client.connected()) {
      String client_id = "esp8266-client-";
      client_id += String(WiFi.macAddress());
      Serial.printf("The client %s connects to the public mqtt broker\n", client_id.c_str());
      if (client.connect(client_id.c_str(), mqtt_username, mqtt_password)) {
          Serial.println("Public emqx mqtt broker connected");
      } else {
          Serial.print("failed with state ");
          Serial.print(client.state());
          delay(2000);
      }
  }
  // publish and subscribe
  //client.publish(topic, "hello emqx");
  client.subscribe(statetopic);
  client.subscribe(commandtopic);
}

void callback(char *topic, byte *payload, unsigned int length) {
  Serial.print("Message arrived in topic: ");
  Serial.println(topic);
  Serial.print("Message:");
  
  Serial.println((char *)payload);
  Serial.println("-----------------------");
if (!strcmp(topic, commandtopic)) {
  Serial.println("inside if");
        if (!strncmp((char *)payload, "ON", length)) {
          Serial.println("inside on");
            digitalWrite(touchPin, HIGH);
            currentValue = HIGH;
  
        } else if (!strncmp((char *)payload, "OFF", length)) {
          Serial.println("inside off");
            digitalWrite(touchPin, LOW);
            currentValue = LOW;
        }
        delay(2000);
}
    
}

void loop() {
  int touchValue = digitalRead(touchPin);
  
Serial.println(touchValue);
if(currentValue != touchValue)
{
  if(touchValue == HIGH)
  {
  //  client.publish(statetopic, "ON");
  }
  else
  {
    //client.publish(statetopic, "OFF");
  }
}
  client.loop();
  delay(1000);
}

I also tried splicing a wire and use it with another pin configured in output mode. In this digitalWrite is working but not digitalRead. In fact the sensor itself is not turning ON.


#include <ESP8266WiFi.h>
#include <PubSubClient.h>

// WiFi
const char *ssid = ""; // Enter your WiFi name
const char *password = "";  // Enter WiFi password

// MQTT Broker
const char *mqtt_broker = "";
const char *commandtopic = "internalController/motorpower/bigwell";
const char *statetopic = "internalController/motorpower/bigwell/state";
const char *mqtt_username = "openhabian";
const char *mqtt_password = "";
int currentValue = LOW;
const int mqtt_port = 1883;
const int touchPin = 5;
const int touchPinR = 16;
WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
  // Set software serial baud to 115200;
  Serial.begin(115200);
  pinMode(touchPinR, INPUT_PULLDOWN_16);
  pinMode(touchPin, OUTPUT);
  // connecting to a WiFi network
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.println("Connecting to WiFi..");
  }
  Serial.println("Connected to the WiFi network");
  //connecting to a mqtt broker
  client.setServer(mqtt_broker, mqtt_port);
  client.setCallback(callback);
  while (!client.connected()) {
      String client_id = "esp8266-client-";
      client_id += String(WiFi.macAddress());
      Serial.printf("The client %s connects to the public mqtt broker\n", client_id.c_str());
      if (client.connect(client_id.c_str(), mqtt_username, mqtt_password)) {
          Serial.println("Public emqx mqtt broker connected");
      } else {
          Serial.print("failed with state ");
          Serial.print(client.state());
          delay(2000);
      }
  }
  // publish and subscribe
  //client.publish(topic, "hello emqx");
  client.subscribe(statetopic);
  client.subscribe(commandtopic);
}

void callback(char *topic, byte *payload, unsigned int length) {
  Serial.print("Message arrived in topic: ");
  Serial.println(topic);
  Serial.print("Message:");
  
  Serial.println((char *)payload);
  Serial.println("-----------------------");
if (!strcmp(topic, commandtopic)) {
  Serial.println("inside if");
        if (!strncmp((char *)payload, "ON", length)) {
          Serial.println("inside on");
            digitalWrite(touchPin, HIGH);
            currentValue = HIGH;
  
        } else if (!strncmp((char *)payload, "OFF", length)) {
          Serial.println("inside off");
            digitalWrite(touchPin, LOW);
            currentValue = LOW;
        }
        delay(2000);
}
    
}

void loop() {
  int touchValue = digitalRead(touchPinR);
  
Serial.println(touchValue);
if(currentValue != touchValue)
{
  if(touchValue == HIGH)
  {
    client.publish(statetopic, "ON");
  }
  else
  {
    client.publish(statetopic, "OFF");
  }
}
  client.loop();
  delay(1000);
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.