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

网络温度计

[复制链接]
一:所需材料
Ethernet W5100扩展板
LM 35温度传感器
导线若干
Yeelink帐号(http://www.yeelink.net/)
二:模拟连接图




代码:

三:代码
  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3. #include <Wire.h>
  4. #include <math.h>
  5. byte buff[2];
  6. #define APIKEY         "去我的个人中心获取API" // replace your yeelink api key here
  7. #define DEVICEID       设备号 // replace your device ID
  8. #define SENSORID       传感器号 // replace your sensor ID
  9. byte mac[] = { 0x00, 0x1D, 0x72, 0x82, 0x35, 0x9D};
  10. EthernetClient client;
  11. char server[] = "api.yeelink.net";
  12. unsigned long lastConnectionTime = 0;      
  13. boolean lastConnected = false;               
  14. const unsigned long postingInterval = 30*1000;
  15. void setup() {
  16.   Wire.begin();  Serial.begin(57600);  if (Ethernet.begin(mac) == 0) {
  17.     Serial.println("Failed to configure Ethernet using DHCP");
  18.     for(;;)
  19.       ;
  20.   }
  21.   else {
  22.     Serial.println("Ethernet configuration OK");
  23.   }
  24. }
  25. void loop() {
  26.   if (client.available()) {
  27.     char c = client.read();
  28.     Serial.print(c);
  29.   }
  30.   if (!client.connected() && lastConnected) {
  31.     Serial.println();
  32.     Serial.println("disconnecting.");
  33.     client.stop();
  34.   }
  35.   if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
  36.     int sensorReading = readLightSensor();   
  37.    sendData(sensorReading);
  38.   }
  39.   lastConnected = client.connected();
  40. }
  41. void sendData(int thisData) {
  42.   if (client.connect(server, 80)) {
  43.     Serial.println("connecting...");
  44.   
  45.     client.print("POST /v1.0/device/");
  46.     client.print(DEVICEID);
  47.     client.print("/sensor/");
  48.     client.print(SENSORID);
  49.     client.print("/datapoints");
  50.     client.println(" HTTP/1.1");
  51.     client.println("Host: api.yeelink.net");
  52.     client.print("Accept: *");
  53.     client.print("/");
  54.     client.println("*");
  55.     client.print("U-ApiKey: ");
  56.     client.println(APIKEY);
  57.     client.print("Content-Length: ");
  58.     // calculate the length of the sensor reading in bytes:
  59.   
  60.     int thisLength = 10 + getLength(thisData);
  61.     client.println(thisLength);
  62.     client.println("Content-Type: application/x-www-form-urlencoded");
  63.     client.println("Connection: close");
  64.     client.println();
  65.   
  66.     client.print("{"value":");
  67.     client.print(thisData);
  68.     client.println("}");
  69.   }
  70.   else {
  71.   
  72.     Serial.println("connection failed");
  73.     Serial.println();
  74.     Serial.println("disconnecting.");
  75.     client.stop();
  76.   }
  77.   lastConnectionTime = millis();
  78. }
  79. int getLength(int someValue) {
  80.   int digits = 1;
  81.   int dividend = someValue /10;
  82.   while (dividend > 0) {
  83.     dividend = dividend /10;
  84.     digits++;
  85.   }  return digits;
  86. }
  87. ///////////////////////////////////////////////////////////////////////////
  88. int readLightSensor()
  89. {
  90.   int val=analogRead(0);
  91.   int data=(125*val)>>8;
  92.   Serial.print("Sensor value is: ");
  93.   Serial.println((int)data);
  94.   return data;
  95. }
复制代码



hnyzcj  版主

发表于 2015-4-10 18:41:39

不错,开始玩YEELINK,这个挺好玩的。
回复

使用道具 举报

大连林海  初级技神
 楼主|

发表于 2015-4-10 18:44:17

hnyzcj 发表于 2015-4-10 18:41
不错,开始玩YEELINK,这个挺好玩的。

学习学习 自己也好好做一个
回复

使用道具 举报

hgmyaoming  见习技师

发表于 2015-4-10 22:10:43


以下是我用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);
    // print
    lcd.print("Hum (%):");
    delay(500);
    // set the cursor to (0,0):
    lcd.setCursor(9, 0);
    // print
    lcd.print(DHT11.humidity);
    delay(500);
     // set the cursor to (0,0):
    lcd.setCursor(0, 1);
    // print
    lcd.print("Tem (C):");
    delay(500);
    // set the cursor to (0,0):
    lcd.setCursor(9, 1);
    // print
    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.
回复

使用道具 举报

hgmyaoming  见习技师

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

大家一起帮看看,到底那里的问题
回复

使用道具 举报

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

本版积分规则

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

硬件清单

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

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

mail