2 dashboard button switches for ESP8266 E12 module Bug?

  • Device & model you are using : ESP8266 E12 module
  • What dashboard are you using? (Web)

i used this code to control the builtin LED of ESP8266 E12 module and every thing was fine , i tried to use 2 switch buttons to control the LED and a DHT 11 sensor but sometimes when i turn on the sensor from the WEB dashboard, the LED button gets turned off by itself !!

can any one explain to me whats happening ?

NOTE: i modified the first 4 line so they appear , i was not able to use any hashtags

#include <CayenneMQTTESP8266.h>
#include <DHT.h>
#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#char ssid[] = "................";
char wifiPassword[] = ".............";
char username[] = "............................";
char password[] = "...........................";
char clientID[] = "....................";
DHT dht(D2, DHT11);
unsigned long lastMillis = 0;
 float x = 0;

void setup() {
  // put ur setup codes here, to run ONCE:
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);
  pinMode(D7,OUTPUT)  ;
  digitalWrite(D7,HIGH);
  pinMode(2,OUTPUT)  ;
  digitalWrite(2,HIGH);
    for (int i=0;i<5; i++)
  {
    digitalWrite(2 ,HIGH);
    delay(700);
    digitalWrite(2 ,LOW);
    delay(700);
  }
} 
void loop() {
  // put ur main code here, to run repeatedly:
  Cayenne.loop();
      if (millis() - lastMillis > 1000) {
    lastMillis = millis();
    x= (lastMillis/1000);
    Cayenne.virtualWrite(3, x);
  }
  float temp = dht.readTemperature(true); // fahrenhit
  float hum = dht.readHumidity();

  Cayenne.virtualWrite(1, temp, TYPE_TEMPERATURE, UNIT_FAHRENHEIT);
  Cayenne.virtualWrite(2, hum , TYPE_RELATIVE_HUMIDITY, UNIT_PERCENT);
delay (5000);
}
 CAYENNE_IN(0)
{
     int currentValue=getValue.asInt();
    if(currentValue==0)
    {
      digitalWrite(2,HIGH);
     }
      else{
       digitalWrite(2,LOW); 
      }
}
CAYENNE_IN(10)
{
    int currentValue=getValue.asInt();
    if(currentValue==1)
    {
      digitalWrite(D7,HIGH);
     }
      else{
       digitalWrite(D7,LOW); 
}
}

@mohamedbinisa,

What module are you using, and what are you using to power it?

The ESP is notoriously sensitive to brown out, and any additional current load, like turning on LEDs or writing a lot of data, can cause it to reset itself.

Make sure you have at least a 1 amp power supply, and/or a large value capacitor across the supply pins to guard against this issue.

Also note, that you shouldn’t have huge delays in your main loop as it, along with the OneWire delays, you can cause timeouts on your dashboard.

You may also want to clean up your code so it is easier to debug.

Cheers,

Craig

A - im using NodeMCU E12 ESP8266 and im powering it by my laptop

B- for the delay i used it cuz without it i keep getting zeros for the temp and humidity for some reason … i made it 1 sec delay now and it works fine

C- i took your advice and made the code a little bit easier to read and debug and made some changes , i have added an external LED

D- i noticed when i remove the code for the built in LED every thing is working just fine … but when i remove the code for the external LED and keep the Built in one this happens :

when i turn ON the builtin LED from the dashboard it stays ON but when i turn the DHT11 sensor from the dashoabord ON and OFF enough times ( 1 to 5 times mostly ) somehow the Builtin LED button ( on the dashboard ) changes to OFF
and it Turns off

Here is the new code


include < CayenneMQTTESP8266.h>
include < DHT.h>
define CAYENNE_DEBUG
define CAYENNE_PRINT Serial
char ssid = “…”;
char wifiPassword = “…”;
char username = “…”;
char password = “…”;
char clientID = “…”;
DHT dht(D2, DHT11);
unsigned long lastMillis = 0;
float x = 0;

void setup(){
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
pinMode(2,OUTPUT); // connected to the built in LED
pinMode(D5,OUTPUT); // connected to external LED
digitalWrite(D5,HIGH);
pinMode(D7,OUTPUT); // connected to DHT11 sensor
digitalWrite(D7,HIGH);

for (int i=0;i<4; i++){
digitalWrite(2 ,HIGH);
delay(300);
digitalWrite(2 ,LOW);
delay(300);
digitalWrite(2 ,HIGH);
delay(300);}
}
void loop(){
Cayenne.loop();

if (millis() - lastMillis > 1000) {
lastMillis = millis();
x= (lastMillis/1000);
Cayenne.virtualWrite(1, x);}

float temp = dht.readTemperature(true);
float hum = dht.readHumidity();
Cayenne.virtualWrite(2, temp, TYPE_TEMPERATURE, UNIT_FAHRENHEIT);
Cayenne.virtualWrite(3, hum , TYPE_RELATIVE_HUMIDITY, UNIT_PERCENT);

delay (1000);
}

CAYENNE_IN(4){
digitalWrite(2, !getValue.asInt());} // connected to the built in LED

CAYENNE_IN(5){
digitalWrite(D5, getValue.asInt());} // connected to external LED

CAYENNE_IN(6){
digitalWrite(D7, getValue.asInt());} // connected to DHT11 sensor 5V pin
// and the SIG pin is connected to D2


and thanks for your time Craig