智能农场 阳光温湿度 检测 远程控制开关 机智云

妈的 这次 接错线了 气死了 找了半天代码的毛病 气人

今天 有个 光照传感器

例子 代码

#include
// GY-30
// BH1750FVI
// in ADDR ‘L’ mode 7bit addr
#define ADDR 0b0100011
// addr ‘H’ mode
// #define ADDR 0b1011100
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Wire.begin();
pinMode(13, OUTPUT);
Wire.beginTransmission(ADDR);
Wire.write(0b00000001);
Wire.endTransmission();
}
void loop() {
// put your main code here, to run repeatedly:
int val = 0;
// reset
Wire.beginTransmission(ADDR);
Wire.write(0b00000111);
Wire.endTransmission();
digitalWrite(13, LOW);
delay(100);
Wire.beginTransmission(ADDR);
Wire.write(0b00100000);
Wire.endTransmission();
// typical read delay 120ms
delay(120);
Wire.requestFrom(ADDR, 2); // 2byte every time
for (val=0; Wire.available()>=1; ) {
char c = Wire.read();
//Serial.println(c, HEX);
val = (val << 8) + (c & 0xFF);
}
val = val / 1.2;
Serial.print("lx: ");
Serial.println(val);
Serial.println(“OK”);
digitalWrite(13, HIGH);
delay(100);
}

下面就是 全部 的代码

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
#include
#include
#include
dht11 DHT11;
#define DHT11PIN 2
#define ADDR 0b0100011

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);
    pinMode(KEY1, INPUT_PULLUP);
    pinMode(KEY2, INPUT_PULLUP);
    pinMode(5,OUTPUT);
    pinMode(9,OUTPUT);
    while (!Serial)
    Wire.begin();

Wire.beginTransmission(ADDR);
Wire.write(0b00000001);
Wire.endTransmission();

myGizwits.begin();

}

/**

  • 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() {
    KEY_Handle();//key handle , network configure
    wifiStatusHandle();//WIFI Status Handle
    bool varR_a = 0;
    if(myGizwits.hasBeenSet(EVENT_a))
    {
    myGizwits.read(EVENT_a,&varR_a);//Address for storing data
    if(varR_a==1)
    {
    digitalWrite(5,HIGH);
    }
    else

digitalWrite(5,LOW);

}

bool varR_b = 0;
if(myGizwits.hasBeenSet(EVENT_b))
{
myGizwits.read(EVENT_b,&varR_b);//Address for storing data
if(varR_b==1)
{
digitalWrite(9,HIGH);
}
else

digitalWrite(9,LOW);
}

int val = 0;
// reset
Wire.beginTransmission(ADDR);
Wire.write(0b00000111);
Wire.endTransmission();
digitalWrite(13, LOW);
delay(100);
Wire.beginTransmission(ADDR);
Wire.write(0b00100000);
Wire.endTransmission();
// typical read delay 120ms
delay(120);
Wire.requestFrom(ADDR, 2); // 2byte every time
for (val=0; Wire.available()>=1; ) {
char c = Wire.read();
//Serial.println(c, HEX);
val = (val << 8) + (c & 0xFF);
}
val = val / 1.2;
Serial.print("lx: ");
Serial.println(val);
Serial.println(“OK”);
digitalWrite(13, HIGH);
delay(100);

int chk = DHT11.read(DHT11PIN);
long varW_wendu = (float)DHT11.temperature;//Add Sensor Data Collection
myGizwits.write(VALUE_wendu, varW_wendu);
long varW_shidu = (float)DHT11.humidity;//Add Sensor Data Collection
myGizwits.write(VALUE_shidu, varW_shidu);
long varW_guangzhao =val;//Add Sensor Data Collection
myGizwits.write(VALUE_guangzhao, varW_guangzhao);

myGizwits.process();
}

基于 机智云 只做了部分修改 拆简单的
智能农场 阳光温湿度 检测 远程控制开关 机智云_第1张图片

你可能感兴趣的:(机智云)