Hi guys,
I have a question regarding the slider widget and getting that to change a setpoint in my program.
Project is a baby monitor and I want to change the temperature setpoint from a slider widget which will have the program flag an alarm if its either too cold or too hot.
Ive been working on this for days with little to go on ref google and blynk exmaples.
Basically cannot get this to work so after some advice please. I am bringing a float value in from a slider on V5 aiming to change float variable “cold2” which is then referenced later in the program driving leds. This doesnt work, however when I use a static variable of “cold” in my led stuff it works so I know my led widgets are working, I just cant get the dynamic slider setpoint to work.
Any ideas?
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <OneWire.h>
#include <dht11.h>
dht11 DHT;
OneWire ds(2); // on pin 4 of wemos d1 using virtual pin 0 for readout on app
#define DHT11_PIN D4 // on pin 2 of wemos d1 using virtual pin 1 for readout on app
// led widget temperature setpoints
const int hot = 23; //set hot parameter
const int cold = 22; //set cold parameter
float cold2; //set cold parameter
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";
BLYNK_WRITE(V5)
{
float cold2 = param.asFloat(); // assigning incoming value from pin V5 (slider) to cold temperature setpoint
}
BlynkTimer timer;
void timingevent()
{
Serial.println(cold2);
DHT.read(DHT11_PIN);
delay(1000);
Blynk.run(); byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius;
if ( !ds.search(addr)) {
ds.reset_search();
delay(250);
return;
}
// the first ROM byte indicates which chip
switch (addr[0]) {
case 0x28:
type_s = 0;
break;
default:
return;
}
ds.reset();
ds.select(addr);
ds.write(0x44, 1); // start conversion, with parasite power on at the end
delay(1000); // maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it.
present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
// Convert the data to actual temperature
// because the result is a 16 bit signed integer, it should
// be stored to an "int16_t" type, which is always 16 bits
// even when compiled on a 32 bit processor.
int16_t raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10) {
// "count remain" gives full 12 bit resolution
raw = (raw & 0xFFF0) + 12 - data[6];
}
} else {
byte cfg = (data[4] & 0x60);
// at lower res, the low bits are undefined, so let's zero them
if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
//// default is 12 bit resolution, 750 ms conversion time
}
celsius = (float)raw / 16.0;
Blynk.virtualWrite(V0, celsius);
Blynk.virtualWrite(V1, DHT.humidity);
// led widget successful using v2, v3 and v4
if (celsius < cold2) { //cold
//Blynk.notify("Room too cold");
WidgetLED cold_led(V2);
WidgetLED hot_led(V3);
WidgetLED fine_led(V4);
Blynk.setProperty(V0, "color", "#04C0F8"); // blue gradient
cold_led.on();
hot_led.off();
fine_led.off();
Serial.println(" It's Cold.");
}
else if (celsius >= hot) { //hot
//Blynk.notify("Room too hot");
WidgetLED cold_led(V2);
WidgetLED hot_led(V3);
WidgetLED fine_led(V4);
Blynk.setProperty(V0, "color", "#D3435C"); // red gradient
cold_led.off();
hot_led.on();
fine_led.off();
//Serial.println(" It's Hot.");
}
else { //fine
WidgetLED cold_led(V2);
WidgetLED hot_led(V3);
WidgetLED fine_led(V4);
Blynk.setProperty(V0, "color", "#23C48E"); // green gradient
cold_led.off();
hot_led.off();
fine_led.on();
//Serial.println(" It's Fine.");
}
}
void setup()
{
// Debug console
Serial.begin(9600);
timer.setInterval(1000L, timingevent); // 1 second timer for timing of looping code
pinMode(4, INPUT);
Blynk.begin(auth, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
}
void loop()
{
Blynk.run();
timer.run();
}