ESP8266 智能配网(SmartConfig + Airkiss) + 天猫精灵 + 点灯科技



#define BLINKER_WIFI
#define BLINKER_ALIGENIE_LIGHT //天猫精灵
#include 
#include 
#include


char auth[] = "";   //点灯APP密匙


int pinRelay =0; 

// 新建组件对象
BlinkerButton Button1("btn-abc");
BlinkerNumber Number1("num-abc"); 

void setup() {
 
   Serial.begin(115200);
   delay(100);
   if (!autoConfig()){
     smartConfig();
   }


    // 初始化有LED的IO
    pinMode(LED_BUILTIN, OUTPUT);
    pinMode(0, OUTPUT);
    digitalWrite(LED_BUILTIN, HIGH);
    digitalWrite(pinRelay, HIGH);
    
    // 初始化blinker
    Serial.println("初始化blinker ");
    Serial.println(WiFi.SSID().c_str() );
    Serial.println(WiFi.psk().c_str() );
    
    Button1.attach(button1_callback);
    BlinkerAliGenie.attachPowerState(aligeniePowerState);
    Blinker.attachHeartbeat(heartbeat);

 

}

void loop() {
  // put your main code here, to run repeatedly:

  Blinker.run();
  //Blinker.run()语句负责处理Blinker收到的数据,每次运行都会将设备收到的数据进行一次解析。
  //在使用WiFi接入时,该语句也负责保持网络连接
}








// 按下按键即会执行该函数
void button1_callback(const String & state) {
    BLINKER_LOG("get button state: ", state);
    if (state=="on") {
        digitalWrite(LED_BUILTIN, LOW);
        digitalWrite(pinRelay,LOW);
        // 反馈开关状态
        Button1.print("on");
    } else if(state=="off"){
        digitalWrite(LED_BUILTIN, HIGH);
        // 反馈开关状态
        digitalWrite(pinRelay, HIGH);
        Button1.print("off");
    }
}

//心跳包
void heartbeat(){
    Button1.icon("fas fa-lightbulb");
    Button1.color("#fddb00");
    Button1.text("关灯","打开啦");
    Number1.print(WiFi.RSSI());  //信号强度
}



//天猫精灵
void aligeniePowerState(const String & state){
    BLINKER_LOG("need set power state: ", state);
    if (state == BLINKER_CMD_ON) {
        digitalWrite(LED_BUILTIN, LOW);
        digitalWrite(pinRelay, LOW);
        BlinkerAliGenie.powerState("off");
        BlinkerAliGenie.print();
        
    }else if (state == BLINKER_CMD_OFF) {
        digitalWrite(LED_BUILTIN, HIGH);
        digitalWrite(pinRelay, HIGH);
        BlinkerAliGenie.powerState("on");
        BlinkerAliGenie.print();
    }
}
 










//*******************************************************   WIFI 配网 ***************************************************
//ap 配网
void smartConfig(){
  WiFi.mode(WIFI_STA);
  Serial.println("\r\n开始配网 Smartconfig...");
  WiFi.beginSmartConfig();
  while (1){
    Serial.print(".");
    delay(500);                   // 等待时间
    if (WiFi.smartConfigDone()){  // 配网成功
      Serial.println("SmartConfig Success");
      Serial.printf("SSID:%s\r\n", WiFi.SSID().c_str());
      Serial.printf("PSW:%s\r\n", WiFi.psk().c_str());
      Serial.print("LocalIP:");
      Serial.print(WiFi.localIP());
      Serial.print(" ,GateIP:");
      Serial.println(WiFi.gatewayIP());  
       
      break;
    }
  }
}


//自动连接
bool autoConfig(){
    WiFi.begin();
    //如果觉得时间太长可改
    for (int i = 0; i < 20; i++){
        int wstatus = WiFi.status();
         
        if (wstatus == WL_CONNECTED){
          Serial.println("WIFI SmartConfig Success");
          Serial.printf("SSID:%s", WiFi.SSID().c_str());
          Serial.printf(", PSW:%s\r\n", WiFi.psk().c_str());
          Serial.print("LocalIP:");
          Serial.print(WiFi.localIP());
          Serial.print(" ,GateIP:");
          Serial.println(WiFi.gatewayIP());
          
          return true;
        }else{
          Serial.println("WIFI AutoConfig Waiting......");
          delay(1000);
        }
    }
    Serial.println("WIFI 配网" );
    return false;
}

 

你可能感兴趣的:(笔记,c++,c语言)