4823浏览
查看: 4823|回复: 4

yeelink数据采样频率低,http响应报406错误

[复制链接]
本帖最后由 大连林海 于 2015-4-10 22:16 编辑

以下是我用DHT11采集数据上传到yeelink的程序,虽然能够上传数据,但是采样频率实在太低。测了几个小时才采到四五个样本点,不知道这是什么原因,这么慢。看了串口发现当采完湿度、温度之后http响应会返回406错误,这个也搞不明白。各位前辈,帮帮忙,看看是什么原因。程序跟代码都附在下面了。
  1. // include the library code:
  2. #include <LiquidCrystal.h>
  3. #include <SPI.h>
  4. #include <Ethernet.h>
  5. #include <dht11.h>
  6. // initialize the library with the numbers of the interface pins
  7. LiquidCrystal lcd(9, 8, A0, A1, A2, A3);
  8. dht11 DHT11;
  9. #define DHT11PIN 3 //DHT11 PIN 3 连接UNO 3
  10. #define LIGHTPIN 0 //light sensor > analog pin 0
  11. #define APIKEY         "c142a1e19dec73dd5dde48309ad20590" // replace your yeelink api key here
  12. #define DEVICEID                19112 // replace your device ID
  13. #define TEMPERATURE_SENSORID    33275 // temperature
  14. #define HUMIDITY_SENSORID       33276 //humidity
  15. #define LIGHT_SENSORID          33270 //light
  16. // assign a MAC address for the ethernet controller.
  17. byte mac[] = { 0x00, 0x1D, 0x72, 0x82, 0x35, 0x9D};
  18. // initialize the library instance:
  19. EthernetClient client;
  20. char server[] = "api.yeelink.net";   // name address for yeelink API
  21. unsigned long lastConnectionTime = 0;          // last time you connected to the server, in milliseconds
  22. boolean lastConnected = false;                 // state of the connection last time through the main loop
  23. const unsigned long postingInterval = 30*1000; // delay between 2 datapoints, 30s
  24. int sensor = 0;   //which sesor to read
  25.                   //0 temperature
  26.                   //1 humidity
  27.                   //2 light
  28. void setup()
  29. {
  30.   Serial.begin(115200);
  31.   // set up the LCD's number of columns and rows:
  32.   lcd.begin(16,2);
  33.   pinMode(LIGHTPIN,OUTPUT);
  34.   if (Ethernet.begin(mac) == 0) {
  35.     Serial.println("Failed to configure Ethernet using DHCP");
  36.     for(;;)
  37.       ;
  38.   }
  39.   else {
  40.     Serial.println("Ethernet configuration OK");
  41.   }
  42.   Serial.println("DHT11 TEST PROGRAM ");
  43.   Serial.print("LIBRARY VERSION: ");
  44.   Serial.println(DHT11LIB_VERSION);
  45.   Serial.println();
  46. }
  47. void loop()
  48. {
  49.    // set the cursor to (0,0):
  50.     lcd.setCursor(0, 0);
  51.     // print
  52.     lcd.print("Hum (%):");
  53.     delay(500);
  54.     // set the cursor to (0,0):
  55.     lcd.setCursor(9, 0);
  56.     // print
  57.     lcd.print(DHT11.humidity);
  58.     delay(500);
  59.      // set the cursor to (0,0):
  60.     lcd.setCursor(0, 1);
  61.     // print
  62.     lcd.print("Tem (C):");
  63.     delay(500);
  64.     // set the cursor to (0,0):
  65.     lcd.setCursor(9, 1);
  66.     // print
  67.     lcd.print(DHT11.temperature);
  68.     delay(500);
  69.   // if there's incoming data from the net connection.
  70.   // send it out the serial port.  This is for debugging
  71.   // purposes only:
  72.   if (client.available()) {
  73.     char c = client.read();
  74.     //Serial.print("NET:");
  75.     Serial.print(c);
  76.   }
  77.   // if there's no net connection, but there was one last time
  78.   // through the loop, then stop the client:
  79.   if (!client.connected() && lastConnected) {
  80.     Serial.println();
  81.     Serial.println("disconnecting.");
  82.     client.stop();
  83.   }
  84.   // if you're not connected, and ten seconds have passed since
  85.   // your last connection, then connect again and send data:
  86.   if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
  87.     // read sensor data, replace with your code
  88.     //int sensorReading = readLightSensor();
  89.     //int tempC = analogRead(0)*500/1024;
  90.     //send data to server
  91.       switch(sensor){
  92.         case 0:
  93.           sendData(readSensor(HUMIDITY_SENSORID), HUMIDITY_SENSORID);
  94.           break;
  95.         case 1:
  96.           sendData(readSensor(TEMPERATURE_SENSORID), TEMPERATURE_SENSORID);
  97.           break;
  98.         case 2:
  99.           sendData(readSensor(LIGHT_SENSORID), LIGHT_SENSORID);
  100.           break;
  101.       }
  102.       if(++sensor > 2) sensor = 0;
  103.   }
  104.   // store the state of the connection for next time through
  105.   // the loop:
  106.   //
  107.   lastConnected = client.connected();
  108. }
  109. int readSensor(long sensor_id){
  110.   Serial.println("\n");
  111.   if (sensor_id == LIGHT_SENSORID)
  112.   {
  113.     int v = analogRead(LIGHTPIN);
  114.     Serial.print("light:");
  115.     Serial.println(v);
  116.     return v;
  117.   }
  118.   int chk = DHT11.read(DHT11PIN);
  119.   Serial.print("Read sensor: ");
  120.   switch (chk)
  121.   {
  122.     case DHTLIB_OK:
  123.                 Serial.println("OK");
  124.                 break;
  125.     case DHTLIB_ERROR_CHECKSUM:
  126.                 Serial.println("Checksum error");
  127.                 break;
  128.     case DHTLIB_ERROR_TIMEOUT:
  129.                 Serial.println("Time out error");
  130.                 break;
  131.     default:
  132.                 Serial.println("Unknown error");
  133.                 break;
  134.   }
  135.   if(sensor_id == HUMIDITY_SENSORID){
  136.     Serial.print("Hum (%): ");
  137.     Serial.println(DHT11.humidity);
  138.     return DHT11.humidity;
  139.   }else{
  140.     Serial.print("Tem (C): ");
  141.     Serial.println(DHT11.temperature);
  142.     return DHT11.temperature;
  143.   }
  144. }
  145. void sendData(int thisData, long sensor_id) {
  146.   // if there's a successful connection:
  147.   if (client.connect(server, 80)) {
  148.     Serial.println("connecting...");
  149.     // send the HTTP PUT request:
  150.     client.print("POST /v1.0/device/");
  151.     client.print(DEVICEID);
  152.     client.print("/sensor/");
  153.     client.print(sensor_id);
  154.     client.print("/datapoints");
  155.     client.println(" HTTP/1.0");
  156.     client.println("Host: api.yeelink.net");
  157.     client.print("Accept: *");
  158.     client.print("/");
  159.     client.println("*");
  160.     client.print("U-ApiKey: ");
  161.     client.println(APIKEY);
  162.     client.print("Content-Length: ");
  163.     // calculate the length of the sensor reading in bytes:
  164.     // 8 bytes for {"value":} + number of digits of the data:
  165.     int thisLength = 10 + getLength(thisData);
  166.     client.println(thisLength);
  167.     client.println("Content-Type: application/x-www-form-urlencoded");
  168.     client.println("Connection: close");
  169.     client.println();
  170.     // here's the actual content of the PUT request:
  171.     client.print("{"value":");
  172.     client.print(thisData);
  173.     client.println("}");
  174.   }
  175.   else {
  176.     // if you couldn't make a connection:
  177.     //
  178.     Serial.println("connection failed");
  179.     Serial.println();
  180.     Serial.println("disconnecting.");
  181.     client.stop();
  182.   }
  183.    // note the time that the connection was made or attempted:
  184.   lastConnectionTime = millis();
  185. }
  186. // This method calculates the number of digits in the
  187. // sensor reading.  Since each digit of the ASCII decimal
  188. // representation is a byte, the number of digits equals
  189. // the number of bytes:
  190. int getLength(int someValue) {
  191.   // there's at least one byte:
  192.   int digits = 1;
  193.   // continually divide the value by ten,
  194.   // adding one to the digit count for each
  195.   // time you divide, until you're at 0:
  196.   int dividend = someValue /10;
  197.   while (dividend > 0) {
  198.     dividend = dividend /10;
  199.     digits++;
  200.   }
  201.   // return the number of digits:
  202.   return digits;
  203. }
  204. 以下为串口调试:
  205. Ethernet configuration OK
  206. DHT11 TEST PROGRAM
  207. LIBRARY VERSION: 0.4.1
  208. Read sensor: OK
  209. Hum (%): 45
  210. connecting...
  211. HTTP/1.1 200 OK
  212. Server: nginx/1.1.19
  213. Date: Fri, 10 Apr 2015 11:16:41 GMT
  214. Content-Type: text/html; charset=UTF-8
  215. Connection: close
  216. X-Powered-By: PHP/5.3.10-1ubuntu3.6
  217. Set-Cookie: CAKEPHP=npjm2qkejj2bv82ev20hbqggt1; expires=Sat, 18-Apr-2015 19:16:41 GMT; path=/
  218. P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
  219. disconnecting.
  220. Read sensor: OK
  221. Tem (C): 19
  222. connecting...
  223. HTTP/1.1 200 OK
  224. Server: nginx/1.1.19
  225. Date: Fri, 10 Apr 2015 11:27:38 GMT
  226. Content-Type: text/html; charset=UTF-8
  227. Connection: close
  228. X-Powered-By: PHP/5.3.10-1ubuntu3.6
  229. Set-Cookie: CAKEPHP=9av3p5l6km31blscqrqol7hfp6; expires=Sat, 18-Apr-2015 19:27:38 GMT; path=/
  230. P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
  231. disconnecting.
  232. light:1023
  233. connecting...
  234. HTTP/1.1 406 Not Acceptable
  235. Server: nginx/1.1.19
  236. Date: Fri, 10 Apr 2015 11:38:33 GMT
  237. Content-Type: text/html; charset=UTF-8
  238. Connection: close
  239. X-Powered-By: PHP/5.3.10-1ubuntu3.6
  240. Set-Cookie: CAKEPHP=022ndq55jch4rptgo6kjhi0ia1; expires=Sat, 18-Apr-2015 19:38:33 GMT; path=/
  241. P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
  242. No access permission to this sensor.
  243. disconnecting.
  244. Read sensor: OK
  245. Hum (%): 42
  246. connecting...
  247. HTTP/1.1 200 OK
  248. Server: nginx/1.1.19
  249. Date: Fri, 10 Apr 2015 11:51:07 GMT
  250. Content-Type: text/html; charset=UTF-8
  251. Connection: close
  252. X-Powered-By: PHP/5.3.10-1ubuntu3.6
  253. Set-Cookie: CAKEPHP=6d77s4pqmdf88cv8r7lae7cqi6; expires=Sat, 18-Apr-2015 19:51:06 GMT; path=/
  254. P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
  255. disconnecting.
  256. Read sensor: OK
  257. Tem (C): 22
  258. connecting...
  259. HTTP/1.1 200 OK
  260. Server: nginx/1.1.19
  261. Date: Fri, 10 Apr 2015 12:02:10 GMT
  262. Content-Type: text/html; charset=UTF-8
  263. Connection: close
  264. X-Powered-By: PHP/5.3.10-1ubuntu3.6
  265. Set-Cookie: CAKEPHP=tku6c7ddjotciijur8l8ije951; expires=Sat, 18-Apr-2015 20:02:09 GMT; path=/
  266. P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
  267. disconnecting.
  268. light:0
  269. connecting...
  270. HTTP/1.1 406 Not Acceptable
  271. Server: nginx/1.1.19
  272. Date: Fri, 10 Apr 2015 12:13:07 GMT
  273. Content-Type: text/html; charset=UTF-8
  274. Connection: close
  275. X-Powered-By: PHP/5.3.10-1ubuntu3.6
  276. Set-Cookie: CAKEPHP=qspcmg389888pq2kr5286m4756; expires=Sat, 18-Apr-2015 20:13:07 GMT; path=/
  277. P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
  278. No access permission to this sensor.
  279. disconnecting.
  280. Read sensor: OK
  281. Hum (%): 42
  282. connecting...
  283. HTTP/1.1 200 OK
  284. Server: nginx/1.1.19
  285. Date: Fri, 10 Apr 2015 12:25:39 GMT
  286. Content-Type: text/html; charset=UTF-8
  287. Connection: close
  288. X-Powered-By: PHP/5.3.10-1ubuntu3.6
  289. Set-Cookie: CAKEPHP=nita95opda3e6i8ff7rob1t770; expires=Sat, 18-Apr-2015 20:25:39 GMT; path=/
  290. P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
  291. disconnecting.
  292. Read sensor: OK
  293. Tem (C): 19
  294. connecting...
  295. HTTP/1.1 200 OK
  296. Server: nginx/1.1.19
  297. Date: Fri, 10 Apr 2015 12:36:35 GMT
  298. Content-Type: text/html; charset=UTF-8
  299. Connection: close
  300. X-Powered-By: PHP/5.3.10-1ubuntu3.6
  301. Set-Cookie: CAKEPHP=jleftkaohg6fivjgam8cqm2ak5; expires=Sat, 18-Apr-2015 20:36:35 GMT; path=/
  302. P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
  303. disconnecting.
  304. light:1023
  305. connecting...
  306. HTTP/1.1 406 Not Acceptable
  307. Server: nginx/1.1.19
  308. Date: Fri, 10 Apr 2015 12:47:32 GMT
  309. Content-Type: text/html; charset=UTF-8
  310. Connection: close
  311. X-Powered-By: PHP/5.3.10-1ubuntu3.6
  312. Set-Cookie: CAKEPHP=jah15v7d3fib81qnhrvor3rc43; expires=Sat, 18-Apr-2015 20:47:32 GMT; path=/
  313. P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
  314. No access permission to this sensor.
  315. disconnecting.
  316. Read sensor: OK
  317. Hum (%): 46
  318. connecting...
  319. HTTP/1.1 200 OK
  320. Server: nginx/1.1.19
  321. Date: Fri, 10 Apr 2015 13:00:06 GMT
  322. Content-Type: text/html; charset=UTF-8
  323. Connection: close
  324. X-Powered-By: PHP/5.3.10-1ubuntu3.6
  325. Set-Cookie: CAKEPHP=c9ask5rf3c09i6rgubvnklsq53; expires=Sat, 18-Apr-2015 21:00:06 GMT; path=/
  326. P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
  327. disconnecting.
  328. Read sensor: OK
  329. Tem (C): 18
  330. connecting...
  331. HTTP/1.1 200 OK
  332. Server: nginx/1.1.19
  333. Date: Fri, 10 Apr 2015 13:11:03 GMT
  334. Content-Type: text/html; charset=UTF-8
  335. Connection: close
  336. X-Powered-By: PHP/5.3.10-1ubuntu3.6
  337. Set-Cookie: CAKEPHP=8eo7dnkuh6p99hf806lgnf3nr6; expires=Sat, 18-Apr-2015 21:11:03 GMT; path=/
  338. 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.

大连林海  初级技神

发表于 2015-4-10 22:17:45

大家帮忙看看
回复

使用道具 举报

大连林海  初级技神

发表于 2015-4-10 22:21:11

到DF群里问问吧
回复

使用道具 举报

hgmyaoming  见习技师
 楼主|

发表于 2015-4-11 16:11:26


Q群吗,我我在这里找不到,你给我吧,感激不尽。
回复

使用道具 举报

大连林海  初级技神

发表于 2015-4-11 16:15:59

hgmyaoming 发表于 2015-4-11 16:11
Q群吗,我我在这里找不到,你给我吧,感激不尽。

DFRobot-教育交流群 319059620
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
关于楼主
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail