用ESP8266传递BMP180的温度和气压到贝壳云

作者:wony366 | 更新时间:2016-04-07 | 浏览量:3160

基本接线:


BMP180           esp8266 

vcc     3.3V   

gnd     gnd

scl              GPIO14

sda              GPIO2



ESP8266通过TTL 接电脑,不再赘述。


以下是代码:


#include <ESP8266WiFi.h> 

#include <SFE_BMP180.h>

#include <Wire.h>

 

WiFiClient client;


const char *ssid     = "xxx";//这里是我的wifi,你使用时修改为你要连接的wifi ssid

const char *password = "xxxx";//你要连接的wifi密码

const char *host = "121.42.180.30";//贝壳云地址

char valueread;


 SFE_BMP180 pressure;// 创建一个气压计对象 

double baseline; // 基准气压


void setup()

{

  Serial.begin(115200);

  Wire.begin(2,14); //定义I2C接口,并启动,ESP8266-12E的 2 和 14 口是原本定义为I2C接口,其他模块查看手册,用于读取BMP180的参数


//下面的代码是确保WIFI连接正确,并打印登录路由器信息

  Serial.print("Connecting to ");

  Serial.println(ssid);  

  WiFi.begin(ssid, password);  

  while (WiFi.status() != WL_CONNECTED) {

    delay(500);

    Serial.print(".");

  }

  Serial.println("");

  Serial.println("WiFi connected");  

  Serial.println("IP address: ");

  Serial.println(WiFi.localIP());

//下面的代码是连接服务器

  const int httpPort =8181;

  client.connect(host, httpPort);

  delay(2000); 

  Serial.println("connecting to host");

  while (!client.connect(host, httpPort)) 

    {

    Serial.println("..");

    delay(500);

    }   

  Serial.print("connecting to ");

  Serial.println(host);

  //下面的代码是用自己的ID 和 KEY 登录服务器,确保服务器返回正确登录信息,否则一直重试登录,直到出现正确的返回信息

  

  client.write("{\"M\":\"checkin\",\"ID\":\"138\",\"K\":\"123456\"}\r\n");//登陆设备,修改成自己的ID和key


  Serial.print("waitting for host answer");

  delay(500);   

    String line = client.readStringUntil('\r');

     Serial.print(line);

    valueread=line[28];//修改自己成发送的指令的位置,我这里指令位置在是28位,此方法是参考前面网友的

    Serial.println(valueread);//此处打印的是欢迎信息,并不代表登录成功

    while(valueread !='M')    // 此处是验证登录成功后返回的信息

    {

     delay(500);

      client.write("{\"M\":\"checkin\",\"ID\":\"138\",\"K\":\"123456\"}\r\n");//登陆设备,修改成自己的ID和key    

      Serial.print(".");

      String line = client.readStringUntil('\r');

     Serial.print(line);

      valueread=line[2];//此处打印的是登录成功信息,checkinok ,这是返回字段,我偷懒,随便取了一位值,你可以按需取

    Serial.println(valueread);

     }    

  delay(100);


  

 //下面是传感器相关,无关云平台

  // 初始化传感器

  if (pressure.begin())

    Serial.println("BMP180 init success");

  else

  {

    // 糟糕,气压计出问题了,多半是连线有问题

    Serial.println("BMP180 init fail (disconnected?)\n\n");

    while(1); // 暂停

  }

  }

   


 

void loop()

{  

  baseline = getP(); //获得基准气压

  Serial.print("baseline pressure: ");

  Serial.print(baseline);

  Serial.println(" hPa");   

  

  double a,p,t;

  p = getP();// 获得一个气压值

  a = pressure.altitude(p,baseline);//获得基于基准气压的高度值

 

  Serial.print("relative altitude: ");

  if (a >= 0.0) Serial.print(" "); // 调整正数显示格式

  Serial.print(a,1);

  Serial.print(" meters ");  

 

  t = getT();// 获得一个温度值

  Serial.print("temperature: ");

  Serial.print(t,1);

  Serial.print(t);

  Serial.println(" degrees");  


 update1(138,119,166,167,p,t,a);//向服务器发数据,138是设备ID,119 166 167 是数据接口,p t a  是传感器参数


 delay(5000);//刷新率5秒,太快,服务器会出错


  client.write("{\"M\":\"say\",\"ID\":\"138\",\"C\":\"123456\"}\r\n"); //保持心跳,这是我的ID 和key,自己修改 ,以防掉线

}

 

 //取得BMP180气压的函数

double getP()

{

  char status;

  double T,P,p0,a;

  // You must first get a temperature measurement to perform a pressure reading.

  // Start a temperature measurement:

  // If request is successful, the number of ms to wait is returned.

  // If request is unsuccessful, 0 is returned.

  status = pressure.startTemperature();

  if (status != 0)

  {

    // Wait for the measurement to complete:

    delay(status);

    // Retrieve the completed temperature measurement:

    // Note that the measurement is stored in the variable T.

    // Use '&T' to provide the address of T to the function.

    // Function returns 1 if successful, 0 if failure.

 

    status = pressure.getTemperature(T);

    if (status != 0)

    {

      // Start a pressure measurement:

      // The parameter is the oversampling setting, from 0 to 3 (highest res, longest wait).

      // If request is successful, the number of ms to wait is returned.

      // If request is unsuccessful, 0 is returned.

      status = pressure.startPressure(3);

      if (status != 0)

      {

        // Wait for the measurement to complete:

        delay(status);

        // Retrieve the completed pressure measurement:

        // Note that the measurement is stored in the variable P.

        // Use '&P' to provide the address of P.

        // Note also that the function requires the previous temperature measurement (T).

        // (If temperature is stable, you can do one temperature measurement for a number of pressure measurements.)

        // Function returns 1 if successful, 0 if failure.

        status = pressure.getPressure(P,T);

        if (status != 0)

        {

          return P;

        }

        else Serial.println("error retrieving pressure measurement\n");

      }

      else Serial.println("error starting pressure measurement\n");

    }

    else Serial.println("error retrieving temperature measurement\n");

  }

  else Serial.println("error starting temperature measurement\n");

}


  //取得BMP180温度的函数

double getT()

{

  char status;

  double T,p0;

  status = pressure.startTemperature();

  if (status != 0)

  {

    delay(status);

    status = pressure.getTemperature(T);

    if (status != 0)

    {

      status = pressure.startPressure(3);

      return T;

    }

    else Serial.println("error retrieving temperature measurement\n");

  }

  else Serial.println("error starting temperature measurement\n");

}


void update1(int did, int inputid1, int inputid2,int inputid3,float value1 ,float value2,float value3) // 定义一次传递3个参数的函数,该函数是借鉴 网友 在路上 的劳动成果

{

String str1="{\"M\":\"update\",\"ID\":\"";

str1+=did;

str1+="\",\"V\":{\"";

str1+=inputid1;

str1+="\":\"";

str1+=value1;

str1+="\",\"" ;

str1+=inputid2;

str1+="\":\"";

str1+=value2;

str1+="\",\"" ;

str1+=inputid3;

str1+="\":\"";

str1+=value3;

str1+="\"" ;

str1+="}}\n";

 client.print(str1);  

//下面是串口打印 传递到服务器的信息,以便调试

  Serial.print("update:");   

  Serial.print(inputid1);   

  Serial.print("->");   

  Serial.println(value1);   

   Serial.print("update:");   

  Serial.print(inputid2);   

  Serial.print("->");   

  Serial.println(value2); 

   Serial.print("update:");   

  Serial.print(inputid3);   

  Serial.print("->");   

  Serial.println(value3); 

}



数据见公开页面 :http://www.bigiot.net/chart/138.html


评论:共2条

贝壳物联 评论于:2016-04-07 20:30:48
多谢分享,可以把代码部分高亮显示,编辑话题,选中代码,代码语音选择C语言。
climate 评论于:2018-04-28 14:19:14
库文件怎么获得呢?
返回顶部