转载自:http://www.dzjishu.com/forum.php?mod=viewthread&tid=142&extra=page%3D1
W5500的具体介绍已经在前面一些章节简单使用介绍过了,就不多述叙述了,只要说几点,他是
继W5100、W5200和W5300之后一款全新的全硬件TCP/IP协议栈网络芯片,这款芯片具有更低功耗与工作温度,及改良工艺,是嵌入式以太网的最佳选择方案;
规格
- 通信协议
- 支持硬件TCP/IP协议:TCP, UDP, ICMP, IPv4, ARP, IGMP, PPPoE
- 支持8个独立端口(Socket)同时通讯
- 内嵌10BaseT/100BaseTX 以太网物理层(PHY)
- 支持自动协商(10/100-Based全双工/半双工)
- 工作特性
- 支持掉电模式
- 支持网络唤醒
- 支持自动应答(全双工/半双工模式)
- 接口特性
- TTL 电平输入
- 单电源供电: 3.3V;
- 不支持IP分片
这里来做一个简单的数据传输,采集A0~A2三个模拟端口发送到yeelink平台,A0口读取LM35模拟温度的数值,A1,A2读取电位器的值,所需要准备的硬件有:
: Dccduino uno
:W5500网络模块
:W5500扩展板
: LM35温度传感器
:面包板
:10K电位器
:面保线
此模块是三层叠加模式,方便拆解调试
整体插上之后比较简洁有点像奥特曼打怪兽感觉
然后就到软件的准备了
在yeelink添加3个新设备,一个是温度数值,两个是小于1024的常数。
然后烧率代码,先要添加W5500的库函数,把原来官方的ethernet库删除掉
- #define SENSOR_NUM 3
-
- const int DEVICEID= xxxxx; // 输入你的设备ID
- const int SENSORID[SENSOR_NUM]={
- xxxxx,xxxxx,xxxxx}; // 输入你的传感器ID
-
- #define APIKEY "xxxxx" // 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)* (5.0 / 1024.0 * 100);
- 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;
- }
-
复制代码
好了,一切就绪了,那就连上网线,记住网线的另一头是接到路由器确保能上网的,坐等看效果图,先看看PC端然后再看手机端