akzono 发表于 2014-12-21 14:55:13

新手用yeelink上传温度数据,但是程序老是提示有错,还望大神帮忙解决,万分感谢

程序如下,用的是DS18B20老是提示那个sendData()没有声明
#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);
    sensors.begin();
    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)) {
   sensors.requestTemperatures();

float tempc=sensors.getTempCByIndex(0);
Serial.println(tempc);
delay(2000);
void sendData(tempc,TEMPERATURE_SENSORID);
lastConnected = client.connected();
}
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;
}

darkorigin 发表于 2014-12-21 17:53:13

少了个“}”
你数下你从“LOOP()”后面的{和}的匹配状况
C语言虽然不讲究代码的完全工整
但是合适的变量名称,简单的条理,工整的书写 便于别人看和自己调试
你代码很多“{”是跟在“()”后面 这样自己容易看不清,一般放下一行会比较好
少写了个“} ”这样“LOOP()”一直包含到了代码末尾。。。
我放到IDE里面验证也发现这个问题是少个“}”

suoma 发表于 2014-12-21 21:26:16

检查程序。。。

akzono 发表于 2015-1-16 13:36:13

darkorigin 发表于 2014-12-21 17:53 static/image/common/back.gif
少了个“}”
你数下你从“LOOP()”后面的{和}的匹配状况
C语言虽然不讲究代码的完全工整


谢谢。前面期末考试没有做,我改了,还是不行,我还买了一本书来看,问题还是没有解决
还望指点

简单侣图 发表于 2015-1-22 16:18:13


//程序如下,用的是DS18B20老是提示那个sendData()没有声明
#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);
    sensors.begin();
    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))
{
   sensors.requestTemperatures();

    float tempc=sensors.getTempCByIndex(0);
    Serial.println(tempc);
    delay(2000);
    //void sendData(tempc,TEMPERATURE_SENSORID);
    lastConnected = client.connected();
}
}

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;
}这样是可以运行的,但是我 client.print(SENSORID);注释了这一句,报错说没有修饰:Q
页: [1]
查看完整版本: 新手用yeelink上传温度数据,但是程序老是提示有错,还望大神帮忙解决,万分感谢