arduino 用网络连接yeelink平台控制led
以下是代码:
#include <SPI.h> #include <Ethernet.h> #include <Wire.h> #include <math.h> #include <Servo.h> //QQqun:51798659 XiaoHe此代码是用以太网连接yeelink平台实现点亮一个led,访问http://api.yeelink.net/v1.1/device/357111/sensor/555555/datapoints/后面提交的是key来获取一个网页信息,然后再加以提出需要的判断 Servo myservo; // create servo object to control a servo // a maximum of eight servo objects can be created int pos = 0; // variable to store the servo position byte buff[2]; int led=5;//led脚 // for yeelink api #define APIKEY "c2a4877a26b7bd000872227c60b67406" // 此处替换为你自己的API KEY #define DEVICEID 35740 // 此处替换为你的设备编号 #define SENSORID 40555 // 此处替换为你的传感器编号 char pd='v'; //写的一个字符判断 // 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"; // 连接 的服务器地址 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 = 1*1000; // delay between 2 datapoints, 延时多少秒访问一次 String returnValue = ""; boolean ResponseBegin = false; void setup() { pinMode(5, OUTPUT); Wire.begin(); // start serial port: Serial.begin(115200); myservo.attach(9); // attaches the servo on pin 9 to the servo object // 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 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);//打印出接收的数据 pd=returnValue.charAt(9);//取第几位字符 Serial.print("Jie Shou de zifu:");//串口方便好看 Serial.println(pd); if (pd == '1') { Serial.println("turn ON ON ON ON the LED"); digitalWrite(led, HIGH); for(pos = 0; pos <180; pos += 1) // goes from 0 degrees to 180 degrees { // in steps of 1 degree myservo.write(180); // tell servo to go to position in variable 'pos' delay(10); // waits 15ms for the servo to reach the position } for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees { myservo.write(0); // tell servo to go to position in variable 'pos' delay(10); // waits 15ms for the servo to reach the position } } else if(pd== '0') { Serial.println("turn off off off the LED_____________"); digitalWrite(led, 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.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.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(); }