├── LED_control.ino └── README.md /LED_control.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Yeelink sensor client power switch example 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | byte buff[2]; 11 | 12 | // for yeelink api 13 | #define APIKEY "dc7d1c98898fa2e4517xxxxxxxxxx" // 此处替换为你自己的API KEY 14 | #define DEVICEID 3933 // 此处替换为你的设备编号 15 | #define SENSORID1 5578 // 此处替换为你的传感器编号 16 | 17 | 18 | // assign a MAC address for the ethernet controller. 19 | byte mac[] = { 0x00, 0x1D, 0x72, 0x82, 0x35, 0x9D}; 20 | // initialize the library instance: 21 | EthernetClient client ; 22 | char server[] = "api.yeelink.net"; // name address for yeelink API 23 | 24 | unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds 25 | boolean lastConnected = false; // state of the connection last time through the main loop 26 | const unsigned long postingInterval = 3*1000; // delay between 2 datapoints, 30s 27 | String returnValue = ""; 28 | boolean ResponseBegin = false; 29 | 30 | void setup() { 31 | pinMode(5, OUTPUT); 32 | Wire.begin(); 33 | // start serial port: 34 | Serial.begin(57600); 35 | 36 | // start the Ethernet connection with DHCP: 37 | if (Ethernet.begin(mac) == 0) { 38 | Serial.println("Failed to configure Ethernet using DHCP"); 39 | for(;;) 40 | ; 41 | } 42 | else { 43 | Serial.println("Ethernet configuration OK"); 44 | } 45 | } 46 | 47 | void loop() { 48 | // if there's incoming data from the net connection. 49 | // send it out the serial port. This is for debugging 50 | // purposes only: 51 | 52 | if (client.available()) { 53 | char c = client.read(); 54 | // Serial.print(c); 55 | if (c == '{') 56 | ResponseBegin = true; 57 | else if (c == '}') 58 | ResponseBegin = false; 59 | 60 | if (ResponseBegin) 61 | returnValue += c; 62 | } 63 | if (returnValue.length() !=0 && (ResponseBegin == false)) 64 | { 65 | Serial.println(returnValue); 66 | 67 | if (returnValue.charAt(returnValue.length() - 1) == '1') { 68 | Serial.println("turn on the LED"); 69 | digitalWrite(5, HIGH); 70 | } 71 | else if(returnValue.charAt(returnValue.length() - 1) == '0') { 72 | Serial.println("turn off the LED"); 73 | digitalWrite(5, LOW); 74 | } 75 | returnValue = ""; 76 | } 77 | // if there's no net connection, but there was one last time 78 | // through the loop, then stop the client: 79 | if (!client.connected() && lastConnected) { 80 | Serial.println(); 81 | Serial.println("disconnecting."); 82 | client.stop(); 83 | } 84 | 85 | // if you're not connected, and ten seconds have passed since 86 | // your last connection, then connect again and send data: 87 | if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) { 88 | // read sensor data, replace with your code 89 | //int sensorReading = readLightSensor(); 90 | Serial.print("yeelink:"); 91 | //get data from server 92 | getData(); 93 | } 94 | // store the state of the connection for next time through 95 | // the loop: 96 | lastConnected = client.connected(); 97 | } 98 | 99 | 100 | 101 | // this method makes a HTTP connection to the server and get data back 102 | void getData(void) { 103 | // if there's a successful connection: 104 | if (client.connect(server, 80)) { 105 | Serial.println("connecting..."); 106 | // send the HTTP GET request: 107 | 108 | client.print("GET /v1.0/device/"); 109 | client.print(DEVICEID); 110 | client.print("/sensor/"); 111 | client.print(SENSORID1); 112 | client.print("/datapoints"); 113 | client.println(" HTTP/1.1"); 114 | client.println("Host: api.yeelink.net"); 115 | client.print("Accept: *"); 116 | client.print("/"); 117 | client.println("*"); 118 | client.print("U-ApiKey: "); 119 | client.println(APIKEY); 120 | client.println("Content-Length: 0"); 121 | client.println("Connection: close"); 122 | client.println(); 123 | Serial.println("print get done."); 124 | 125 | } 126 | else { 127 | // if you couldn't make a connection: 128 | Serial.println("connection failed"); 129 | Serial.println(); 130 | Serial.println("disconnecting."); 131 | client.stop(); 132 | } 133 | // note the time that the connection was made or attempted: 134 | lastConnectionTime = millis(); 135 | } 136 | 137 | 138 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | example_basic_1_LED 2 | =================== 3 | --------------------------------------------------------------------------------