基于esp8266的智能台灯DIY

Posted 极客阿正

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于esp8266的智能台灯DIY相关的知识,希望对你有一定的参考价值。

研究esp8266已经很久了,特制作了一个智能灯,

代码如下,总结几点

一、关于arduino pro mini 的烧写方法

一定要在编译成功之后的下载提示出现的瞬间,单击复位按钮。才可进入烧写

二、esp8266APP界面图标功能串联混乱的解决方法,即,重新加载 机智云库文件即可。

  1 #include <Gizwits.h>
  2 #include <Wire.h>
  3 #include <SoftwareSerial.h>
  4 SoftwareSerial mySerial(A2, A3); // A2 -> RX, A3 -> TX
  5 Gizwits myGizwits;
  6 #define   KEY1              6
  7 #define   KEY2              7
  8 #define   KEY1_SHORT_PRESS  1
  9 #define   KEY1_LONG_PRESS   2
 10 #define   KEY2_SHORT_PRESS  4
 11 #define   KEY2_LONG_PRESS   8
 12 #define   NO_KEY            0
 13 #define   KEY_LONG_TIMER    3
 14 int flag =0;
 15 unsigned long Last_KeyTime = 0;
 16 unsigned long gokit_time_s(void)
 17 {
 18   return millis() / 1000;
 19 }
 20 char gokit_key1down(void)
 21 {
 22   unsigned long keep_time = 0;
 23   if (digitalRead(KEY1) == LOW)
 24   {
 25     delay(100);
 26     if (digitalRead(KEY1) == LOW)
 27     {
 28       keep_time = gokit_time_s();
 29       while (digitalRead(KEY1) == LOW)
 30       {
 31         if ((gokit_time_s() - keep_time) > KEY_LONG_TIMER)
 32         {
 33           Last_KeyTime = gokit_time_s();
 34           return KEY1_LONG_PRESS;
 35         }
 36       } //until open the key
 37 
 38       if ((gokit_time_s() - Last_KeyTime) > KEY_LONG_TIMER)
 39       {
 40         return KEY1_SHORT_PRESS;
 41       }
 42       return 0;
 43     }
 44     return 0;
 45   }
 46   return 0;
 47 }
 48 
 49 char gokit_key2down(void)
 50 {
 51   int unsigned long keep_time = 0;
 52   if (digitalRead(KEY2) == LOW)
 53   {
 54     delay(100);
 55     if (digitalRead(KEY2) == LOW)
 56     {
 57       keep_time = gokit_time_s();
 58       while (digitalRead(KEY2) == LOW) //until open the key
 59       {
 60 
 61         if ((gokit_time_s() - keep_time) > KEY_LONG_TIMER)
 62         {
 63           Last_KeyTime = gokit_time_s();
 64           return KEY2_LONG_PRESS;
 65         }
 66       }
 67       if ((gokit_time_s() - Last_KeyTime) > KEY_LONG_TIMER)
 68       {
 69         return KEY2_SHORT_PRESS;
 70       }
 71       return 0;
 72     }
 73     return 0;
 74   }
 75   return 0;
 76 }
 77 char gokit_keydown(void)
 78 {
 79   char ret = 0;
 80   ret |= gokit_key2down();
 81   ret |= gokit_key1down();
 82   return ret;
 83 }
 84 void KEY_Handle(void)
 85 {
 86   switch (gokit_keydown())
 87   {
 88     case KEY1_SHORT_PRESS:
 89       myGizwits.setBindMode(WIFI_PRODUCTION_TEST);
 90       break;
 91     case KEY1_LONG_PRESS:
 92       myGizwits.setBindMode(WIFI_RESET_MODE);
 93       break;
 94     case KEY2_SHORT_PRESS:
 95       myGizwits.setBindMode(WIFI_SOFTAP_MODE);
 96       break;
 97     case KEY2_LONG_PRESS:
 98       myGizwits.setBindMode(WIFI_AIRLINK_MODE);
 99     digitalWrite(5,HIGH);
100     digitalWrite(8,HIGH);
101     delay(1000);
102    digitalWrite(8,LOW);
103    digitalWrite(5,LOW);
104       break;
105     default:
106       break;
107   }
108 }
109 void wifiStatusHandle()
110 {
111   if(myGizwits.wifiHasBeenSet(WIFI_SOFTAP))
112   {
113   }  
114   if(myGizwits.wifiHasBeenSet(WIFI_AIRLINK))
115   {
116   } 
117 }
118 
119 void setup() {
120   // put your setup code here, to run once:
121 
122   mySerial.begin(115200);
123    pinMode(KEY1, INPUT_PULLUP);
124   pinMode(KEY2, INPUT_PULLUP);
125   pinMode(10,INPUT);//红外探头
126   pinMode(5,OUTPUT);//指示灯
127   pinMode(8,OUTPUT);//beeWIFI_AIRLINK成功报警提示
128   pinMode(9,OUTPUT);//test
129   pinMode(11,OUTPUT);
130   digitalWrite(5,LOW);
131   digitalWrite(8,LOW);
132   digitalWrite(9,HIGH);
133   myGizwits.begin();
134 }
135 void loop() {  
136    KEY_Handle();//key handle , network configure网络配置
137   wifiStatusHandle();//WIFI Status Handle无线网络状态处理
138   unsigned long varW_timeh ;//Add Sensor Data Collection
139   unsigned long varR_T_Minutes;
140   bool varR_on_off ;
141   if (digitalRead(10)==LOW)
142   { 
143    digitalWrite(9,LOW);
144     }
145   if(myGizwits.hasBeenSet(EVENT_on_off))
146   {
147     myGizwits.read(EVENT_on_off,&varR_on_off);//Address for storing data
148     if(varR_on_off==1)
149     {
150       digitalWrite(9,LOW);
151       }
152      else
153      digitalWrite(9,HIGH);
154   }
155   if(myGizwits.hasBeenSet(EVENT_T_Minutes))
156   {
157     myGizwits.read(EVENT_T_Minutes,&varR_T_Minutes);//Address for storing data
158 
159     analogWrite(11,int(varR_T_Minutes));
160     varW_timeh=varR_T_Minutes;
161      myGizwits.write(VALUE_timeh, varW_timeh);
162   }
163   myGizwits.process();
164 
165 }

 

以上是关于基于esp8266的智能台灯DIY的主要内容,如果未能解决你的问题,请参考以下文章

智能家居入门DIY——组合

智能家居入门DIY——执行命令

使用ESP8266NodeMCU机智云改造传统插座为智能插座

基于ESP8266的开源MCU方案(一)智能家居方案总体介绍

基于ESP8266的WIFI智能体重秤

#yyds干货盘点# 基于STM32+ESP8266+华为云IoT设计的智能门锁