编程语言
首页 > 编程语言> > 【NodeMCU】01 使用WIFI或IR点亮3色LED灯同时使用U8G2驱动0.96寸OLED

【NodeMCU】01 使用WIFI或IR点亮3色LED灯同时使用U8G2驱动0.96寸OLED

作者:互联网

NODEMCU ESP-12 还是 ESP-12E 我也忘了
项目里用了WIFI、红外 IRrecv、还有 0.96 OLED (u8g2 驱动)

#include <Arduino.h>

// 引入红外驱动
#include <IRrecv.h>

// 引入WIFI驱动
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

// 引入OLED驱动
#include <U8g2lib.h>
#include <Wire.h>

// 定义WIFI路由接入
ESP8266WiFiMulti WiFiMulti;

// 定义WEB服务接口
ESP8266WebServer server(80);

// 定义红外接收针脚
const uint16_t kRecvPin = D4;

// 启动红外外接收
IRrecv irrecv(kRecvPin);

// 定义红外接收数据对象
decode_results results;

// 定义RGB灯针脚
int R = D1;
int G = D2;
int B = D3;

// 定义RGB灯亮度值 (共阳型灯 数越大 灯越暗)
int r = 1023;
int g = 1023;
int b = 1023;

// 为了支持红外遥控器连续按住 所以要保存上次有效命令
unsigned long last;

// 定义OLED屏针脚
const uint8_t SCL_ = D5;
const uint8_t SDA_ = D6;

// 定义OLED驱动
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL_, /* data=*/ SDA_, /* reset=*/ U8X8_PIN_NONE);   // All Boards without Reset of the Display


// 定义IP信息
String wifi_name;
String wifi_ip;

// WEB服务处理请求
void handleNotFound() {
  String message = "WiFi Server Three colors LED R:";
  message += 1023-r;
  message += " G:";
  message += 1023-g;
  message += " B:";
  message += 1023-b;
  message += "\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\n Method: ";
  message += (server.method() == HTTP_GET) ? "GET" : "POST";
  message += "\n Arguments: ";
  message += server.args();
  message += "\n ";

  // 列出URL路径参数
  for (uint8_t i = 0; i < server.args(); i++) {
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";

    // 红光
    if(server.argName(i) == "r"){
      r = 1023 - server.arg(i).toInt();
      
      if (r > 1023){
        r = 1023;
      }

      if (r < 0){
        r = 0;
      }
    }

    // 绿光
    if(server.argName(i) == "g"){
      g = 1023 - server.arg(i).toInt();
      
      if (g > 1023){
        g = 1023;
      }

      if (g < 0){
        g = 0;
      }
    }

    // 蓝光
    if(server.argName(i) == "b"){
      b = 1023 - server.arg(i).toInt();
      
      if (b > 1023){
        b = 1023;
      }

      if (b < 0){
        b = 0;
      }
    }
  }
  
  server.send(200, "text/plain", message);
}

void setup() {
  // 启动串口通讯
  Serial.begin(9600);

    // 启动OLED
  u8g2.begin();
  u8g2.setFont(u8g2_font_6x10_tr);
  
  // 设置WIFI模式 因为使用了Multi库,因此下边链接相关的语句都不用了
  // WiFi.mode(WIFI_STA);
  // WiFi.begin(ssid, password);
  
  //  while (WiFi.status() != WL_CONNECTED) {
  //    delay(500);
  //    Serial.print(".");
  //  }
  // 

	// 下边改成自己的SSID 和 PWD 有多少可以写多少~
  WiFiMulti.addAP("SSID1","PWD1");
  WiFiMulti.addAP("SSID2","PWD2");
  WiFiMulti.addAP("SSID3","PWD3");

  String loading = "";
  int i = 0;
  
  // 循环链接WIFI直至链接成功
  while(WiFiMulti.run() != WL_CONNECTED){
    Serial.print(".");

		// 在OLED屏幕显示链接状态
    i++;
    loading = loading + ".";
    
    // 设置屏幕显示
    u8g2.firstPage();
    do {
      u8g2.setCursor(3,13);
      u8g2.print(loading);
    }while(u8g2.nextPage());
  
    delay(500);
  }

  // 显示接入的WIFI网络
  wifi_name = WiFi.SSID();
  Serial.println(wifi_name);
  
  // 显示获取到的IP地址
  wifi_ip = WiFi.localIP().toString();
  Serial.println(wifi_ip);

  // 设置并启动WEB服务
  server.onNotFound(handleNotFound);
  server.begin();
  Serial.println("server.OK");

  // 设置LED针脚模式
  pinMode(R,OUTPUT);
  pinMode(B,OUTPUT);
  pinMode(G,OUTPUT);

  // 初始化LED针脚
  analogWrite(R,r);
  analogWrite(G,g);
  analogWrite(B,b);

  // 启动红外接收
  irrecv.enableIRIn();  // Start the receiver
  Serial.println("irrecv.OK");
}

void loop() {

  // 设置屏幕显示
  u8g2.firstPage();
  do {
    u8g2.setCursor(3,13);
    u8g2.print("R:");
    u8g2.setCursor(u8g2.getStrWidth("R:")+5,13);
    u8g2.print(1023-r);
    
    u8g2.setCursor(42,13);
    u8g2.print("G:");
    u8g2.setCursor(u8g2.getStrWidth("G:")+44,13);
    u8g2.print(1023-g);
    
    u8g2.setCursor(85,13);
    u8g2.print("B:");
    u8g2.setCursor(u8g2.getStrWidth("B:")+87,13);
    u8g2.print(1023-b);

    u8g2.setCursor(3,26);
    u8g2.print("IP:");
    u8g2.setCursor(u8g2.getStrWidth("IP:")+5,26);
    u8g2.print(wifi_ip);

    
    u8g2.setCursor(3,38);
    u8g2.print("NAME:");
    u8g2.setCursor(u8g2.getStrWidth("NAME:")+5,38);
    u8g2.print(wifi_name);
    
  }while(u8g2.nextPage());
  
  // 如果接收到红外指令 则进行相关处理
  if (irrecv.decode(&results)) {
    unsigned long code = results.value;
    
    Serial.println(code, HEX);

  // 红光
  //1
    if (code == 0xFFA25D){
      if (r != 1023){
        r = 1023;
      }else{
        r = 0;
      }
    }
  //2
    if (code == 0xFF629D || (last == 0xFF629D && code == 0xFFFFFFFF)){
        r = r - 10;

        if (r < 0){
          r = 0;
        }
    }
  //3
    if (code == 0xFFE21D || (last == 0xFFE21D && code == 0xFFFFFFFF)){
        r = r + 10;
        
        if (r > 1023){
          r = 1023;
        }
    }

  // 绿光
  //4
    if (code == 0xFF22DD){
        if (b != 1023){
        b = 1023;
      }else{
        b = 0;
      }
    }
  //5
    if (code == 0xFF02FD || (last == 0xFF02FD && code == 0xFFFFFFFF)){
        b = b - 10;

        if (b < 0){
          b = 0;
        }
    }
  //6
    if (code == 0xFFC23D || (last == 0xFFC23D && code == 0xFFFFFFFF)){
        b = b + 10;
        
        if (b > 1023){
          b = 1023;
        }
    }

  // 蓝光
  // 7
    if (code == 0xFFE01F){
        if (g != 1023){
        g = 1023;
      }else{
        g = 0;
      }
    }
  // 8
    if (code == 0xFFA857 || (last == 0xFFA857 && code == 0xFFFFFFFF)){
        g = g - 10;

        if (g < 0){
          g = 0;
        }
    }
  // 9
    if (code == 0xFF906F || (last == 0xFF906F && code == 0xFFFFFFFF)){
        g = g + 10;
        
        if (g > 1023){
          g = 1023;
        }
    }

    // 0 
    if (code == 0xFF9867){
      if (r != 1023 || g != 1023 || b != 1023){
        r = 1023;
        g = 1023;
        b = 1023;
      }else{
        r = 0;
        g = 0;
        b = 0;
      }
    }

    if (code != 0xFFFFFFFF){
      last = code;
    }
    
   // 清除当前指令 准备接收下一个指令
    irrecv.resume();  // Receive the next value

    delay(50);
  }
  
  analogWrite(R,r);
  analogWrite(G,g);
  analogWrite(B,b);

  server.handleClient();
}

该注释的都注释了,有不清楚的可以留言交流~~

标签:U8G2,u8g2,01,LED,1023,server,code,print,message
来源: https://blog.csdn.net/louyunlong/article/details/122177220