Arduino blynk controlled server coolling

Hi all i am trying to make this unit turn on V1 led when D9 is High and V2 led when D7 is High & have a V7 button to turn on D7 from the app i have the temp side working. thank you

#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <SimpleTimer.h>
#include <DHT.h>

char auth[] = “***************************”;
int redLed = 9;
int greenLed = 8;
int over = 7;
float time;
#define DHTPIN 2 // What digital pin we’re connected to
WidgetLED led1(V1);
WidgetLED led2(V2);

// Uncomment whatever type you’re using!
#define DHTTYPE DHT11 // DHT 11

DHT dht(DHTPIN, DHTTYPE);
SimpleTimer timer;

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;
}
// 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); // See the connection status in Serial Monitor
Blynk.begin(auth);
BLYNK_READ(PIN_UPTIME);
dht.begin();
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(over, OUTPUT);
// Setup a function to be called every second
timer.setInterval(1000L, sendSensor);

}
void LedWidget()
{
if (led1.getValue()) {
led1.on();
Serial.println(“LED on V1: on”);
} else {
led1.off();
Serial.println(“LED on V1: off”);
}
if (led2.getValue()) {
led2.on();
Serial.println(“LED on V2: on”);
} else {
led2.off();
Serial.println(“LED on V2: off”);
}
}
void loop()
{
Blynk.run(); // Initiates Blynk
float h = dht.readHumidity();
float t = dht.readTemperature();

// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t) || isnan(h)) {
Serial.println(“Failed to read from DHT”);
} else {
Serial.print(“Time: “);
time = millis()/1000;
Serial.print(time);
Serial.print(” secs\t”);
Serial.print(“Humidity: “);
Serial.print(h);
Serial.print(” %\t”);
Serial.print(“Temperature: “);
Serial.print(t);
Serial.println(” *C”);

}
// Turn red LED on and green LED off if the temperature is 25 degrees or more.
// Turn green LED on and red LED off if the temperature is less than 25 degrees.
if (t >= 25) {
digitalWrite(redLed, HIGH);
digitalWrite(greenLed, LOW);
}
else {
digitalWrite(redLed, LOW);
digitalWrite(greenLed, HIGH);
}
// Wait 5 seconds before starting the next reading.
delay(5000);
timer.run(); // Initiates SimpleTimer
}

Please use code formatting. It is hard to read.

#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <SimpleTimer.h>
#include <DHT.h>

char auth[] = “580aa70621034b6e82acd5182bd31304”;
int redLed = 9;
int greenLed = 8;
int over = 7; //connect LED to Pin 7
float time;
#define DHTPIN 2 // What digital pin we’re connected to
WidgetLED led1(V1);
WidgetLED led2(V2);

// Uncomment whatever type you’re using!
#define DHTTYPE DHT11 // DHT 11

DHT dht(DHTPIN, DHTTYPE);
SimpleTimer timer;

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;
}
// 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); // See the connection status in Serial Monitor
Blynk.begin(auth);
BLYNK_READ(PIN_UPTIME);
dht.begin();
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(over, OUTPUT);
// Setup a function to be called every second
timer.setInterval(1000L, sendSensor);

}
void LedWidget()
{
if (led1.getValue()) {
led1.on();
Serial.println(“LED on V1: on”);
} else {
led1.off();
Serial.println(“LED on V1: off”);
}
if (led2.getValue()) {
led2.on();
Serial.println(“LED on V2: on”);
} else {
led2.off();
Serial.println(“LED on V2: off”);
}
}
void loop()
{
Blynk.run(); // Initiates Blynk
float h = dht.readHumidity();
float t = dht.readTemperature();

// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t) || isnan(h)) {
Serial.println(“Failed to read from DHT”);
} else {
Serial.print(“Time: “);
time = millis() / 1000;
Serial.print(time);
Serial.print(” secs\t”);
Serial.print(“Humidity: “);
Serial.print(h);
Serial.print(” %\t”);
Serial.print(“Temperature: “);
Serial.print(t);
Serial.println(” *C”);

}
// Turn red LED on and green LED off if the temperature is 25 degrees or more.
// Turn green LED on and red LED off if the temperature is less than 25 degrees.
if (t >= 25) {
digitalWrite(redLed, HIGH);
digitalWrite(greenLed, LOW);
}
else {
digitalWrite(redLed, LOW);
digitalWrite(greenLed, HIGH);
}
// Wait 5 seconds before starting the next reading.
delay(5000);
timer.run(); // Initiates SimpleTimer
}

First of all, remove the delay from the loop(). Do it with simpletimer because Blynk will get stuck on it. Also remove, all other stuff from the loop except timer.run and blynk.run. That should make it a lot more stable :slight_smile:

thanks for that so far its working but i am trying to get 3 Virtual led to show when (greenled ,redled & over ) is on as a indication on the app.
thanks

You can put the LED’s into the same function where you do the readings with SimpleTimer. That would be the most simple solution.

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <SimpleTimer.h>
#include <DHT.h>

char auth[] = “++++++++++”;
int redLed = 8;
int greenLed = 9;
int over = 7;
float time;
#define DHTPIN 6
const int btnPin0 = 0;
const int btnPin1 = 1;
const int btnPin2 = 2;

WidgetLED led1(V1);
WidgetLED led2(V2);
WidgetLED led3(V3);

#define DHTTYPE DHT11 // DHT 11

DHT dht(DHTPIN, DHTTYPE);
SimpleTimer timer;

void sendSensor()
{
float h = dht.readHumidity();
float t = dht.readTemperature();

if (isnan(h) || isnan(t)) {
Serial.println(“Failed to read from DHT sensor!”);
return;
}
Blynk.virtualWrite(V5, h);
Blynk.virtualWrite(V6, t);

}

void setup()
{
Serial.begin(9600);
Blynk.begin(auth);

dht.begin();
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(over, OUTPUT);
pinMode(btnPin0, INPUT_PULLUP);
pinMode(btnPin1, INPUT_PULLUP);
pinMode(btnPin2, INPUT_PULLUP);

timer.setInterval(500L, sendSensor);
timer.setInterval(500L, buttonLedWidget);

}
boolean btnState = false;
void buttonLedWidget()
{
// Read button
boolean isPressed = (digitalRead(btnPin0) == LOW);

if (isPressed != btnState) {
if (isPressed) {
led1.on();
} else {
led1.off();
}
btnState = isPressed;
}
isPressed = (digitalRead(btnPin1) == LOW);

if (isPressed != btnState) {
if (isPressed) {
led2.on();
} else {
led2.off();
}
btnState = isPressed;
}
isPressed = (digitalRead(btnPin2) == LOW);

if (isPressed != btnState) {
if (isPressed) {
led3.on();
} else {
led3.off();
}
btnState = isPressed;

}
}
void loop()
{

float h = dht.readHumidity();
float t = dht.readTemperature();

// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t) || isnan(h)) {
Serial.println(“Failed to read from DHT”);
} else {
Serial.print(“Time: “);
time = millis() / 500;
Serial.print(time);
Serial.print(” secs\t”);
Serial.print(“Humidity: “);
Serial.print(h);
Serial.print(” %\t”);
Serial.print(“Temperature: “);
Serial.print(t);
Serial.println(” *C”);

}
// Turn red LED on and green LED off if the temperature is 25 degrees or more.
// Turn green LED on and red LED off if the temperature is less than 25 degrees.
if (t >= 25) {
digitalWrite(redLed, HIGH);
digitalWrite(greenLed, LOW);

}
else {
digitalWrite(redLed, LOW);
digitalWrite(greenLed, HIGH);

}
Blynk.run(); // Initiates Blynk
timer.run();
}

this is the code i have settled on it seams stable. thanks for your hints i am still new to this coding

How so I have tried but I can t get them to work

Have you checked the example for the led widget? From your code it seems you forgot to instantiate the widget, e.g.

WidgetLED led1(V1);

Serial.println("That image is a bit misleading");

For optimum clarity the 3 prefixed backticks should be on a line above the start of your code and the 3 suffixed backticks on a line below the end of your code.

thanks, edited post.

original source has been edited since it was screenshotted :slight_smile: