遥控灯的几种玩法——网络遥控灯

2017-02-073万
精华项目
AI 快速预览详细 收起
本文介绍了一个基于Arduino和W5100网络扩展板的项目,通过Yeelink平台实现手机App和网页远程控制LED开关。项目从用户注册、添加设备和传感器、线路连接、加载库文件到烧录程序,详细讲解了每一步的操作。适合STEM教育项目,让学生在实践中掌握物联网技术。
作为网络时代的居民,怎么能不了解物联网呢?物联网简单点说,就是物物相连的互联网。它是以互联网为核心延伸和扩展的网络,使得用户端延伸和扩展到了任何物品,物品之间能进行信息交换和通信。如图1所示,只要你的路由器连接了互联网,你就可以通过手机App远程控制你的灯,就像在家里控制一样,不论你身处国内还是国外。
遥控灯的几种玩法——网络遥控灯图8
图1 通过App控制灯
今天我们就使用Arduino+W5100网络扩展板,结合Yeelink平台,实现通过手机App和网页远程控制LED的开关。
器材
1.  Overlord Pro 3D打印
2.  PLA 3D打印耗材(直径1.75mm,白色)
3.  Arduino Uno
4.  W5100 DFR0125
5.  LED红色
6.  220Ω电阻

制作过程
1.用户注册
由于该作品是基于Yeelink平台的,所以首先需要登录http://www.yeelink.net进行用户注册。
2.添加设备和传感器
登录Yeelink平台,单击用户头像进入用户中心,然后单击“我的设备”→“添加新设备”进入设置设备界面,按要求填写有关设备的情况(见图2)。
遥控灯的几种玩法——网络遥控灯图1
图2添加新设备
    当完成新设备“WebLed”的添加后,单击“管理设备”进入“WebLed”设备管理界面(见图3),下方有“添加传感器”按钮,可为该设备添加一个传感器并设置其属性。在本例中,传感器类型请选择为“开关”,设置完成后保存即可。
遥控灯的几种玩法——网络遥控灯图4
图3 添加传感器
    此时,再次单击“管理设备”界面,我们就新建了一个名为 “WebLed”的设备,并且为该设备添加了开关型传感器“WebLed开关”。如果需要查看设备编号和传感器编号,在下图中“状态URL”可见。其中device后面跟的数字“344482”为设备编号,sensor后面跟的数字“382935”为传感器编号。这两个数字,请记好后面我们还会用到。至此,我们的前期平台注册工作已经结束,下面开始数据连线。
遥控灯的几种玩法——网络遥控灯图5
(3)线路连接
本次数据连线较为简单,将W5100网络拓展板对应叠加插在Arduino Uno上,LED(数字口5)与电阻的连接LED正极。
遥控灯的几种玩法——网络遥控灯图9
4)加载库文件
安装Yeelink Sdk,进入Yeelink官网下载Yeelink Sdk,下载完成后解压,并将文件夹复制到Arduino安装目录下的Libraries目录下,例如:“D:\Arduino-1.0.5\Libraries”,复制完成之后重启Arduino软件,即可使用Yeelink的库。
注意事项:
a.这里尤其请注意将文件夹名中的下划线去掉,否则会出现无法调用库的编译错误。
b解压到Arduino安装目录下的Libraries目录下必须是根目录文件夹,不能再包含文件夹。 否则一样会出现无法调用库的编译错误。
5)烧录程序.
将以下代码复制到Arduino编译器中,注意修改用户API号,设备号和传感器号后,将程序刷入控制板。API编号在“账户”——“我的账户设置”中查看。
遥控灯的几种玩法——网络遥控灯图10
代码
  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3. #include <Wire.h>
  4. #include <math.h>
  5. byte buff[2];// for yeelink api
  6. #define APIKEY "a7ddf40c770aafd53d4d5df3161f215c" // 此处替换为你自己的API Key
  7. #define DEVICEID 344482 // 此处替换为你的设备编号
  8. #define SENSORID1 382935 // 此处替换为你的传感器编号
  9. // assign a MAC address for the ethernet controller.
  10. byte mac[] = { 0x00, 0x1D, 0x72, 0x82, 0x35, 0x9D};
  11. // initialize the library instance:
  12. EthernetClient client ;
  13. char server[] = "api.yeelink.net"; // name address for yeelink API
  14. unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
  15. boolean lastConnected = false; // state of the connection last time through the main loop
  16. const unsigned long postingInterval = 3*1000; // delay between 2 datapoints, 30s
  17. String returnValue = "";
  18. boolean ResponseBegin = false;
  19. void setup() {
  20. pinMode(5, OUTPUT);
  21. Wire.begin();
  22. // start serial port:
  23. Serial.begin(57600);
  24. // start the Ethernet connection with DHCP:
  25. if (Ethernet.begin(mac) == 0) {
  26. Serial.println("Failed to configure Ethernet using DHCP");
  27. for(;;)
  28. ;
  29. }
  30. else {
  31. Serial.println("Ethernet configuration OK");
  32. }
  33. }
  34. void loop() {
  35. // if there's incoming data from the net connection.
  36. // send it out the serial port. This is for debugging
  37. // purposes only:
  38. if (client.available()) {
  39. char c = client.read();
  40. // Serial.print(c);
  41. if (c == '{')
  42. ResponseBegin = true;
  43. else if (c == '}')
  44. ResponseBegin = false;
  45. if (ResponseBegin)
  46. returnValue += c;
  47. }
  48. if (returnValue.length() !=0 && (ResponseBegin == false))
  49. {
  50. Serial.println(returnValue);
  51. if (returnValue.charAt(returnValue.length() - 1) == '1') {
  52. Serial.println("turn on the LED");
  53. digitalWrite(5, HIGH);
  54. }
  55. else if(returnValue.charAt(returnValue.length() - 1) == '0') {
  56. Serial.println("turn off the LED");
  57. digitalWrite(5, LOW);
  58. }
  59. returnValue = "";
  60. }
  61. // if there's no net connection, but there was one last time
  62. // through the loop, then stop the client:
  63. if (!client.connected() && lastConnected) {
  64. Serial.println();
  65. Serial.println("disconnecting.");
  66. client.stop();
  67. }
  68. // if you're not connected, and ten seconds have passed since
  69. // your last connection, then connect again and send data:
  70. if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
  71. // read sensor data, replace with your code
  72. //int sensorReading = readLightSensor();
  73. Serial.print("yeelink:");
  74. //get data from server
  75. getData();
  76. }
  77. // store the state of the connection for next time through
  78. // the loop:
  79. lastConnected = client.connected();
  80. }
  81. // this method makes a HTTP connection to the server and get data back
  82. void getData(void) {
  83. // if there's a successful connection:
  84. if (client.connect(server, 80)) {
  85. Serial.println("connecting...");
  86. // send the HTTP GET request:
  87. client.print("GET /v1.0/device/");
  88. client.print(DEVICEID);
  89. client.print("/sensor/");
  90. client.print(SENSORID1);
  91. client.print("/datapoints");
  92. client.println(" HTTP/1.1");
  93. client.println("Host: api.yeelink.net");
  94. client.print("Accept: *");
  95. client.print("/");
  96. client.println("*");
  97. client.print("U-ApiKey: ");
  98. client.println(APIKEY);
  99. client.println("Content-Length: 0");
  100. client.println("Connection: close");
  101. client.println();
  102. Serial.println("print get done.");
  103. }
  104. else {
  105. // if you couldn't make a connection:
  106. Serial.println("connection failed");
  107. Serial.println();
  108. Serial.println("disconnecting.");
  109. client.stop();
  110. }
  111. // note the time that the connection was made or attempted:
  112. lastConnectionTime = millis();
  113. }
复制代码

6)制作外壳
使用SketchupWebLed设计制作一个外壳,具体模型文件如下。
遥控灯的几种玩法——网络遥控灯图2遥控灯的几种玩法——网络遥控灯图3
遥控灯的几种玩法——网络遥控灯图11
将叠加后的板子放入打印件中,将红色LED固定在顶盖上。
遥控灯的几种玩法——网络遥控灯图12
7)测试运行.
将网线插入W5100网口,给Arduino Uno上电。分别用手机客户端、网页客户端和打开和关闭LED。
遥控灯的几种玩法——网络遥控灯图6遥控灯的几种玩法——网络遥控灯图7

8)任务拓展.
     也许你觉得这个LED不是很亮,那就弄个亮的吧,完全可以通过下面的设备来做一盏更亮的WebLed。
遥控灯的几种玩法——网络遥控灯图13
下图中的灯与上面的没有本质区别,都是通过网络来进行开关,只不过通过继电器外接了大电流设备。如果你胆大点完全可以弄个交流设备的灯来玩玩,接线如下,这里不再详细介绍。
遥控灯的几种玩法——网络遥控灯图14

创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(7)
派大星ym
派大星ym
,,,,
派大星ym
派大星ym
厉害厉害了
小马1
小马1
哇啊 我真的好喜欢呀 做的真不错呀 非常棒
luna
luna
棒呆! 是不是还可以做到同时控制好几个灯?
hnyzcj
hnyzcj
作者
回复luna
可以的LUNA
hnyzcj
hnyzcj
作者
聪明
hnyzcj
hnyzcj
作者
可以呀
fy7554
fy7554回复hnyzcj
是不是就用一块W5500主控模块和数字继电器模块就可以啊?
fy7554
fy7554
用你最后一个图来控制家里的AC220V的灯,和原先家里墙上的开关可以一起用吗?
- 没有更多了 -