进阶制作

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

字号+ 作者:PLC工程师 来源:未知 2016-03-19 23:43 我要评论( )

如何通过arduino向yeelink平台发送多个数据 有个朋友遇到这个问题所以做了一个,大家指正

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

有个朋友遇到这个问题所以做了一个,大家指正啊。其实利用官方的库更好一点,这个纯粹是为了学习。



#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#include <math.h>
#include <OneWire.h>
//http://www.plclive.com
byte buff[2];

// for yeelink api
#define APIKEY         "*****************" // 此处替换为你自己的API KEY
#define DEVICEID       345732 // 此处替换为你的设备编号
#define SENSORID1       385081 // 此处替换为你的传感器编号
#define SENSORID2       385082 // 此处替换为你的传感器编号
#define SENSORID3       385099 // 此处替换为你的传感器编号
int sensor1Acted = 0;
int sensor2Acted = 0;
int sensor3Acted = 1;
// assign a MAC address for the ethernet controller.
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192, 168, 1, 177);
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 = 5*1000; // delay between 2 datapoints, 30s
String returnValue = ""; 
boolean ResponseBegin = false;

void setup() {
  Wire.begin();
  // start serial port:
  Serial.begin(9600);
  delay(1000);
  // start the Ethernet connection with DHCP:
  Ethernet.begin(mac, ip);  
  Serial.print("My IP address: ");
  Serial.println(Ethernet.localIP());
  delay(5000);
}

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 (!client.connected() && lastConnected) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
  }

  if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
    double rd1 = lastConnectionTime/20.0;
    double rd2 = lastConnectionTime/10.0;
    double rd3 = lastConnectionTime/5;;

   if(sensor3Acted) { sendData(SENSORID1, rd1);
   sensor3Acted = 0;
   sensor2Acted = 0;
   sensor1Acted = 1;
    delay(1000);}

   else if(sensor1Acted) { sendData(SENSORID2, rd2);
   sensor3Acted = 0;
   sensor2Acted = 1;
   sensor1Acted = 0;
    delay(1000);}
    
   else if(sensor2Acted) { sendData(SENSORID3, rd3);
   sensor3Acted = 1;
   sensor2Acted = 0;
   sensor1Acted = 0;
    delay(1000);}
  }
  lastConnected = client.connected();  
}

// this method makes a HTTP connection to the server:
void sendData(long sensorID, int Data) {
  // if there's a successful connection:
  if (client.connect(server, 80)) {
    long ssID = sensorID;
    
    Serial.println("connecting...");
    // send the HTTP PUT request:
    client.print("POST /v1.1/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.print("Content-Length: ");

    // calculate the length of the sensor reading in bytes:
    // 8 bytes for {"value":} + number of digits of the data:
    int thisLength = 10 + getLength(Data);
    client.println(thisLength);
    
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.println("Connection: close");
    client.println();

    // here's the actual content of the PUT request:
    client.print("{\"value\":");
    client.print(Data);
    client.println("}");
  } 
  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();
}



// This method calculates the number of digits in the
// sensor reading.  Since each digit of the ASCII decimal
// representation is a byte, the number of digits equals
// the number of bytes:
int getLength(int someValue) {
  // there's at least one byte:
  int digits = 1;
  // continually divide the value by ten, 
  // adding one to the digit count for each
  // time you divide, until you're at 0:
  int dividend = someValue /10;
  while (dividend > 0) {
    dividend = dividend /10;
    digits++;
  }
  // return the number of digits:
  return digits;
}





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

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

请加群:214461008

相关文章
网友点评
Arduino
热门教程
关注我们
自动化世界