Hey guys,
I’m getting more and more familiar with the voids/functions and timers/intervalls.
My new written code (if I’m right I just copied some variables and the whole Blynk part, the other parts are new and running pretty well!
What I have to do for now is the part with the check of time for the PIR thing.
The sketch below will actually work like this:
Motion detected => turn on LED with RGB settings of ZeRGBa for given seconds in blynk
Alarm Time reached => turn off LED from Motion detection if they are on at this moment and then make a LED stripe effect with the color settings from ZeRGBa for given seconds in blynk
Stripe effect is taken from this example page: ColorWipe
Could someone take a look at this and tell me where the best position would be for the time calculation when the PIR will be active (timeInput Widget with Start and Endtime).
Here is my new sketch
#define BLYNK_PRINT Serial
#include <Adafruit_NeoPixel.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266WiFi.h>
#include <NTPClient.h>
#include <SPI.h>
#include <SimpleTimer.h>
#include <WiFiUdp.h>
// LED Stuff
#define PIN D5
#define NUM_LEDS 37
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ400);
void colorWipe(int, int, int, int);
void showStrip();
void clearStrip();
void setPixel(int, int, int, int);
void ZeRGBa();
// WiFi Stuff
char auth[] = "************";
char ssid[] = "************";
char pass[] = "************";
char serv[] = "************";
#define BLYNK_PRINT Serial
#include <Adafruit_NeoPixel.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266WiFi.h>
#include <NTPClient.h>
#include <SPI.h>
#include <SimpleTimer.h>
#include <WiFiUdp.h>
// LED Stuff
#define PIN D5
#define NUM_LEDS 37
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ400);
void colorWipe(int, int, int, int);
void showStrip();
void clearStrip();
void setPixel(int, int, int, int);
void ZeRGBa();
// WiFi Stuff
char auth[] = "59e6fabd771c46158bd201cd85733cb1";
char ssid[] = "SkyNet";
char pass[] = "Hb8#!D00fX!";
char serv[] = "blynk.maal.myds.me";
// Variables
int interval = 1000;
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
int red = 2;
int green = 2;
int blue = 0;
int connectionDelay = 5 * 1000;
int connectionSuccess = 0;
// Motion Sensor Variables
int PIR_pin = D3;
int PIR_val;
int PIR_start;
int PIR_stop;
int PIR_timer = 10 * 1000;
bool PIR_days[] = {0, 0, 0, 0, 0, 0, 0};
bool PIR_status = false;
unsigned long PIR_start_millis = 0;
int now;
int finish;
int weekday;
// Alarm Variables
int ALARM_start = -1;
int ALARM_timer = 5 * 1000;
bool ALARM_days[] = {0, 0, 0, 0, 0, 0, 0};
bool ALARM_status = false;
unsigned long ALARM_start_millis = 0;
const long utcOffsetInSeconds = 2 * 60 * 60;
unsigned long delayStart = 0;
bool delayRunning = false;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "10.0.4.1", utcOffsetInSeconds, 24 * 60 * 60 * 1000);
SimpleTimer timer;
void WIFI_check();
void TIME_check();
void PIR_check();
void ALARM_check();
void setup()
{
Serial.begin(115200);
Blynk.begin(auth, ssid, pass, serv, 8080);
timeClient.begin();
timer.setInterval(5.0 * 1000L, WIFI_check);
timer.setInterval(0.2 * 1000L, TIME_check);
timer.setInterval(0.5 * 1000L, PIR_check);
timer.setInterval(1.0 * 1000L, ALARM_check);
pinMode(PIR_pin, INPUT);
timeClient.begin();
pixels.begin();
pixels.clear();
delayStart = millis();
delayRunning = false;
Blynk.virtualWrite(V0, red, green, blue);
Blynk.virtualWrite(V5, "First Start");
Blynk.virtualWrite(V8, ALARM_timer / 1000);
Blynk.virtualWrite(V9, PIR_timer / 1000);
for (int i = 1; i <= 7; i++) {
Blynk.virtualWrite(V11, ALARM_start, ALARM_days[i]);
Blynk.virtualWrite(V12, PIR_start, PIR_stop, PIR_days[i]);
}
}
void loop()
{
timer.run();
Blynk.run();
timeClient.update();
currentMillis = millis();
if ((unsigned long)(currentMillis - previousMillis) >= interval) {
previousMillis = currentMillis;
}
if (ALARM_status) {
colorWipe(red, green, blue, 50);
colorWipe(0, 0, 0, 50);
}
if (PIR_status) {
ZeRGBa();
}
}
void WIFI_check()
{
if (WiFi.status() != WL_CONNECTED) {
WiFi.begin((char*)ssid, (char*)pass);
Serial.println("Lost WiFi Connection, trying to reconnect... ");
connectionSuccess = 1;
}
else if (WiFi.status() == WL_CONNECTED && connectionSuccess == 1) {
Serial.print("Connected to WiFi: ");
Serial.println(ssid);
Serial.print("Local IP address: ");
Serial.println(WiFi.localIP());
connectionSuccess = 0;
}
}
void TIME_check()
{
now = timeClient.getHours() * 60 * 60 + timeClient.getMinutes() * 60 + timeClient.getSeconds();
weekday = timeClient.getDay();
if (timeClient.getDay() == 0) {
weekday = 7;
}
}
void PIR_check()
{
// PIR_start/stop >= 0 tracks, if both Start and Stop have time set in Blynk
if (!ALARM_status && PIR_days[weekday] && PIR_start >= 0 && PIR_stop >= 0) {
// Time between 5:00 and 6:00 f.e.
if (PIR_stop > PIR_start) {
finish = PIR_stop;
}
// Time between 6:00 and 5:00 f.e.
else if (PIR_stop < PIR_start) {
finish = PIR_stop + 24 * 60 * 60;
// Now is at time on the next day after Start
// Start = 20:00 | Stop = 4:00 | Now = 2:00
if (now < PIR_start) {
now = now + 24 * 60 * 60;
}
}
// Check if Now is between Start and Stop
if (now >= PIR_start && now < finish) {
PIR_val = digitalRead(PIR_pin);
if (PIR_val == HIGH) {
Blynk.virtualWrite(V5, "Motion deteced");
PIR_status = true;
PIR_start_millis = currentMillis;
}
else {
Blynk.virtualWrite(V5, "No Motion");
}
if (PIR_status) {
if ((unsigned long)(currentMillis - PIR_start_millis) >= PIR_timer) {
PIR_status = false;
}
}
else {
clearStrip();
}
}
}
}
void ALARM_check()
{
if (now == ALARM_start && ALARM_days[weekday] && ALARM_start >= 0) {
clearStrip(); // Strip turned off if other Effects are running while Alarm is triggered
ALARM_status = true;
ALARM_start_millis = currentMillis;
}
if (ALARM_status) {
if ((unsigned long)(currentMillis - ALARM_start_millis) >= ALARM_timer) {
ALARM_status = false;
}
}
else if (!PIR_status)
{
clearStrip();
}
}
void showStrip() {
pixels.show();
}
void clearStrip() {
pixels.fill(0, 0, NUM_LEDS);
showStrip();
}
void ZeRGBa() {
for (int i = 0; i < NUM_LEDS; i++) {
setPixel(i, red, green, blue);
showStrip();
}
}
void setPixel(int Pixel, int red, int green, int blue) {
pixels.setPixelColor(Pixel, pixels.Color(red, blue, green));
}
void colorWipe(int red, int green, int blue, int SpeedDelay) {
for (uint16_t i = 0; i < NUM_LEDS; i++) {
setPixel(i, red, green, blue);
showStrip();
delay(SpeedDelay);
}
}
BLYNK_CONNECTED()
{
Blynk.virtualWrite(V0, red, green, blue);
Blynk.virtualWrite(V8, ALARM_timer / 1000);
Blynk.virtualWrite(V9, PIR_timer / 1000);
for (int i = 1; i <= 7; i++) {
Blynk.virtualWrite(V11, ALARM_start, ALARM_days[i]);
Blynk.virtualWrite(V12, PIR_start, PIR_stop, PIR_days[i]);
}
}
BLYNK_WRITE(V0)
{
int r = param[0].asInt();
int g = param[1].asInt();
int b = param[2].asInt();
red = r;
green = g;
blue = b;
}
BLYNK_WRITE(V8)
{
int pinValue = param.asInt();
ALARM_timer = pinValue * 1000;
}
BLYNK_WRITE(V9)
{
int pinValue = param.asInt();
PIR_timer = pinValue * 1000;
}
BLYNK_WRITE(V11)
{
TimeInputParam t(param);
if (t.hasStartTime()) {
ALARM_start = t.getStartHour() * 60 * 60 + t.getStartMinute() * 60 + t.getStartSecond();
}
else {
ALARM_start = -1;
}
for (int i = 1; i <= 7; i++) {
ALARM_days[i] = t.isWeekdaySelected(i);
}
}
BLYNK_WRITE(V12)
{
TimeInputParam t(param);
if (t.hasStartTime()) {
PIR_start = t.getStartHour() * 60 * 60 + t.getStartMinute() * 60 + t.getStartSecond();
}
else {
PIR_start = -1;
}
if (t.hasStopTime()) {
PIR_stop = t.getStopHour() * 60 * 60 + t.getStopMinute() * 60 + t.getStopSecond();
}
else {
PIR_stop = -1;
}
for (int i = 1; i <= 7; i++) {
PIR_days[i] = t.isWeekdaySelected(i);
}
}