|
本帖最后由 504835618 于 2016-12-24 11:03 编辑
首先去酱菜创客(jcck.online)注册申请apikey和添加设备
硬件:
1、ESP8266 一块
2、DHT11 一个
3、导线若干
当然上面三个是最主要的,除了这两个大家还需要准备3.3V电源,因为8266和DHT11的工作电压是3.3V,电源问题应该对大家来说不是难事吧
烧写模式接线方法:(用USB转TTL串口连接模块与PC)
esp8266-01 u转串
VCC-------------3.3
GND------------GND
GPIO0----------GND
CH_PD---------3.3
RX---------------TX
TX---------------Rx
运行模式ESP8266接线:
VCC---------------3.3
CH_PD-----------3.3
GND-------------GND
GPIO2----------DHT11 data脚
软件要求:arduino1.6.5版本以上,可以编译esp8266的就可以了.esp-01选择烧写设备如图
esp-12模块烧写选择如图:
这样基本就可以了
上传到酱菜创客的温湿度如下:
代码来了
///---------------------------------------------
double Fahrenheit(double celsius)
{
return 1.8 * celsius + 32;
} //摄氏温度度转化为华氏温度
double Kelvin(double celsius)
{
return celsius + 273.15;
} //摄氏温度转化为开氏温度
// 露点(点在此温度时,空气饱和并产生露珠)
// 参考: http://wahiduddin.net/calc/density_algorithms.htm
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()
// 参考: http://en.wikipedia.org/wiki/Dew_point
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 <dht11.h>
dht11 DHT11;
//-----------
#include <ESP8266WiFi.h>
WiFiClient client;
const char *ssid = "*****";//这里是我的wifi,你使用时修改为你要连接的wifi ssid
const char *password = "****";//你要连接的wifi密码
const char *host = "jcck.online";
const int httpPort =8266;
String line ="";
int m=0;
#define DHT11PIN 2 //温湿度输入
//------------
void setup()
{
Serial.begin(115200);
Serial.println();
//---------------
pinMode(DHT11PIN,INPUT);
Serial.print("Connecting to ");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
//smartConfig();
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
while (!client.connect(host, httpPort)) {
Serial.println("connection failed");
//return;
}
Serial.print("connecting to ");
Serial.println(host);
delay(150);
client.write("mode=bind&apikey=xxx&data={ck001000bind}\r\n"); //修改成自己的apikey
delay(100);
Serial.println("DHT11 TEST PROGRAM ");
Serial.print("LIBRARY VERSION: ");
Serial.println(DHT11LIB_VERSION);
delay(60000);//1分钟上传一次
}
void loop()
{
Serial.println("\n");
int chk = DHT11.read(DHT11PIN);
Serial.print("Read sensor: ");
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;
}
Serial.print("Humidity (%): ");
Serial.println((float)DHT11.humidity, 2);
Serial.print("Temperature (oC): ");
Serial.println((float)DHT11.temperature, 2);
/*Serial.print("Temperature (oF): ");
Serial.println(Fahrenheit(DHT11.temperature), 2);
Serial.print("Temperature (K): ");
Serial.println(Kelvin(DHT11.temperature), 2);
Serial.print("Dew Point (oC): ");
Serial.println(dewPoint(DHT11.temperature, DHT11.humidity));
Serial.print("Dew PointFast (oC): ");
Serial.println(dewPointFast(DHT11.temperature, DHT11.humidity));/*/
String str1="mode=up&apikey=f5a4a83d00b08870&data={ck001002";
str1+=DHT11.humidity; //湿度
str1+=".";
str1+=DHT11.temperature; //温度
str1+="}\r\n";
client.print(str1);
Serial.print(str1);
String line ="";
m++;
if(m%40==0)
{
client.write("mode=up&apikey=xxx &data={ck002001life}\r\n");//发送心跳消息,xxx修改成你自己的apikey
m=0;
}
delay(2000);
}
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|