【arduino for esp8266】ESP8266上传温湿度(DHT11)给云服务器
本帖最后由 chenyuechi 于 2017-7-18 11:04 编辑接下来,我就带着大家试着用DHT11检测温湿度并用ESP8266上传到云服务器
大家先根据这个帖子搭好开发环境----基于esp8266的智能家居控制系统-基础篇1介绍arduino ide for esp8266
这里我用的云服务器是酱菜创客(www.jcckiot.com)大家可以去注册申请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脚
代码:
#include <ESP8266WiFi.h>
WiFiClient client;
const char *ssid = "****";//这里是我的wifi,你使用时修改为你要连接的wifi ssid
const char *password = "*********";//你要连接的wifi密码
const char *host = "www.jcckiot.com";//修改为手机的的tcpServer服务端的IP地址,即手机在路由器上的ip
const int httpPort =8266;
#define pin 2
//voidkeeponline();
//void wificonnect();
void wenshidu();
void wenshidu1();
int n=0;
void setup() {
Serial.begin(115200);
delay(10);
//pinMode(relay1,INPUT);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
//Serial.println(ssid);
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);
client.write("mode=bind&apikey=你自己的apikey&data={ck001000bind}\r\n"); //修改成自己的apikey
delay(100);
}
void loop() {
while(client.available()){
String line = client.readStringUntil('\n');
Serial.println(line);}
n++;
if(n%1==0)
{
wenshidu();
n=0;
}
delay(3000);
client.write("mode=up&apikey=你的apikey&data={ck001002life\r\n");//修改成自己的apikey
delay(3000);
}
void wenshidu()
{
int temp;//温度
int humi;//湿度
int tol;//校对码
int j;
unsigned int loopCnt;
int chr = {0};//创建数字数组,用来存放40个bit
unsigned long time1;
bgn:
delay(2000);
//设置2号接口模式为:输出
//输出低电平20ms(>18ms)
//输出高电平40μs
pinMode(pin,OUTPUT);
digitalWrite(pin,LOW);
delay(20);
digitalWrite(pin,HIGH);
delayMicroseconds(40);
digitalWrite(pin,LOW);
//设置2号接口模式:输入
pinMode(pin,INPUT);
//高电平响应信号
loopCnt=10000;
while(digitalRead(pin) != HIGH)
{
if(loopCnt-- == 0)
{
//如果长时间不返回高电平,输出个提示,重头开始。
Serial.println("HIGH");
goto bgn;
}
}
//低电平响应信号
loopCnt=30000;
while(digitalRead(pin) != LOW)
{
if(loopCnt-- == 0)
{
//如果长时间不返回低电平,输出个提示,重头开始。
Serial.println("LOW");
goto bgn;
}
}
//开始读取bit1-40的数值
for(int i=0;i<40;i++)
{
while(digitalRead(pin) == LOW)
{}
//当出现高电平时,记下时间“time”
time1 = micros();
while(digitalRead(pin) == HIGH)
{}
//当出现低电平,记下时间,再减去刚才储存的time
//得出的值若大于50μs,则为‘1’,否则为‘0’
//并储存到数组里去
if (micros() - time1>50)
{
chr=1;
}else{
chr=0;
}
}
//湿度,8位的bit,转换为数值
humi=chr*128+chr*64+chr*32+chr*16+chr*8+chr*4+chr*2+chr;
//温度,8位的bit,转换为数值
temp=chr*128+chr*64+chr*32+chr*16+chr*8+chr*4+chr*2+chr;
//校对码,8位的bit,转换为数值
//tol=chr*128+chr*64+chr*32+chr*16+chr*8+chr*4+chr*2+chr;
//输出:温度、湿度、校对码
Serial.print("temp:");
Serial.println(temp);
String str1="mode=up&apikey=674c0a5616458361&data={ck001002";
str1+=temp;
str1+=".";
str1+=humi;
str1+="}\r\n";
client.print(str1);
Serial.print(str1);
Serial.print("humi:");
Serial.println(humi);
} 我可不可以直接给esp8266连上加速度传感器,然后上传数据给arduino开发板,让开发板处理信号?
我本来打算用arduino nano接MPU-6050和esp8266,不过看资料8266自带MCU,而且楼主这样做了,我也想试试
盗版智能创客的 LOVE_I 发表于 2016-12-11 12:43
盗版智能创客的
是的,改智能创客的:lol 丶陪妳看流星 发表于 2016-12-10 22:27
我可不可以直接给esp8266连上加速度传感器,然后上传数据给arduino开发板,让开发板处理信号?
我本来打算 ...
可以试一下噢,加油:victory: 优化得不错喔!!学习了!谢谢 您好,对您的设计很感兴趣,可否联系一下?我的QQ:470053905 楼主你用的是那个云端? 现在还有没有免费的物联平台?
页:
[1]