Wondered if somebody could have a look at my code and tell me where i’m going wrong… I’m trying to get a virtual LED to light when I flick a switch in Blynk, but it just wont Light the virtual LED. I’m only an amatuer programmer so my code is probably a little long winded, for that I apologies! Any help you can give will be much appreciated!! I’ve tried a few different ways looking through passed threads but none of them have worked… Please Help!
#include <arduino.h>
#include "DHT.h"
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
//Globals for DHT sensor
#define DHTPIN 3
#define DHTTYPE DHT22
DHT dht (DHTPIN, DHTTYPE);
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "token here";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "ssid";
char pass[] = "password";
//Select your pin with physical button
const int btnPin = 1;
WidgetLED led3(V3);
SimpleTimer timer;
//V3 LED Widget represents the physical button state
boolean btnState = false;
void buttonLedWidget()
{
// Read button
boolean isPressed = (digitalRead(btnPin) == LOW);
//If State has changed...
if (isPressed != btnState) {
if (isPressed) {
led3.on();
} else {
led3.off();
}
btnState = isPressed;
}
}
void sendUptime()
{
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(4, millis()/1000);
}
void sendSensor()
{
float h = dht.readHumidity();
float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
Blynk.virtualWrite(V5, h);
Blynk.virtualWrite(V6, t);
}
}
void setup()
{
Blynk.begin(auth, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);
timer.setInterval(1000, sendUptime);
// Setup physical button pin (active low)
pinMode(btnPin, INPUT_PULLUP);
timer.setInterval(500L, buttonLedWidget);
dht.begin();
// Setup a function to be called every second
timer.setInterval(1000L, sendSensor);
}
void loop()
{
Blynk.run();
timer.run(); // Initiates SimpleTimer
}