Stonelamp with Wemos D1 mini and 5050 NeoPixel

Thanks for that but I actually meant a photo of the bottom of the base to see how it is physically mounted in the wood ?

Sorry to be a pain!!!

Cheers

kev

Blynk runs on my own server and I just do not have this functionality, maybe because I use an older server version?

1 Like

Brill thanks!!!

New Local Server versions do seem to allow cloning of Apps again :slight_smile:

I have been experimenting with your prior code with my 90 LED RGB Shower… Fire and Water :stuck_out_tongue: (click image for full effect)

So far so good, but there appears to be an auto shut off somewhere in your code? but I haven’t found it yet.

Not that I know.

@Gunner: i am using his code for a long time now, my lamp never shuts off…

@mhroner: THANK YOU!
I have replicate your work with very few modifications. Works perfect.
Then i was thinking to add to this the vocal control using Alexa. The library is wemo.manager.
Given the fact that Alexa already works, i was thinking why not asking alexa to set the lamp to one color.
And so the pain begins. Your code is almost rocket science for me, please do not laugh :slight_smile:

So, where i am now? If your code is untouched (posting below) the lamp is working as espected. But Alexa says “OK” and the lap is turning red (for example) and switch back to whatever the lamp was doing before the voice request. If i am commenting activity(current_activity); from the void loop, all voice requests work, but none of the lamp activities anymore. So i am thinking that activity running in loop will override whatever alexa set, and that is why is going back to current activity.

Can someone please help me to solve this? Meaning the lamp to work as intended, and the voice control to work also? For me it is enough to work with just 3 colors on voice request, that is why are just 3 there.


    #include <ESP8266WiFi.h>
    #include <ESP8266mDNS.h>
    #include <BlynkSimpleEsp8266.h>
    #include <WiFiUdp.h>
    #include <SimpleTimer.h>
    SimpleTimer timer;
    #include "WemoSwitch.h"
    #include "WemoManager.h"
    #include "CallbackFunction.h"

        // Wireless settings
    const char AUTH[] = "xxx";
    const char SSID[] = "xxx"; 
    const char PASSWORD[] = "xxx";
    
WemoManager wemoManager;
WemoSwitch *red1 = NULL;
WemoSwitch *green1 = NULL;
WemoSwitch *blue1 = NULL;
    
    int ONOFF_LED = V13;
    int UP_LED = V14;
    long int last_UP_change = millis();
    bool runn = 1;

    // RGB Lights
    #include <Adafruit_NeoPixel.h>
    #define NUM_LEDS 60
    const int NUM_LEDS4 = 15;
    #define PIN 0

    Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);
    
    //const int long red = 16711680;
    //const int long green = 65280;
    const int long blue = 255;
    int brightness = 155;
    int standard_speed = 50;
    int current_activity = 1;
    int this_activity = 0;
    long current_color = blue;
    bool lamp_on = false;
    bool blinkon = false;

    // Timing
    volatile int WDTCount = 0;

    void setup() {
     Serial.begin(115200);
     
    //Blynk
     WiFi.mode(WIFI_STA);
     Blynk.begin(AUTH, SSID, PASSWORD,"myserver.com",8442);
       while (Blynk.connect() == false) {  // Wait until connected
         if ((millis() - last_UP_change) > 30000) { // 30s before reset
           ESP.restart();
        }
      }
     wait(500);
     
    // Init Neopixels
      strip.begin();
      all_lights(0);


      wait(3000);
      timer.setInterval(500L, blinkonoff);
      timer.setInterval(1000L, ISRwatchdog);

      Blynk.virtualWrite(ONOFF_LED,0); 
      Blynk.virtualWrite(UP_LED,0); 
      wemoManager.begin();
  // Format: Alexa invocation name, local port no, on callback, off callback
  red1 = new WemoSwitch("Red", 80, redOn, redOff);
  green1 = new WemoSwitch("Green", 81, greenOn, greenOff);
  blue1 = new WemoSwitch("Blue", 82, blueOn, blueOff);
  
  wemoManager.addDevice(*red1);
  wemoManager.addDevice(*green1);
  wemoManager.addDevice(*blue1);
    // Init Neopixels
      strip.begin();
  for (int i = 0; i < NUM_LEDS; i++)
  strip.setPixelColor(i, strip.Color(0, 0, 0)); 
  strip.show(); // This sends the updated pixel color to the hardware.
  
    }


    void loop() {
       wemoManager.serverLoop();
      Blynk.run();
      if(!Blynk.connected()) {
        //Serial.println(F("Resetting in loop"));
        ESP.restart();
      }
      WDTCount = 0;
      timer.run();
      activity(current_activity);
    }

     
    void activity(int activity_button) {
      if (lamp_on) {
        this_activity = activity_button;
        strip.setBrightness(brightness);
        switch (activity_button) {
          case 0:  // no activity
            all_lights(current_color);
            if (blinkon) {
              wait(standard_speed*2);
              strip.setBrightness(0);
              strip.show();
              wait(standard_speed*2);
              strip.setBrightness(brightness);
             }
          break;
          case 1:  // colorWipe
          colorWipe(0); 
          colorWipe(current_color); 
        break;
        case 2:  // theaterChase
          theaterChase(strip.Color(random(255), random(255), random(255)));
        break;
        case 3:  // rainbow
          rainbow();
        break;
        case 4:  // rainbowCycle
          rainbowCycle();
        break;
        case 5:  // theaterChaseRainbow
          theaterChaseRainbow();
        break;
        case 6:  // CylonBounce
          CylonBounce(splitColor (current_color,'r'), splitColor (current_color,'g'), splitColor (current_color,'b'), 3);
        break;
        case 7:  // TwinkleRandom
          TwinkleRandom(20,false);
        break;
        case 8:  // Sparkle
          Sparkle(random(255), random(255), random(255));
         break;
        case 9:  // RunningLights
          RunningLights(splitColor (current_color,'r'), splitColor (current_color,'g'), splitColor (current_color,'b'));
        break;
        case 10:  // Fire
          Fire(55,120);// was 55,120
        break;
        case 11:  // fade
          FadeInOut(splitColor (current_color,'r'), splitColor (current_color,'g'), splitColor (current_color,'b'));
        break;
        case 12:  // rotate
          rotate();
        break; 
       }
     }
     else {
      strip.setBrightness(0);
      strip.show();
     }
    }


    // Fill the dots one after the other with a color (1)
    void colorWipe(uint32_t c) {
        for (int i = 0; i < NUM_LEDS4; i++) {
          if (!lamp_on || this_activity != current_activity) break;
          for (int j = 0; j < 4; j++) {
          strip.setPixelColor(i+j*NUM_LEDS4,c);
          }
          strip.show();
          wait(standard_speed/3);
       }
    }


    //Theatre-style crawling lights. (2)
    void theaterChase(uint32_t c) {
      for (int j = 0; j < 10; j++) { //do 10 cycles of chasing
        if (!lamp_on || this_activity != current_activity) break;
        for (int q = 0; q < 3; q++) {
          if (!lamp_on || this_activity != current_activity) break;
          for (int i = 0; i < NUM_LEDS4; i = i + 3) {
            if (!lamp_on || this_activity != current_activity) break;
            for (int m = 0; m < 4; m++) {
            strip.setPixelColor(i + q + m*NUM_LEDS4, c);  //turn every third pixel on
            }
          }
          strip.show();
          wait(standard_speed*3);
          for (int i = 0; i < NUM_LEDS; i = i + 3) {
            if (!lamp_on || this_activity != current_activity) break;
            for (int m = 0; m < 4; m++) {
            strip.setPixelColor(i + q + m*NUM_LEDS4, 0);      //turn every third pixel off
            }
          }
        }
      }
    }


    // (3)
    void rainbow() {
      uint16_t i, j;
      for (j = 0; j < 256; j+=2) {
        if (!lamp_on || this_activity != current_activity) break;
        for (i = 0; i < NUM_LEDS4; i++) {
          if (!lamp_on || this_activity != current_activity) break;
          for (int m = 0; m < 4; m++) {
            strip.setPixelColor(i + m*NUM_LEDS4,Wheel((i + j) & 255));
            strip.show();
            wait(standard_speed/5);
          }
         }
       }
    }


    // Slightly different, this makes the rainbow equally distributed throughout (4)
    void rainbowCycle() {
      uint16_t i, j;
      for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
        if (!lamp_on || this_activity != current_activity) break;
        for (i = 0; i < NUM_LEDS4; i++) {
          if (!lamp_on || this_activity != current_activity) break;
          for (int m = 0; m < 4; m++) {
            strip.setPixelColor(i + m*NUM_LEDS4, Wheel(((i * 256 / NUM_LEDS4) + j) & 255));
           }
         }
        strip.show();
        wait(standard_speed/2);
      }
    }


    //Theatre-style crawling lights with rainbow effect (5)
    void theaterChaseRainbow() {
      for (int j = 0; j < 256; j+=2) {   // cycle all 256 colors in the wheel
        if (!lamp_on || this_activity != current_activity) break;
        for (int q = 0; q < 3; q++) {
          if (!lamp_on || this_activity != current_activity) break;
          for (int i = 0; i < NUM_LEDS4; i = i + 3) {
            if (!lamp_on || this_activity != current_activity) break;
            for (int m = 0; m < 4; m++) {
            strip.setPixelColor(i + q + m*NUM_LEDS4, Wheel( (i + j) % 255)); //turn every third pixel on
            }
          }
          strip.show();
          wait(standard_speed*3);
          for (int i = 0; i < NUM_LEDS4; i = i + 3) {
            if (!lamp_on || this_activity != current_activity) break;
            for (int m = 0; m < 4; m++) {
            strip.setPixelColor(i + q + m*NUM_LEDS4, 0);      //turn every third pixel off
            }
          }
        }
      }
    }


    // (6)
    void CylonBounce(byte red, byte green, byte blue, int EyeSize){
      for(int i = 0; i < NUM_LEDS4-EyeSize-2; i++) {
        if (!lamp_on || this_activity != current_activity) break;
        all_lights(0);
         for (int m = 0; m < 4; m++) {
           setPixel(i+ m*NUM_LEDS4, red/10, green/10, blue/10);
         }
        for(int j = 1; j <= EyeSize; j++) {
          if (!lamp_on || this_activity != current_activity) break;
           for (int m = 0; m < 4; m++) {
             setPixel(i + j + m*NUM_LEDS4, red, green, blue); 
           }
        }
          for (int m = 0; m < 4; m++) {
            setPixel(i + EyeSize + 1 + m*NUM_LEDS4, red/10, green/10, blue/10);
          }
        strip.show();
        wait(standard_speed/2);
      }
      wait(standard_speed);

      for(int i = NUM_LEDS4-EyeSize-2; i > 0; i--) {
        if (!lamp_on || this_activity != current_activity) break;
        all_lights(0);
        for (int m = 0; m < 4; m++) {
          setPixel(i + m*NUM_LEDS4, red/10, green/10, blue/10);
        }
        for(int j = 1; j <= EyeSize; j++) {
          if (!lamp_on || this_activity != current_activity) break;
          for (int m = 0; m < 4; m++) {
            setPixel(i + j + m*NUM_LEDS4, red, green, blue); 
          }
        }
        for (int m = 0; m < 4; m++) {
          setPixel(i +EyeSize + 1 + m*NUM_LEDS4, red/10, green/10, blue/10);
        }
        strip.show();
        wait(standard_speed/2);
      }
      wait(standard_speed);
    }


    // (7)
    void TwinkleRandom(int Count,boolean OnlyOne) {
      all_lights(0);
       for (int i=0; i<Count; i++) {
         if (!lamp_on || this_activity != current_activity) break;
         setPixel(random(NUM_LEDS),random(0,255),random(0,255),random(0,255));
         strip.show();
         wait(standard_speed);
         if(OnlyOne) { 
           all_lights(0); 
         }
       }
       wait(standard_speed/2);
    }


    // (8)
    void Sparkle(byte red, byte green, byte blue) {
      all_lights(0);
      int Pixel = random(NUM_LEDS);
      setPixel(Pixel,red,green,blue);
      strip.show();
      wait(standard_speed);
      setPixel(Pixel,0,0,0);
    }


    // (9)
    void RunningLights(byte red, byte green, byte blue) {
      int Position=0;
      for(int i=0; i<NUM_LEDS4*2; i++)  {
        if (!lamp_on || this_activity != current_activity) break;
          Position++; // = 0; //Position + Rate;
          for(int i=0; i<NUM_LEDS4; i++) {
            if (!lamp_on || this_activity != current_activity) break;
              for (int m = 0; m < 4; m++) {
                if (!lamp_on || this_activity != current_activity) break;
                setPixel(i + m*NUM_LEDS4,((sin(i+Position) * 127 + 128)/255)*red,
                       ((sin(i+Position) * 127 + 128)/255)*green,
                       ((sin(i+Position) * 127 + 128)/255)*blue);
            }
          }
          strip.show();
          wait(standard_speed*2);
      }
    }


    // (10)
    void Fire(int Cooling, int Sparking) {
      static byte heat[NUM_LEDS];
      int cooldown;
      // Step 1.  Cool down every cell a little
      for (int m = 0; m < 4; m++) {
        for( int i = 0; i < NUM_LEDS4; i++) {
          cooldown = random(0, ((Cooling * 10) / NUM_LEDS4) + 2);
          if(cooldown>heat[i + m*NUM_LEDS4]) {
            heat[i + m*NUM_LEDS4]=0;
         } else {
            heat[i + m*NUM_LEDS4]=heat[i + m*NUM_LEDS4]-cooldown;
          }
        }
      }
      // Step 2.  Heat from each cell drifts 'up' and diffuses a little
      for (int m = 0; m < 4; m++) {
        for( int k = NUM_LEDS4 - 1; k >= 2; k--) {
          if (!lamp_on || this_activity != current_activity) break;
          heat[k + m*NUM_LEDS4] = (heat[k + m*NUM_LEDS4 - 1] + heat[k + m*NUM_LEDS4 - 2] + heat[k + m*NUM_LEDS4 - 2]) / 3;
        }
      }
      // Step 3.  Randomly ignite new 'sparks' near the bottom
      for (int m = 0; m < 4; m++) {
        if( random(255) < Sparking ) {
          int y = random(3); //was 7
          heat[y + m*NUM_LEDS4] = heat[y + m*NUM_LEDS4] + random(160,255);
        }
      }
      // Step 4.  Convert heat to LED colors
      for( int j = 0; j < NUM_LEDS; j++) {
        if (!lamp_on || this_activity != current_activity) break;
        setPixelHeatColor(j, heat[j]);
      }
      strip.show();
      wait(standard_speed);
    }


    void setPixelHeatColor (int Pixel, byte temperature) {
      // Scale 'heat' down from 0-255 to 0-191
      byte t192 = round((temperature/255.0)*191);
      // calculate ramp up from
      byte heatramp = t192 & 0x3F; // 0..63
      heatramp <<= 2; // scale up to 0..252
      // figure out which third of the spectrum we're in:
      if( t192 > 0x80) {                     // hottest
        setPixel(Pixel, 255, 255, heatramp);
      } else if( t192 > 0x40 ) {             // middle
        setPixel(Pixel, 255, heatramp, 0);
      } else {                               // coolest
        setPixel(Pixel, heatramp, 0, 0);
      }
    }


    //(11)
    void FadeInOut(byte red, byte green, byte blue){
      float r, g, b;
          
      for(int k = 20; k < 256; k=k+1) {
        if (!lamp_on || this_activity != current_activity) break; 
        r = (k/256.0)*red;
        g = (k/256.0)*green;
        b = (k/256.0)*blue;
        all_lights(r,g,b);
        wait(standard_speed/6);
      }
         
      for(int k = 255; k >= 20; k=k-2) {
        if (!lamp_on || this_activity != current_activity) break;
        r = (k/256.0)*red;
        g = (k/256.0)*green;
        b = (k/256.0)*blue;
        all_lights(r,g,b);
        wait(standard_speed/6);
      }
    }


    //Rotate (12)
    void rotate() {
      all_lights(0);
      for (int m = 0; m < 4; m++) {
        for (int i = m*NUM_LEDS4; i < (m+1)*NUM_LEDS4; i++) {
          if (!lamp_on || this_activity != current_activity) break;
            strip.setPixelColor(i,current_color);
           }
          strip.show();
          wait(standard_speed*2);
        for (int i = m*NUM_LEDS4; i < (m+1)*NUM_LEDS4; i++) {
          if (!lamp_on || this_activity != current_activity) break;
            strip.setPixelColor(i,0);
           }
          strip.show();
      }
    }


    void all_lights(int g, int r, int b) {
      wait(1);
        for (int x = 0; x < NUM_LEDS; x++) {
          strip.setPixelColor(x, g, r, b);
        }
        strip.show();
    }


    void all_lights(int color) {
      wait(1);
        for (int x = 0; x < NUM_LEDS; x++) {
          strip.setPixelColor(x,color);
        }
        strip.show();
        }


    // Input a value 0 to 255 to get a color value.
    // The colours are a transition r - g - b - back to r.
    uint32_t Wheel(byte WheelPos) {
      WheelPos = 255 - WheelPos;
      if (WheelPos < 85) {
        return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
      } else if (WheelPos < 170) {
        WheelPos -= 85;
        return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
      } else {
        WheelPos -= 170;
        return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
      }
    }


    /**
       splitColor() - Receive a uint32_t value, and spread into bits.
    */
    byte splitColor ( uint32_t c, char value )
    {
      switch ( value ) {
        case 'r': return (uint8_t)(c >> 16);
        case 'g': return (uint8_t)(c >>  8);
        case 'b': return (uint8_t)(c >>  0);
        default:  return 0;
      }
    }

    void wait (int ms) {
      long current_time = millis();
      long end_time = current_time + ms; 
      while (current_time < end_time) {
        if (!lamp_on || this_activity != current_activity) break;
        current_time = millis();
      }
      Blynk.run();
      timer.run();
    }


    void blinkonoff(){
     // Turn runn LED on/off
       Blynk.virtualWrite(UP_LED,runn * 255);  // turn Up off
       runn = !runn;
       last_UP_change = millis();  
       WDTCount = 0;
    }

    BLYNK_WRITE(V0) { //ON Off
        if (param.asInt()) lamp_on = 1;
        else {
          all_lights(0);
          lamp_on = 0;
        }
        Blynk.virtualWrite(ONOFF_LED,lamp_on * 255);  // turn Up off      
    }

    BLYNK_WRITE(V1) { //Activity slider
       current_activity = param.asInt();      
    }
      
    BLYNK_WRITE(V8) { //Blink
      if (param.asInt()) blinkon = true;
      else blinkon = false;     
    }

    BLYNK_WRITE(V10) { // Brightness slider
        brightness = param.asInt();
        strip.setBrightness(brightness);
        strip.show();     
    }

    BLYNK_WRITE(V11) { // Speed
      standard_speed = param.asInt();      
    }

    BLYNK_WRITE(V12) { //RGB light
      current_color = param[2].asInt() + 256*param[1].asInt() + 256*256*param[0].asInt();  
    }


    void ISRwatchdog() {
      WDTCount++;
      if (WDTCount == 5) {
        ESP.reset();
      }
    }


    void setPixel(int Pixel, byte red, byte green, byte blue) {
       strip.setPixelColor(Pixel, strip.Color(red, green, blue));
    }

    
    
// Toggle the GREEN on
void greenOn() {
   for (int i = 0; i < NUM_LEDS; i++)
    strip.setPixelColor(i, strip.Color(0, 255, 0)); 
    strip.show(); // This sends the updated pixel color to the hardware.
    Blynk.virtualWrite(V9, 1023);     // Sync the Blynk button widget state
    Blynk.virtualWrite(V7, 0);      // Sync the Blynk button widget state
    Blynk.virtualWrite(V6, 0);     // Sync the Blynk button widget state
}

// Toggle the GREEN off
void greenOff() {
   for (int i = 0; i < NUM_LEDS; i++)
    strip.setPixelColor(i, strip.Color(0, 0, 0)); 
    strip.show(); // This sends the updated pixel color to the hardware
    Blynk.virtualWrite(V9, 0);      // Sync the Blynk button widget state
    Blynk.virtualWrite(V7, 0);      // Sync the Blynk button widget state
    Blynk.virtualWrite(V6, 0);     // Sync the Blynk button widget state
}
 
    // Toggle the RED on
void redOn() {
  for (int i = 0; i < NUM_LEDS; i++)
    strip.setPixelColor(i, strip.Color(255, 0, 0));
    strip.show(); // This sends the updated pixel color to the hardware.
    Blynk.virtualWrite(V7, 1023);     // Sync the Blynk button widget state
    Blynk.virtualWrite(V9, 0);      // Sync the Blynk button widget state
    Blynk.virtualWrite(V6, 0);     // Sync the Blynk button widget state
}

// Toggle RED off
void redOff() {
   for (int i = 0; i < NUM_LEDS; i++)
    strip.setPixelColor(i, strip.Color(0, 0, 0)); 
    strip.show(); // This sends the updated pixel color to the hardware.
    Blynk.virtualWrite(V7, 0);      // Sync the Blynk button widget state
    Blynk.virtualWrite(V9, 0);      // Sync the Blynk button widget state
    Blynk.virtualWrite(V6, 0);     // Sync the Blynk button widget state
}

void blueOn() {
   for (int i = 0; i < NUM_LEDS; i++)
    strip.setPixelColor(i, strip.Color(0, 0, 255)); 
    strip.show(); // This sends the updated pixel color to the hardware.
    Blynk.virtualWrite(V6, 1023);     // Sync the Blynk button widget state
    Blynk.virtualWrite(V9, 0);     // Sync the Blynk button widget state
    Blynk.virtualWrite(V7, 0);      // Sync the Blynk button widget state
}


void blueOff() {
   for (int i = 0; i < NUM_LEDS; i++)
    strip.setPixelColor(i, strip.Color(0, 0, 0)); 
    strip.show(); // This sends the updated pixel color to the hardware
    Blynk.virtualWrite(V6, 0);     // Sync the Blynk button widget state
    Blynk.virtualWrite(V9, 0);      // Sync the Blynk button widget state
    Blynk.virtualWrite(V7, 0);      // Sync the Blynk button widget state
}

Me again.
Well, maybe i was not waiting enough. Mine also stops after some time.

You did not populate the necessary variables.
For a light to be on set the current_activity to zero and current_color must be set to a color value (i.e. 255 for blue or 16711680 for red etc. and lamp_on must be true.

My lamp never stops, but if it looses connection it restarts and reloads the latest status from the EEPROM in order to continue with the same activity where it stops (see my updated code V1.1. for EEPROM support). Second you could try to uncomment this line
//timer.setInterval(1000L, ISRwatchdog);
this could help to stabilize your system

It worked :slight_smile: Thank you
Is there anyplace where i can find espeeprom.h library to test your latest version of the code?

P.S. ISRwatchdog timer was already uncommented…

I used this library; https://github.com/esp8266/Arduino/tree/master/libraries/EEPROM from Ivan Grokhotkov. I had to rename it and make some changes in the files as I already had an EEPROM library for Arduinos

I have some connect issues and i dont get why.
What could it be exempt for the ssid Password in Auth?

3 posts were split to a new topic: I added a sunrise alarm clock function which is controlled by the new eventor widget, but now Wemos crashes multiple time a night

hi sir/madam i am beginner with blynk
can i get app that you designed for this code

Hi,
I have changed the concept of all my IoT’s and decided to use Blynk only indirectly. This means that all my microprocessors are running without Blynk but communicate with MQTT instead in order to better integrate them into my home automation system. I run Blynk only on one dedicated ESP01. With this instance I control many IoT’s also the Wemos D1 of my Stonelamp. It basically uses the Blynk GUI to publish MQTT messages only. The newest Stonelamp code (V2) can be downloaded from github:
Stonelanp V2

GUI

The Blynk - MQTT bridge code is here:

/*
  Blynk to MQTT bridge
  Version 1.1     24.April 2018 added Spot and Shells
  Version 1.0     9. Feb   2018

  Funtion: Use Blynk GUI to control MQTT attached devices
  
Pin assignments:
 * ESP01

Blynk Virtual Pins: 127 pins available
1. kitchen top (Arilux LC01 running Tasmota)
V0: ON OFF Button 
V1: Dimmer slider
V2: Speed slider
V3: Color select
V4: Fade switch
V5: Scheme slider

2. Kitchen board (ESP01 running Tasmota)
V6: ON OFF Button 

3. Stonelamp (Wemos D1 mini running my own stuff)
V7: ON OFF Button 
V8: Blink On/Off
V9: Color select
V10: Activity slider
V11: Brightness slider
V12: Speed slider

4. For own purposes (ESP01 running Blynk)
V13: ON OFF LED
V14: UP LED

5. Spot (Sonoff Basic running Tasmota)
V15: ON OFF Button 

6. Mussels (Wemos D1 mini running my own stuff)
V16: ON OFF Button 
V17: Blink On/Off
V18: Color select
V19: Activity slider
V20: Brightness slider
V21: Speed slider

Bill of material:
  -ESP01 ESP8266 and appropriate 3.3V Power
  -1000uF capacitor (I found that a capacitor around 5V and Ground increases stability of the Wemos D1)
  -5V 750mA Power Supply
*/

#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>

// Set ESP8266 Serial object
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
SimpleTimer timer;

// Blynk
//#define BLYNK_DEBUG
//#define BLYNK_PRINT Serial
int ONOFF_LED = V13;
int UP_LED = V14;
long int last_UP_change = millis();
bool runn = 1;
bool lamp_on;
int brightness;
int current_activity;
int standard_speed;
int counter = 0;

// Wireless settings
const char AUTH[] = "656476457475656858548678686";
const char SSID[] = "******"; 
const char PASSWORD[] = "*******";
IPAddress mqtt_server(192,168,178,59);
const char* mqtt_username = "mqtt";
const char* mqtt_password = "****";
char* InTopic = "tele/arilux74/STATE"; //subscribe to topic to be notified about
char* InTopic1 = "tele/ESP71/STATE";
char* Stonelamp = "Stonelamp"; 
char* Shells = "Shells"; 

WiFiClient espClient;
PubSubClient client(espClient);


void setup() {
 Serial.begin(115200);
 Serial.println(F("setup start"));
 
//Blynk
 WiFi.mode(WIFI_STA);
 Blynk.begin(AUTH, SSID, PASSWORD,"192.168.178.59",8442);
   while (Blynk.connect() == false) {  // Wait until connected
     if ((millis() - last_UP_change) > 30000) { // 30s before reset
       Serial.println(F("resetting"));
       ESP.restart();
    }
  }
 wait(10);
 client.setServer(mqtt_server, 8883);
 client.setCallback(callback);

//OTA
  // Port defaults to 8266
  // ArduinoOTA.setPort(8266);
  // Hostname defaults to esp8266-[ChipID]
  ArduinoOTA.setHostname("ESP43-MQTT_Bridge");
  // No authentication by default
  ArduinoOTA.setPassword((const char *)"***");
  ArduinoOTA.onStart([]() {
    Serial.println("Start");
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("\nEnd");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  });
  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
    else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
    else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
    else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
    else if (error == OTA_END_ERROR) Serial.println("End Failed");
  });
  ArduinoOTA.begin();

  Serial.println(F("Entering loop"));
  wait(200);
  timer.setInterval(1001L, blinkonoff);
  Blynk.virtualWrite(ONOFF_LED,0); 
  Blynk.virtualWrite(UP_LED,0); 
}


void loop() {
  Blynk.run();
  if(!Blynk.connected()) {
    Serial.println(F("Resetting in loop"));
    ESP.restart();
  }
  timer.run();
  if (!client.connected()) {
    reconnect();
   }
  client.loop();
  ArduinoOTA.handle();
}


void wait (int ms) {
  long current_time = millis();
  long end_time = current_time + ms; 
  while (current_time < end_time) {
    current_time = millis();
  }
}


void blinkonoff(){
 // Turn runn LED on/off
   Blynk.virtualWrite(UP_LED,runn * 255);  // turn Up off
   runn = !runn;
   last_UP_change = millis();  
}

//Kitchen top
BLYNK_WRITE(V0) { //ON Off switch
  if (param.asInt()) {
    client.publish("cmnd/arilux74/power","1");
    lamp_on = 1;
  }
  else {
    client.publish("cmnd/arilux74/power","0");
    lamp_on = 0;
  }
  Blynk.virtualWrite(ONOFF_LED,lamp_on * 255);  
}

BLYNK_WRITE(V1) { // Dimmer 0-100
  brightness = param.asInt();
  char bright[4];
  dtostrf(brightness, 3, 0, bright);
  client.publish("cmnd/arilux74/Dimmer",bright);
}

BLYNK_WRITE(V2) { // Speed 0-100
  standard_speed = param.asInt();
  char new_speed[4];
  dtostrf(standard_speed, 3, 0, new_speed);
  client.publish("cmnd/arilux74/speed",new_speed);
}

BLYNK_WRITE(V3) { //Color select 0-255 each
  char hex[7] = {0};
  sprintf(hex,"%02X%02X%02X",param[0].asInt(),param[1].asInt(),param[2].asInt()); //convert to an hexadecimal string. Lookup sprintf for what %02X means.
  client.publish("cmnd/arilux74/color",hex);
}

BLYNK_WRITE(V4) { // Fade switch
  if (param.asInt()) client.publish("cmnd/arilux74/fade","1");
  else client.publish("cmnd/arilux74/fade","0");
}

BLYNK_WRITE(V5) { //Scheme slider 0-4
  current_activity = param.asInt();
  char activity[2];
  dtostrf(current_activity, 1, 0, activity);
  client.publish("cmnd/arilux74/scheme",activity);
}

//Kitchen Board
BLYNK_WRITE(V6) { //ON Off Board
  if (param.asInt()) {
    client.publish("cmnd/ESP71/power","1");
   }
  else {
    client.publish("cmnd/ESP71/power","0");
  }
}

//Stonelamp
BLYNK_WRITE(V7) { //ON Off Stonelamp
  if (param.asInt()) publish(Stonelamp,"Power",1);
  else publish(Stonelamp,"Power",0);
}


BLYNK_WRITE(V8) { //Blink ON Off Stonelamp
  if (param.asInt()) publish(Stonelamp,"Blink",1);
  else publish(Stonelamp,"Blink",0);
}


BLYNK_WRITE(V9) { //Color select 0-255 each Stonelamp
  long current_color = param[2].asInt() + 256*param[1].asInt() + 256*256*param[0].asInt();
  publish(Stonelamp,"Color",current_color);
}


BLYNK_WRITE(V10) { //Scheme slider 0-12 Stonelamp
  current_activity = param.asInt();
  publish(Stonelamp,"Scheme",current_activity);
}


BLYNK_WRITE(V11) { // Brightness Stonelamp 0-255
  brightness = param.asInt();
  publish(Stonelamp,"Dimmer",brightness);
}


BLYNK_WRITE(V12) { // Speed 100-1 Stonelamp
  standard_speed = param.asInt();
  publish(Stonelamp,"Speed",standard_speed);
}

//Spot
BLYNK_WRITE(V15) { 
  }

//Shells
BLYNK_WRITE(V16) { //ON Off Stonelamp
  if (param.asInt()) publish(Shells,"Power",1);
  else publish(Shells,"Power",0);
}


BLYNK_WRITE(V17) { //Blink ON Off Stonelamp
  if (param.asInt()) publish(Shells,"Blink",1);
  else publish(Shells,"Blink",0);
}


BLYNK_WRITE(V18) { //Color select 0-255 each Stonelamp
  long current_color = param[2].asInt() + 256*param[1].asInt() + 256*256*param[0].asInt();
  publish(Shells,"Color",current_color);
}


BLYNK_WRITE(V19) { //Scheme slider 0-12 Stonelamp
  current_activity = param.asInt();
  publish(Shells,"Scheme",current_activity);
}


BLYNK_WRITE(V20) { // Brightness Stonelamp 0-255
  brightness = param.asInt();
  publish(Shells,"Dimmer",brightness);
}


BLYNK_WRITE(V21) { // Speed 100-1 Stonelamp
  standard_speed = param.asInt();
  publish(Shells,"Speed",standard_speed);
}


void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("BlinkMQTTBridge",mqtt_username,mqtt_password)) {
      Serial.println("connected");
      counter = 0;
      // ... and resubscribe
      client.subscribe(InTopic);
      client.subscribe(InTopic1);
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 0.3 second");
      ++counter;
      if (counter > 180) ESP.reset();
      // Wait .3 seconds before retrying
      delay(300);
      ArduinoOTA.handle();
    }
  }
}


void callback(char* topic, byte* payload, unsigned int length) {
  StaticJsonBuffer<450> jsonBuffer;
  //const char* json = "{"Time":"2018-02-09T14:55:30","Uptime":1,"Vcc":3.180,"POWER":"ON","Dimmer":73,"Color":"BA3E00","Scheme":0,"Fade":"OFF","Speed":1,"LedTable":"OFF","Wifi":{"AP":1,"SSId":"mmwireless","RSSI":66,"APMac":"0E:41:58:01:03:F0"}}";
  JsonObject& root = jsonBuffer.parseObject((char*)payload);
  const char* Time = root["Time"]; // "2018-02-09T14:55:30"
  int Uptime = root["Uptime"]; // 1
  float Vcc = root["Vcc"]; // 3.18
  const char* POWER = root["POWER"]; // "ON"
  int Dimmer = root["Dimmer"]; // 73
  const char* Color = root["Color"]; // "BA3E00"
  int Scheme = root["Scheme"]; // 0
  const char* Fade = root["Fade"]; // "OFF"
  int Speed = root["Speed"]; // 1
  //here you can use this information (optional)
}


void publish(char* target, char* command, long argument){ 
  //{"CMD":"Speed","ARG":29}
  char output[90];
  snprintf_P(output, sizeof(output), PSTR("{\"CMD\":\"%s\",\"ARG\":%d}"),command,argument);
  client.publish(target,output);
}
1 Like

Yay! another one comes over to the dark side!
Have you thought about using Node-Red as your Blynk to MQTT bride, as Node-Red gives really easy integration with other things such as Amazon Alexa.

Pete.

Yes, I do use node-red combined with “Node-RED Alexa Home Skill Bridge”, a free service which allows ON/OFF, asking for a temperature, or +/- dimming levels, asking if a door is locked, etc.

Over time I found I need several interfaces to control my IoT’s. My wife likes Blynk as a GUI, but also physical switches (usually I use 433MHz wall switches). I like Alexa better but I also use Domoticz to increase automation.

1 Like

hi i need app for this code can you provide it

Hi, I’m new here and I want to learn more about Blynk.
I want to put the led on the ceilling.
But I want to put one Button to On/Off and another Button to change the effects, in your project have only one Button to change effects?

I am from Brazil, Sorry for my English.