进阶制作

arduino+w5100通过yeelink远程控制舵机

字号+ 作者:PLC工程师 来源:未知 2016-03-01 12:59 我要评论( )

通过yeelink远程控制舵机

一、简述:

使用Arduino和W5100网络扩展板并结合Yeelink平台,实现通过手机APP或者网页远程控制继电器的通断,继而控制大电流用电器的开关,本例中以舵机为例。

二、硬件准备:




三、软件环境准备:

1. 成为Yeelink开发者,如果您还没有注册,点击这里注册。

2. 下载Yeelink移动客户端APP,点击这里下载

3. 安装Arduino开发环境,如果您的电脑上没有安装Arduino开发环境,点击这里学习如何安装Arduino

 

四、在Yeelink平台上添加设备和传感器

1. 进入用户中心




2. 增加新设备





3. 增加传感器







五、传输程序

1.将以下代码复制到Arduino编译器中

/*
 Yeelink sensor client power switch example
 */

#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#include <math.h>
#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0;    // variable to store the servo position 
byte buff[2];

// for yeelink api
#define APIKEY         "dc7d1c98898fa2e451xxxxxxxxxx" // 此处替换为你自己的API KEY
#define DEVICEID       3933 // 此处替换为你的设备编号
#define SENSORID       5578 // 此处替换为你的传感器编号

// 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);
  Wire.begin();
  // start serial port:
 Serial.begin(57600);
      myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  // 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(5, HIGH);
        for(pos = 0; pos <180; pos += 1)  // goes from 0 degrees to 180 degrees 
         {                                  // in steps of 1 degree 
          myservo.write(180);              // tell servo to go to position in variable 'pos' 
          delay(10);                       // waits 15ms for the servo to reach the position 
         } 
        for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
          {                                
          myservo.write(0);              // tell servo to go to position in variable 'pos' 
          delay(10);                       // waits 15ms for the servo to reach the position 
         } 
    } 
      else if(returnValue.charAt(returnValue.length() - 1) == '0') {
      Serial.println("turn off the LED"); 
     digitalWrite(5, LOW);


      }
     returnValue = "";
  }
  // 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();
  }
  // 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...");
    // send the HTTP GET request:
    
    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();
}

2. 在程序中相应的位置替换自己的API KEY、设备编号和传感器编号








修改完程序后保存一下,接下来我们来连接线路

 

六、线路连接:

1将W5100扩展版对应安插在Arduino上




2. 按下图连接线路


3. 用USB数据线将Arduino与电脑连接起来



4. 用网线将W5100连接到路由器



5. 将准备好的程序传输到Arduino开发板上



大功告成!我们可以通过手机APP和网页来进行控制了!

 

七、通过手机APP或者网页控制

1. 通过手机APP控制




2. 通过Yeelink网页平台控制

在用户中心中选择“我的设备”—“管理设备”选择相应的设备即可看到控制开关




恭喜您,在yeelink平台的帮助下,你已经能够通过继电器来控制舵机的开关,您可以用同样的原理控制更多大电流用电器,或者探索舵机的更多使用方法。


如需更详细资料,如有好资源分享,请加入QQ交流群:214461008

欢迎阅读,欢迎转载,不用注明出处!本站只提供入门和进阶资料,做您学习的小伙伴!

老司机也欢迎光临指导!有好内容请加群:214461008 不要看声明了,看内容吧!

相关文章
  • 如何通过arduino向yeelink平台发送多个数据

    如何通过arduino向yeelink平台发送多个数据

    2016-03-19 23:43

  • 通过Arduino获取web内容

    通过Arduino获取web内容

    2016-03-19 16:33

  • 基于Arduino的智能农业系统,采用Yeelink监视

    基于Arduino的智能农业系统,采用Yeelink监视

    2016-03-01 22:36

  • arduino+w5100通过网络监控室内温度变化

    arduino+w5100通过网络监控室内温度变化

    2016-03-01 15:52

网友点评