极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 18758|回复: 9

Yeelink arduino uno + W5100 实现11个LED开关点亮

[复制链接]
发表于 2014-12-11 21:49:30 | 显示全部楼层 |阅读模式
本帖最后由 布列松 于 2014-12-13 23:03 编辑

Yeelink arduino uno + W5100 把11个LED开关点亮,分别接数字口{5, 6, 7, 8, 9, 14, 15, 16, 17, 18, 19},A0~A5代替数字D14~D19。



  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3. #include <Wire.h>
  4. #include <math.h>

  5. byte buff[2];
  6. #define APIKEY         "xxxxxx" // 此处替换为你自己的API KEY
  7. #define DEVICEID       xxxxxx // 此处替换为你的设备编号
  8. char flag = 0; //定义开关循环控制变量
  9. String SENSORID[] = {"xxxxx", "xxxxx", "xxxxx","xxxxx","xxxxx","xxxxx","xxxxx","xxxxx","xxxxx","xxxxx","xxxxx"}; // 把sensor ID 依次替在这里,程序定义为数组,几个开关就写几个
  10. byte ledPin[] = {5, 6, 7, 8, 9, 14, 15, 16, 17, 18, 19};
  11. // assign a MAC address for the ethernet controller.
  12. byte mac[] = { 0x00, 0x1D, 0x72, 0x82, 0x35, 0x9D};
  13. // initialize the library instance:
  14. EthernetClient client ;
  15. char server[] = "api.yeelink.net";   // name address for yeelink API
  16. unsigned long lastConnectionTime = 0;          // last time you connected to the server, in milliseconds
  17. boolean lastConnected = false;                 // state of the connection last time through the main loop
  18. const unsigned long postingInterval = 3 * 1000; // delay between 2 datapoints, 30s
  19. String returnValue = "";
  20. boolean ResponseBegin = false;

  21. void setup() {
  22.   pinMode(5, OUTPUT);
  23.   pinMode(6, OUTPUT);
  24.   pinMode(7, OUTPUT);
  25.   pinMode(8, OUTPUT);
  26.   pinMode(9, OUTPUT);
  27.   pinMode(14, OUTPUT);
  28.   pinMode(15, OUTPUT);
  29.   pinMode(16, OUTPUT);
  30.   pinMode(17, OUTPUT);
  31.   pinMode(18, OUTPUT);
  32.   pinMode(19, OUTPUT);

  33.   for (int i = 0; i < 11; i++) //I<7,几个开关,就把7改成几,其实就是循次数
  34.   {
  35.     pinMode(ledPin[i], OUTPUT);
  36.   }
  37.   Wire.begin();

  38.   // start serial port:
  39.   Serial.begin(57600);
  40.   // start the Ethernet connection with DHCP:
  41.   if (Ethernet.begin(mac) == 0) {
  42.     Serial.println("Failed to configure Ethernet using DHCP");
  43.     for (;;)
  44.       ;
  45.   }
  46.   else {
  47.     Serial.println("Ethernet configuration OK");
  48.   }
  49. }

  50. void loop() {
  51.   // if there's incoming data from the net connection.
  52.   // send it out the serial port.  This is for debugging
  53.   // purposes only:
  54.   if (client.available()) {
  55.     char c = client.read();
  56.     // Serial.print(c);
  57.     if (c == '{')
  58.       ResponseBegin = true;
  59.     else if (c == '}')
  60.       ResponseBegin = false;
  61.     if (ResponseBegin)
  62.       returnValue += c;
  63.   }
  64.   if (returnValue.length() != 0 && (ResponseBegin == false))
  65.   {
  66.     Serial.println(returnValue);
  67.     if (returnValue.charAt(returnValue.length() - 1) == '1') {
  68.       Serial.println("turn on the LED");
  69.       digitalWrite(ledPin[flag], HIGH);
  70.     }
  71.     else if (returnValue.charAt(returnValue.length() - 1) == '0') {
  72.       Serial.println("turn off the LED");
  73.       digitalWrite(ledPin[flag], LOW);
  74.     }
  75.     returnValue = "";
  76.     flag++;
  77.   }
  78.   // if there's no net connection, but there was one last time
  79.   // through the loop, then stop the client:
  80.   if (!client.connected() && lastConnected) {
  81.     Serial.println();
  82.     Serial.println("disconnecting.");
  83.     client.stop();
  84.   }
  85.   // if you're not connected, and ten seconds have passed since
  86.   // your last connection, then connect again and send data:
  87.   if (!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
  88.     // read sensor data, replace with your code
  89.     //int sensorReading = readLightSensor();
  90.     Serial.print("yeelink:");
  91.     //get data from server
  92.     // getData();
  93.     if (flag < 11) ///更改为开关的个数
  94.     {
  95.       //get data from server
  96.       getData();
  97.     }
  98.     else
  99.     {
  100.       //put data to server
  101.       if (flag = 12) {
  102.         flag = 0; ///更改为开关的个数+1
  103.       }
  104.     }
  105.   }

  106.   // store the state of the connection for next time through
  107.   // the loop:
  108.   lastConnected = client.connected();
  109. }
  110. // this method makes a HTTP connection to the server and get data back
  111. void getData(void) {
  112.   // if there's a successful connection:
  113.   if (client.connect(server, 80)) {
  114.     Serial.println("connecting...");
  115.     client.print("GET /v1.0/device/");
  116.     client.print(DEVICEID);
  117.     client.print("/sensor/");
  118.     client.print(SENSORID[flag]);
  119.     client.print("/datapoints");
  120.     client.println(" HTTP/1.1");
  121.     client.println("Host: api.yeelink.net");
  122.     client.print("Accept: *");
  123.     client.print("/");
  124.     client.println("*");
  125.     client.print("U-ApiKey: ");
  126.     client.println(APIKEY);
  127.     client.println("Content-Length: 0");
  128.     client.println("Connection: close");
  129.     client.println();
  130.     Serial.println("print get done.");
  131.   }
  132.   else {
  133.     // if you couldn't make a connection:
  134.     Serial.println("connection failed");
  135.     Serial.println();
  136.     Serial.println("disconnecting.");
  137.     client.stop();
  138.   }
  139.   // note the time that the connection was made or attempted:
  140.   lastConnectionTime = millis();
  141. }
复制代码



这段代码原本是 论坛:风生水起、口腔(戴志强)等,然后我再修改。

有几个问题请大家帮忙:
1、    if (flag < 11) ///更改为开关的个数
    {
      //get data from server
      getData();
    }
    else
    {
      //put data to server
      if (flag = 12) {
        flag = 0; ///更改为开关的个数+1
      }
这部分代码不明白作用,但对整个有影响。请问我要加入LM35上传数据,应该怎样修改。

2、D18、D19数字口,不知为什么无法控制。(初步验证,Yeelink的网络传输是正常的,是UNO主板的D18、D19(A4、A5)数字端口的电平电压过分的低,不知原因,更换其它UNO主板问题都一样。而且这两个口的会被其它数据口的开关控制着、联动)10、11、12、13号端口用于SPI通信;  4号端口用于TF卡片选; 10号端口为W5100片选;


2014/12/13 发现W5100主板会有死机现象,LED灯开了就关不了,不知是主板问题还是代码问题。



测试视频:

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2014-12-11 21:53:24 | 显示全部楼层
上视频吧,读代码多枯燥啊
回复 支持 反对

使用道具 举报

发表于 2014-12-13 22:17:38 | 显示全部楼层
这个还是比较厉害的!!!!!哟哟一定的使用价值!!!
回复 支持 反对

使用道具 举报

发表于 2014-12-22 17:14:10 | 显示全部楼层
你不是买的r3网卡吗.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-12-22 18:38:04 | 显示全部楼层
kokomi 发表于 2014-12-22 17:14
你不是买的r3网卡吗.

W5100 R3版本的 就正常
回复 支持 反对

使用道具 举报

发表于 2015-1-22 14:00:32 | 显示全部楼层
if (flag = 12) {
        flag = 0; ///更改为开关的个数+1
      }
是不是是应该换成falg ==12呢?这是错误吧?
回复 支持 反对

使用道具 举报

发表于 2015-1-22 14:12:46 | 显示全部楼层
而且我想知道我随机打开开关也是可以控制的,那flag的作用是用来干嘛的,按顺序才能打开灯吗
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-1-23 09:28:39 | 显示全部楼层
简单侣图 发表于 2015-1-22 14:00
if (flag = 12) {
        flag = 0; ///更改为开关的个数+1
      }

这个我也觉得是个问题。。。。。。
回复 支持 反对

使用道具 举报

发表于 2015-5-31 14:13:15 | 显示全部楼层
纳尼为什么你控制那么快,我才带了六个灯就延迟很厉害
回复 支持 反对

使用道具 举报

发表于 2015-5-31 14:16:50 | 显示全部楼层
怎么将DHT11加上去,代码怎么样,谢谢!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-16 21:14 , Processed in 0.058254 second(s), 23 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表