1. 程式人生 > >wifi舵機門鎖 開關燈 機智雲

wifi舵機門鎖 開關燈 機智雲

double Fahrenheit(double celsius)
{
return 1.8 * celsius + 32;
} //攝氏溫度度轉化為華氏溫度

double Kelvin(double celsius)
{
return celsius + 273.15;
} //攝氏溫度轉化為開氏溫度

// 露點(點在此溫度時,空氣飽和併產生露珠)
// 參考: [url=http://wahiduddin.net/calc/density_algorithms.htm]http://wahiduddin.net/calc/density_algorithms.htm[/url]
double dewPoint(double celsius, double humidity)
{
double A0 = 373.15 / (273.15 + celsius);
double SUM = -7.90298 * (A0 - 1);
SUM += 5.02808 * log10(A0);
SUM += -1.3816e-7 * (pow(10, (11.344 * (1 - 1 / A0))) - 1) ;
SUM += 8.1328e-3 * (pow(10, (-3.49149 * (A0 - 1))) - 1) ;
SUM += log10(1013.246);
double VP = pow(10, SUM - 3) * humidity;
double T = log(VP / 0.61078); // temp var
return (241.88 * T) / (17.558 - T);
}

// 快速計算露點,速度是5倍dewPoint()
// 參考: [url=http://en.wikipedia.org/wiki/Dew_point]http://en.wikipedia.org/wiki/Dew_point[/url]
double dewPointFast(double celsius, double humidity)
{
double a = 17.271;
double b = 237.7;
double temp = (a * celsius) / (b + celsius) + log(humidity / 100);
double Td = (b * temp) / (a - temp);
return Td;
}

#include <Gizwits.h>
#include <Wire.h>
#include <SoftwareSerial.h>
#include <dht11.h>
#include <Servo.h>
#include <IRremote.h>
#include <SPI.h>
Servo myservo;
Servo myservo1;
int PIR_sensor = A5; //指定PIR模擬埠 A5
int LED = 13; //指定LED埠 13
int val = 0; //儲存獲取到的PIR數值

int d = 3;

const int PIN = A0;//接紅外介面
IRrecv HW(PIN);//宣告一個IRrecv型別的物件,並接到PIN,也就是11腳
decode_results results;//宣告一個decode_results型別的物件results,存放紅外接收的訊號解碼結果
extern uint8_t SmallFont[];
dht11 DHT11;

#define DHT11PIN 2
SoftwareSerial mySerial(A2, A3); // A2 -> RX, A3 -> TX

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
unsigned long Last_KeyTime = 0;

unsigned long gokit_time_s(void)
{
return millis() / 1000;
}

char gokit_key1down(void)
{
unsigned long keep_time = 0;
if (digitalRead(KEY1) == LOW)
{
delay(100);
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;
}
} //until open the key

  if ((gokit_time_s() - Last_KeyTime) > KEY_LONG_TIMER)
  {
    return KEY1_SHORT_PRESS;
  }
  return 0;
}
return 0;

}
return 0;
}

char gokit_key2down(void)
{
unsigned long keep_time = 0;
if (digitalRead(KEY2) == LOW)
{
delay(100);
if (digitalRead(KEY2) == LOW)
{
keep_time = gokit_time_s();
while (digitalRead(KEY2) == LOW) //until open the key
{

    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 0;
}
return 0;

}
return 0;
}

char gokit_keydown(void)
{
char ret = 0;
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);
//AirLink mode
break;
default:
break;
}
}

/**
Serial Init , Gizwits Init
@param none
@return none
*/
void setup() {
// put your setup code here, to run once:
mySerial.begin(115200);
digitalWrite(8, LOW);
HW.enableIRIn();//初始化紅外接收裝置
pinMode(LED, OUTPUT); //設定埠2為輸出模式
Serial.begin(9600);
//接繼電器
pinMode(KEY1, INPUT_PULLUP);
pinMode(KEY2, INPUT_PULLUP);
myservo1.attach(10);
myservo1.write(90);
myservo.attach(9);//舵機針腳位9
myservo.write(0); //舵機初始化0度
SPI.begin();

pinMode(d, OUTPUT);
digitalWrite(d, HIGH);

Serial.begin(9600);
myGizwits.begin();

mySerial.println(“GoKit init OK \n”);
}

/**
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() {
if(HW.decode(&results))
{
Serial.println(results.value,HEX);
delay(500);
switch(results.value)//通過這個迴圈,比對
{
case 0XFFA25D://注意這個地方紅外遙控器距離遠了,資料不一樣
digitalWrite(LED,HIGH);
myservo1.write(110);
delay(300);
myservo1.write(90);
break;
case 0XFFE21D://注意這個地方紅外遙控器距離遠了,資料不一樣
digitalWrite(LED,LOW);
myservo1.write(70);
delay(300);
myservo1.write(90);
break;
case 0XFF629D://注意這個地方紅外遙控器距離遠了,資料不一樣
myservo.write(180);
digitalWrite(8, HIGH);
delay(3500);
digitalWrite(8, LOW);
myservo.write(0);
break;
//FF629D

}
HW.resume();//接收下一個紅外訊號

}

KEY_Handle();//key handle , network configure
wifiStatusHandle();//WIFI Status Handle
int chk = DHT11.read(DHT11PIN);

switch (chk)
{
case DHTLIB_OK:
Serial.println(“OK”);
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.println(“Checksum error”);
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.println(“Time out error”);
break;
default:
Serial.println(“Unknown error”);
break;
}
unsigned long varW_wendu = (float)DHT11.temperature ; //Add Sensor Data Collection
myGizwits.write(VALUE_wendu, varW_wendu);
unsigned long varW_shidu = (float)DHT11.humidity;//Add Sensor Data Collection
myGizwits.write(VALUE_shidu, varW_shidu);
unsigned long varW_ren = 0;//Add Sensor Data Collection
myGizwits.write(VALUE_ren, varW_ren);
val = analogRead(PIR_sensor); //讀取A0口的電壓值並賦值到val

varW_ren = analogRead(PIR_sensor);
if (val > 150)//判斷PIR數值是否大於150,
{
digitalWrite(LED,HIGH); //大於表示感應到有人

}
else
{
digitalWrite(LED,LOW);

}

bool varR_deng = 0;
if (myGizwits.hasBeenSet(EVENT_deng))
{
myGizwits.read(EVENT_deng, &varR_deng); //Address for storing data
if (varR_deng == 1)
{

    myservo.write(180);
  digitalWrite(8, HIGH);
  delay(3500);
  digitalWrite(8, LOW);
  myservo.write(0);
}
else
  digitalWrite(8, LOW);
myservo.write(0);
mySerial.println(F("EVENT_deng"));
mySerial.println(varR_deng, DEC);

}
myGizwits.process();
}