maybachwang 发表于 2012-12-12 09:34:01

W5100传送照度值到yeelink出现下面从错误,大家帮忙看看?

大家帮忙看看,为什么找不到网页啊?我用的yeelink的样例,串口输出如下:


Ethernet configuration OK
Sensor value is: 0
connecting...
HTTP/1.1 404 Not Found
Server: nginx
Date: Wed, 12 Dec 2012 19:03:55 GMT
Content-Type: text/html
Content-Length: 162
Connection: close

<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx</center>
</body>
</html>

disconnecting.
Sensor value is: 0
connecting...
HTTP/1.1 404 Not Found
Server: nginx
Date: Wed, 12 Dec 2012 19:04:26 GMT
Content-Type: text/html
Content-Length: 162
Connection: close

zcbzjx 发表于 2012-12-15 16:34:45

请发布源代码。。。光从这个真的没法弄。。。

maybachwang 发表于 2012-12-17 08:40:10

#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#include <math.h>

int BH1750address = 0x23;
byte buff;

// for yeelink api
#define APIKEY         "0**********************e15" // 这里用您的yeelink API key替换
#define DEVICEID       1299 // 这就是您yeelink网站上的设备号,请参见Yeelink平台手册获取
#define SENSORID       1643 // 这个更换成您的传感器号码,如何取得请参阅Yeelinkreplace your sensor ID

// assign a MAC address for the ethernet controller.
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
// initialize the library instance:
EthernetClient client;
//char server[] = "api.yeelink.net";   //yeelink API 的服务器名称
IPAddress server(202,136,60,231);      // 这里填的是yeelink平台的入口服务器IP

unsigned long lastConnectionTime = 0;          // 上次连接到服务器的时间,毫秒
boolean lastConnected = false;               // 上次连接状态
const unsigned long postingInterval = 30*1000; // 默认发送间隔30秒

void setup() {
Wire.begin();
// start serial port:
Serial.begin(57600);
// start the Ethernet connection with DHCP:
if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    for(;;)
      ;
}
else {
    Serial.println("Ethernet configuration OK");
}
}

void loop() {
// if there's incoming data from the net connection.
// send it out the serial port.This is for debugging
// purposes only:
if (client.available()) {
    char c = client.read();
    Serial.print(c);
}

// if there's no net connection, but there was one last time
// through the loop, then stop the client:
if (!client.connected() && lastConnected) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
}

// if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data:
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
    // read sensor data, replace with your code
    int sensorReading = readLightSensor();
    //send data to server
    sendData(sensorReading);
}
// store the state of the connection for next time through
// the loop:
lastConnected = client.connected();
}

// this method makes a HTTP connection to the server:
void sendData(int thisData) {
// if there's a successful connection:
if (client.connect(server, 80)) {
    Serial.println("connecting...");
    // send the HTTP PUT request:
    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: ");

    // calculate the length of the sensor reading in bytes:
    // 8 bytes for {"value":} + number of digits of the data:
    int thisLength = 10 + getLength(thisData);
    client.println(thisLength);

    client.println("Content-Type: application/x-www-form-urlencoded");
    client.println("Connection: close");
    client.println();

    // here's the actual content of the PUT request:
    client.print("{\"value\":");
    client.print(thisData);
    client.println("}");
}
else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
}
   // note the time that the connection was made or attempted:
lastConnectionTime = millis();
}

// This method calculates the number of digits in the
// sensor reading.Since each digit of the ASCII decimal
// representation is a byte, the number of digits equals
// the number of bytes:
int getLength(int someValue) {
// there's at least one byte:
int digits = 1;
// continually divide the value by ten,
// adding one to the digit count for each
// time you divide, until you're at 0:
int dividend = someValue /10;
while (dividend > 0) {
    dividend = dividend /10;
    digits++;
}
// return the number of digits:
return digits;
}

///////////////////////////////////////////////////////////////////////////
// 获取光纤传感器的读书,可以把这部分替换掉,换成你自己的传感器
int readLightSensor()
{
uint16_t val=0;
BH1750_Init(BH1750address);
delay(200);
if(2==BH1750_Read(BH1750address))
{
    val=((buff<<8)|buff)/1.2;
}

Serial.print("Sensor value is: ");
Serial.println((int)val);

return val;
}

int BH1750_Read(int address) //
{
int i=0;
Wire.beginTransmission(address);
Wire.requestFrom(address, 2);
while(Wire.available()) //
{
    buff = Wire.read();// receive one byte
    i++;
}
Wire.endTransmission();
return i;
}

void BH1750_Init(int address)
{
Wire.beginTransmission(address);
Wire.write(0x10);//1lx reolution 120ms
Wire.endTransmission();
}

radiocat 发表于 2013-1-1 00:12:59

我也遇到同样的问题,头痛

radiocat 发表于 2013-1-1 18:53:05

返回 404错误,请检查一下连接的地址,HTTP1.1后面,

radiocat 发表于 2013-1-1 18:58:21

后来我仔细检查了一下程序,发现从网上拷下来的程序里面有问题,是下面二行代码:
//char server[] = "api.yeelink.net";   //yeelink API 的服务器名称
IPAddress server(202,136,60,231);      // 这里填的是yeelink平台的入口服务器IP

改正:
1,把第一行前面的二个斜杠去掉,
2,删去第二行,
然后重新编译、上载到Arduino板子运行就可以了。
另外,建议把mac地址修改成另外的值更好。

radiocat 发表于 2013-1-1 20:48:55

程序运行正常时,串口监视窗内显示信息如下:
Ethernet configuration OK
Sensor value is: 2
yeelink:2
connection failed

disconnecting.
Sensor value is: 3
yeelink:3
connecting...
HTTP/1.1 200 OK
Server: nginx/1.0.14
Date: Tue, 01 Jan 2013 10:39:39 GMT
Content-Type: text/html
Connection: close
X-Powered-By: PHP/5.3.10
Set-Cookie: CAKEPHP=fgu5kdgdphcu9030ph189tft74; expires=Wed, 09-Jan-2013 18:39:39 GMT; path=/
P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
Vary: Accept-Encoding
Content-Length: 0
页: [1]
查看完整版本: W5100传送照度值到yeelink出现下面从错误,大家帮忙看看?