Arduino W5100 Yeelink PIN7控制

代码段参考

/*
  Yeelink 网页远程控制Arduino演示代码
  1. 使用arduino UNO和 ethernet shield
  2.  使用数字7管脚网页控制LED灯
*/
#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
byte buff[2];
// for yeelink api
#define APIKEY "ff9763faf29b9ba9b231ee207e77d2ba" //更换 yeelink api key
#define DEVICEID 360359 // 更换设备IDreplace your device ID
#define SENSORID 411138 // 更换传感器IDreplace your sensor ID
// 分配MAC地址.
byte mac[] = { 0x00, 0x1D, 0x72, 0x82, 0x35, 0x9D};
// 初始化以太网库:
EthernetClient client;
char server[] = "api.yeelink.net"; // 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(7, OUTPUT);
  Wire.begin();
  // start serial port:
  Serial.begin(9600);
  // 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 (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(7, LOW);
    }
    else if (returnValue.charAt(returnValue.length() - 1) == '0') {
      Serial.println("turn off the LED");
      digitalWrite(7, HIGH);
    }
    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();
}#define DHT11_PIN 0
int Led=8;
int Buzzer=7;
byte read_dht11_dat()
{
byte i = 0;
byte result = 0;
for(i=0;i<8;i++)
{
while(!(PINC&_BV(DHT11_PIN)));
delayMicroseconds(30);
if(PINC&_BV(DHT11_PIN))
result|=(1<<(7-i));
while((PINC&_BV(DHT11_PIN)));
}
return result;
}
void setup()
{
  DDRC|=_BV(DHT11_PIN);
  PORTC|=_BV(DHT11_PIN);
  pinMode(Led,OUTPUT);
  pinMode(Buzzer,OUTPUT);
  Serial.begin(19200);
  Serial.println("Ready");
}
void loop()
{
byte dht11_dat[5];
byte dht11_in;
byte i;
PORTC &= ~_BV(DHT11_PIN);
delay(18);
PORTC|=_BV(DHT11_PIN);
delayMicroseconds(40);
DDRC &= ~_BV(DHT11_PIN);
delayMicroseconds(40);
dht11_in = PINC & _BV(DHT11_PIN);
if(dht11_in)
{
Serial.println("dht11 start condition 1 not met");
return;
}
delayMicroseconds(80);
dht11_in=PINC & _BV(DHT11_PIN);
if(!dht11_in)
{
  Serial.println("dht11 start condition 2 not met");
  return;
}
delayMicroseconds(80);
for(i=0;i<5;i++)
dht11_dat[ i]=read_dht11_dat();
DDRC|=_BV(DHT11_PIN);
PORTC|=_BV(DHT11_PIN);
byte dht11_check_sum = dht11_dat[0]+dht11_dat[1]+dht11_dat[2]+dht11_dat[3];
if(dht11_dat[4]!=dht11_check_sum)
{
Serial.println("DHT11 checksum error");
}
Serial.print("Current humdity= ");
Serial.print(dht11_dat[0],DEC);
Serial.print(".");
Serial.print(dht11_dat[1],DEC);
Serial.print("%");
Serial.print("temperature = ");
Serial.print(dht11_dat[2],DEC);
Serial.print(".");
Serial.print(dht11_dat[3],DEC);
Serial.println("C");
if(dht11_dat[0]==25)
digitalWrite(Led,HIGH);
else
digitalWrite(Led,LOW);
if(dht11_dat[0]==28)
digitalWrite(Buzzer,LOW);
else
digitalWrite(Buzzer,HIGH);
delay(2000);
}
      
      
     
     
    
    
   
   



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值