IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 嵌入式 -> IoT Controlling LED with Blynk -> 正文阅读

[嵌入式]IoT Controlling LED with Blynk

使用的是ESP8266 and ESP32 OLED driver for SSD1306 displays

通过Arduino编写和部署代码,安装依赖
在这里插入图片描述

通过Blynk APP控制LED灯的颜色、亮度,以及展示数据

/***********************************************************************/
/*IoT workshop                                                         */
/*Part one: Connect to Blynk                                           */
/*Part two: change RGB LED value via Blynk app                         */
/*Part three: setup physical and virtual button to turn RGB ON/OFF     */
/*Part four: light intensity reading and control RGB Brightness        */
/***********************************************************************/


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

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YOUR-AUTH-CODE";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "YOUR-WIFI-SSID";
char wifi_pass[] = "YOUR-WIFI-PASSWORD";

const int BUTTON_PIN = 4;    // Define pin the button is connected to
const int ON_BOARD_LED = 2;  // Define pin the on-board LED is connected to
const int RGB_R_PIN = 15;    // RGB Red LED
const int RGB_G_PIN = 12;    // RGB Green LED
const int RGB_B_PIN = 13;    // RGB Blue LED
const int LDR_PIN = A0;      // Define the analog pin the LDR is connected to

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
BlynkTimer timer;

//initialize varible
int ledState = HIGH;
int btnState = HIGH;
float rgbBrightness = 1.0;
int value_r = 255, value_g = 255, value_b = 255;


// process received value from Blynk  V2
BLYNK_WRITE(V2)
{
    //send msg to serial monitor
    Serial.print("Lightness is changed");
    rgbBrightness = param.asInt()/1024.0;
}

// process received value from Blynk zeRGBa V3
BLYNK_WRITE(V3)
{
    //send msg to serial monitor
    Serial.print("Color V3 is changed");
      value_r= param[0].asInt();
      value_g= param[1].asInt();
      value_b= param[2].asInt();
}

//process received value from Blynk virtual button V4
BLYNK_WRITE(V4) {
  ledState = param.asInt();
  digitalWrite(BUTTON_PIN, ledState);

  Serial.print("Virtual button V4 is pressed. RGB turn on :");
  Serial.println(ledState);
}

//Display RGB with ON/OFF checking
void showButtonLed()
{

  //check physical button is pressed to change ON/OFF status
  // btnState is used to avoid sequential toggles
  if (digitalRead(BUTTON_PIN) == LOW) {
    
    if (btnState != LOW) { //Status going to be changed

      // Toggle LED state
      btnState = LOW;
      digitalWrite(BUTTON_PIN, ledState);

      // Update Virtual Button V4 Widget
      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);

  //set dim if turn RGB "OFF"
  if(ledState == LOW) rgbBrightness = 0; 
  if(ledState == HIGH) {
    if(rgbBrightness<=0.0001){
      rgbBrightness = 1; 
    }
  }

  //display RGB
  analogWrite(RGB_R_PIN, (int) value_r*(rgbBrightness));
  analogWrite(RGB_G_PIN, (int) value_g*(rgbBrightness));
  analogWrite(RGB_B_PIN, (int) value_b*(rgbBrightness));
  
}

//Light sensing and set RGB brightness by its intensity level, which is inversely proportional to RGB brightness
void lightSensor()
{
     int lightIntensity;

     lightIntensity = analogRead(LDR_PIN);  // Read the light intensity
     // rgbBrightness = 1.0 - (float) lightIntensity/1000;

     //send msg to serial monitor
     Serial.print("Light Intensity :");
     Serial.println(lightIntensity);
     Serial.print("RGB Brightness :");
     Serial.println(rgbBrightness);
          
     Blynk.virtualWrite(V1, lightIntensity);
     // Blynk.virtualWrite(V2, rgbBrightness); 
}

/************************************/
/****** Start point of program ******/
/************************************/
void setup()
{
  Serial.begin(115200);
  //send msg to serial monitor
  Serial.println("Blynk connection setup start");

  //Build in LED as indicator - output
  pinMode(ON_BOARD_LED, OUTPUT); // Initialize the LED_BUILTIN pin as an output
  digitalWrite(ON_BOARD_LED, LOW); // Turn the LED_BUILTIN on when setup starts
  
  //connect to Blynk app
  Blynk.begin(auth, ssid, wifi_pass);
  
  //Button - input
  pinMode(BUTTON_PIN, INPUT_PULLUP);  // Make BUTTON_PIN HIGH by default
  
  //Photoresistor - input
  pinMode(LDR_PIN, INPUT);

  //RGB - output
  pinMode(RGB_R_PIN, OUTPUT);  // RGB LED - RED
  pinMode(RGB_G_PIN, OUTPUT);  // RGB LED - GREEN
  pinMode(RGB_B_PIN, OUTPUT);  // RGB LED - BLUE

  Serial.println("Setup done. Blynk app connected.");
  digitalWrite(BUTTON_PIN, ledState); // Turn RGB LED on

  //setup timer to do sth
  timer.setInterval(100L, showButtonLed);
  timer.setInterval(1000L, lightSensor);
  
  digitalWrite(ON_BOARD_LED, HIGH); // Turn the LED_BUILTIN off when setup finish
  
}

//Run from time to time
void loop()
{
  
  Blynk.run();
  timer.run();
  
}

app界面

在这里插入图片描述

  嵌入式 最新文章
基于高精度单片机开发红外测温仪方案
89C51单片机与DAC0832
基于51单片机宠物自动投料喂食器控制系统仿
《痞子衡嵌入式半月刊》 第 68 期
多思计组实验实验七 简单模型机实验
CSC7720
启明智显分享| ESP32学习笔记参考--PWM(脉冲
STM32初探
STM32 总结
【STM32】CubeMX例程四---定时器中断(附工
上一篇文章      下一篇文章      查看所有文章
加:2021-10-13 11:36:15  更:2021-10-13 11:37:45 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/26 4:40:22-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码