Need help by making Wake-up light with WeMos d1 R2 + TEA5767 FM Radio

Hi,

I am working on a wake-up light with the WeMos d1 R2 and Blynk local server on Raspberry Pi (Which is fully operational)
With a RGB floodlight.

I need to slowly dim the lights from 0 to 1023 in an amount of time.
The time of dimming needs to be adjusted from within the app. (Menu widget to select 5, 10 or 15 minutes)
The start time is easy to configure with the timer widget.
The final color is a fixed color, beginning with a red color and ending with an orange/yellow color.
The waking up state (alarm) can be stopped when pushing a button widget.
During the alarm, the zeRGBa widget can’t be used.
I have a PWM fan for cooling the RGB. The speed of this fan is calculated on the brightness of the RGB. (Example: Value R+G+B/3=fanSpeed)

Optional: I want to connect the TEA5767 FM Radio to wake up with music, controlling with I2C. (fading volume is inside the TEA5767)

I tried to make it work, but I don’t know if I have to put things inside of the loop. Otherwise I don’t know where/how I can properly program this.
Normally I program everything inside the loop and need to assign every pin if it’s an output or input.

#include <Wire.h>
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>


char auth[] = "xxxxxxxxxxxxxxxxxxx";


char ssid[] = "xxxxxxxxxx";
char pass[] = "xxxxxxxxx";

//Pin LAYOUT
int red = 4;
int green = 5;
int blue = 6;
int fan = 7;

//Virtual pin declarations

//LIGHT CONTROL

boolean alarmTimer; // V1 time of alarm
boolean alarm; //V2 Toggle alarm
//V3 Reads fanSpeed value
boolean lightToggle; //V4 Toggles light
//V5 LCD in Tab "Light Control"
int redWidget; //V6 ZeRGBa red
int greenWidget; //V7 ZeRGBa green
int blueWidget; //V8 ZeRGBa blue

//ALARM SETTINGS

int redAlarm; //V9 Max value red alarm
int greenAlarm; //V10 Max value green alarm
int blueAlarm; //V11 Max value blue alarm
int timeOfFade; //V12 Index of fading time alarm [5min, 10min, 15min, 20min, 30min]
int maxAlarmRadioVolume; //V13 Max alarm radio volume
int alarmRadio; //V14 alarm radio on/off [With, Without]

//RADIO CONTROL

boolean radioToggle; //V15 Toggles radio on/off
boolean volumeUp; //V16 Button Volume up
boolean volumeDown; //V17 Button Volume down
boolean scanForward; //V18 Button Scan radio channel forward
boolean scanBackward; //V19 Button scan radio channel backward
int channelPreset; //V20 Preset Radio frequencies [Q-Music, Sky-Radio, 3FM, 538, NPO.
//V21 LCD in tab "Radio Control"  

//Pin values

int fanSpeed; // = redCur+greenCur+blueCur/3
int redCur; //Current value
int greenCur; //Current value
int blueCur;  //Current value


void setup(){
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass, "RASPBERRYPI");

  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);
  pinMode(fan, OUTPUT);
}

//BLYNK Declarations
BLYNK_CONNECTED() {
  Blynk.syncAll();
}
BLYNK_WRITE(V1){
  int timer = param.asInt(); // assigning incoming value from pin V1 to a variable
  if (timer == HIGH) {
    alarm = true;
  }
  else {
    alarm = false;
  }
}
BLYNK_WRITE(V2){
  button = param.asInt(); // assigning incoming value from pin V1 to a variable
}
BLYNK_WRITE(V6){
  redWidget = param.asInt(); // assigning incoming value from pin V6 to a variable
}
BLYNK_WRITE(V7){
  greenWidget = param.asInt(); // assigning incoming value from pin V7 to a variable
}
BLYNK_WRITE(V8){
  redWidget = param.asInt(); // assigning incoming value from pin V8 to a variable
}
BLYNK_READ(V3){
  Blynk.virtualWrite(3, fanSpeed); // Writes the speed of the fan to value widget
}

void loop(){
  Blynk.run();
  if (alarm == true){
    //Time dimming code, SimpleTimer is a solution.
  }
  else(){
  analogWrite(red, redWidget);
  analogWrite(green, greenWidget);
  analogWrite(blue, blueWidget);
  }

  //Fan Speed

  if (redCur > 0 || greenCur > 0 || blueCur > 0) {
    fanSpeed = (redCur + greenCur + blueCur) / 3;
    if (fanSpeed < 0)fanSpeed = 0;
    if (fanSpeed > 1023)fanSpeed = 1023;
  }
  //Fan driver
  analogWrite(fan, fanSpeed);

}
1 Like

Tried several ways, but I don’t know where to start right now…



This is what I had in mind…

Do I need to make new methods instead of putting everything in the loop?
Do I have to make a method for my fanSpeed, dimming alarm, light control and radio control?
It’s also not clear for me whether I need to declare every pin or not.

I hope someone can help me get started…

You definitely need to take all stuff out of the loop. That’s the first thing.

The other thing to remember is avoiding delays, but I’m not seeing those, so you are good on that front. Now for the wake-up part…

That’s gonna be tricky. As it happens I’m making something similar, but I want my lamp to emulate sunset/sunrise by calling an api from a website so I can get the correct times, instead of calculating them.

Than there is the matter of the lights itself. I used RGB leds, but you have to able to crossfade them, which is a pain in the you-know-where. I decided to use the WS2812B RGB leds. They can be controlled using the FastLED library and only require 1 data line. You can hook them up in a serial order, so you can enjoy them with as much as you need with only 1 cable! It’s awesome, they have pre-fab led strips too (which may be nice for your project, I would make the light in two strips behind the headboard of the bed or something, cool!).

In the case you are using those you can forget about the fan too. The ones you are using now must be really big, can you show me which ones they are? Just out of curiousity.

Anyway, if you know how much time they need to wake up you can setup the (variable) timers with simple timer and keep count of things, like r, g and b values (which happen to go from 0 - 255, not 1023, anyway, in case of WS2812, doesn’t matter really, just make sure the math is right). You can add/disable/enable timers on the fly. The documentation on the arduino.cc about SimpleTimer is well written. It should get you started good and otherwise, we are here. I’ll try to post my progress with the sunrise light here too, it’s basically the same, just a different alarm :wink:



I already have everything working. Maybe for a project next time, I will think of ws2812. With arduino I had everything working. Dimming with serial. But now I want to control it with WeMos d1 and Blynk😉

I have three constant current 350mA led drivers 0-38v controlled with PWM input to control the 30W RGB

Now I only need to make the fading work…
I tried this library, but I got an #include <avr/pgmspace.h> error.
I can’t find a library to get pgmspace.h

#include <Wire.h>
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
char auth[] = "xxxxxxxxxxxxxxxxxxx";
char ssid[] = "xxxxxxxxxx";
char pass[] = "xxxxxxxxx";

//Pin LAYOUT
int red = 4;
int green = 5;
int blue = 6;
int fan = 7;

//Virtual pin declarations

//LIGHT CONTROL
boolean alarmTrigger; // V1 trigger of alarm
boolean alarmToggle; //V2 Toggle alarm
//V3 Reads fanSpeed value
boolean lightToggle; //V4 Toggles light
//Option: V5 LCD in Tab "Light Control"
int redWidget; //V6 ZeRGBa red
int greenWidget; //V7 ZeRGBa green
int blueWidget; //V8 ZeRGBa blue

//ALARM SETTINGS
int redAlarm; //V9 Max value red alarm
int greenAlarm; //V10 Max value green alarm
int blueAlarm; //V11 Max value blue alarm
int timeOfFade; //V12 Index of fading time alarm [5min, 10min, 15min, 20min, 30min]
int maxAlarmRadioVolume; //V13 max volume Radio at end of timeOfFade
boolean alarmRadio; //V14 alarm radio on/off
boolean wakeUpModeON; //Blocks RGB Control during alarm !ONLY IN CODE NOT IN BLYNK-APP! 

//RADIO CONTROL
boolean radioToggle; //V15 Toggles radio on/off
boolean volumeUp; //V16 Button Volume up
boolean volumeDown; //V17 Button Volume down
int volume; //volume of radio
boolean scanForward; //V18 Button Scan radio channel forward
boolean scanBackward; //V19 Button scan radio channel backward
int channelPreset; //V20 Preset Radio frequencies [Q-Music, Sky-Radio, 3FM, 538, NPO.
//V21 LCD in tab "Radio Control"

//Pin values
int fanSpeed; // = redCur+greenCur+blueCur/3
int redCur; //Current value
int greenCur; //Current value
int blueCur;  //Current value

void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass, "RASPBERRYPI");
}

//BLYNK Declarations
BLYNK_CONNECTED() {
  Blynk.syncAll();
}
BLYNK_WRITE(V1) {
  alarmTrigger = param.asInt(); // is HIGH when alarm goes off
  if(alarmToggle == HIGH && alarmTrigger == HIGH){ //Starts WakeUpMode when I turned on the alarm && alarm goes off
    wakeUpMode();
  }
  else{
    wakeUpModeON = false;
  }
}
BLYNK_WRITE(V2) {
   alarmToggle = param.asInt(); // Gives the current value of the alarmToggle
}
BLYNK_READ(V3) {
  Blynk.virtualWrite(3, fanSpeed); // Writes the speed of the fan to value widget
}
BLYNK_WRITE(V4) {
   lightToggle = param.asInt(); // Gives the current value of the lightToggle
}
//V5 LCD LightController

BLYNK_WRITE(V6) {
  redWidget = param.asInt(); // writes red value ZeRGBa to redWidget
}
BLYNK_WRITE(V7) {
  greenWidget = param.asInt(); // writes green value ZeRGBa to greenWidget
}
BLYNK_WRITE(V8) {
  redWidget = param.asInt(); // writes blue value ZeRGBa to blueWidget
}
BLYNK_WRITE(V9) {
   redAlarm = param.asInt(); // writes max red value to redAlarm
}
BLYNK_WRITE(V10) {
   greenAlarm = param.asInt(); // writes max green value to greenAlarm
}
BLYNK_WRITE(V11) {
   blueAlarm = param.asInt(); // writes max blue value to blueAlarm
}
BLYNK_WRITE(V12) {
   timeOfFade = param.asInt(); // writes time of fading fixed [5min, 10min, 15min, 20min, 30min]
}
BLYNK_WRITE(V13) {
   maxAlarmRadioVolume = param.asInt(); // max volume Radio at end of timeFade
}
BLYNK_WRITE(V14) {
   alarmRadio = param.asInt();
}
BLYNK_WRITE(V15) {
   radioToggle = param.asInt();
   startRadio();
}
BLYNK_WRITE(V16) {
   volume += param.asInt();
   volumeRadio();
}
BLYNK_WRITE(V17) {
   volumeDown -= param.asInt();
   volumeRadio();
}
BLYNK_WRITE(V18) {
   scanForward = param.asInt();
   
}
BLYNK_WRITE(V19) {
   scanBackward = param.asInt();

}
BLYNK_WRITE(V20) {
   channelPreset = param.asInt();
    //1=Q-Music, 2=Sky-Radio, 3=3FM, 4=538, 5=NPO
}

//V21 LCD RadioController

//COLOR CHANGE
void colorChange(){
  //Maybe only write when changes!!!
  analogWrite(red, redCur);
  analogWrite(green, greenCur);
  analogWrite(blue, blueCur);
  fanSpeedControl();
}

//AUTOMATION

void wakeUpMode(){
  wakeUpModeON = true; //Blocks RGBControl during wakeUpMode
  if (alarmRadio == HIGH){
    startRadio();
  }
 
  //Fading script
}
//Fan Control
void fanSpeedControl(){
  if (redCur > 0 || greenCur > 0 || blueCur > 0) {
    fanSpeed = (redCur + greenCur + blueCur) / 3;
    if (fanSpeed < 0)fanSpeed = 0;
    if (fanSpeed > 1023)fanSpeed = 1023;
  }
  analogWrite(fan, fanSpeed);
}

void ZeRGBa(){
  if (wakeUpModeON == false && lightToggle == HIGH){
    redCur = redWidget;
    greenCur = greenWidget;
    blueCur = blueWidget;
    colorChange();
    
  }
}
//Radio Control

void startRadio(){
  // Start and stop radio command
}
void scanRadio(){
  //scan forward and backward
}
void volumeRadio(){
  //Controls volume Radio
}
void loop() {
  Blynk.run();
  colorChange();
  
  
}

I’d definitely try to get the colorChange out of the loop. I’m just not sure how to do that properly with SimpleTimer, but there will be an answer … it will probably be a bit of math based on which color you want to end up with and how much time elapses. In 5 minutes there are 300 seconds, so you’d need about a 3-point increase each step to fully light up the LED. And so on …

Otherwise, nice coding :slight_smile:

Yeah, I saw when I wanted to quit for the day. Now I got it in the end of wakeUpMode() and at the end of ZeRGBa().
Also I think I need to make a variable changedRGB to only write new value to RGB’s when changed and not always sending the same value…

That would probably be a wise decision, especially with projects like these which are not time critical to the second, they can be a bit off, right? :slight_smile:

I got everything working, but when I want to write to the actual pins, the system reboots.
Also when I want to declare the pinMode it reboots.
And when I make a Blynk.virtualWrite(4, xxx) I get a value in the app. So the processing of the data is doing just fine.

What am I doing wrong?

#include <Wire.h>
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#define PWMRANGE 1023
char auth[] = "XXXXXXXXXX"
char ssid[] = "XXXXXX"
char pass[] = "XXXXXX"

//Pin LAYOUT
int red = 4;
int green = 5;
int blue = 6;
int fan = 7;

//Virtual pin declarations

//LIGHT CONTROL
boolean alarmTrigger; // V1 trigger of alarm
boolean alarmToggle; //V2 Toggle alarm
//V3 Reads fanSpeed value
boolean lightToggle; //V4 Toggles light
WidgetLCD lcdLight(V5);
int redWidget; //V6 ZeRGBa red
int greenWidget; //V7 ZeRGBa green
int blueWidget; //V8 ZeRGBa blue
boolean colorChanged; // prevents writing lightValue if already did
//ALARM SETTINGS
int redAlarm; //V9 Max value red alarm
int greenAlarm; //V10 Max value green alarm
int blueAlarm; //V11 Max value blue alarm
int timeOfFade; //V12 Index of fading time alarm [5min, 10min, 15min, 20min, 30min]
int maxAlarmRadioVolume; //V13 max volume Radio at end of timeOfFade
boolean alarmRadio; //V14 alarm radio on/off
boolean wakeUpModeON = false; //Blocks RGB Control during alarm !ONLY IN CODE NOT IN BLYNK-APP!

//RADIO CONTROL
boolean radioToggle; //V15 Toggles radio on/off
boolean volumeUp; //V16 Button Volume up
boolean volumeDown; //V17 Button Volume down
int volume; //volume of radio
boolean scanForward; //V18 Button Scan radio channel forward
boolean scanBackward; //V19 Button scan radio channel backward
int channelPreset; //V20 Preset Radio frequencies [Q-Music, Sky-Radio, 3FM, 538, NPO.
WidgetLCD lcdRadio(V21);

//Pin values
int fanSpeed; // = redCur+greenCur+blueCur/3
int redCur; //Current value
int greenCur; //Current value
int blueCur;  //Current value

void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass, "RASPBERRYPI");

while (Blynk.connect() == false) {
}
pinMode(red, OUTPUT); //**REBOOTS ERROR**
pinMode(green, OUTPUT); //**REBOOT ERROR**
pinMode(blue, OUTPUT); //**REBOOT ERROR**
pinMode(fan, OUTPUT); //**REBOOT ERROR**
}
//BLYNK Declarations
BLYNK_CONNECTED() {
  Blynk.syncAll();
}
BLYNK_WRITE(V1) {
  alarmTrigger = param.asInt(); // is HIGH when alarm goes off
  if (alarmToggle == HIGH && alarmTrigger == HIGH) { //Starts WakeUpMode when I turned on the alarm && alarm goes off
    wakeUpMode();
  }
  else {
    wakeUpModeON = false;
  }
}
BLYNK_WRITE(V2) {
  alarmToggle = param.asInt(); // Gives the current value of the alarmToggle
}
BLYNK_READ(V3) {
  Blynk.virtualWrite(3, fanSpeed); // Writes the speed of the fan to value widget
}
BLYNK_WRITE(V4) {
  lightToggle = param.asInt(); // Gives the current value of the lightToggle
}
//V5 LCD LightController

BLYNK_WRITE(V6) {
  redWidget = param.asInt(); // writes red value ZeRGBa to redWidget
  if (redWidget != redCur) {
    ZeRGBa();
  }
}
BLYNK_WRITE(V7) {
  greenWidget = param.asInt(); // writes green value ZeRGBa to greenWidget
  if (greenWidget != greenCur) {
    ZeRGBa();
  }
}
BLYNK_WRITE(V8) {
  blueWidget = param.asInt(); // writes blue value ZeRGBa to blueWidget
  if (blueWidget != blueCur) {
    ZeRGBa();
  }
}
BLYNK_WRITE(V9) {
  redAlarm = param.asInt(); // writes max red value to redAlarm
}
BLYNK_WRITE(V10) {
  greenAlarm = param.asInt(); // writes max green value to greenAlarm
}
BLYNK_WRITE(V11) {
  blueAlarm = param.asInt(); // writes max blue value to blueAlarm
}
BLYNK_WRITE(V12) {
  timeOfFade = param.asInt(); // writes time of fading fixed [5min, 10min, 15min, 20min, 30min]
}
BLYNK_WRITE(V13) {
  maxAlarmRadioVolume = param.asInt(); // max volume Radio at end of timeFade
}
BLYNK_WRITE(V14) {
  alarmRadio = param.asInt();
}
BLYNK_WRITE(V15) {
  radioToggle = param.asInt();
  startRadio();
}
BLYNK_WRITE(V16) {
  volume += param.asInt();
  volumeRadio();
}
BLYNK_WRITE(V17) {
  volumeDown -= param.asInt();
  volumeRadio();
}
BLYNK_WRITE(V18) {
  scanForward = param.asInt();

}
BLYNK_WRITE(V19) {
  scanBackward = param.asInt();

}
BLYNK_WRITE(V20) {
  channelPreset = param.asInt();
  //1=Q-Music, 2=Sky-Radio, 3=3FM, 4=538, 5=NPO
}

//V21 LCD RadioController

//COLOR CHANGE
void colorChange() {
  if (colorChanged == true) {
    analogWrite(red, redCur); //**REBOOT ERROR**
    analogWrite(green, greenCur); //**REBOOT ERROR**
    analogWrite(blue, blueCur); //**REBOOT ERROR**

    fanSpeedControl();
    lcdLightControl();
    colorChanged = false;
  }
}

//AUTOMATION

void wakeUpMode() {
  wakeUpModeON = true; //Blocks RGBControl during wakeUpMode
  if (alarmRadio == HIGH) {
    startRadio();
  }

  //Fading script
  //lightChanged = true;
  //inside loop colorChange();

}
//Fan Control
void fanSpeedControl() {
  if (redCur > 0 || greenCur > 0 || blueCur > 0) {
    fanSpeed = (redCur + greenCur + blueCur) / 3;
    if (fanSpeed < 0)fanSpeed = 0;
    if (fanSpeed > 1023)fanSpeed = 1023;
  }
    analogWrite(fan, fanSpeed); //**REBOOT ERROR**
}

void ZeRGBa() {
  if (wakeUpModeON == false && lightToggle == true) {
    redCur = redWidget;
    greenCur = greenWidget;
    blueCur = blueWidget;
    colorChanged = true;
    colorChange();

  }
}
//Radio Control

void startRadio() {
  // Start and stop radio command
}
void scanRadio() {
  //scan forward and backward
}
void volumeRadio() {
  //Controls volume Radio
}

//LCD Control

void lcdLightControl() {
  lcdLight.clear();
    lcdLight.print(0, 0, "R");
    lcdLight.print(1, 0, redCur);
    lcdLight.print(5, 0, "G");
    lcdLight.print(6, 0, greenCur);
    lcdLight.print(0, 1, "B");
    lcdLight.print(1, 1, blueCur);
  
}
void loop() {
  Blynk.run();


}

@hutje the Blynk functions are standalone functions that don’t go in setup().

@Costas
What do you mean with Blynk functions?
while (Blynk.connect() == false) { // Wait until connected }
This code is in nearly every example…

All these:

BLYNK_WRITE(V2) {

}

They are not in the setup…

You fooled me by having the shortest setup() I have ever seen and the pinMode’s outside setup() , albeit commented out.

Surely you are going to want a timer or ten in setup(), no?

I don’t know what I want. My setup includes the pinMode…
I want to make a variable timer to make a fade in for my wake up light but don’t know yet how…
But now I get various errors when I want to write to any pinOut

@hutjeOk I have loaded your sketch into the compiler.

Not the reason for your problems but I will mention them anyway:

  1. Whitespace is free so perhaps use some between each fucntion.
  2. Everything within a function should be indented including the setup() function. Your while statement looked like it was something outside setup() because it isn’t indented.

I always do the pinMode as the first thing in setup(), you might want to try moving it.

I’ll look to see if I can find anything else.