回答

收藏

[原创] [原创] 【LinkNode D1开发板】LinkNode D1连接贝壳联网传输数据

#拆解/开源硬件 #拆解/开源硬件 2989 人阅读 | 0 人回复 | 2016-10-08

           LinkNode D1连接贝壳联网传输数据已经实现了,之前的时候测试了开发板和LINKSPRITE IO平台的联网,功能倒是实现了,不过满屏的E文还是不习惯。于是转到贝壳,注册账号测试了下,感觉还是很不错,这里跟大家分享下。
        我选的是BMP180模块,传输气压,温度和海拔的数据,地址在   http://www.bigiot.net/chart/836.html,具体的功能可以看看,以下是图片:





   测试了段时间,感觉还是比较稳定的。
  以下是整体代码:
//vcc     3.3V   
//gnd     gnd
//scl              GPIO14
//sda              GPIO2
#include <ESP8266WiFi.h>
#include <SFE_BMP180.h>
#include <Wire.h>
WiFiClient client;
const char *ssid     = "要连接的wifi ssid";
const char *password = "要连接的wifi密码";
const char *host = "121.42.180.30";
char valueread;
SFE_BMP180 pressure;
double baseline;
void setup()
{
  Serial.begin(115200);
  Wire.begin(2,14);

  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);
   client.write("{\"M\":\"checkin\",\"ID\":\"自己的ID名称\",\"K\":\"自己的KEY\"}\r\n");
  Serial.print("waitting for host answer");
  delay(500);   
    String line = client.readStringUntil('\r');
     Serial.print(line);
    valueread=line[28];//
    Serial.println(valueread);//
    while(valueread !='M')    //
    {
     delay(500);
     client.write("{\"M\":\"checkin\",\"ID\":\"自己的ID名称\",\"K\":\"自己的KEY\"}\r\n");
      Serial.print(".");
      String line = client.readStringUntil('\r');
     Serial.print(line);
      valueread=line[2];
    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(836,774,775,403,p,t,a);
delay(10000);
  client.write("{\"M\":\"say\",\"ID\":\"836\",\"C\":\"b8663c936\"}\r\n");
}
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");
}

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)
{
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);
}
    欢迎同学们互相分享学习讨论!


分享到:
回复

使用道具 举报

您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /3 下一条