极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 12658|回复: 2

arduino+wifi shield链接yeelink控制led 的问题

[复制链接]
发表于 2016-4-11 06:36:45 | 显示全部楼层 |阅读模式
在yeelink官网上和其 它网络上多是w5100联网的案例,我用的是wifi shield网页设置好wifi,控制led,代码(来自论坛)如下:
boolean ResponseBegin = false;
String returnValue = "";
unsigned long previousMillis = 0;        
unsigned long interval = 900;
char c ;
void setup()
{
  Serial.begin(115200);
  pinMode(13,OUTPUT);
  
}

void loop()
{

  if(Serial.available())
    c = Serial.read();
  if(c == '{')
    ResponseBegin = true;
  if(c == '}')
    ResponseBegin = false;
  if (ResponseBegin)
    returnValue += c;

  if (returnValue.length() !=0 && (ResponseBegin == false))
  {
    if(returnValue.charAt(returnValue.length() - 1) == '1')
      {digitalWrite(13,HIGH);Serial.println("ON");}
    if(returnValue.charAt(returnValue.length() - 1) == '0')
      {digitalWrite(13,LOW);Serial.println("OFF");}      
    returnValue = "";
  }

  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis > interval)
  {
    previousMillis = currentMillis;
    Serial.println("GET /v1.0/device/7794/sensor/12316/datapoints HTTP/1.1"); //“/device/4290/sensor/9970/”device,sensor 后面的数值用你自己的替代
    Serial.println("Host: api.yeelink.net");
    Serial.print("Accept: *");
    Serial.print("/");
    Serial.println("*");
    Serial.print("U-ApiKey: ");
    Serial.println("****************");   // 此处用你的APIKEY替代
    Serial.println("Content-Length: 0");
    Serial.println("Connection: close");
    Serial.println();
    }
}  
总是实现不了控制led的功能,请问怎么回事》谢谢

本帖子中包含更多资源

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

x
回复

使用道具 举报

发表于 2016-4-17 11:09:36 | 显示全部楼层
参考下这个吧!
  1. #include <EtherCard.h>

  2. static byte mymac[] = {0x74,0x69,0x69,0x2D,0x30,0x21};
  3. const char server[] PROGMEM = "api.yeelink.net";

  4. byte Ethernet::buffer[700];
  5. byte sensorData;
  6. static uint32_t lastConnectionTime = 0;   
  7. const unsigned long postingInterval = 100;

  8. const char SensorID[] PROGMEM = "171286";
  9. const char APIKey[] PROGMEM = "2c4265c045794880588ffeea97d70216";// replace your yeelink api key here
  10. const char DeviceID[] PROGMEM = "153110"; // replace your device ID

  11. Stash stash;
  12. void setup () {
  13. InitIP();
  14. }

  15. void loop() {

  16.   ether.packetLoop(ether.packetReceive());
  17.   if(millis() - lastConnectionTime > postingInterval){
  18.     GetData();
  19.   }
  20. }

  21. void GetData()
  22. {
  23.     float demo = random(0,500);
  24.     word one = random(0,500);
  25.     String msje;

  26.     if (demo < 250){
  27.       msje = "low";
  28.     }
  29.     else{
  30.       msje = "high";
  31.     }

  32.   byte sd = stash.create();
  33.     stash.print("demo,");
  34.     stash.println(demo);
  35.     stash.print("one,");
  36.     stash.println(one);
  37.     stash.print("mensaje,");
  38.     stash.println(msje);
  39.     stash.save();

  40.   Stash::prepare(PSTR("GET /v1.0/device/$F/sensor/$F/datapoints HTTP/1.1" "\r\n"
  41.       "Host:api.yeelink.net" "\r\n"
  42.       "U-ApiKey: $F" "\r\n"
  43.       "Content-Length:$D" "\r\n"
  44.       "\r\n""$H"),DeviceID,SensorID,APIKey,stash.size(),sd);

  45.   sensorData = ether.tcpSend();

  46.    const char* reply = ether.tcpReply(sensorData);
  47.     const char* re;
  48.     char t[3];
  49.     if(reply != 0) {
  50.        Serial.println(reply);
  51.        Serial.println(strstr(reply,"{"));

  52.       if(strstr(reply,"1}") != NULL)
  53.       { Serial.println("Led state: ON");return;}

  54.         if(strstr(reply,"0}") != NULL)
  55.       { Serial.println("Led state: OFF");return;}

  56.     }else
  57.     {
  58. //      Serial.println("2");
  59.     }

  60.     lastConnectionTime = millis();
  61. }


  62. void InitIP()
  63. {
  64. Serial.begin(57600);
  65.   Serial.println("Client Demo");
  66.   Serial.println();

  67.   if (!ether.begin(sizeof Ethernet::buffer, mymac, 10))
  68.     Serial.println( "Failed to access Ethernet controller");
  69. else
  70.    Serial.println("Ethernet controller initialized");
  71. Serial.println();

  72.   if (!ether.dhcpSetup())
  73.     Serial.println("Failed to get configuration from DHCP");
  74.   else
  75.     Serial.println("DHCP configuration done");

  76.   ether.printIp("IP Address:\t", ether.myip);
  77.   ether.printIp("Netmask:\t", ether.netmask);
  78.   ether.printIp("Gateway:\t", ether.gwip);
  79.   Serial.println();

  80.   if (!ether.dnsLookup(server))
  81.     Serial.println("DNS failed");
  82.   else
  83.    Serial.println("DNS resolution done");  
  84.   ether.printIp("SRV IP:\t", ether.hisip);
  85.   Serial.println();
  86. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-4-18 11:50:32 | 显示全部楼层
363759554 发表于 2016-4-17 11:09
参考下这个吧!

谢谢,非常感谢
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-15 00:45 , Processed in 0.057130 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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