依然存在稳定性问题
机智云官网——机智云
一个比较详细的教程——esp8266
一开始采用的是esp12f 可是他太不稳定,总是掉线,机智云的固件我也是刷了无数遍,哎太难了。
我比较懒,走过了太多的弯路。太迷茫又太累。博客很少写教程,只是做个笔记记述一下心路历程。
/**
************************************************************
***********************************************************/
#include <Gizwits.h>
#include <Wire.h>
#include <SoftwareSerial.h>
//#include <PinChangeInt.h> //外部中断
SoftwareSerial mySerial(A2, A3); // A2 -> RX, A3 -> TX
bool varR_open = ;
Gizwits myGizwits;
#define KEY1 6
#define KEY2 7
#define KEY1_SHORT_PRESS 1
#define KEY1_LONG_PRESS 2
#define KEY2_SHORT_PRESS 4
#define KEY2_LONG_PRESS 8
#define NO_KEY 0
#define KEY_LONG_TIMER 3
const int TrigPin = ;
const int EchoPin = ;
float cm;
unsigned long Last_KeyTime = ;
unsigned long gokit_time_s(void)
{
return millis() / ;
}
char gokit_key1down(void)
{
unsigned long keep_time = ;
if (digitalRead(KEY1) == LOW)
{
delay();
if (digitalRead(KEY1) == LOW)
{
keep_time = gokit_time_s();
while (digitalRead(KEY1) == LOW)
{
if ((gokit_time_s() - keep_time) > KEY_LONG_TIMER)
{
Last_KeyTime = gokit_time_s();
return KEY1_LONG_PRESS;
}
}
if ((gokit_time_s() - Last_KeyTime) > KEY_LONG_TIMER)
{
return KEY1_SHORT_PRESS;
}
return ;
}
return ;
}
return ;
}
char gokit_key2down(void)
{
int unsigned long keep_time = ;
if (digitalRead(KEY2) == LOW)
{
delay();
if (digitalRead(KEY2) == LOW)
{
keep_time = gokit_time_s();
while (digitalRead(KEY2) == LOW)
{ if ((gokit_time_s() - keep_time) > KEY_LONG_TIMER)
{
Last_KeyTime = gokit_time_s();
return KEY2_LONG_PRESS;
}
}
if ((gokit_time_s() - Last_KeyTime) > KEY_LONG_TIMER)
{
return KEY2_SHORT_PRESS;
}
return ;
}
return ;
}
return ;
}
char gokit_keydown(void)
{
char ret = ;
ret |= gokit_key2down();
ret |= gokit_key1down();
return ret;
}
/**
* KEY_Handle
* @param none
* @return none
*/
void KEY_Handle(void)
{
/* Press for over than 3 second is Long Press */
switch (gokit_keydown())
{
case KEY1_SHORT_PRESS:
// mySerial.println(F("KEY1_SHORT_PRESS , Production Test Mode "));
myGizwits.setBindMode(WIFI_PRODUCTION_TEST);
break;
case KEY1_LONG_PRESS:
// mySerial.println(F("KEY1_LONG_PRESS ,Wifi Reset"));
myGizwits.setBindMode(WIFI_RESET_MODE);
break;
case KEY2_SHORT_PRESS:
// mySerial.println(F("KEY2_SHORT_PRESS Soft AP mode"));
myGizwits.setBindMode(WIFI_SOFTAP_MODE);
//Soft AP mode
break;
case KEY2_LONG_PRESS:
// mySerial.println(F("KEY2_LONG_PRESS ,AirLink mode"));
myGizwits.setBindMode(WIFI_AIRLINK_MODE);
digitalWrite(,HIGH);
//AirLink mode
break;
default:
break;
}
}
void ceju()
{
digitalWrite(TrigPin, LOW);
delayMicroseconds();
digitalWrite(TrigPin, HIGH);
delayMicroseconds();
digitalWrite(TrigPin, LOW);
cm = pulseIn(EchoPin, HIGH) / 58.0; //算成厘米
cm = (int(cm * 100.0)) / 100.0; //保留两位小数
mySerial.println(cm);
//Serial.print(cm);
if(cm<)
{
digitalWrite(,LOW);
digitalWrite(,HIGH);
} //Serial.print("cm");
//Serial.println();
}
/**
* Serial Init , Gizwits Init
* @param none
* @return none
*/
void setup() {
// put your setup code here, to run once:
mySerial.begin();
pinMode(KEY1, INPUT_PULLUP);
pinMode(KEY2, INPUT_PULLUP);
pinMode(,OUTPUT);// led
pinMode(,OUTPUT);
pinMode(,INPUT);
//pinMode (10,INPUT); pinMode(,OUTPUT);
pinMode (,OUTPUT);
digitalWrite(,HIGH);
digitalWrite(,HIGH);
digitalWrite(,HIGH);
digitalWrite(,HIGH);
digitalWrite(,HIGH);
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT); myGizwits.begin();
// mySerial.println("GoKit init OK \n");
//attachPinChangeInterrupt(4,xianwei, CHANGE); //开启外部中断
//attachInterrupt(0,xianwei,FALLING);
}
void xianwei()
{
digitalWrite(,HIGH);
digitalWrite(,LOW);
delay();
digitalWrite(,HIGH);
digitalWrite(,HIGH);
}
/**
* Wifi status printf
* @param none
* @return none
*/
void wifiStatusHandle()
{
if(myGizwits.wifiHasBeenSet(WIFI_SOFTAP))
{
// mySerial.println(F("WIFI_SOFTAP!"));
} if(myGizwits.wifiHasBeenSet(WIFI_AIRLINK))
{
// mySerial.println(F("WIFI_AIRLINK!"));
} if(myGizwits.wifiHasBeenSet(WIFI_STATION))
{
// mySerial.println(F("WIFI_STATION!"));
} if(myGizwits.wifiHasBeenSet(WIFI_CON_ROUTER))
{
//mySerial.println(F("WIFI_CON_ROUTER!"));
} if(myGizwits.wifiHasBeenSet(WIFI_DISCON_ROUTER))
{
// mySerial.println(F("WIFI_DISCON_ROUTER!"));
} if(myGizwits.wifiHasBeenSet(WIFI_CON_M2M))
{
// mySerial.println(F("WIFI_CON_M2M!"));
} if(myGizwits.wifiHasBeenSet(WIFI_DISCON_M2M))
{
// mySerial.println(F("WIFI_DISCON_M2M!"));
}
}
/**
* Arduino loop
* @param none
* @return none
*/
void loop() {
ceju();
if(digitalRead()==LOW)
{
xianwei();
}
bool varR_open = ;
if(myGizwits.hasBeenSet(EVENT_open))
{
myGizwits.read(EVENT_open,&varR_open);
if(varR_open ==){
digitalWrite(,LOW);
digitalWrite(,HIGH); }
else
{
digitalWrite(,HIGH);
digitalWrite(,HIGH);
}
}
bool varR_test = ;
if(myGizwits.hasBeenSet(EVENT_test))
{
myGizwits.read(EVENT_test,&varR_test);
if(varR_test ==){
digitalWrite(,LOW);
}
else
{
digitalWrite(,HIGH);
}
} KEY_Handle();
wifiStatusHandle();
myGizwits.process();
}