Yeelink W5100控制步进电机(问题)改进程序-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 3249|回复: 0

[过期] Yeelink W5100控制步进电机(问题)改进程序

[复制链接]
发表于 2014-6-10 08:59 | 显示全部楼层 |阅读模式
大家好,我在用yeelink 的开关控制步进电机的时候希望实现的功能是,当按打开是电机可以正转100下之后停止(直到下一个指令的发出)不希望他继续在loop里面不停的转。反之当按开关关闭时电机反转100下下之后停止(直到下一个指令的发出)。现在已经实现可以正转100下和反转100下但是无法实现让他停下来。
代码:
#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#include <math.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

byte buff[2];

// for yeelink api
#define APIKEY         "a90a707e427e6b6e1cf71d3bb9723eaa" // 此处替换为你自己的API KEY
#define DEVICEID       10934 // 此处替换为你的设备编号
#define SENSORID1      18119 // 此处替换为你的传感器编号


// 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*500; // delay between 2 datapoints, 15s
String returnValue = "";
boolean ResponseBegin = false;
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);

void setup() {
  pinMode(5, 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");
  }
  AFMS.begin();
  myMotor->setSpeed(10);
}

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 curtain");
      digitalWrite(5, HIGH);
      myMotor->step(100, FORWARD, DOUBLE);
     digitalWrite(5,LOW);
      
    }
      else if(returnValue.charAt(returnValue.length() - 1) == '0') {
      Serial.println("turn off the curtain");
      digitalWrite(5, HIGH);
        myMotor->step(100, BACKWARD, DOUBLE);
     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(SENSORID1);
    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();
}
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-12-5 10:11 , Processed in 0.092472 second(s), 16 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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