dymrkj 发表于 2016-4-11 06:36:45

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

在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的功能,请问怎么回事》谢谢

363759554 发表于 2016-4-17 11:09:36

参考下这个吧!#include <EtherCard.h>

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

byte Ethernet::buffer;
byte sensorData;
static uint32_t lastConnectionTime = 0;   
const unsigned long postingInterval = 100;

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

Stash stash;
void setup () {
InitIP();
}

void loop() {

ether.packetLoop(ether.packetReceive());
if(millis() - lastConnectionTime > postingInterval){
    GetData();
}
}

void GetData()
{
    float demo = random(0,500);
    word one = random(0,500);
    String msje;

    if (demo < 250){
      msje = "low";
    }
    else{
      msje = "high";
    }

byte sd = stash.create();
    stash.print("demo,");
    stash.println(demo);
    stash.print("one,");
    stash.println(one);
    stash.print("mensaje,");
    stash.println(msje);
    stash.save();

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

sensorData = ether.tcpSend();

   const char* reply = ether.tcpReply(sensorData);
    const char* re;
    char t;
    if(reply != 0) {
       Serial.println(reply);
       Serial.println(strstr(reply,"{"));

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

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

    }else
    {
//      Serial.println("2");
    }

    lastConnectionTime = millis();
}


void InitIP()
{
Serial.begin(57600);
Serial.println("Client Demo");
Serial.println();

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

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

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

if (!ether.dnsLookup(server))
    Serial.println("DNS failed");
else
   Serial.println("DNS resolution done");
ether.printIp("SRV IP:\t", ether.hisip);
Serial.println();
}

dymrkj 发表于 2016-4-18 11:50:32

363759554 发表于 2016-4-17 11:09 static/image/common/back.gif
参考下这个吧!

谢谢,非常感谢
页: [1]
查看完整版本: arduino+wifi shield链接yeelink控制led 的问题