開源第六週 ethernet shield數據傳輸+yeelink

    開源第六週:

 這周使用了ethernet shield模塊將數據傳輸到yeelink平臺,通過yeelink平臺服務器處理顯示發送的數據。

一、ethernet shie:

   着款控制器是一款很奇特的控制器,它可以連接網線,通過程序設置本地mac地址和目標服務器地址實現數據的發送和接收功能。可以利用已經連接網絡的電腦連接網絡,也可以直接把程序燒入直接連接路由器連接網絡。如果給模塊加一個無線串口,也完全可以把這個傳感器當作數據節點使用。
   這個控制器是基於Arduino Ethernet的微控制器板,板載以W5500爲核心的網絡模塊,可以使Arduino成爲簡單的Web服務器或者通過網絡控制讀寫Arduino的數字和模擬接口等網絡應用。可直接使用IDE中的Ethernet庫文件便可實現一個簡單Web服務器。
   W5500是一款集TCP/IP協議、MAC和PHY(10/100M Base T)於一體的網絡芯片,支持直接總線接口、間接總線接口和 SPI總線,Arduino IDE內置的Ethernet庫仍兼容於W5500。直接把W5500當外部RAM使用,MCU初始化一下I/O,寄存器等就能使用了。是一種簡易快速拓展Ethernet的方案,在穩定性及高效性方面表現也非常突出。 支持硬件 TCP/IP 協議:TCP, UDP, ICMP, IPv4, ARP, IGMP, PPPoE 。
   名稱  性能    說明
輸入電壓(推薦) 7~12V 最高可到40V
輸入POE模塊電壓(推薦) 7~30V 需支持48V,參見“高級進階”
數字I/O腳 14個 其中3、5、6、9、10、11號端口可用於PWM輸出;10、11、12、13號端口用於SPI通信;4號端口用於TF卡片選;10號端口爲W5500片選;2號端口可通過短接
模擬輸入腳 6個
每個I/O直流輸出能力 40 mA 整個處理器工作電流不得超過200mA
3.3V直流輸出能力 50 mA
Flash Memory 32 KB 其中0.5 KB 用於 bootloader
其他 TCP/IP Ethernet控制器W5500支持Power Over Ethernet供電輸入MicroSD卡(TF卡)座MicroUSB下載
說明 支持Micro SD卡讀/寫;Ethernet和MicroSD卡共用SPI口,通過4、10號端口分時複用。4、10號端口置高後,可繼續接入其他的SPI設備。
使用直通線將Ethernet擴展板和路由器連接好以後,Ethernet擴展板RJ45網絡插座上的綠色燈會亮,庫文件(Ethernet擴展板的庫文件下載(Web Server),請下載後解壓到arduino-0011\hardware\libraries)裏面有個一個Web Server例程,打開編譯後下載進Arduino,完成後Ethernet擴展板RJ45網絡插座上的橙色燈會閃爍幾下,表示網絡芯片初始化完成。這時我們的Arduino就一個超小型的Web服務器了。打開IE瀏覽器,地址欄輸入http://192.168.0.15/後回車,便進入Arduino內部的網頁了
 
通過這個模塊,給模塊燒入連接程序就可以連接網絡了。注意的是一定要設置好網絡的參數(mac地址,目標IP地址,api key等),否則數據是在平臺上顯示不出來的。

二、連接yeelink,上傳數據:

 
yeelink平臺提供了兩種方式,一種是arduino/單片機通過直接socket網絡連接的辦法,連入平臺上,保持和服務器的長連接,這種方法控制的實時性相對較強;另外一種辦法是arduino作爲客戶端,定期的向服務器查詢傳感器的當前值。
     首先,照例我們要先申請到yeelink的API-KEY纔可以進行:
第一步: 註冊之後,增加一個開關類的傳感器。
第二步,獲取這次插入的控制設備的設備號和傳感器號。
第三步:連接網絡,設置參數。
要注意這裏的設置
1.APIKEY: 這個需要自己賬號的APIKEY

2.DEVICEID :這個需要設備號,獲取這次插入的控制設備的設備號和傳感器號:如下圖來說,就是設備號=13147,傳感器號=21579

3.SENSORID:這個需要傳感器號
    
    另外,需要注意一點,ethernet shield是需要你家中的路由器開啓DHCP功能的,如果沒有開啓,可以參考將
1. 代碼中添加 byte ip[] = { 192, 168, 1, 12 }; (根據網絡環境更改)
2. 將Ethernet.begin(mac) 替換成Ethernet.begin(mac, ip);
   這裏是通過ethernet shie上傳yeelink的光照數據的展示圖:
   下面是上傳光照數據到yeelink的源代碼:
  
#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#include <math.h>
int BH1750address = 0x23;
byte buff[2];

// for yeelink api
#define APIKEY         "c10bce8bbb373f95f5a08c9fcc637a89" // replace your yeelink api key here
#define DEVICEID       13488 // replace your device ID
#define SENSORID       22210 // replace your sensor ID

// assign a MAC address for the ethernet controller.
byte mac[] = {0x6c,0x71,0xd9,0x64,0x6e,0xda};
// 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

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: ");
    Serial.println("upload  ok...");

    // 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;
}

///////////////////////////////////////////////////////////////////////////
// get data from light sensor
// you can replace this code for your sensor
int readLightSensor()
{
  uint16_t val=0;
  BH1750_Init(BH1750address);
  delay(200);
  if(2==BH1750_Read(BH1750address))
  {
    val=((buff[0]<<8)|buff[1])/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[i] = 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();
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章