Hi again, this project is from my wifi air freshener that I did a while back and trying to expand on it.
It runs on d1 wemos board and a board I had made and to be honest it worked
anyways, It has a separate output for LED and one for airfreshener. The LED is intened to be an indicator its about to spray, but havnt gotten it to work as intended. It is currently a "night light" lol
It currently works as designed, I can control LED or airfreshner with Alexa.
What I want to do is:
Make the LED blink 5 times as a warning before activation, but I couldn't have a long code / delay or even milis before Alexa says device is not responding. I eventually figured out I need to set a state, and then read the state and then running a blink without delay function which works :
void loop()
{
if (wifiConnected)
{
upnpBroadcastResponder.serverLoop();
kitchen->serverLoop();
}
// blinks led, does not cause Alexa to say device not responding :)
if (wifiState == HIGH) { // Set HIGH from Alexa ON
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval && blinkCount <=10) { // on and off counts as 1 each , 10 = 10 blinks
previousMillis = currentMillis; // save the last time you blinked the LED
if (wifiState1 == HIGH) { // if the LED is off turn it on and vice-versa:
wifiState1 = LOW;
}
else {
wifiState1 = HIGH;
}
digitalWrite(wifi_led, wifiState1); // set the LED with the ledState of the variable:
blinkCount++;
Serial.println (blinkCount);
Serial.println ("blink");
Serial.println ();
}
// else {
// digitalWrite(wifi_led, LOW); // LOW turns LED on
// Serial.println (blinkCount);
// Serial.println ("else");
// Serial.println ();
// }
}
}
'''
output is as expected, it blinks every even number. It counts off as 1 "blink" thats fine
21:11:18.547 ->
21:11:20.434 -> 2
21:11:20.434 -> blink
21:11:20.434 ->
21:11:23.429 -> 3
21:11:23.429 -> blink
21:11:23.429 ->
21:11:26.410 -> 4
21:11:26.410 -> blink
21:11:26.410 ->
21:11:29.408 -> 5
21:11:29.408 -> blink
21:11:29.408 ->
21:11:32.404 -> 6
21:11:32.404 -> blink
led blinks 5 times and stops, end in OFF
The problem is when I want an else after, to turn on the the LED ON and stay on (in this sketch above uncommented ) the serial monitor output is always showing "else" and the counter increases and the LED does not blink.
20:54:11.052 -> 0
20:54:11.052 -> else
20:54:11.052 ->
20:54:11.052 -> 0
20:54:11.052 -> else
20:54:11.052 ->
20:54:11.052 -> 0
20:54:11.052 -> else
20:54:11.052 ->
20:54:11.052 -> 0
20:54:11.052 -> else
20:54:11.052 ->
20:54:11.052 -> 0
20:54:11.052 -> else
20:54:11.052 ->
20:54:11.052 -> 0
20:54:11.052 -> else
20:54:11.052 ->
20:54:11.052 -> 0
20:54:11.052 -> else
20:54:11.052 ->
20:54:11.052 -> 0
20:54:11.052 -> else
20:54:11.052 ->
20:54:11.052 -> 0
20:54:11.052 -> else
20:54:11.052 ->
20:54:11.052 -> 0
I expected
if (currentMillis - previousMillis >= interval && blinkCount <=10)
to run the blink + else count
and then when it gets to 10, it runs the final else
digitalWrite(wifi_led, LOW); // LOW turns LED on
Serial.println (blinkCount);
Serial.println ("else");
Serial.println ();
}
but the led stays on and counts to 11 and stops
21:15:37.275 -> 11
21:15:37.275 -> else
21:15:37.275 ->
21:15:37.275 -> 11
21:15:37.275 -> else
21:15:37.275 ->
21:15:37.275 -> 11
I'm missing why it goes strait to the final else.
Thanks
Clint
the whole part of the main sketch
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiUdp.h>
#include <functional>
#include "Switch.h"
#include "UpnpBroadcastResponder.h"
#include "CallbackFunction.h"
#include "credentials.h"
unsigned long currentMillis; // will store current millis value
unsigned long previousMillis = 0; // will store last time request was requested
const long interval = 3000; // .5 seconds interval
int wifi_led = 2; // 2 = BUILTIN_LED 2 by antenna
int ledState = LOW;
int wifiState = 3;
int wifiState1 = 4;
int buttonState = 0;
int lastButtonState = 0;
int blinkCount = 0; // Initialize the blink counter
boolean connectWifi();
// on/off callbacks
bool kitchenOn();
bool kitchenOff();
// Change this before you flash
//#######################################
const char* ssid = WIFI_SSID; // enter your access point/wifi router name
const char* password = WIFI_PASS; // enter router password
//#######################################
boolean wifiConnected = false;
UpnpBroadcastResponder upnpBroadcastResponder;
Switch *kitchen = NULL;
bool iskitchenOn = true;
void setup()
{
Serial.begin(115200);
pinMode(wifi_led, OUTPUT);
digitalWrite(wifi_led, HIGH); // turn LED off, not sure why HIGH = off but it works as intended
// Initialise wifi connection
wifiConnected = connectWifi();
if (wifiConnected) {
upnpBroadcastResponder.beginUdpMulticast();
// Define your switches here. Max 14
// Format: Alexa invocation name, local port no, on callback, off callback
kitchen = new Switch("kitchen", 83, kitchenOn, kitchenOff);
Serial.println("Adding switches upnp broadcast responder");
upnpBroadcastResponder.addDevice(*kitchen);
}
}
void loop()
{
if (wifiConnected)
{
upnpBroadcastResponder.serverLoop();
kitchen->serverLoop();
}
// blinks led, does not cause Alexa to say device not responding :)
if (wifiState == HIGH) { // Set HIGH from Alexa ON
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval && blinkCount <=10) { // on and off counts as 1 each , 10 = 10 blinks
previousMillis = currentMillis; // save the last time you blinked the LED
if (wifiState1 == HIGH) { // if the LED is off turn it on and vice-versa:
wifiState1 = LOW;
}
else {
wifiState1 = HIGH;
}
digitalWrite(wifi_led, wifiState1); // set the LED with the ledState of the variable:
blinkCount++;
Serial.println (blinkCount);
Serial.println ("blink");
Serial.println ();
}
// else {
// digitalWrite(wifi_led, LOW); // LOW turns LED on
// Serial.println (blinkCount);
// Serial.println ("else");
// Serial.println ();
// }
}
}
bool kitchenOn() {
Serial.print("wifi_led turned on ...");
wifiState = HIGH;
// af_on(); // look at bottom for funcion
// Serial.print("wifi_led turned on ...");
// digitalWrite(wifi_led, LOW);
// previousMillis = millis(); // time when command was given
return true; // Shows ON in app
}
bool kitchenOff() {
Serial.print("wifi_led turned off ...");
wifiState = LOW; // LOW = LED ON
digitalWrite (wifi_led, HIGH);
return false; // Shows OFF in app
}
// connect to wifi – returns true if successful or false if not
boolean connectWifi() {
boolean state = true;
int i = 0;
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
Serial.println("Connecting to WiFi");
// Wait for connection
Serial.print("Connecting ...");
while (WiFi.status() != WL_CONNECTED) {
delay(700);
Serial.print(".");
if (i > 15) {
state = false;
break;
}
i++;
}
if (state) {
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
digitalWrite(wifi_led, LOW); // LOW turns the led by wifi antenna on
delay(500);
digitalWrite(wifi_led, HIGH);
previousMillis = millis(); // time stamp when command was given
}
else {
Serial.println("");
Serial.println("Connection failed.");
}
return state;
}