yeelink数据采样频率低,http响应报406错误
本帖最后由 大连林海 于 2015-4-10 22:16 编辑以下是我用DHT11采集数据上传到yeelink的程序,虽然能够上传数据,但是采样频率实在太低。测了几个小时才采到四五个样本点,不知道这是什么原因,这么慢。看了串口发现当采完湿度、温度之后http响应会返回406错误,这个也搞不明白。各位前辈,帮帮忙,看看是什么原因。程序跟代码都附在下面了。
// include the library code:
#include <LiquidCrystal.h>
#include <SPI.h>
#include <Ethernet.h>
#include <dht11.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(9, 8, A0, A1, A2, A3);
dht11 DHT11;
#define DHT11PIN 3 //DHT11 PIN 3 连接UNO 3
#define LIGHTPIN 0 //light sensor > analog pin 0
#define APIKEY "c142a1e19dec73dd5dde48309ad20590" // replace your yeelink api key here
#define DEVICEID 19112 // replace your device ID
#define TEMPERATURE_SENSORID 33275 // temperature
#define HUMIDITY_SENSORID 33276 //humidity
#define LIGHT_SENSORID 33270 //light
// assign a MAC address for the ethernet controller.
byte mac[] = { 0x00, 0x1D, 0x72, 0x82, 0x35, 0x9D};
// initialize the library instance:
EthernetClient client;
char server[] = "api.yeelink.net"; // name address for yeelink API
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 30*1000; // delay between 2 datapoints, 30s
int sensor = 0; //which sesor to read
//0 temperature
//1 humidity
//2 light
void setup()
{
Serial.begin(115200);
// set up the LCD's number of columns and rows:
lcd.begin(16,2);
pinMode(LIGHTPIN,OUTPUT);
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
for(;;)
;
}
else {
Serial.println("Ethernet configuration OK");
}
Serial.println("DHT11 TEST PROGRAM ");
Serial.print("LIBRARY VERSION: ");
Serial.println(DHT11LIB_VERSION);
Serial.println();
}
void loop()
{
// set the cursor to (0,0):
lcd.setCursor(0, 0);
lcd.print("Hum (%):");
delay(500);
// set the cursor to (0,0):
lcd.setCursor(9, 0);
lcd.print(DHT11.humidity);
delay(500);
// set the cursor to (0,0):
lcd.setCursor(0, 1);
lcd.print("Tem (C):");
delay(500);
// set the cursor to (0,0):
lcd.setCursor(9, 1);
lcd.print(DHT11.temperature);
delay(500);
// 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("NET:");
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();
//int tempC = analogRead(0)*500/1024;
//send data to server
switch(sensor){
case 0:
sendData(readSensor(HUMIDITY_SENSORID), HUMIDITY_SENSORID);
break;
case 1:
sendData(readSensor(TEMPERATURE_SENSORID), TEMPERATURE_SENSORID);
break;
case 2:
sendData(readSensor(LIGHT_SENSORID), LIGHT_SENSORID);
break;
}
if(++sensor > 2) sensor = 0;
}
// store the state of the connection for next time through
// the loop:
//
lastConnected = client.connected();
}
int readSensor(long sensor_id){
Serial.println("\n");
if (sensor_id == LIGHT_SENSORID)
{
int v = analogRead(LIGHTPIN);
Serial.print("light:");
Serial.println(v);
return v;
}
int chk = DHT11.read(DHT11PIN);
Serial.print("Read sensor: ");
switch (chk)
{
case DHTLIB_OK:
Serial.println("OK");
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.println("Checksum error");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.println("Time out error");
break;
default:
Serial.println("Unknown error");
break;
}
if(sensor_id == HUMIDITY_SENSORID){
Serial.print("Hum (%): ");
Serial.println(DHT11.humidity);
return DHT11.humidity;
}else{
Serial.print("Tem (C): ");
Serial.println(DHT11.temperature);
return DHT11.temperature;
}
}
void sendData(int thisData, long sensor_id) {
// 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(sensor_id);
client.print("/datapoints");
client.println(" HTTP/1.0");
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;
}
以下为串口调试:
Ethernet configuration OK
DHT11 TEST PROGRAM
LIBRARY VERSION: 0.4.1
Read sensor: OK
Hum (%): 45
connecting...
HTTP/1.1 200 OK
Server: nginx/1.1.19
Date: Fri, 10 Apr 2015 11:16:41 GMT
Content-Type: text/html; charset=UTF-8
Connection: close
X-Powered-By: PHP/5.3.10-1ubuntu3.6
Set-Cookie: CAKEPHP=npjm2qkejj2bv82ev20hbqggt1; expires=Sat, 18-Apr-2015 19:16:41 GMT; path=/
P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
disconnecting.
Read sensor: OK
Tem (C): 19
connecting...
HTTP/1.1 200 OK
Server: nginx/1.1.19
Date: Fri, 10 Apr 2015 11:27:38 GMT
Content-Type: text/html; charset=UTF-8
Connection: close
X-Powered-By: PHP/5.3.10-1ubuntu3.6
Set-Cookie: CAKEPHP=9av3p5l6km31blscqrqol7hfp6; expires=Sat, 18-Apr-2015 19:27:38 GMT; path=/
P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
disconnecting.
light:1023
connecting...
HTTP/1.1 406 Not Acceptable
Server: nginx/1.1.19
Date: Fri, 10 Apr 2015 11:38:33 GMT
Content-Type: text/html; charset=UTF-8
Connection: close
X-Powered-By: PHP/5.3.10-1ubuntu3.6
Set-Cookie: CAKEPHP=022ndq55jch4rptgo6kjhi0ia1; expires=Sat, 18-Apr-2015 19:38:33 GMT; path=/
P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
No access permission to this sensor.
disconnecting.
Read sensor: OK
Hum (%): 42
connecting...
HTTP/1.1 200 OK
Server: nginx/1.1.19
Date: Fri, 10 Apr 2015 11:51:07 GMT
Content-Type: text/html; charset=UTF-8
Connection: close
X-Powered-By: PHP/5.3.10-1ubuntu3.6
Set-Cookie: CAKEPHP=6d77s4pqmdf88cv8r7lae7cqi6; expires=Sat, 18-Apr-2015 19:51:06 GMT; path=/
P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
disconnecting.
Read sensor: OK
Tem (C): 22
connecting...
HTTP/1.1 200 OK
Server: nginx/1.1.19
Date: Fri, 10 Apr 2015 12:02:10 GMT
Content-Type: text/html; charset=UTF-8
Connection: close
X-Powered-By: PHP/5.3.10-1ubuntu3.6
Set-Cookie: CAKEPHP=tku6c7ddjotciijur8l8ije951; expires=Sat, 18-Apr-2015 20:02:09 GMT; path=/
P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
disconnecting.
light:0
connecting...
HTTP/1.1 406 Not Acceptable
Server: nginx/1.1.19
Date: Fri, 10 Apr 2015 12:13:07 GMT
Content-Type: text/html; charset=UTF-8
Connection: close
X-Powered-By: PHP/5.3.10-1ubuntu3.6
Set-Cookie: CAKEPHP=qspcmg389888pq2kr5286m4756; expires=Sat, 18-Apr-2015 20:13:07 GMT; path=/
P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
No access permission to this sensor.
disconnecting.
Read sensor: OK
Hum (%): 42
connecting...
HTTP/1.1 200 OK
Server: nginx/1.1.19
Date: Fri, 10 Apr 2015 12:25:39 GMT
Content-Type: text/html; charset=UTF-8
Connection: close
X-Powered-By: PHP/5.3.10-1ubuntu3.6
Set-Cookie: CAKEPHP=nita95opda3e6i8ff7rob1t770; expires=Sat, 18-Apr-2015 20:25:39 GMT; path=/
P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
disconnecting.
Read sensor: OK
Tem (C): 19
connecting...
HTTP/1.1 200 OK
Server: nginx/1.1.19
Date: Fri, 10 Apr 2015 12:36:35 GMT
Content-Type: text/html; charset=UTF-8
Connection: close
X-Powered-By: PHP/5.3.10-1ubuntu3.6
Set-Cookie: CAKEPHP=jleftkaohg6fivjgam8cqm2ak5; expires=Sat, 18-Apr-2015 20:36:35 GMT; path=/
P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
disconnecting.
light:1023
connecting...
HTTP/1.1 406 Not Acceptable
Server: nginx/1.1.19
Date: Fri, 10 Apr 2015 12:47:32 GMT
Content-Type: text/html; charset=UTF-8
Connection: close
X-Powered-By: PHP/5.3.10-1ubuntu3.6
Set-Cookie: CAKEPHP=jah15v7d3fib81qnhrvor3rc43; expires=Sat, 18-Apr-2015 20:47:32 GMT; path=/
P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
No access permission to this sensor.
disconnecting.
Read sensor: OK
Hum (%): 46
connecting...
HTTP/1.1 200 OK
Server: nginx/1.1.19
Date: Fri, 10 Apr 2015 13:00:06 GMT
Content-Type: text/html; charset=UTF-8
Connection: close
X-Powered-By: PHP/5.3.10-1ubuntu3.6
Set-Cookie: CAKEPHP=c9ask5rf3c09i6rgubvnklsq53; expires=Sat, 18-Apr-2015 21:00:06 GMT; path=/
P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
disconnecting.
Read sensor: OK
Tem (C): 18
connecting...
HTTP/1.1 200 OK
Server: nginx/1.1.19
Date: Fri, 10 Apr 2015 13:11:03 GMT
Content-Type: text/html; charset=UTF-8
Connection: close
X-Powered-By: PHP/5.3.10-1ubuntu3.6
Set-Cookie: CAKEPHP=8eo7dnkuh6p99hf806lgnf3nr6; expires=Sat, 18-Apr-2015 21:11:03 GMT; path=/
P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
disconnecting.
light:0
connecting...
HTTP/1.1 406 Not Acceptable
Server: nginx/1.1.19
Date: Fri, 10 Apr 2015 13:21:58 GMT
Content-Type: text/html; charset=UTF-8
Connection: close
X-Powered-By: PHP/5.3.10-1ubuntu3.6
Set-Cookie: CAKEPHP=bqie561m2v7mtkt4si78ncr7b5; expires=Sat, 18-Apr-2015 21:21:58 GMT; path=/
P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
No access permission to this sensor.
disconnecting.
大家帮忙看看
到DF群里问问吧 大连林海 发表于 2015-4-10 22:21
到DF群里问问吧
Q群吗,我我在这里找不到,你给我吧,感激不尽。 hgmyaoming 发表于 2015-4-11 16:11
Q群吗,我我在这里找不到,你给我吧,感激不尽。
DFRobot-教育交流群 319059620
页:
[1]