|
|
完整的程序代码如下:
#include <Ethernet.h>
#include <WiFi.h>
#include <SPI.h>
#include <http_client.h>
#include <yl_data_point.h>
#include <yl_device.h>
#include <yl_w5100_client.h>
#include <yl_wifi_client.h>
#include <yl_messenger.h>
#include <yl_sensor.h>
#include <yl_value_data_point.h>
#include <yl_sensor.h>
#include <idDHT11.h>
int idDHT11pin = 2; //Digital pin for comunications
int idDHT11intNumber = 0; //interrupt number (must be the one that use the previus defined pin (see table above)
//declaration
void dht11_wrapper(); // must be declared before the lib initialization
// Lib instantiate
idDHT11 DHT11(idDHT11pin,idDHT11intNumber,dht11_wrapper);
// 配置你的yeelink api
#define APIKEY "e42010328eb6accf419129805d1e53b9" // 输入你的 yeelink api key
#define DEVICEID 4212 // 输入你的设备ID
#define TEMPERATURE_SENSORID 6111 // 输入你的温度传感器ID
#define HUMIDITY_SENSORID 6186 // 输入你的湿度传感器ID
yl_device mydev(DEVICEID);
yl_sensor hum(HUMIDITY_SENSORID, &mydev);
yl_w5100_client client;
yl_messenger messenger(&client, APIKEY, "api.yeelink.net");
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // 为Ethernet分配MAC地址
byte ip[] = { 192, 168, 0, 144 };
void setup()
{
Serial.begin(9600); //开启串口
// 开启Ethernet的连接
Ethernet.begin(mac, ip);
// 打印DHT11的库文件版本
Serial.print("LIBRARY VERSION: ");
Serial.println(IDDHT11LIB_VERSION);
Serial.println("---------------");
}
// This wrapper is in charge of calling
// mus be defined like this for the lib work
void dht11_wrapper() {
DHT11.isrCallback();
}
void loop()
{
// 分别发送温度、湿度数值
sendData(readSensor(0),HUMIDITY_SENSORID);
}
// 定义接受数据函数
int readSensor(int sensor_id)
{
Serial.println("\n");
// 判断传感器是否正常
Serial.print("Read sensor: ");
int result = DHT11.acquireAndWait();
switch (result)
{
case IDDHTLIB_OK:
Serial.println("OK");
break;
case IDDHTLIB_ERROR_CHECKSUM:
Serial.println("Error\n\r\tChecksum error");
break;
case IDDHTLIB_ERROR_TIMEOUT:
Serial.println("Error\n\r\tTime out error");
break;
case IDDHTLIB_ERROR_ACQUIRING:
Serial.println("Error\n\r\tAcquiring");
break;
case IDDHTLIB_ERROR_DELTA:
Serial.println("Error\n\r\tDelta time to small");
break;
case IDDHTLIB_ERROR_NOTSTARTED:
Serial.println("Error\n\r\tNot started");
break;
default:
Serial.println("Unknown error");
break;
}
// 打印温度、湿度
if(sensor_id == 0)
{
Serial.print("Humidity (%): ");
Serial.println(DHT11.getHumidity(), 2);
return DHT11.getHumidity();
}
else
{
Serial.print("Temperature (oC): ");
Serial.println(DHT11.getCelsius(), 2);
return DHT11.getCelsius();
}
}
// 定义发送数据函数
void sendData(int thisData, int SENSORID)
{
Serial.print("thisData is: ");
Serial.println(thisData);
Serial.print("SENSORID is: ");
Serial.println(SENSORID);
yl_value_data_point dp(thisData);
hum.single_post(messenger, dp);
delay(1000 * 30);
}
----------------------------------------------------------------------------------------------
在com窗口里 输出的信息如下:
LIBRARY VERSION: 0.1
---------------
Read sensor: OK
Humidity (%): 36.00
thisData is: 36
SENSORID is: 6186
Read sensor: OK
Humidity (%): 36.00
thisData is: 36
SENSORID is: 6186
----------------------------------------------------------------------------------------
大家帮忙看看是什么原因
|
|