#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
char auth[] = "xxx"; //WeMos D1 Mini
char ssid[] = "xxx";
char password[] = "xxx";
IPAddress server_ip (192, 168, 1, 154);
// Mac address should be different for each device in your LAN
IPAddress arduino_ip ( 192, 168, 1, 154);
IPAddress dns_ip ( 8, 8, 8, 8);
IPAddress gateway_ip ( 192, 168, 1, 1);
IPAddress subnet_mask(255, 255, 255, 0);
SimpleTimer timer;
#define UPButton D5 // Set increment button pin.
#define DNButton D6 // Set decrement button pin.
#define WLED D7 // Set LED pin.
int brightness = 0; // Start on with LED on.
SimpleTimer timerBUTTONS; // Setup timerBUTTONS to check buttons.
/////////////////////////////////////////////////
void setup()
{
pinMode(UPButton, INPUT_PULLUP); // Set increment button pin to INPUT - Add _PULLUP if not already set in hardware.
pinMode(DNButton, INPUT_PULLUP); // Set decrement button pin to INPUT - Add _PULLUP if not already set in hardware.
Serial.begin(115200);
Serial.println();
WiFi.config(arduino_ip, gateway_ip, subnet_mask);
WiFi.begin(ssid, password);
Blynk.config(auth);
while (Blynk.connect(1000) == false) {
}
timerBUTTONS.setInterval(100L, whiteLED); // Timer to run whiteLED function every half second.
}
////////////////////////////////////////////////
BLYNK_WRITE(V0) // Slider Widget Function - runs every-time slider widget is physically moved.
{
brightness = param.asInt(); // Get slider value.
analogWrite(WLED, brightness); // Send slider value to LED
Serial.print("Slider: ");
Serial.println(brightness);
}
/////////////////////////////////////////////////
void whiteLED() // This function checks buttons status and updates White LED and slider (frequency dependent on timer).
{
// Check increment button and respond.
if (digitalRead(UPButton) == LOW and brightness < 1023) { // Only do something if button pressed and brightness counter in range.
brightness += 10; // Increment counter by 50
Blynk.virtualWrite(V0, constrain(brightness, 0, 1023)); // Send feedback to Slider Widget - but keep within 0-255
analogWrite(WLED, constrain(brightness, 0, 1023)); // Send data to LED - but keep within 0-255
Serial.print("Button: ");
Serial.println(brightness);
Serial.print(brightness);
}
// Check decrement button and respond..
else if (digitalRead(DNButton) == LOW and brightness > 0) { // Only do something if button pressed and brightness counter in range.
brightness -= 10; // Decrement counter by 50
Blynk.virtualWrite(V0, constrain(brightness, 0, 1023)); // Send feedback to Slider Widget - but keep within 0-255
analogWrite(WLED, constrain(brightness, 0, 1023)); // Send data to LED - but keep within 0-255
Serial.print("Button: ");
Serial.println(brightness);
Serial.print(brightness);
}
else { // nothing happening here... move along :)
}
}
/////////////////////////////////////////////////
/////////////////////////////////////////////////
void loop()
{
Blynk.run(); // Runs usual Blynk routine.
timerBUTTONS.run(); // Checks timer and runs whiteLED function.
}