2 state button problem

  • ESP 8266
  • IOS, Android, Web

Hi, I have a problem whith a 2 state button. I have a button that controls the relay through ESP8266.

  1. I press the button
  2. The relay is triggered - On - the light is On
  3. The 2 state button changes its state to On
  4. I press the button - the relay is triggered off - the 2 state button changes its state to off - the light is off
  5. But after 5-15 minutes 2 state button changes its value arbitrarily to ON (button - off, light - off).
    The time can be different.

The code that changes the 2 state button

CAYENNE_IN(4){
lamp_state = getValue.asInt();
Cayenne.virtualWrite(3,lamp_state,“digital_sensor”,“d”);
if (lamp_state == 1){
digitalWrite(14, 1);
}
else {
digitalWrite(14, 0);
}

Relay in NC in normal mode

can you post the entire code.

Can you turn on debugging and watch the serial output at the time it turns back on then paste the output here? Sounds like the connection may be unstable, but debug should tell us what’s going on.

I can 't because i use nodeMCU version WITTY -


But - the connection is stable, because:

  1. The relay is Normal close. When I power the device - the lamp is turning on.
  2. I push the button and relay is become NO (normal open) and the lamp is turning off.
  3. The 2 state button after 5-15 minutes turning on, but the lamp and push button is still turning off.
    Pice of code:

// This example shows how to connect to Cayenne using an ESP8266 and send/receive sample data.
// Make sure you install the ESP8266 Board Package via the Arduino IDE Board Manager and select the correct ESP8266 board before compiling.

#define CAYENNE_PRINT Serial
#define DHTTYPE DHT22

#include <CayenneMQTTESP8266.h>
#include “DHT.h”

// WiFi network info.
char ssid = “MyNetwork”;
char wifiPassword = “MyNetworkCode”;

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “";
char password[] = "
”;
char clientID = “******”;

unsigned long lastmillis = 0;

// DHT Sensor
const int DHTPin = 5;
// Initialize DHT sensor.
DHT dht(DHTPin, DHTTYPE);

// Temporary variables
static char celsiusTemp[7];
static char humidityTemp[7];
boolean lamp_state = 0;

void sendSensor()
{
*****Big block of measuring
}

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

// GPIO
pinMode(14, OUTPUT);
digitalWrite(14, 1);

Cayenne.begin(username, password, clientID, ssid, wifiPassword);
Cayenne.virtualWrite(3,1,“digital_sensor”,“d”); //change state to ON
Cayenne.virtualWrite(4,1,“digital_sensor”,“d”); //change state to ON
}

void loop() {
Cayenne.loop();

//delay millisecond
if (millis() - lastmillis > 60000){
lastmillis = millis ();
sendSensor();
}
}

// Default function for sending sensor data at intervals to Cayenne.
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
CAYENNE_OUT_DEFAULT()
{
// Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
//Cayenne.virtualWrite(0, millis());
//Cayenne.virtualwrite(3,lamp_state,“digital_sensor”,“d”);
// Some examples of other functions you can use to send data.
//Cayenne.celsiusWrite(1, 22.0);
//Cayenne.luxWrite(2, 700);
//Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
}

// Default function for processing actuator commands from the Cayenne Dashboard.
// You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
CAYENNE_IN_DEFAULT()
{
CAYENNE_LOG(“Channel %u, value %s”, request.channel, getValue.asString());
//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError(“Error message”);
}

CAYENNE_IN(4){
lamp_state = getValue.asInt();
Cayenne.virtualWrite(3,lamp_state,“digital_sensor”,“d”);
if (lamp_state == 1){
digitalWrite(14, 1);
}
else {
digitalWrite(14, 0);
}
Serial.println(“----------------------------------------------”);
Serial.print(“Lamp1 on/off:”);
Serial.println(lamp_state);
Serial.println(“----------------------------------------------”);
}

That’s why i Can’t use serial in working mode - WITTY

Sample for testing. Do not judge strictly :grinning:

I don’t have that particular board, but AFAIK all Arduino compatible boards are capable of printing to serial. Is there a specific limitation with that board? Didn’t see anything about it on a quick google search.

CH340G (USB driver) is located on the bottom, which is disconnected when connecting DHT and relay

But if the device reconnected then the light would light up

I dunno, kind of really hard to tell without getting the debug info. You should be able to use different pins for the DHT sensor so you can still get serial prints.

If i recall NodeMCU has some “funny” pin maping, in your case D5 (in code) = 14 (onboard)
Since your DHT is coded to pin 5 maybe that is causing it to fail.

Initialize the GPIO pin pinMode(14, OUTPUT) like you did but previously define integer variable with #define D5 14

Do the same for DHT pin.

GPIO5 is for DHT22
GPIO14 is for Relay

I know that by looking at code you posted earlyer.
What i was saying is to try and do pin remaping because your board is based on nodemcu fw.

1 Like

Ok, I will connect DHT22 on GPIO16 (D0) and Relay in GPIO14(D5) - they are not used in

some “funny” pin

And will connect the serial

@ryzhikovse try this code :

CAYENNE_IN(4){
lamp_state = getValue.asInt();

if (lamp_state == 1){
digitalWrite(14, 1);
Cayenne.virtualWrite(4,lamp_state,“digital_sensor”,“d”); // put Dashbord Button to ON state
}

else {

digitalWrite(14, 0);
Cayenne.virtualWrite(4,lamp_state,“digital_sensor”,“d”);// put Dashbord  Button to OFF state
}

to force your “Button_street” dashboard widget (channel 4), to ON or OFF state , at each Cayenne IN(4) demand.

4 channel is “pushbutton”
3 channel is “2 state button”
Push button working well. In that piece of code - then the “pushbutton” is on - the “2statebutton” is changing it state to on - else - to off

CAYENNE_IN(4){
lamp_state = getValue.asInt(); //get value of "pushbutton"
Cayenne.virtualWrite(3,lamp_state,“digital_sensor”,“d”); //changing state of "2statebutton"
if (lamp_state == 1){
digitalWrite(14, 1); //changing relay state to ON
}
else {
digitalWrite(14, 0); //changing relay state to OFF
}
Serial.println("----------------------------------------------");
Serial.print(“Lamp1 on/off:”);
Serial.println(lamp_state);
Serial.println("----------------------------------------------");
}

if your “2statebutton” is channel 3 = “lamp” widget , i don’t know why it’s changing state alone …
Try to force channel 4 anyway :
Cayenne.virtualWrite(4,lamp_state,“digital_sensor”,“d”);

perhaps it solve your issue by another road .

assign the pin and try with this code.

// This example shows how to connect to Cayenne using an ESP8266 and send/receive sample data.
// Make sure you install the ESP8266 Board Package via the Arduino IDE Board Manager and select the correct ESP8266 board before compiling. 

//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
int ledpin;
// WiFi network info.
char ssid[] = "ssid";
char wifiPassword[] = "wifiPassword";

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "MQTT_USERNAME";
char password[] = "MQTT_PASSWORD";
char clientID[] = "CLIENT_ID";

unsigned long lastMillis = 0;

void setup() {
	Serial.begin(9600);
	Cayenne.begin(username, password, clientID, ssid, wifiPassword);
}

void loop() {
	Cayenne.loop();

	//Publish data every 10 seconds (10000 milliseconds). Change this value to publish at a different interval.
	if (millis() - lastMillis > 10000) {
		lastMillis = millis();
		//Write data to Cayenne here. This example just sends the current uptime in milliseconds.
      Cayenne.virtualWrite(0, x, "digital_sensor", "d");

	}
}

CAYENNE_IN(1)
{
    int x = getValue.asInt();
    digitalWrite(ledpin,x);
}

Hi everyone!
I take your all oppinions and do:

  1. Change GPIO for DHT sensor on GPIO0;

  2. Unplug and disabled all sensors.

  3. Connect to USB just ESP (nodeMCU) and get serial data.
    When I powered NodeMCU, I push on the dashBoard “pushbutton” - to OFF, “2statebutton” chenged it state to OFF. After 10 minutes I refresh DashBoard and saw, that “pushbutton” still OFF, but “2stateButton” is ON

    [1261] Connecting to mqtt.mydevices.com:1883
    [1578] Connected

    Lamp1 on/off:0

    Failed to read from DHT sensor!
    Failed to read from DHT sensor!
    Failed to read from DHT sensor!
    Failed to read from DHT sensor!
    Failed to read from DHT sensor!
    Failed to read from DHT sensor!
    Failed to read from DHT sensor!
    Failed to read from DHT sensor!
    Failed to read from DHT sensor!
    Failed to read from DHT sensor!
    Failed to read from DHT sensor!
    Failed to read from DHT sensor!
    Failed to read from DHT sensor!
    Failed to read from DHT sensor!
    Failed to read from DHT sensor!
    Failed to read from DHT sensor!
    Failed to read from DHT sensor!

it’s ttrouble in 2statebutton.