Esp8266 mqtt arduino wireless multi-stage control lights

Using ws2812 digital lamp beads + esp826601s + mqtt use arduino IDE development
can be implemented using a plurality of brightness control MQTT lamp beads

The demo: link

ws2812 digital lamp beads

enter description here
Input and output signals of the
input and output signals

DO is the output signal DI is an input signal
to a plurality of lamps in series RGB signal is then transmitted lights achieved.
enter description here

There are many that I use is to act as the led lights
enter description here

Joint use esp826601s

Only to a GPIO terminal ws2812 signal can
thus consider the esp8266 -01s GPIO2

Esp8266 mqtt students to use arduino development without foundation can look at these articles

http://niehen.cn/esp8266/esp8266-mqtt-%e4%bd%bf%e7%94%a8arduino%e5%bc%80%e5%8f%91%e6%95%99%e7%a8%8b/

http://niehen.cn/esp8266/mqtt%e5%8d%8f%e8%ae%ae%e4%bb%8b%e7%bb%8d%e4%b8%8e%e5%ba%94%e7%94%a8/

http://niehen.cn/esp8266/esp8266-01s%e4%bb%8b%e7%bb%8d/

Early use fast led library repeatedly attempt is unsuccessful,
check the information found due to the need for continuous scanning frequency ws2812 great, but esp01s frequency less than it did in the early success try sometimes, but use ws2812 only need to use a pin 12f seem wasteful

Find a lot of information discovery can be achieved using Adafruit_NeoPixel.h
below speaks

只需要ws2812 的信号引脚接到 esp01s的GPIO2
5v接到电源5v
GND接地
这里推荐使用 这种模块 方便简单
enter description here

接线省事很多
enter description here

Adafruit_NeoPixel库

用于控制基于单线的LED像素和条带

安装

IDE内安装:

  • 在arduino IDE中 项目—》加载库----》管理库
  • 然后使用搜索栏搜索Neopixel strip。
  • 选择版本进行安装

源码安装:

  • 找到源码 GitHub链接下载下来
  • 下载后,将文件夹重命名为“ Adafruit_NeoPixel”并安装在Arduino Libraries文件夹中
  • 重新启动Arduino IDE,打开示例 找到这个库的示例代码
支持的芯片

enter description here

使用

有两种创建灯带形式

  • GRB:绿红蓝三基色 值从0到255
  • GRBW:在RGB的基础上加上一种白光,增加亮度的作用。

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800); // 创建 GRB灯带

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRBW + NEO_KHZ800); // 创建 GRBW灯带

在使用时 显示的形式不一样

函数

  • begin() 灯带初始化
  • setPixelColor() 设置灯带的颜色(第一个参数是灯珠索引,第二个是颜色值(GRB或GRBW))
  • setBrightness() 设置灯带的亮度(第一个参数是灯珠索引,第二个是亮度值0-255)
  • show() 显示灯带

    这些函数控制对象 就是前面创建的灯带对象(可以看下面的示例代码)

在使用时可以根据自己需要显示的样子
编写控制灯珠颜色 亮度变化的代码
当然也可以使用这个库提供的一些示例代码 也是很有意思的

示例代码

内含注释

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

//使用的引脚
#define PIN            6
//数字灯珠数量
#define NUMPIXELS      16

// 当我们设置NeoPixel库时,我们告诉它要使用多少像素,以及使用哪个pin发送信号。

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int delayval = 500; // delay for half a second
void setup() {
  // 这是5V 16MHz的小饰品,如果不使用小饰品,可以去掉这三条线
#if defined (__AVR_ATtiny85__)
  if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
  pixels.begin(); // 这将初始化neopix库。
}
void loop() {

  // 对于一组NeoPixel,第一个NeoPixel是0,第二个是1,一直到像素数减1为止.
  for(int i=0;i<NUMPIXELS;i++){
    // pixels.Color 设置RGB的值 区间是 0,0,0 到 255,255,255
    pixels.setPixelColor(i, pixels.Color(0,150,0)); // 中等明亮的绿色。
    pixels.show(); // 这会将更新的像素颜色发送到硬件。
    delay(delayval); // 延迟一段时间(以毫秒为单位)。
  }
}

这个库的GitHub链接

代码

在这个项目中 只需要显示白色
多级调节 其实也就是调节灯珠的亮度
mqtt发过来一个数字值,然后控制亮度变化
思路很简单

使用的函数

将LED灯珠变为全部白色 设置亮度只需要改变传入的第一个参数即可

在亮度等级调整的时候,为了变换没有那么突兀,程序里面采用的是逐渐变化.而不是直接的全部设为目标亮度。


int bright = 0;
void pulseWhite(uint8_t result,uint8_t wait) {  // 第一个参数为要变为的亮度值 第二个参数为变化间隔的时间单位为毫秒
  if (bright < result){
  for(int j = bright; j < result ; j++){
      for(uint16_t i=0; i<pixels.numPixels(); i++) {
          pixels.setPixelColor(i, pixels.Color(j,j,j) );
        }
        delay(wait);
        pixels.show();
      }
      bright = result;
  }else if(bright > result){
  for(int j = bright; j >= result ; j--){
      for(uint16_t i=0; i<pixels.numPixels(); i++) {
          pixels.setPixelColor(i, pixels.Color(j,j,j) );
        }
        delay(wait);
        pixels.show();
      }
      bright = result;
  }  
}

项目参考代码链接

GitHub地址

更多学习教程

ESP开发学习基础知识

基础知识包括对esp模块的认识与了解 mqtt协议的了解,arduino IDE运用代码编写等等。

  1. arduino基础学习
  2. esp系列模块的介绍
  3. mqtt协议的介绍与使用
  4. 利用mqtt esp模块 基于arduino IDE开发方法
  5. esp模块的AT指令 刷固件
  6. esp模块睡眠模式使用
  7. esp8266-01s介绍与使用
  8. esp8266-12f介绍与使用
  9. NodeMcu介绍与使用
esp开发IOT应用

基于esp8266的模块以及其他模块根据实际的应用场景与需求制作的物联网应用

  1. 基于FRID arduino 继电器 电磁锁开发的FRID门禁系统
  2. esp32-cam获取视频流图像处理
  3. 基于步进电机 esp8266 mqtt开发的自动窗帘控制
  4. 基于DHT11 Esp8266 mqtt获取室内温湿度
  5. 基于CCS811 esp8266 mqtt 获取室内空气质量
  6. 基于红外模块 esp8266 mqtt开发的智能遥控控制
  7. 基于ws2812 esp8266 mqtt开发的智能多级照明灯
  8. 基于ws2812 esp8266 mqtt开发的智能多模式氛围灯
  9. 基于mp3player esp8266 mqtt开发的智能语音播报系统
  10. The wisdom of the comprehensive application of IOT classroom project development
Published 46 original articles · won praise 59 · views 70000 +

Guess you like

Origin blog.csdn.net/Nirvana_6174/article/details/104683717