開源第六週:
這周使用了ethernet shield模塊將數據傳輸到yeelink平臺,通過yeelink平臺服務器處理顯示發送的數據。
一、ethernet shie:
輸入電壓(推薦) 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設備。
二、連接yeelink,上傳數據:
2.DEVICEID :這個需要設備號,獲取這次插入的控制設備的設備號和傳感器號:如下圖來說,就是設備號=13147,傳感器號=21579
3.SENSORID:這個需要傳感器號
1. 代碼中添加 byte ip[] = { 192, 168, 1, 12 }; (根據網絡環境更改)
2. 將Ethernet.begin(mac) 替換成Ethernet.begin(mac, ip);
#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();
}