Smart Switch with OLED Display

Hello all! I have been working on a project for a while. My goal is to make a super smart switch using Blynk. I have the switch part of it (that was the easy part), but the next step is to add a display. I am using a WEMOS D1 mini lite, a relay shield, and an OLED shield.

Here is the relay shield
Here is the OLED display

What I want is for the OLED screen to display “ON” when the relay is switched on and for it to display “OFF” when the display is switched off. I have gotten the OLED to display "ON or “OFF” simply by typing “ON” or “OFF” into display.println().

I have no idea how to do this and I would really appreciate some help.
Also, I would be controlling this from the Blynk app

Here is the code I’m using

Relay:

#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>


char auth[] = "################################";


char ssid[] = "#############";
char pass[] = "########";

void setup()
{Preformatted text
  Serial.begin(115200);

  Blynk.begin(auth, ssid, pass);
}

void loop()
{
  Blynk.run();
}

OLED:

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>


#define OLED_RESET 0  
Adafruit_SSD1306 display(OLED_RESET);

#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2


#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH  16


void setup()   {
  Serial.begin(9600);

 
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  
  display.display();
  delay(2000);


  display.clearDisplay();


  display.setTextSize(3);
  display.setTextColor(WHITE);
  display.setCursor(45, 10);
  display.println("On");
  display.display();
  delay(2000);
  display.clearDisplay();

}


void loop() {

}

I have only gotten as far as this in combining the control of the relay and the display together:

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
 

#define OLED_RESET 0  
Adafruit_SSD1306 display(OLED_RESET);

#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2


#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH  16
#define BLYNK_PRINT Serial



void setup()   {
  char auth[] = "###############";
char ssid[] = "#############";
char pass[] = "############";

  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  

 
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  
  display.display();
  delay(2000);


  display.clearDisplay();


  display.setTextSize(3);
  display.setTextColor(WHITE);
  display.setCursor(45, 10);
  display.println("ON");
  display.display();
  delay(2000);
  display.clearDisplay();
}

void loop()
{
  Blynk.run();
}

Unfortunately, what is displayed is only affected by what I tell it to display in the code, not the state of the relay.

What I need to know is how to write the code that will update the display based on the state of the relay.

@directhacker, please do not cross-post. Other thread removed.

Thanks for the help....

in Blynk app add a button and attach it to virtual pin V0.

in sketch add a handler for this virtual pin

#define BUTTON_WIDGET V0

BLYNK_WRITE(BUTTON_WIDGET) {
 boolean pushed = param.asInt();
 if (pushed) {
   display.setCursor(45, 10);
   if (buttonState == 0) {
     display.println("ON");
     buttonState = 1;
   } else {
     display.println("OFF");
     buttonState = 0;
   }
   display.display();    
 }
}

Blynk has a good forum

Ok so I have been really busy lately and I haven't had a chance to update my code until today. i keep running into this error:

BLYNK_RELAY:36: error: a function-definition is not allowed here before '{' token
{ boolean pushed = param.asInt();
^

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>


#define OLED_RESET 0
Adafruit_SSD1306 display(OLED_RESET);
#define BUTTON_WIDGET V0

#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2


#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH  16
#define BLYNK_PRINT Serial



void setup()   {
  char auth[] = "xxxxxx";
  char ssid[] = "xxxxxx";
  char pass[] = "xxxxxx";

  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);



  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.display();
  delay(2000);


  display.clearDisplay();

  BLYNK_WRITE(BUTTON_WIDGET) {
    boolean pushed = param.asInt();
    if (pushed) {
      display.setCursor(45, 10);
      if (buttonState == 0) {
        display.println("ON");
        buttonState = 1;
      } else {
        display.println("OFF");
        buttonState = 0;
      }
      display.display();
    }
  }

void loop()
{
  Blynk.run();
}

Hi,
Count your { and }, there might be one missing?

Possibly the closing } on the void setup() part.

Tom... :slight_smile:

Thanks for your input! I checked and I was missing a }. I added one but I still have the same error, except now I have this one in addition.

BLYNK_RELAY:52: error: expected '}' at end of input
}
^

This error is referring to the closing } of the void loop() but there is one there already ::slight_smile:

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>


#define OLED_RESET 0
Adafruit_SSD1306 display(OLED_RESET);

#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2



#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH  16
#define BLYNK_PRINT Serial



void setup()   {
  char auth[] = "3a5d5b84e1ac4ef48d4dbf196f03184c";
  char ssid[] = "TP-LINK_2.4GHz_8D5014";
  char pass[] = "Analog22";

  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);

  BLYNK_WRITE(V0) {
    { boolean pushed = param.asInt();
      if (pushed) {
        display.setCursor(45, 10);
        if (buttonState == 0) {
          display.println("ON");
          buttonState = 1;
        } else {
          display.println("OFF");
          buttonState = 0;
        }
        display.display();
      }
    }
  }
}
  void loop()
  {
    Blynk.run();
  }

it is a 'function' put it at top level, not into setup. you didn't see any Blynk examples?

That's what I started with, but it says that buttonState was not declared in the scope.