转自:极客工坊
采用了Microduino W5500以太网模块~功能是读取三个模拟口值,通过序列依次传到Yeelink
继W5100、W5200和W5300之后一款全新的全硬件TCP/IP协议栈网络芯片,这款芯片具有更低功耗与工作温度,及改良工艺,是嵌入式以太网的最佳选择方案;
规格
下面是程序部分:
#define SENSOR_NUM 3
const int DEVICEID= xx; // 输入你的设备ID
const int SENSORID[SENSOR_NUM]={
xx,xx,xx}; // 输入你的传感器ID
#define APIKEY “xx” // replace your pachube api key here
char server[] = “api.yeelink.net”; // yeelink API的地址
#include <SPI.h>
#include <Ethernet.h>
// fill in an available IP address on your network here,
// for manual configuration:
IPAddress ip(192,168,8,177);
IPAddress gw(0,0,0,0);
IPAddress snip(0,0,0,0);
IPAddress dnsip(0,0,0,0);
// initialize the library instance:
EthernetClient client;
// if you don’t want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(173,203,98,29); // In cases where it is not possible to use DNS, you can use the following bare-IP address alternative
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 = 4*1000; //delay between updates to cosm.com
int sensorReading[3];
int SENSORID_NUM=0;
void setup() {
// start serial port:
Serial.begin(115200);
// start the Ethernet connection:
if (Ethernet.begin() == 0) {
Serial.println(“Failed to configure Ethernet using DHCP”);
Ethernet.begin(ip, dnsip, gw,snip);
}
}
void loop() {
// read the analog sensor:
sensorReading[0]= analogRead(A0);
sensorReading[1]= analogRead(A1);
sensorReading[2]= analogRead(A2);
// 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)) {
SENSORID_NUM++;
if(SENSORID_NUM>=SENSOR_NUM) SENSORID_NUM=0;
Serial.print(“======NUM:”);
Serial.println(SENSORID_NUM);
sendData(DEVICEID,SENSORID[SENSORID_NUM],sensorReading[SENSORID_NUM]);
}
// 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 DEV_Data, int SEN_Data,int thisData) {
// if there’s a successful connection:
if (client.connect(server, 80)) {
Serial.println(“connecting…”);
// 发送HTTP PUT请求
client.print(“POST /v1.0/device/”);
client.print(DEV_Data);
client.print(“/sensor/”);
client.print(SEN_Data);
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: “);
// 计算http包里面内容部分的长度,即content-length长度
int thisLength = 10 + getLength(thisData);
client.println(thisLength);
client.println(“Content-Type: application/x-www-form-urlencoded”);
client.println(“Connection: close”);
client.println();
// PUT回复内容
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库)
点击下载
W5500更多资料:
http://www.microduino.cc/wiki/index.php?title=Microduino-W5500/zh
更多动态,关注WIZnet官方微博:http://weibo.com/wiznet2012