自己改了一点,但是错的很离谱,新手不是很明白,求大神指导,万分感谢
#include <SPI.h>
#include <Ethernet.h>
#include<OneWire.h>
#include <math.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 3
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
#define APIKEY "xxxxxxxx" // 输入你的 yeelink api key
#define DEVICEID ""// 输入你的设备ID
#define TEMPERATURE_SENSORID ""//输入你的温度传感器ID
byte mac[] = { 0x00, 0x1D, 0x72, 0x82, 0x35, 0x9D}; // 为Ethernet分配MAC地址
// 初始化库
EthernetClient client;
char server[] = "api.yeelink.net"; // yeelink API的地址
unsigned long lastConnectionTime = 0; // 初始化连接时间
boolean lastConnected = false;
const unsigned long postingInterval = 30*1000;
void setup()
{
Serial.begin(9600);
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
}
else {
Serial.println("Ethernet configuration OK");
}
Serial.println();
}
void loop() {
if (client.available()) {
char c = client.read();
Serial.print(c);
}
if (!client.connected() && lastConnected) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
sendData(readSensor(TEMPERATURE_SENSORID), TEMPERATURE_SENSORID);
lastConnected = client.connected();
}
float readSensor(int sensor_id){
sensors.requestTemperatures();
float temp=sensors.getTempCByIndex(0);
delay(2000);
return temp;
}
}
void sendData(float thisData);
{
if (client.connect(server, 80)) {
Serial.println("connecting...");
// 发送HTTP PUT请求
client.print("POST /v1.0/device/");
client.print(DEVICEID);
client.print("/sensor/");
client.print(SENSORID);
client.print("/datapoints");
client.println(" HTTP/1.1");
client.println("Host: api.yeelink.net");
client.print("Accept: *");
client.print("/");
client.println("*");
client.print("U-ApiKey: ");
client.println(APIKEY);
client.print("Content-Length: ");
int thisLength = 10 + getLength(thisData);
client.println(thisLength);
client.println("Content-Type: application/x-www-form-urlencoded");
client.println("Connection: close");
client.println();
client.print("{\"value\":");
client.print(thisData);
client.println("}");
}
else {
// 如果连接失败
Serial.println("connection failed");
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
lastConnectionTime = millis();
}
int getLength(int someValue) {
int digits = 1;
int dividend = someValue /10;
while (dividend > 0) {
dividend = dividend /10;
digits++;
}
return digits;
}
#include <SPI.h>
#include <Ethernet.h>
#include<OneWire.h>
#include <math.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 3
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
#define APIKEY "xxxxxxxx" // 输入你的 yeelink api key
#define DEVICEID ""// 输入你的设备ID
#define TEMPERATURE_SENSORID ""//输入你的温度传感器ID
byte mac[] = { 0x00, 0x1D, 0x72, 0x82, 0x35, 0x9D}; // 为Ethernet分配MAC地址
// 初始化库
EthernetClient client;
char server[] = "api.yeelink.net"; // yeelink API的地址
unsigned long lastConnectionTime = 0; // 初始化连接时间
boolean lastConnected = false;
const unsigned long postingInterval = 30*1000;
void setup()
{
Serial.begin(9600);
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
}
else {
Serial.println("Ethernet configuration OK");
}
Serial.println();
}
void loop() {
if (client.available()) {
char c = client.read();
Serial.print(c);
}
if (!client.connected() && lastConnected) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
sendData(readSensor(TEMPERATURE_SENSORID), TEMPERATURE_SENSORID);
lastConnected = client.connected();
}
float readSensor(int sensor_id){
sensors.requestTemperatures();
float temp=sensors.getTempCByIndex(0);
delay(2000);
return temp;
}
}
void sendData(float thisData);
{
if (client.connect(server, 80)) {
Serial.println("connecting...");
// 发送HTTP PUT请求
client.print("POST /v1.0/device/");
client.print(DEVICEID);
client.print("/sensor/");
client.print(SENSORID);
client.print("/datapoints");
client.println(" HTTP/1.1");
client.println("Host: api.yeelink.net");
client.print("Accept: *");
client.print("/");
client.println("*");
client.print("U-ApiKey: ");
client.println(APIKEY);
client.print("Content-Length: ");
int thisLength = 10 + getLength(thisData);
client.println(thisLength);
client.println("Content-Type: application/x-www-form-urlencoded");
client.println("Connection: close");
client.println();
client.print("{\"value\":");
client.print(thisData);
client.println("}");
}
else {
// 如果连接失败
Serial.println("connection failed");
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
lastConnectionTime = millis();
}
int getLength(int someValue) {
int digits = 1;
int dividend = someValue /10;
while (dividend > 0) {
dividend = dividend /10;
digits++;
}
return digits;
}