|
楼主 |
发表于 2012-11-21 10:27:46
|
显示全部楼层
本帖最后由 zcbzjx 于 2012-11-21 10:34 编辑
- /*
- Yeelink sensor client example
- */
- #include <SPI.h>
- #include <Ethernet.h>
- #include <OneWire.h>
- #define ONEWIRE_PIN A0
-
- OneWire ds(ONEWIRE_PIN);
- byte buff[2];
- // for yeelink api
- #define APIKEY "xxxxxxxxxxxxxxxxxxxxxxxxxxx" // replace your yeelink api key here
- #define DEVICEID xx // replace your device ID
- #define SENSORID xxx // replace your sensor ID
- // assign a MAC address for the ethernet controller.
- byte mac[] = {
- 0x00, 0x1D, 0x72, 0x82, 0x35, 0x9D};
- IPAddress myip(10,21,0,195);
- IPAddress mydns(10,11,5,25);
- IPAddress mygateway(10,21,0,1);
- // initialize the library instance:
- 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 = 15*1000; // delay between 2 datapoints, 30s
- // Some global variables
- char sensorData[20];
- uint8_t dataLength;
- void setup() {
- // Start up the dallas library
- // start serial port:
- Serial.begin(57600);
- // start the Ethernet connection with DHCP:
- Ethernet.begin(mac,myip,mydns,mygateway);
- }
- 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 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
- get_Send_String();
- Serial.println(sensorData);
- //send data to server
- sendData(sensorData,dataLength);
- }
- // store the state of the connection for next time through
- // the loop:
- lastConnected = client.connected();
- }
- // this method makes a HTTP connection to the server:
- void sendData(char* thisData,byte thisLength) {
- // if there's a successful connection:
- if (client.connect(server, 80)) {
- Serial.println("connecting...");
- // send the HTTP PUT request:
- client.print("POST /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.print("Content-Length: ");
- 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(thisData);
- }
- 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();
- }
- ///////////////////////////////////////////////////////////////////////////
- // get data from light sensor
- // you can replace this code for your sensor
- ///////////////////////////////////////////////////////////////////////////
- // get data from temperature sensor
- // you can replace this code for your sensor
- void get_Send_String(){
- uint16_t Tc_100 = get_Current_Temp();
- uint8_t i,whole, fract;
- whole = Tc_100/10 ; // separate off the whole and fractional portions
- fract = Tc_100 % 10;
- dataLength = sprintf(sensorData,"{"value":%d.%d}",whole,fract);
- }
- uint16_t get_Current_Temp(){ //0.1C
- //returns the temperature from one DS18S20 in DEG Celsius
- byte data[12];
- byte addr[8];
- if ( !ds.search(addr)) {
- //no more sensors on chain, reset search
- ds.reset_search();
- return 0;
- }
- if ( OneWire::crc8( addr, 7) != addr[7]) {
- return 1000;
- }
- if ( addr[0] != 0x28) {
- return 1000;
- }
- ds.reset();
- ds.select(addr);
- ds.write(0x44,1); // start conversion, with parasite power on at the end
- delay(1000);
- ds.reset();
- ds.select(addr);
- ds.write(0xBE); // Read Scratchpad
- for (int i = 0; i < 9; i++) { // we need 9 bytes
- data[i] = ds.read();
- }
- ds.reset_search();
- uint16_t tempRead = (data[1] << 8) | data[0]; //using two's compliment
- return tempRead*10/16;
- }
复制代码 |
|