使用的是ESP8266 and ESP32 OLED driver for SSD1306 displays
通过Arduino编写和部署代码,安装依赖
通过Blynk APP控制LED灯的颜色、亮度,以及展示数据
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "YOUR-AUTH-CODE";
char ssid[] = "YOUR-WIFI-SSID";
char wifi_pass[] = "YOUR-WIFI-PASSWORD";
const int BUTTON_PIN = 4;
const int ON_BOARD_LED = 2;
const int RGB_R_PIN = 15;
const int RGB_G_PIN = 12;
const int RGB_B_PIN = 13;
const int LDR_PIN = A0;
#define BLYNK_PRINT Serial
BlynkTimer timer;
int ledState = HIGH;
int btnState = HIGH;
float rgbBrightness = 1.0;
int value_r = 255, value_g = 255, value_b = 255;
BLYNK_WRITE(V2)
{
Serial.print("Lightness is changed");
rgbBrightness = param.asInt()/1024.0;
}
BLYNK_WRITE(V3)
{
Serial.print("Color V3 is changed");
value_r= param[0].asInt();
value_g= param[1].asInt();
value_b= param[2].asInt();
}
BLYNK_WRITE(V4) {
ledState = param.asInt();
digitalWrite(BUTTON_PIN, ledState);
Serial.print("Virtual button V4 is pressed. RGB turn on :");
Serial.println(ledState);
}
void showButtonLed()
{
if (digitalRead(BUTTON_PIN) == LOW) {
if (btnState != LOW) {
btnState = LOW;
digitalWrite(BUTTON_PIN, ledState);
Blynk.virtualWrite(V4, ledState);
Serial.print("Physical button V4 is pressed. RGB turn on :");
Serial.println(ledState);
}
btnState = LOW;
} else {
btnState = HIGH;
}
Serial.print("Physical button ledState :");
Serial.println(ledState);
if(ledState == LOW) rgbBrightness = 0;
if(ledState == HIGH) {
if(rgbBrightness<=0.0001){
rgbBrightness = 1;
}
}
analogWrite(RGB_R_PIN, (int) value_r*(rgbBrightness));
analogWrite(RGB_G_PIN, (int) value_g*(rgbBrightness));
analogWrite(RGB_B_PIN, (int) value_b*(rgbBrightness));
}
void lightSensor()
{
int lightIntensity;
lightIntensity = analogRead(LDR_PIN);
Serial.print("Light Intensity :");
Serial.println(lightIntensity);
Serial.print("RGB Brightness :");
Serial.println(rgbBrightness);
Blynk.virtualWrite(V1, lightIntensity);
}
void setup()
{
Serial.begin(115200);
Serial.println("Blynk connection setup start");
pinMode(ON_BOARD_LED, OUTPUT);
digitalWrite(ON_BOARD_LED, LOW);
Blynk.begin(auth, ssid, wifi_pass);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LDR_PIN, INPUT);
pinMode(RGB_R_PIN, OUTPUT);
pinMode(RGB_G_PIN, OUTPUT);
pinMode(RGB_B_PIN, OUTPUT);
Serial.println("Setup done. Blynk app connected.");
digitalWrite(BUTTON_PIN, ledState);
timer.setInterval(100L, showButtonLed);
timer.setInterval(1000L, lightSensor);
digitalWrite(ON_BOARD_LED, HIGH);
}
void loop()
{
Blynk.run();
timer.run();
}
app界面
|