wasdpkj 发表于 2014-5-29 11:18:43

以太网W5500上传多路传感器数据到Yeelink~

本帖最后由 wasdpkj 于 2014-5-29 11:45 编辑





采用了Microduino W5500以太网模块~功能是读取三个模拟口值,通过序列依次传到Yeelink

继W5100、W5200和W5300之后一款全新的全硬件TCP/IP协议栈网络芯片,这款芯片具有更低功耗与工作温度,及改良工艺,是嵌入式以太网的最佳选择方案;

规格
[*]通信协议
[*]支持硬件TCP/IP协议:TCP, UDP, ICMP, IPv4, ARP, IGMP, PPPoE
[*]支持8个独立端口(Socket)同时通讯
[*]内嵌10BaseT/100BaseTX 以太网物理层(PHY)
[*]支持自动协商(10/100-Based全双工/半双工)


[*]工作特性
[*]支持掉电模式
[*]支持网络唤醒
[*]支持自动应答(全双工/半双工模式)


[*]更新速率
[*]支持高速串行外设接口
[*]内部32K字节收发缓存


[*]接口特性
[*]TTL 电平输入
[*]单电源供电: 3.3V;
[*]不支持IP分片

状态指示
[*]

[*]两个用来表示连接、发送、接收、冲突和全/ 半双工状态的可编程LED 输出;

堆叠之后是这样:



下面是程序部分:
#define SENSOR_NUM 3

const int DEVICEID= xx;// 输入你的设备ID
const int SENSORID={
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;
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= analogRead(A0);
sensorReading= analogRead(A1);
sensorReading= 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,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 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

wasdylb 发表于 2014-5-29 11:24:04

{:soso_e142:}

井底添蛙 发表于 2014-5-29 11:26:06

哈哈哈,暂暂暂

移动大金条 发表于 2014-5-29 11:29:13

干得漂亮!

JosephLeung 发表于 2014-5-29 11:30:58

老潘出品,必属佳品。

futuremeng 发表于 2014-5-29 12:27:28

把库名改一下,就不用删掉原来的库了吧

i7456 发表于 2014-5-29 12:45:44

microduino的东西做工好、体积小。就是价格太不亲民,让人又爱又恨。

wasdpkj 发表于 2014-5-29 14:02:24

futuremeng 发表于 2014-5-29 12:27 static/image/common/back.gif
把库名改一下,就不用删掉原来的库了吧

也要删除,调用的语法会冲突

Rambutan 发表于 2014-5-29 16:29:21

W5500+Microduino+Yeelink 绝佳组合。

。。。。 发表于 2014-5-29 19:23:43

学习了。。。。。谢谢

你笑的好甜 发表于 2014-5-29 19:34:56

非常感谢~



wetnt 发表于 2014-5-30 09:11:49

void sendData,函数,正需要,谢谢!

topdog 发表于 2014-11-17 22:25:23

老潘加油,学习了。

BG7IDJ 发表于 2019-9-6 16:28:31

非常感谢~
页: [1]
查看完整版本: 以太网W5500上传多路传感器数据到Yeelink~