標(biāo)題: ESP8266開(kāi)發(fā)板NODEMCU點(diǎn)燈科技WIFI插座小度智能屏遠(yuǎn)控測(cè)試成功 [打印本頁(yè)]

作者: slaoliu    時(shí)間: 2021-11-23 08:06
標(biāo)題: ESP8266開(kāi)發(fā)板NODEMCU點(diǎn)燈科技WIFI插座小度智能屏遠(yuǎn)控測(cè)試成功
制作出來(lái)的實(shí)物圖如下:

直接上代碼

  1. // 低電平繼電器動(dòng)作,高電平繼電器釋放 小度語(yǔ)音 操作 成功,20211121
  2. /////////////////////////////////////////////////////////////////////////////////


  3. #define BLINKER_WIFI   
  4. #define BLINKER_MIOT_OUTLET   
  5. #define BLINKER_DUEROS_OUTLET      
  6. #define BLINKER_ALIGENIE_OUTLET  
  7. #define BLINKER_PRINT Serial  

  8. #include <WiFiManager.h>
  9. #include <Blinker.h>         

  10. char auth[] = "--------";     

  11. int relay = 2;
  12. int key   = 0;  
  13. int counter = 0;


  14. BlinkerButton Button1("btn-abc");   
  15. WiFiManager wifiManager;
  16. char devname[] = "ESP8266_chazuo";  
  17. void WebConfig() {
  18.   Serial.println("Entered config mode...");

  19.   wifiManager.setConfigPortalTimeout(60);   
  20.   wifiManager.autoConnect(devname);
  21.   WiFi.hostname(devname);                     
  22.   Serial.println("");
  23.     Serial.print("ESP8266 Connected to ");
  24.     Serial.println(WiFi.SSID());            
  25.     Serial.print("IP address:\t");
  26.     Serial.println(WiFi.localIP());         
  27. }

  28. //=================================================================
  29. void button1_callback(const String & state)      
  30. {
  31.   BLINKER_LOG("app操作了!: ", state);            

  32.   if (digitalRead(relay)==LOW) {              
  33.     BLINKER_LOG("滅燈!");                    
  34.     digitalWrite(relay, HIGH);
  35.     Button1.color("gray");                  
  36.     Button1.text("插座已關(guān)閉");
  37.     Button1.print("off");                  
  38.   }
  39.   else if (digitalRead(relay)==HIGH) {      
  40.     BLINKER_LOG("亮燈!");                     
  41.     digitalWrite(relay,LOW);
  42.     Button1.color("red");                    
  43.     Button1.text("插座已打開(kāi)");
  44.     Button1.print("on");                     
  45.   }
  46. }

  47. //=================================================================
  48. void miotPowerState(const String & state)
  49. {
  50.     BLINKER_LOG("小愛(ài)語(yǔ)音操作!");      

  51.     if (state == BLINKER_CMD_ON){
  52.         digitalWrite(relay,LOW);
  53.         BlinkerMIOT.powerState("on");
  54.         BLINKER_LOG("亮燈!");
  55.         BlinkerMIOT.print();
  56.     }
  57.     else if (state == BLINKER_CMD_OFF){
  58.         digitalWrite(relay,HIGH);
  59.         BlinkerMIOT.powerState("off");
  60.         BLINKER_LOG("滅燈!");
  61.         BlinkerMIOT.print();
  62.     }
  63. }

  64. //=================================================================
  65. void miotQuery(int32_t queryCode)      
  66. {
  67.     BLINKER_LOG("MIOT Query codes: ", queryCode);

  68.     switch (queryCode)
  69.     {
  70.         case BLINKER_CMD_QUERY_ALL_NUMBER :
  71.             BLINKER_LOG("MIOT Query All");
  72.             BlinkerMIOT.powerState(relay ? "on" : "off");
  73.             BlinkerMIOT.print();
  74.             break;
  75.         case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
  76.             BLINKER_LOG("MIOT Query Power State");
  77.             BlinkerMIOT.powerState(relay ? "on" : "off");
  78.             BlinkerMIOT.print();
  79.             break;

  80.         default :
  81.             BlinkerMIOT.powerState(relay ? "on" : "off");  
  82.             BlinkerMIOT.print();
  83.             break;
  84.     }
  85. }

  86. //=================================================================
  87. void duerPowerState(const String & state)
  88. {
  89.     BLINKER_LOG("小度語(yǔ)音操作!");              //串口打印

  90.     if (state == BLINKER_CMD_ON){
  91.         digitalWrite(relay,LOW);
  92.         BlinkerDuerOS.powerState("on");     //????
  93.         BLINKER_LOG("亮燈!");
  94.         BlinkerDuerOS.print();
  95.     }
  96.     else if (state == BLINKER_CMD_OFF){
  97.         digitalWrite(relay,HIGH);
  98.         BlinkerDuerOS.powerState("off");
  99.         BLINKER_LOG("滅燈!");
  100.         BlinkerDuerOS.print();
  101.     }
  102. }

  103. //=================================================================
  104. void duerQuery(int32_t queryCode)   
  105. {
  106.     BLINKER_LOG("DuerOS Query codes: ", queryCode);

  107.     switch (queryCode)
  108.     {
  109.         case BLINKER_CMD_QUERY_ALL_NUMBER :
  110.             BLINKER_LOG("DuerOS Query All");
  111.             BlinkerDuerOS.powerState(relay ? "on" : "off");
  112.             BlinkerDuerOS.print();
  113.             break;
  114.         case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
  115.             BLINKER_LOG("DuerOS Query Power State");
  116.             BlinkerDuerOS.powerState(relay ? "on" : "off");
  117.             BlinkerDuerOS.print();
  118.             break;

  119.         default :
  120.             BlinkerDuerOS.powerState(relay ? "on" : "off");  
  121.             BlinkerDuerOS.print();
  122.             break;
  123.     }
  124. }

  125. //=================================================================
  126. void aligeniePowerState(const String & state)
  127. {
  128.     BLINKER_LOG("天貓精靈語(yǔ)音操作!");      

  129.     if (state == BLINKER_CMD_ON){
  130.         digitalWrite(relay,LOW);
  131.         BlinkerAliGenie.powerState("on");
  132.         BLINKER_LOG("亮燈!");
  133.         BlinkerAliGenie.print();
  134.     }
  135.     else if (state == BLINKER_CMD_OFF){
  136.         digitalWrite(relay,HIGH);
  137.         BlinkerAliGenie.powerState("off");
  138.         BLINKER_LOG("滅燈!");
  139.         BlinkerAliGenie.print();
  140.     }
  141. }

  142. //=================================================================
  143. void aligenieQuery(int32_t queryCode)     

  144. {
  145.     BLINKER_LOG("AliGenie Query codes: ", queryCode);

  146.     switch (queryCode)
  147.     {
  148.         case BLINKER_CMD_QUERY_ALL_NUMBER :
  149.             BLINKER_LOG("AliGenie Query All");
  150.             BlinkerAliGenie.powerState(relay ? "on" : "off");
  151.             BlinkerAliGenie.print();
  152.             break;
  153.         case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
  154.             BLINKER_LOG("AliGenie Query Power State");
  155.             BlinkerAliGenie.powerState(relay ? "on" : "off");
  156.             BlinkerAliGenie.print();
  157.         default :
  158.             BlinkerAliGenie.powerState(relay ? "on" : "off");
  159.             BlinkerAliGenie.print();
  160.             break;
  161.     }
  162. }

  163. //=================================================================
  164. void dataRead(const String & data)
  165. {
  166.     BLINKER_LOG("Blinker readString: ", data);

  167.     Blinker.vibrate();

  168.     uint32_t BlinkerTime = millis();

  169.     Blinker.print("millis", BlinkerTime);
  170. }

  171. //=========================================================================
  172. void heartbeat()
  173. {
  174.   BLINKER_LOG("狀態(tài)同步!");
  175.    if (digitalRead(relay)==LOW)
  176.     {
  177.         Button1.print("on");
  178.         Button1.color("red");            
  179.         Button1.text("繼電器打開(kāi)");
  180.     }
  181.     else
  182.     {
  183.         Button1.print("off");
  184.         Button1.color("gray");              
  185.         Button1.text("繼電器關(guān)閉");
  186.     }
  187. }

  188. //=================================================================

  189. void sdms()
  190. {
  191. if (digitalRead(relay)==HIGH&&digitalRead(key)==LOW)
  192.   {
  193.      Blinker.delay(400);                     
  194.      if (digitalRead(key)==HIGH)
  195.      {            
  196.       BLINKER_LOG("本地開(kāi)關(guān)動(dòng)作!");      
  197.       BLINKER_LOG("亮燈!");                 
  198.       digitalWrite(relay,LOW);
  199.       Button1.color("red");                 
  200.       Button1.text("繼電器打開(kāi)");
  201.       Button1.print("on");
  202.       }
  203.    }
  204. if (digitalRead(relay)==LOW&&digitalRead(key)==LOW)
  205.     {              
  206.       Blinker.delay(400);
  207.       if (digitalRead(key)==HIGH)
  208.         {
  209.         BLINKER_LOG("本地開(kāi)關(guān)動(dòng)作!");
  210.         BLINKER_LOG("滅燈!");                        
  211.         digitalWrite(relay, HIGH);
  212.         Button1.color("gray");                    
  213.         Button1.text("繼電器關(guān)閉");
  214.         Button1.print("off");
  215.         }
  216.       }
  217.   }

  218. //=================================================================
  219. void setup()
  220. {  Serial.begin(115200);
  221.   delay(10);
  222.   BLINKER_DEBUG.stream(Serial);
  223.   BLINKER_DEBUG.debugAll();
  224.   pinMode(relay, OUTPUT);
  225.   digitalWrite(relay, LOW);   
  226.   pinMode(key,INPUT_PULLUP);      
  227.   WebConfig();            
  228.   Blinker.begin(auth, WiFi.SSID().c_str(), WiFi.psk().c_str());
  229.   Button1.attach(button1_callback);                        
  230.   Blinker.attachData(dataRead);                                 
  231.   Blinker.attachHeartbeat(heartbeat);                              

  232.   BlinkerMIOT.attachPowerState(miotPowerState);   
  233.   BlinkerDuerOS.attachPowerState(duerPowerState);      
  234.   BlinkerAliGenie.attachPowerState(aligeniePowerState);  

  235.   BlinkerMIOT.attachQuery(miotQuery);      
  236.   BlinkerDuerOS.attachQuery(duerQuery);      
  237.   BlinkerAliGenie.attachQuery(aligenieQuery);

  238. }

  239. //=================================================================
  240. void loop() {
  241.   Blinker.run();
  242.   sdms();                  

  243.   }
復(fù)制代碼

8266插座ARDUINO.jpg (211.86 KB, 下載次數(shù): 145)

8266插座ARDUINO.jpg

作者: dzc3458    時(shí)間: 2021-12-14 00:49
arduino的WiFi庫(kù)能用在esp8266上嗎,需不需要自己下載庫(kù)
作者: zhangli019    時(shí)間: 2021-12-21 12:51
沒(méi)注釋?




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1