[SOLVED] Extreme Newbie Here asking for advice

Hello all,

I’m trying to do something simple, or at least I thought it was…

I’m making a garage door opener, yes I know there are several out there, but I am adding a LDR to determine if I left the lights on, as well as a DHT11 to get the temperature with a four switch relay to open the doors. However, when I flash the Photon, nothing seems to work, or I get constantly the device is disconnected. Could someone kindly review my code and help point me in the right direction? I have commented out the code to tell me if the lights are on or off, and it seems to work, so I believe that is where I am failing at.

Thank you in advance!!!


#include <blynk.h>
#include <DHT.h>

char auth[] = "";

#define DHTPIN 6          // What digital pin we're connected to

#define DHTTYPE DHT11     // DHT 11


DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(true); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
}


void setup()
{
  Serial.begin(9600);
  delay(5000); // Allow board to settle
  Blynk.begin(auth);
  dht.begin();
  pinMode(A0, INPUT);
  pinMode(D0, OUTPUT);
  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
}


void loop()
{
  int ldrval=analogRead(A0);
  if(ldrval>100)
  {
    Blynk.virtualWrite(7, "Lights On");
  }
  else
  {
    Blynk.virtualWrite(7, "Lights Off");
  }


Blynk.run();
timer.run();

}

Take this out of the void loop()… you do not need it running hundreds of times a second. Create a new timer loop of it’s own… even 250ms (4 times a second) will be sufficiently fast.

e.g.

timer.setInterval(250L, CheckLightState); // runs a void called CheckLightState() 4 times a second.

Thank you! That solved my problem…

Now off to figure out why it turns on my relay every time I power it up.

Not sure what hardware you are using… those #includes where missing in your code. But many ESP’s seem to boot with their GPIO’s pulled high, so you need to wire your relay and/or adjust your code accordingly.

Or you may have a low trigger relay which will be turned on if the gpio is low.
Some relays are low trigger, some are high trigger, and some you can choose between low/high with a jumper cap.