【smartconfig】直接用ESP8266上传温湿度给贝壳服务器(不需要arduino)

作者:chenyuechi | 更新时间:2016-05-07 | 浏览量:5153

本设备是不需要arduino等其他MCU,直接一个ESP8266就可以上传温湿度,使用的温湿度模块是DHT11

前提是大家会用arduino IDE给8266烧写程序

不会的话,可以先看一下下面两篇帖子:

http://www.arduino.cn/thread-17895-1-1.html

http://www.arduino.cn/forum.php?mod=viewthread&tid=17896&page=1&extra=#pid148026

#include <ESP8266WiFi.h>
WiFiClient client;
int temp;//温度
int humi;//湿度
int tol;//校对码
int j;
unsigned int loopCnt;
int chr[40] = {0};//创建数字数组,用来存放40个bit
unsigned long time1;
const char *ssid     = "******";//这里是我的wifi,你使用时修改为你要连接的wifi ssid
const char *password = "********";//你要连接的wifi密码
const char *host = "121.42.180.30";//贝壳物联IP
const int httpPort =8181;

#define pin 2//将dht11的data口接在8266的GPIO2上
//char *ssid="";
//char *password="";
/*void smartConfig()
{
  WiFi.mode(WIFI_STA);
  Serial.println("\r\nWait for Smartconfig");
  WiFi.beginSmartConfig();
  while (1)
  {
    Serial.print(".");
    //digitalWrite(relay1, 0);
    //delay(500);
    //digitalWrite(relay1, 1);
    //delay(500);
    if (WiFi.smartConfigDone())
    {
      Serial.println("SmartConfig Success");
      Serial.printf("SSID:%s\r\n", WiFi.SSID().c_str());
      Serial.printf("PSW:%s\r\n", WiFi.psk().c_str());
      strcpy(ssid, WiFi.SSID().c_str());
      strcpy(password, WiFi.psk().c_str());
      break;
    }
  }
}*/  
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;
    delay(1000);
  }
  Serial.print("connecting to ");
  Serial.println(host);
  client.write("{\"M\":\"checkin\",\"ID\":\"**\",\"K\":\"******\"}\r\n");//登陆设备,修改成自己的ID和KEY
  delay(100);
}
void loop()
{
  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[i]=1;
    }else{
      chr[i]=0;
    }
  }
   
//湿度,8位的bit,转换为数值
humi=chr[0]*128+chr[1]*64+chr[2]*32+chr[3]*16+chr[4]*8+chr[5]*4+chr[6]*2+chr[7];
   
//温度,8位的bit,转换为数值
temp=chr[16]*128+chr[17]*64+chr[18]*32+chr[19]*16+chr[20]*8+chr[21]*4+chr[22]*2+chr[23];
  //校对码,8位的bit,转换为数值
//tol=chr[32]*128+chr[33]*64+chr[34]*32+chr[35]*16+chr[36]*8+chr[37]*4+chr[38]*2+chr[39];
//输出:温度、湿度、校对码
  Serial.print("temp:");
  Serial.println(temp);
 
  String str1="{\"M\":\"update\",\"ID\":\"**\",\"V\":{\"**\":\"";//修改成你自己的ID和数据接口ID1存温度
  str1+=temp;
  str1+="\",\"**\":\"";//修改成你自己的数据接口ID2存湿度
  str1+=humi;
  str1+="\"}}\n";
 
 client.print(str1);
   Serial.print(str1);
  Serial.print("humi:");
  Serial.println(humi);
  

while(client.available()){
    String line = client.readStringUntil('\r');
     Serial.print(line);}
delay(3000);
}

广播软件.zip



评论:共16条

贝壳物联 评论于:2016-04-15 19:21:31
不错,学习了。。。
ridxqqqq 评论于:2016-04-18 09:59:28
湿度这块我不太懂 用用DHT22通用吗
chenyuechi 回复于:2016-04-20 12:05:14
回复 @ridxqqqq:好像是通用,我也没试过
simonc 评论于:2016-04-28 20:45:05
为什么我连上路由后把3处的id和key都加上去了,但是网络没有任何显示。还有下面那个广播软件怎么用的,连上同一个路由?
chenyuechi 回复于:2016-05-04 13:46:20
回复 @simonc:手机要连在通过一个路由
simonc 回复于:2016-05-06 20:44:32
回复 @chenyuechi:嗯,我根据你的代码加了一个火焰传感器,但是不能连上wifi了,怎么解决
simonc 回复于:2016-05-06 23:16:37
回复 @chenyuechi:不会返回update等个人信息
chenyuechi 回复于:2016-05-07 21:18:24
回复 @simonc:我改了一下代码,看好多人都不用,不用广播软件了,直接上程序里设置wifi的账号密码
木子李 评论于:2016-05-25 12:44:30
成功了!太厉害了,我之前的接口一直弄不对,用这个就成功了
client.print(str1);这个是上传数据吗?
chenyuechi 回复于:2016-05-25 17:34:33
回复 @木子李:继续加油!
我 姓林 回复于:2017-05-31 16:01:49
回复 @chenyuechi:如果用uno板的话,dht11的data线接uno的D4,那直接把程序的PIN2改成PIN4就可以了吗?还要改别的吗?感觉跟UNO差不多,就是接线不一样
qqrr111 评论于:2017-01-05 22:04:29
const char *host = "121.42.180.30";//贝壳物联IP,这个不好使,改成“www.bigiot.net”,可以用了!!!
评论于:2017-05-14 10:30:09
为什么在loop()函数里,接口2(GPIO2)设置为输出模式后又设置为输入模式呢?
HENRY223 评论于:2018-07-10 11:08:44
连接上过后还没到一天就断线了,不知道断线后怎么自动上线,这是什么原因呢?谢谢解答@chenyuechi
deng 评论于:2020-06-01 14:16:32
我程序下载进去,串口一直显示low诶
deng 评论于:2020-06-01 14:18:21
请问,贝壳物联平台里的接口选择是模拟量还是数字量?
返回顶部