Dccduino物联网应用——基于W5500的数据传输

转载自: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全双工/半双工)

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

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

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





这里来做一个简单的数据传输,采集A0~A2三个模拟端口发送到yeelink平台,A0口读取LM35模拟温度的数值,A1,A2读取电位器的值,所需要准备的硬件有:


: Dccduino uno
:W5500网络模块
:W5500扩展板
: LM35温度传感器
:面包板
:10K电位器
:面保线

此模块是三层叠加模式,方便拆解调试






整体插上之后比较简洁有点像奥特曼打怪兽感觉







然后就到软件的准备了
在yeelink添加3个新设备,一个是温度数值,两个是小于1024的常数。



然后烧率代码,先要添加W5500的库函数,把原来官方的ethernet库删除掉
  1. #define SENSOR_NUM 3

  2. const int DEVICEID= xxxxx;  // 输入你的设备ID
  3. const int SENSORID[SENSOR_NUM]={
  4. xxxxx,xxxxx,xxxxx};  // 输入你的传感器ID

  5. #define APIKEY    "xxxxx" // replace your pachube api key here
  6. char server[] = "api.yeelink.net";   // yeelink API的地址

  7. #include <SPI.h>
  8. #include <Ethernet.h>

  9. // fill in an available IP address on your network here,
  10. // for manual configuration:
  11. IPAddress ip(192,168,8,177);
  12. IPAddress gw(0,0,0,0);
  13. IPAddress snip(0,0,0,0);
  14. IPAddress dnsip(0,0,0,0);
  15. // initialize the library instance:
  16. EthernetClient client;

  17. // if you don't want to use DNS (and reduce your sketch size)
  18. // use the numeric IP instead of the name for the server:
  19. //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

  20. unsigned long lastConnectionTime = 0;          // last time you connected to the server, in milliseconds
  21. boolean lastConnected = false;                 // state of the connection last time through the main loop
  22. const unsigned long postingInterval = 4*1000; //delay between updates to cosm.com

  23. int sensorReading[3];
  24. int SENSORID_NUM=0;

  25. void setup() {
  26.   // start serial port:
  27.   Serial.begin(115200);

  28.   // start the Ethernet connection:
  29.   if (Ethernet.begin() == 0) {
  30.     Serial.println("Failed to configure Ethernet using DHCP");
  31.     Ethernet.begin(ip, dnsip, gw,snip);
  32.   }
  33. }

  34. void loop()
  35. {
  36.   // read the analog sensor:
  37.   sensorReading[0]= analogRead(A0)* (5.0 / 1024.0 * 100);
  38.   sensorReading[1]= analogRead(A1);
  39.   sensorReading[2]= analogRead(A2);

  40.   // if there's incoming data from the net connection.
  41.   // send it out the serial port.  This is for debugging
  42.   // purposes only:
  43.   if (client.available()) 
  44.   {
  45.     char c = client.read();
  46.     Serial.print(c);
  47.   }

  48.   // if there's no net connection, but there was one last time
  49.   // through the loop, then stop the client:
  50.   if (!client.connected() && lastConnected)
  51.   {
  52.     Serial.println();
  53.     Serial.println("disconnecting.");
  54.     client.stop();
  55.   }

  56.   // if you're not connected, and ten seconds have passed since
  57.   // your last connection, then connect again and send data:
  58.   if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) 
  59.   {
  60.     SENSORID_NUM++;
  61.     if(SENSORID_NUM>=SENSOR_NUM) SENSORID_NUM=0;
  62.     Serial.print("======NUM:");
  63.     Serial.println(SENSORID_NUM);
  64.     sendData(DEVICEID,SENSORID[SENSORID_NUM],sensorReading[SENSORID_NUM]);
  65.   }
  66.   // store the state of the connection for next time through
  67.   // the loop:
  68.   lastConnected = client.connected();
  69. }

  70. // this method makes a HTTP connection to the server:
  71. void sendData(int DEV_Data, int SEN_Data,int thisData)
  72. {
  73.   // if there's a successful connection:
  74.   if (client.connect(server, 80)) 
  75.   {
  76.     Serial.println("connecting...");
  77.     // 发送HTTP PUT请求
  78.     client.print("POST /v1.0/device/");
  79.     client.print(DEV_Data);
  80.     client.print("/sensor/");
  81.     client.print(SEN_Data);
  82.     client.print("/datapoints");
  83.     client.println(" HTTP/1.1");
  84.     client.println("Host: api.yeelink.net");
  85.     client.print("Accept: *");
  86.     client.print("/");
  87.     client.println("*");
  88.     client.print("U-ApiKey: ");
  89.     client.println(APIKEY);
  90.     client.print("Content-Length: ");

  91.     // 计算http包里面内容部分的长度,即content-length长度
  92.     int thisLength = 10 + getLength(thisData);
  93.     client.println(thisLength);

  94.     client.println("Content-Type: application/x-www-form-urlencoded");
  95.     client.println("Connection: close");
  96.     client.println();

  97.     // PUT回复内容
  98.     client.print("{\"value\":");
  99.     client.print(thisData);
  100.     client.println("}");  
  101.   } 
  102.   else 
  103.   {
  104.     // if you couldn't make a connection:
  105.     Serial.println("connection failed");
  106.     Serial.println();
  107.     Serial.println("disconnecting.");
  108.     client.stop();
  109.   }
  110.   // note the time that the connection was made or attempted:
  111.   lastConnectionTime = millis();
  112. }


  113. // This method calculates the number of digits in the
  114. // sensor reading.  Since each digit of the ASCII decimal
  115. // representation is a byte, the number of digits equals
  116. // the number of bytes:

  117. int getLength(int someValue) {
  118.   // there's at least one byte:
  119.   int digits = 1;
  120.   // continually divide the value by ten, 
  121.   // adding one to the digit count for each
  122.   // time you divide, until you're at 0:
  123.   int dividend = someValue /10;
  124.   while (dividend > 0) {
  125.     dividend = dividend /10;
  126.     digits++;
  127.   }
  128.   // return the number of digits:
  129.   return digits;
  130. }
复制代码
好了,一切就绪了,那就连上网线,记住网线的另一头是接到路由器确保能上网的,坐等看效果图,先看看PC端然后再看手机端

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个示例代码,演示了如何使用W5500TCP库在Arduino上进行数据传输: ```cpp #include <SPI.h> #include <Ethernet.h> // 定义MAC地址 byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // 定义IP地址、子网掩码和网关 IPAddress ip(192, 168, 1, 177); IPAddress subnet(255, 255, 255, 0); IPAddress gateway(192, 168, 1, 1); // 定义远程主机的IP地址和端口号 IPAddress remoteIP(192, 168, 1, 100); unsigned int remotePort = 1234; EthernetClient client; void setup() { // 初始化以太网连接 Ethernet.begin(mac, ip, gateway, subnet); // 等待以太网连接成功 delay(1000); // 连接远程主机 if (client.connect(remoteIP, remotePort)) { Serial.println("Connected to remote host"); // 发送数据 client.println("Hello, World!"); } else { Serial.println("Connection failed"); } } void loop() { // 检查是否有数据可接收 if (client.available()) { // 读取并打印接收到的数据 char c = client.read(); Serial.print(c); } // 检查连接是否断开 if (!client.connected()) { Serial.println(); Serial.println("Disconnected from remote host"); // 关闭连接 client.stop(); // 重新连接远程主机 if (client.connect(remoteIP, remotePort)) { Serial.println("Connected to remote host"); // 发送数据 client.println("Hello, World!"); } else { Serial.println("Connection failed"); } } } ``` 这段代码使用了Arduino的Ethernet库来管理W5500芯片的网络连接。在`setup()`函数中,首先初始化以太网连接,然后尝试连接远程主机并发送数据。在`loop()`函数中,通过不断检查可用数据和连接状态,实现了数据的接收和连接的重新建立。你可以根据需要进行修改和扩展。 请注意,这只是一个基本示例,具体实现还需要根据你的具体需求进行适当调整。确保你正确设置了MAC地址、IP地址、子网掩码、网关和远程主机的IP地址和端口号。另外,确保你已经安装了相应的W5500TCP库,并在代码中进行引用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值