小智插座3.0成功
先上图 等考试完了,我再详细记录小可爱的制作过程。
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 if ((gokit_time_s() - Last_KeyTime) > KEY_LONG_TIMER) 38 { 39 return KEY1_SHORT_PRESS; 40 } 41 return 0; 42 } 43 return 0; 44 } 45 return 0; 46 } 47 char gokit_key2down(void) 48 { 49 int unsigned long keep_time = 0; 50 if (digitalRead(KEY2) == LOW) 51 { 52 delay(100); 53 if (digitalRead(KEY2) == LOW) 54 { 55 keep_time = gokit_time_s(); 56 while (digitalRead(KEY2) == LOW) //until open the key 57 { 58 if ((gokit_time_s() - keep_time) > KEY_LONG_TIMER) 59 { 60 Last_KeyTime = gokit_time_s(); 61 return KEY2_LONG_PRESS; 62 } 63 } 64 if ((gokit_time_s() - Last_KeyTime) > KEY_LONG_TIMER) 65 { 66 return KEY2_SHORT_PRESS; 67 } 68 return 0; 69 } 70 return 0; 71 } 72 return 0; 73 } 74 char gokit_keydown(void) 75 { 76 char ret = 0; 77 ret |= gokit_key2down(); 78 ret |= gokit_key1down(); 79 return ret; 80 } 81 void KEY_Handle(void) 82 { 83 switch (gokit_keydown()) 84 { 85 case KEY1_SHORT_PRESS: 86 myGizwits.setBindMode(WIFI_PRODUCTION_TEST); 87 break; 88 case KEY1_LONG_PRESS: 89 myGizwits.setBindMode(WIFI_RESET_MODE); 90 break; 91 case KEY2_SHORT_PRESS: 92 myGizwits.setBindMode(WIFI_SOFTAP_MODE); 93 break; 94 case KEY2_LONG_PRESS: 95 myGizwits.setBindMode(WIFI_AIRLINK_MODE); 96 digitalWrite(5,HIGH); 97 digitalWrite(8,HIGH); 98 delay(1000); 99 digitalWrite(8,LOW); 100 digitalWrite(5,LOW); 101 break; 102 default: 103 break; 104 } 105 } 106 void wifiStatusHandle() 107 { 108 if(myGizwits.wifiHasBeenSet(WIFI_SOFTAP)) 109 { 110 } 111 if(myGizwits.wifiHasBeenSet(WIFI_AIRLINK)) 112 { 113 } 114 } 115 void setup() { 116 mySerial.begin(115200); 117 pinMode(KEY1, INPUT_PULLUP); 118 pinMode(KEY2, INPUT_PULLUP); 119 pinMode(5,OUTPUT);//指示灯 120 pinMode(8,OUTPUT);//beeWIFI_AIRLINK成功报警提示 121 pinMode(9,OUTPUT);//作业执行 122 digitalWrite(5,LOW); 123 digitalWrite(8,LOW); 124 digitalWrite(9,HIGH); 125 myGizwits.begin(); 126 } 127 void loop() { 128 KEY_Handle();//key handle , network configure网络配置 129 wifiStatusHandle();//WIFI Status Handle无线网络状态处理 130 unsigned long varW_back = 0;//Add Sensor Data Collection 131 myGizwits.write(VALUE_back, varW_back); 132 ////////////////////////////////////////////////////////////////////////////////// 133 bool varR_on = 0; 134 if(myGizwits.hasBeenSet(EVENT_on)) 135 { 136 myGizwits.read(EVENT_on,&varR_on);//Address for storing data 137 if(varR_on==1) 138 { 139 digitalWrite(9,LOW); 140 } 141 else 142 digitalWrite(9,HIGH); 143 } 144 bool varR_off = 0; 145 if(myGizwits.hasBeenSet(EVENT_off)) 146 { 147 myGizwits.read(EVENT_off,&varR_off);//Address for storing data 148 } 149 unsigned long varR_time = 0; 150 if(myGizwits.hasBeenSet(EVENT_time)) 151 { 152 myGizwits.read(EVENT_time,&varR_time);//Address for storing data 153 } 154 myGizwits.process(); 155 }
外观美到炸,总体很棒!很满意哦!
@青山不移,文笔不息。学习,坚持,梦想青春!