依然存在稳定性问题
机智云官网——机智云
一个比较详细的教程——esp8266
一开始采用的是esp12f 可是他太不稳定,总是掉线,机智云的固件我也是刷了无数遍,哎太难了。
我比较懒,走过了太多的弯路。太迷茫又太累。博客很少写教程,只是做个笔记记述一下心路历程。
1 /** 2 ************************************************************ 3 ***********************************************************/ 4 #include <Gizwits.h> 5 #include <Wire.h> 6 #include <SoftwareSerial.h> 7 //#include <PinChangeInt.h> //外部中断 8 SoftwareSerial mySerial(A2, A3); // A2 -> RX, A3 -> TX 9 bool varR_open = 0; 10 Gizwits myGizwits; 11 #define KEY1 6 12 #define KEY2 7 13 #define KEY1_SHORT_PRESS 1 14 #define KEY1_LONG_PRESS 2 15 #define KEY2_SHORT_PRESS 4 16 #define KEY2_LONG_PRESS 8 17 #define NO_KEY 0 18 #define KEY_LONG_TIMER 3 19 const int TrigPin = 10; 20 const int EchoPin = 13; 21 float cm; 22 unsigned long Last_KeyTime = 0; 23 unsigned long gokit_time_s(void) 24 { 25 return millis() / 1000; 26 } 27 char gokit_key1down(void) 28 { 29 unsigned long keep_time = 0; 30 if (digitalRead(KEY1) == LOW) 31 { 32 delay(100); 33 if (digitalRead(KEY1) == LOW) 34 { 35 keep_time = gokit_time_s(); 36 while (digitalRead(KEY1) == LOW) 37 { 38 if ((gokit_time_s() - keep_time) > KEY_LONG_TIMER) 39 { 40 Last_KeyTime = gokit_time_s(); 41 return KEY1_LONG_PRESS; 42 } 43 } 44 if ((gokit_time_s() - Last_KeyTime) > KEY_LONG_TIMER) 45 { 46 return KEY1_SHORT_PRESS; 47 } 48 return 0; 49 } 50 return 0; 51 } 52 return 0; 53 } 54 char gokit_key2down(void) 55 { 56 int unsigned long keep_time = 0; 57 if (digitalRead(KEY2) == LOW) 58 { 59 delay(100); 60 if (digitalRead(KEY2) == LOW) 61 { 62 keep_time = gokit_time_s(); 63 while (digitalRead(KEY2) == LOW) 64 { 65 66 if ((gokit_time_s() - keep_time) > KEY_LONG_TIMER) 67 { 68 Last_KeyTime = gokit_time_s(); 69 return KEY2_LONG_PRESS; 70 } 71 } 72 if ((gokit_time_s() - Last_KeyTime) > KEY_LONG_TIMER) 73 { 74 return KEY2_SHORT_PRESS; 75 } 76 return 0; 77 } 78 return 0; 79 } 80 return 0; 81 } 82 char gokit_keydown(void) 83 { 84 char ret = 0; 85 ret |= gokit_key2down(); 86 ret |= gokit_key1down(); 87 return ret; 88 } 89 /** 90 * KEY_Handle 91 * @param none 92 * @return none 93 */ 94 void KEY_Handle(void) 95 { 96 /* Press for over than 3 second is Long Press */ 97 switch (gokit_keydown()) 98 { 99 case KEY1_SHORT_PRESS: 100 // mySerial.println(F("KEY1_SHORT_PRESS , Production Test Mode ")); 101 myGizwits.setBindMode(WIFI_PRODUCTION_TEST); 102 break; 103 case KEY1_LONG_PRESS: 104 // mySerial.println(F("KEY1_LONG_PRESS ,Wifi Reset")); 105 myGizwits.setBindMode(WIFI_RESET_MODE); 106 break; 107 case KEY2_SHORT_PRESS: 108 // mySerial.println(F("KEY2_SHORT_PRESS Soft AP mode")); 109 myGizwits.setBindMode(WIFI_SOFTAP_MODE); 110 //Soft AP mode 111 break; 112 case KEY2_LONG_PRESS: 113 // mySerial.println(F("KEY2_LONG_PRESS ,AirLink mode")); 114 myGizwits.setBindMode(WIFI_AIRLINK_MODE); 115 digitalWrite(8,HIGH); 116 //AirLink mode 117 break; 118 default: 119 break; 120 } 121 } 122 void ceju() 123 { 124 digitalWrite(TrigPin, LOW); 125 delayMicroseconds(2); 126 digitalWrite(TrigPin, HIGH); 127 delayMicroseconds(10); 128 digitalWrite(TrigPin, LOW); 129 cm = pulseIn(EchoPin, HIGH) / 58.0; //算成厘米 130 cm = (int(cm * 100.0)) / 100.0; //保留两位小数 131 mySerial.println(cm); 132 //Serial.print(cm); 133 if(cm<20) 134 { 135 digitalWrite(12,LOW); 136 digitalWrite(11,HIGH); 137 } 138 139 //Serial.print("cm"); 140 //Serial.println(); 141 } 142 /** 143 * Serial Init , Gizwits Init 144 * @param none 145 * @return none 146 */ 147 void setup() { 148 // put your setup code here, to run once: 149 mySerial.begin(9600); 150 pinMode(KEY1, INPUT_PULLUP); 151 pinMode(KEY2, INPUT_PULLUP); 152 pinMode(5,OUTPUT);// led 153 pinMode(8,OUTPUT); 154 pinMode(9,INPUT); 155 //pinMode (10,INPUT); 156 157 pinMode(12,OUTPUT); 158 pinMode (11,OUTPUT); 159 digitalWrite(12,HIGH); 160 digitalWrite(11,HIGH); 161 digitalWrite(5,HIGH); 162 digitalWrite(9,HIGH); 163 digitalWrite(10,HIGH); 164 pinMode(TrigPin, OUTPUT); 165 pinMode(EchoPin, INPUT); 166 167 myGizwits.begin(); 168 // mySerial.println("GoKit init OK "); 169 //attachPinChangeInterrupt(4,xianwei, CHANGE); //开启外部中断 170 //attachInterrupt(0,xianwei,FALLING); 171 } 172 void xianwei() 173 { 174 digitalWrite(12,HIGH); 175 digitalWrite(11,LOW); 176 delay(1000); 177 digitalWrite(12,HIGH); 178 digitalWrite(11,HIGH); 179 } 180 /** 181 * Wifi status printf 182 * @param none 183 * @return none 184 */ 185 void wifiStatusHandle() 186 { 187 if(myGizwits.wifiHasBeenSet(WIFI_SOFTAP)) 188 { 189 // mySerial.println(F("WIFI_SOFTAP!")); 190 } 191 192 if(myGizwits.wifiHasBeenSet(WIFI_AIRLINK)) 193 { 194 // mySerial.println(F("WIFI_AIRLINK!")); 195 } 196 197 if(myGizwits.wifiHasBeenSet(WIFI_STATION)) 198 { 199 // mySerial.println(F("WIFI_STATION!")); 200 } 201 202 if(myGizwits.wifiHasBeenSet(WIFI_CON_ROUTER)) 203 { 204 //mySerial.println(F("WIFI_CON_ROUTER!")); 205 } 206 207 if(myGizwits.wifiHasBeenSet(WIFI_DISCON_ROUTER)) 208 { 209 // mySerial.println(F("WIFI_DISCON_ROUTER!")); 210 } 211 212 if(myGizwits.wifiHasBeenSet(WIFI_CON_M2M)) 213 { 214 // mySerial.println(F("WIFI_CON_M2M!")); 215 } 216 217 if(myGizwits.wifiHasBeenSet(WIFI_DISCON_M2M)) 218 { 219 // mySerial.println(F("WIFI_DISCON_M2M!")); 220 } 221 } 222 /** 223 * Arduino loop 224 * @param none 225 * @return none 226 */ 227 void loop() { 228 ceju(); 229 if(digitalRead(9)==LOW) 230 { 231 xianwei(); 232 } 233 bool varR_open = 0; 234 if(myGizwits.hasBeenSet(EVENT_open)) 235 { 236 myGizwits.read(EVENT_open,&varR_open); 237 if(varR_open ==1){ 238 digitalWrite(12,LOW); 239 digitalWrite(11,HIGH); 240 241 } 242 else 243 { 244 digitalWrite(12,HIGH); 245 digitalWrite(11,HIGH); 246 } 247 } 248 bool varR_test = 0; 249 if(myGizwits.hasBeenSet(EVENT_test)) 250 { 251 myGizwits.read(EVENT_test,&varR_test); 252 if(varR_test ==1){ 253 digitalWrite(5,LOW); 254 } 255 else 256 { 257 digitalWrite(5,HIGH); 258 } 259 } 260 261 KEY_Handle(); 262 wifiStatusHandle(); 263 myGizwits.process(); 264 }