纪念yeelink,那些年你很棒
本想用seeeduino配合yeelink玩一下,但是最近yeelink老出故障,下面的作品是之前用uno完成的,大家可以直接移植到seeeduino,无需修改。
接上一篇帖子http://bbs.ickey.cn/group-topic-id-63268.html
硬件:seeeduino V4,GP2D12、W5100
1.将W5100扩展版对应安插在Arduino上,参考
http://bbs.ickey.cn/group-topic-id-62818.html
2. 按下图连接线路(传感器脚图中接ARDUINO A0脚),需要进行模数转换。DA转换原理不再赘述。arduino的AD引脚是10位的采样精度。即范围0-1023,。
3. 用USB数据线将Arduino与电脑连接起来
4. 用网线将W5100连接到路由器
5. 将准备好的程序传输到Arduino开发板上
主程序
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: ");
int thisLength = 10 + getLength(thisData);
client.println(thisLength);
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("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:
// return the number of digits:
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;
}
客户端
|