6162浏览
查看: 6162|回复: 1

[求助] 18b20温度传到yeelink,有没有解释,和Adafruit_CC3000.h的具体内容

[复制链接]
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>

#define Wido_IRQ   7        //引脚定义,这三个引脚在wido中已经被定义死了,没必要改
#define Wido_VBAT  5
#define Wido_CS    10

#define ONE_WIRE_BUS 3        //定义温度传感器引脚
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

Adafruit_CC3000 Wido = Adafruit_CC3000(Wido_CS, Wido_IRQ, Wido_VBAT,
SPI_CLOCK_DIVIDER);                                       //定义cc3000的名字和引脚
#define WLAN_SECURITY   WLAN_SEC_WPA2                    //加密方式


#define WLAN_SSID       "Xiaomi_8898"         //无线网名字
#define WLAN_PASS       "anyida2015"          //密码


#define TCP_TIMEOUT      3000
//#define CC3000_TINY_DRIVER

#define WEBSITE  "api.yeelink.net"
#define API_key  "1533dc46ece68df8f1524082fad83488"  // Update Your API Key. To get your API Key, please check the link below
                                                     // http://www.yeelink.net/user/user_profile
double temp;
void setup(){
  temp=sensors.getTempCByIndex(0);                  //获取温度值,把它赋给temp
  sensors.begin();                                  //初始化总线
  Serial.begin(115200);
  Serial.println(F("Hello, CC3000!\n"));

  /* wido初始化,如果wido初始化失败,那么串口返回信息进入死循环 */
  Serial.println(F("\nInitialising the CC3000 ..."));
  if (!Wido.begin())
  {
    Serial.println(F("Unable to initialise the CC3000! Check your wiring?"));
    while(1);
  }

  /* Attempt to connect to an access point */
  char *ssid = WLAN_SSID;             /* Max 32 chars */
  Serial.print(F("\nAttempting to connect to "));
  Serial.println(ssid);

  /* 此函数用来连接WiFi的名字如果连接失败,返回failed,进入死循环*/
  if (!Wido.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
    Serial.println(F("Failed!"));
    while(1);
  }

  Serial.println(F("Connected!"));

  /* 获取动态IP地址的,是一个Boolean的函数 */
  Serial.println(F("Request DHCP"));
  while (!Wido.checkDHCP())
  {
    delay(100); // ToDo: Insert a DHCP timeout!
  }  

}

uint32_t ip = 0;


void loop(){
  sensors.requestTemperatures();
  temp=sensors.getTempCByIndex(0);
  static Adafruit_CC3000_Client WidoClient;         //取名字
  static unsigned long RetryMillis = 0;
  static unsigned long uploadtStamp = 0;
  static unsigned long sensortStamp = 0;

  if(!WidoClient.connected() && millis() - RetryMillis > TCP_TIMEOUT){
    // Update the time stamp
    RetryMillis = millis();

    Serial.println(F("Try to connect the cloud server"));
    WidoClient.close();

    // Get Yeelink IP address
    Serial.print(F("api.yeelink.net -> "));
    while  (ip  ==  0)  {
      if  (!Wido.getHostByName(WEBSITE, &ip))  {    // 获取基于域名的服务器IP地址
        Serial.println(F("Couldn't resolve!"));
      }
      delay(500);
    }  
    Wido.printIPdotsRev(ip);
    Serial.println(F(""));

    // Connect to the Yeelink Server
    WidoClient = Wido.connectTCP(ip, 80);          // 尝试连接云服务器
  }

  if(WidoClient.connected() && millis() - uploadtStamp > 10000){
    uploadtStamp = millis();
    // If the device is connected to the cloud server, upload the data every 2000ms.

    // Prepare Http Package for Yeelink & get length
    int length = 0;
    char lengthstr[3];

    // Create Http data package
    char httpPackage[60] = "";

    strcat(httpPackage,"{\"value\":");
    itoa(temp,httpPackage+strlen(httpPackage),10);          //将数据(temp)推到http数据包
    strcat(httpPackage,"}");


   // strcat(httpPackage,"{\"value\":");这三行是湿度的
    //itoa(DHT.humidity,httpPackage+strlen(httpPackage),10);          // 将数据(temp)推到http数据包
    //strcat(httpPackage,"}");


    length = strlen(httpPackage);                           // 获取数据包的长度
    itoa(length,lengthstr,10);                              //将int转换为char数组,用于发布
    Serial.print(F("Length = "));
    Serial.println(length);

    Serial.println(F("Connected to Yeelink server."));

    // Send headers
    Serial.print(F("Sending headers"));

    WidoClient.fastrprint(F("POST /v1.0/device/"));
    WidoClient.fastrprint(F("359840/sensor/410293/datapoints"));  //请在创建后更改您的设备ID和传感器ID

    WidoClient.fastrprintln(F(" HTTP/1.1"));
    Serial.print(F("."));

    WidoClient.fastrprintln(F("Host: api.yeelink.net"));
    Serial.print(F("."));

    WidoClient.fastrprint(F("U-ApiKey: "));
    WidoClient.fastrprintln(API_key);
    Serial.print(F("."));

    WidoClient.fastrprint("Content-Length: ");
    WidoClient.fastrprintln(lengthstr);
    WidoClient.fastrprintln("");
    Serial.print(F("."));

    Serial.println(F(" done."));

    // Send data
    Serial.print(F("Sending data"));
    WidoClient.fastrprintln(httpPackage);

    Serial.println(F(" done."));

    /********** 获取http页面反馈 ***********/

    unsigned long rTimer = millis();
    Serial.println(F("Reading Cloud Response!!!\r\n"));
    while (millis() - rTimer < 10000) {
      while (WidoClient.connected() && WidoClient.available()) {
        char c = WidoClient.read();
        Serial.print(c);
      }
    }
    delay(1000);             // 等待1s结束数据流
    WidoClient.close();      // 关闭服务连接

    RetryMillis = millis();  //重置用于应用与服务的连接的计时器戳记
  }
}

20060606  高级技匠

发表于 2020-8-13 05:23:36

请教一下2个18b20怎么连接到arduino
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail