妈的 这次 接错线了 气死了 找了半天代码的毛病 气人
今天 有个 光照传感器
例子 代码
#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;
}
/**
/**
Wire.beginTransmission(ADDR);
Wire.write(0b00000001);
Wire.endTransmission();
myGizwits.begin();
}
/**
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!”));
}
}
/**
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();
}