布列松 发表于 2014-12-11 21:49:30

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

本帖最后由 布列松 于 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。



#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#include <math.h>

byte buff;
#define APIKEY         "xxxxxx" // 此处替换为你自己的API KEY
#define DEVICEID       xxxxxx // 此处替换为你的设备编号
char flag = 0; //定义开关循环控制变量
String SENSORID[] = {"xxxxx", "xxxxx", "xxxxx","xxxxx","xxxxx","xxxxx","xxxxx","xxxxx","xxxxx","xxxxx","xxxxx"}; // 把sensor ID 依次替在这里,程序定义为数组,几个开关就写几个
byte ledPin[] = {5, 6, 7, 8, 9, 14, 15, 16, 17, 18, 19};
// assign a MAC address for the ethernet controller.
byte mac[] = { 0x00, 0x1D, 0x72, 0x82, 0x35, 0x9D};
// initialize the library instance:
EthernetClient client ;
char server[] = "api.yeelink.net";   // name address for yeelink API
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 = 3 * 1000; // delay between 2 datapoints, 30s
String returnValue = "";
boolean ResponseBegin = false;

void setup() {
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(14, OUTPUT);
pinMode(15, OUTPUT);
pinMode(16, OUTPUT);
pinMode(17, OUTPUT);
pinMode(18, OUTPUT);
pinMode(19, OUTPUT);

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

// start serial port:
Serial.begin(57600);
// start the Ethernet connection with DHCP:
if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    for (;;)
      ;
}
else {
    Serial.println("Ethernet configuration OK");
}
}

void loop() {
// 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 (c == '{')
      ResponseBegin = true;
    else if (c == '}')
      ResponseBegin = false;
    if (ResponseBegin)
      returnValue += c;
}
if (returnValue.length() != 0 && (ResponseBegin == false))
{
    Serial.println(returnValue);
    if (returnValue.charAt(returnValue.length() - 1) == '1') {
      Serial.println("turn on the LED");
      digitalWrite(ledPin, HIGH);
    }
    else if (returnValue.charAt(returnValue.length() - 1) == '0') {
      Serial.println("turn off the LED");
      digitalWrite(ledPin, LOW);
    }
    returnValue = "";
    flag++;
}
// 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)) {
    // read sensor data, replace with your code
    //int sensorReading = readLightSensor();
    Serial.print("yeelink:");
    //get data from server
    // getData();
    if (flag < 11) ///更改为开关的个数
    {
      //get data from server
      getData();
    }
    else
    {
      //put data to server
      if (flag = 12) {
      flag = 0; ///更改为开关的个数+1
      }
    }
}

// store the state of the connection for next time through
// the loop:
lastConnected = client.connected();
}
// this method makes a HTTP connection to the server and get data back
void getData(void) {
// if there's a successful connection:
if (client.connect(server, 80)) {
    Serial.println("connecting...");
    client.print("GET /v1.0/device/");
    client.print(DEVICEID);
    client.print("/sensor/");
    client.print(SENSORID);
    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.println("Content-Length: 0");
    client.println("Connection: close");
    client.println();
    Serial.println("print get done.");
}
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();
}


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

有几个问题请大家帮忙:
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灯开了就关不了,不知是主板问题还是代码问题。



测试视频:
http://v.youku.com/v_show/id_XODQ2NzUyMDcy.html

林定祥 发表于 2014-12-11 21:53:24

上视频吧,读代码多枯燥啊:(

逍遥子 发表于 2014-12-13 22:17:38

这个还是比较厉害的!!!!!哟哟一定的使用价值!!!

kokomi 发表于 2014-12-22 17:14:10

你不是买的r3网卡吗.

布列松 发表于 2014-12-22 18:38:04

kokomi 发表于 2014-12-22 17:14 static/image/common/back.gif
你不是买的r3网卡吗.

W5100 R3版本的 就正常

简单侣图 发表于 2015-1-22 14:00:32

if (flag = 12) {
      flag = 0; ///更改为开关的个数+1
      }
是不是是应该换成falg ==12呢?这是错误吧?:L

简单侣图 发表于 2015-1-22 14:12:46

而且我想知道我随机打开开关也是可以控制的,那flag的作用是用来干嘛的,按顺序才能打开灯吗

布列松 发表于 2015-1-23 09:28:39

简单侣图 发表于 2015-1-22 14:00 static/image/common/back.gif
if (flag = 12) {
      flag = 0; ///更改为开关的个数+1
      }


这个我也觉得是个问题。。。。。。

雨轩 发表于 2015-5-31 14:13:15

纳尼:o为什么你控制那么快,我才带了六个灯就延迟很厉害

雨轩 发表于 2015-5-31 14:16:50

怎么将DHT11加上去,代码怎么样,谢谢!
页: [1]
查看完整版本: Yeelink arduino uno + W5100 实现11个LED开关点亮