7927浏览
查看: 7927|回复: 6

[项目] 关于Arduino Wido上传数据到Yeelink的问题请教各位大神

[复制链接]
现在就是想把DHT11温湿度传感器的温度与湿度通过wifi上传到yeelink上,但例子里给出的代码只能上传一个温度或一个湿度值,并不能把温湿度同时上传到yeelink上监测。我想知道怎么改程序才能让温湿度值同时上传上去。代码如下:

[mw_shl_code=applescript,true]#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <dht11.h>
dht11 DHT;
#define DHT11_PIN 0
#define Wido_IRQ   7
#define Wido_VBAT  5
#define Wido_CS    10

Adafruit_CC3000 Wido = Adafruit_CC3000(Wido_CS, Wido_IRQ, Wido_VBAT,
SPI_CLOCK_DIVIDER); // you can change this clock speed
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define WLAN_SECURITY   WLAN_SEC_WPA2


#define WLAN_SSID       "ChinaNGB-Yd4fWw"           // cannot be longer than 32 characters!
#define WLAN_PASS       "arrd2472"          // For connecting router or AP, don't forget to set the SSID and password here!!


#define TCP_TIMEOUT      3000
//#define CC3000_TINY_DRIVER

#define WEBSITE  "api.yeelink.net"
#define API_key  "87c0a2706cf9f5f5062a6ef3cb7b8ffc"  // Update Your API Key. To get your API Key, please check the link below
                                                     // http://www.yeelink.net/user/user_profile

void setup(){

  Serial.begin(115200);
  Serial.println(F("Hello, CC3000!\n"));

  /* Initialise the module */
  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);

  /* NOTE: Secure connections are not available in 'Tiny' mode!
   By default connectToAP will retry indefinitely, however you can pass an
   optional maximum number of retries (greater than zero) as the fourth parameter.
   */
  if (!Wido.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
    Serial.println(F("Failed!"));
    while(1);
  }

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

  /* Wait for DHCP to complete */
  Serial.println(F("Request DHCP"));
  while (!Wido.checkDHCP())
  {
    delay(100); // ToDo: Insert a DHCP timeout!
  }  

}

uint32_t ip = 0;


void loop(){
  
  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))  {    //  Get the server IP address based on the domain name
        Serial.println(F("Couldn't resolve!"));
      }
      delay(500);
    }  
    Wido.printIPdotsRev(ip);
    Serial.println(F(""));
   
    // Connect to the Yeelink Server
    WidoClient = Wido.connectTCP(ip, 80);          // Try to connect cloud server
  }
  
  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(DHT.temperature,httpPackage+strlen(httpPackage),10);          // push the data(temp) to the http data package
    strcat(httpPackage,"}");
   
   
   // strcat(httpPackage,"{\"value\":");这三行是湿度的
    //itoa(DHT.humidity,httpPackage+strlen(httpPackage),10);          // push the data(temp) to the http data package
    //strcat(httpPackage,"}");

   
    length = strlen(httpPackage);                           // get the length of data package
    itoa(length,lengthstr,10);                              // convert int to char array for posting
    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("356326/sensor/403687/datapoints"));  //Please change your device ID and sensor ID here, after creating
   
   // WidoClient.fastrprintln(F("POST /v1.0/device/"));湿度传感器
   // WidoClient.fastrprint(F("356326/sensor/403688/datapoints"));  湿度传感器   
                                                //Please check the link: http://www.yeelink.net/user/devices
                                                           //The example URL: http://api.yeelink.net/v1.0/device/100/sensor/20/datapoints
    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."));
   
    /********** Get the http page feedback ***********/
   
    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);             // Wait for 1s to finish posting the data stream
    WidoClient.close();      // Close the service connection
  
    RetryMillis = millis();  // Reset the timer stamp for applying the connection with the service
  }

  if(millis() - sensortStamp > 2000){
    sensortStamp = millis();
    // read the LM35 sensor value and convert to the degrees every 100ms.
    setDHT11();
}
}
   void setDHT11(){
    int chk;
  
  chk = DHT.read(DHT11_PIN);    // READ DATA
  switch (chk){
    case DHTLIB_OK:  
                Serial.print("OK,\t");
                break;
    case DHTLIB_ERROR_CHECKSUM:
                Serial.print("Checksum error,\t");
                break;
    case DHTLIB_ERROR_TIMEOUT:
                Serial.print("Time out error,\t");
                break;
    default:
                Serial.print("Unknown error,\t");
                break;
  }
// DISPLAT DATA
  Serial.print(DHT.humidity);
  Serial.print(",\t");

  Serial.println(DHT.temperature);

  delay(1000);
    }

  [/mw_shl_code]

吹口琴的钢铁侠  初级技匠

发表于 2017-4-11 11:58:50

好久没用yeelink了,它支持一个数据点存两个数据吗?
不能的话你可能要建两个数据点来上传了
回复

使用道具 举报

songoku  见习技师
 楼主|

发表于 2017-4-11 13:18:25

吹口琴的钢铁侠 发表于 2017-4-11 11:58
好久没用yeelink了,它支持一个数据点存两个数据吗?
不能的话你可能要建两个数据点来上传了 ...

那请问下怎么建立两个数据点啊? 还有你是用什么平台的啊?
回复

使用道具 举报

Grey  中级技匠

发表于 2017-4-11 17:13:44

之前写过一篇W5500的上传温湿度的帖子,你可以借鉴一下https://mc.dfrobot.com.cn/forum. ... p;highlight=yeelink
加了一个time变量,来同时执行两个传感器的数据上传
回复

使用道具 举报

吹口琴的钢铁侠  初级技匠

发表于 2017-4-13 11:03:36

songoku 发表于 2017-4-11 13:18
那请问下怎么建立两个数据点啊? 还有你是用什么平台的啊?

我可以给你推荐一个稍微麻烦一点但很自由的方案
http://www.arduino.cn/forum.php? ... amp;highlight=ulink
回复

使用道具 举报

xbox  学徒

发表于 2017-5-20 22:51:19

请问有没有wido的库,现在找不到了
回复

使用道具 举报

O_oYYQ  高级技师

发表于 2017-5-22 15:27:47

xbox 发表于 2017-5-20 22:51
请问有没有wido的库,现在找不到了

wido用的是<Adafruit_CC3000.h>的库呀
回复

使用道具 举报

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

本版积分规则

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

硬件清单

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

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

mail