├── README.md ├── examples ├── get_costomized_data_point │ └── get_costomized_data_point.ino ├── get_gps_data_point │ └── get_gps_data_point.ino ├── get_value_data_point │ └── get_value_data_point.ino ├── post_costomized_data_point │ └── post_costomized_data_point.ino ├── post_gps_data_point │ └── post_gps_data_point.ino ├── post_multi_data_point_1 │ └── post_multi_data_point_1.ino ├── post_multi_data_point_2 │ └── post_multi_data_point_2.ino ├── post_value_data_point │ └── post_value_data_point.ino └── wifi_post_value_data_point │ └── wifi_post_value_data_point.ino ├── keywords.txt ├── yl_data_point.cpp ├── yl_data_point.h ├── yl_device.cpp ├── yl_device.h ├── yl_generic_data_point.cpp ├── yl_generic_data_point.h ├── yl_gps_data_point.cpp ├── yl_gps_data_point.h ├── yl_messenger.cpp ├── yl_messenger.h ├── yl_sensor.cpp ├── yl_sensor.h ├── yl_tcp_client.h ├── yl_value_data_point.cpp ├── yl_value_data_point.h ├── yl_w5100_client.cpp ├── yl_w5100_client.h ├── yl_wifi_client.cpp └── yl_wifi_client.h /README.md: -------------------------------------------------------------------------------- 1 | YeeLinkLib 2 | ========== 3 | FIRSTLY, ANYONE can use & distribute this software under the GPLv3. 4 | 5 | yeelink is a professional platform of the Internet of Things.It provides stable & high-quality service. 6 | Unfortunately its development environment is not so good,even no offical sdk. So I made one. 7 | yeelink supports various hardware by which U can communicate with yeelink,i don't think i can enumerate all of them. 8 | Now this lib just supports Arduino + W5100——my favorite composition ^_^ 9 | -------------------------------------------------------------------------------- /examples/get_costomized_data_point/get_costomized_data_point.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | class personal_info_dp : public yl_generic_data_point 13 | { 14 | public: 15 | personal_info_dp(const String &key) 16 | : yl_generic_data_point(key) 17 | {} 18 | 19 | virtual String value_to_string() const 20 | { 21 | return "{\"name\":\"" + name_ + "\",\"age\":" + String(age_) + ",\"sex\":\"" + sex_ + "\"}"; 22 | } 23 | 24 | virtual bool value_from_string(const String &str) 25 | { 26 | name_ = sub_string(str, 0, "\"name\":\"", "\","); 27 | age_ = sub_string(str, 0, "\"age\":\"", ",").toInt(); 28 | sex_ = sub_string(str, 0, "\"sex\":\"", "\"}"); 29 | return true; 30 | } 31 | 32 | 33 | public: 34 | String name_; 35 | int age_; 36 | String sex_; 37 | }; 38 | 39 | //replace 2633 3539 with ur device id and sensor id 40 | yl_device ardu(2633); 41 | yl_sensor generic(4013, &ardu); 42 | //replace first param value with ur u-apikey 43 | yl_w5100_client client; 44 | yl_messenger messenger(&client, "u-apikey", "api.yeelink.net"); 45 | personal_info_dp qinqingege("personal_info"); 46 | 47 | void setup() 48 | { 49 | Serial.begin(9600); //for output information 50 | byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xAA}; 51 | Ethernet.begin(mac); 52 | 53 | // qinqingege.name_ = "qinqingege"; 54 | // qinqingege.age_ = 26; 55 | // qinqingege.sex_ = "male"; 56 | } 57 | 58 | void loop() 59 | { 60 | generic.single_get(messenger, qinqingege); 61 | Serial.println(qinqingege.name_); 62 | Serial.println(qinqingege.age_); 63 | Serial.println(qinqingege.sex_); 64 | delay(1000 * 30); 65 | } -------------------------------------------------------------------------------- /examples/get_gps_data_point/get_gps_data_point.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | //replace 2633 3539 with ur device id and sensor id 15 | yl_device ardu(2633); 16 | yl_sensor gps(3539, &ardu); 17 | //replace first param value with ur u-apikey 18 | yl_w5100_client client; 19 | yl_messenger messenger(&client, "u-apikey", "api.yeelink.net"); 20 | 21 | 22 | void setup() 23 | { 24 | Serial.begin(9600); //for output information 25 | byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xAA}; 26 | Ethernet.begin(mac); 27 | } 28 | 29 | void loop() 30 | { 31 | yl_gps_data_point dp; 32 | gps.single_get(messenger, dp); 33 | Serial.print("lat:"); 34 | Serial.print(dp.get_location().lat); 35 | Serial.print("lng:"); 36 | Serial.println(dp.get_location().lng); 37 | delay(1000 * 30); 38 | } -------------------------------------------------------------------------------- /examples/get_value_data_point/get_value_data_point.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | //this example gets newest data point from yeelink 12 | 13 | //replace 2633 3539 with ur device id and sensor id 14 | yl_device ardu(2633); 15 | yl_sensor therm(3539, &ardu); 16 | //replace first param value with ur u-apikey 17 | yl_w5100_client client; 18 | yl_messenger messenger(&client, "u-apikey", "api.yeelink.net"); 19 | 20 | void setup() 21 | { 22 | Serial.begin(9600); //for output information 23 | byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xAA}; 24 | Ethernet.begin(mac); 25 | } 26 | 27 | void loop() 28 | { 29 | yl_value_data_point dp; 30 | therm.single_get(messenger, dp); 31 | Serial.println(dp.get_value()); 32 | } -------------------------------------------------------------------------------- /examples/post_costomized_data_point/post_costomized_data_point.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | class personal_info_dp : public yl_generic_data_point 14 | { 15 | public: 16 | personal_info_dp(const String &key) 17 | : yl_generic_data_point(key) 18 | {} 19 | 20 | virtual String value_to_string() const 21 | { 22 | return "{\"name\":\"" + name_ + "\",\"age\":" + String(age_) + ",\"sex\":\"" + sex_ + "\"}"; 23 | } 24 | 25 | virtual bool value_from_string(const String &str) 26 | { 27 | name_ = sub_string(str, 0, "\"name\":\"", "\","); 28 | age_ = sub_string(str, 0, "\"age\":\"", ",").toInt(); 29 | sex_ = sub_string(str, 0, "\"sex\":\"", "\"}"); 30 | return true; 31 | } 32 | 33 | 34 | public: 35 | String name_; 36 | int age_; 37 | String sex_; 38 | }; 39 | 40 | //replace 2633 3539 with ur device id and sensor id 41 | yl_device ardu(2633); 42 | yl_sensor generic(4013, &ardu); 43 | //replace first param value with ur u-apikey 44 | yl_w5100_client client; 45 | yl_messenger messenger(&client, "u-apikey", "api.yeelink.net"); 46 | personal_info_dp qinqingege("personal_info"); 47 | 48 | void setup() 49 | { 50 | Serial.begin(9600); //for output information 51 | byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xAA}; 52 | Ethernet.begin(mac); 53 | 54 | qinqingege.name_ = "qinqingege"; 55 | qinqingege.age_ = 26; 56 | qinqingege.sex_ = "male"; 57 | } 58 | 59 | void loop() 60 | { 61 | generic.single_post(messenger, qinqingege); 62 | delay(1000 * 30); 63 | } -------------------------------------------------------------------------------- /examples/post_gps_data_point/post_gps_data_point.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | //replace 2633 3539 with ur device id and sensor id 14 | yl_device ardu(2633); 15 | yl_sensor gps(3539, &ardu); 16 | //replace first param value with ur u-apikey 17 | yl_w5100_client client; 18 | yl_messenger messenger(&client, "u-apikey", "api.yeelink.net"); 19 | 20 | 21 | void setup() 22 | { 23 | Serial.begin(9600); //for output information 24 | byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xAA}; 25 | Ethernet.begin(mac); 26 | } 27 | 28 | void loop() 29 | { 30 | location loc = {35.4567, 46.1234}; 31 | yl_gps_data_point dp(loc, 50.5, true); 32 | gps.single_post(messenger, dp); 33 | delay(1000 * 30); 34 | } -------------------------------------------------------------------------------- /examples/post_multi_data_point_1/post_multi_data_point_1.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | //this example reads data from a lm35dz sensor, convert value to degree Celsius 15 | //and then post it to yeelink.net 16 | 17 | //replace 2633 3539 with ur device id and sensor id 18 | yl_device ardu(2633); 19 | yl_sensor therm(3539, &ardu); 20 | yl_sensor therm1(4169, &ardu); 21 | //replace first param value with ur u-apikey 22 | yl_w5100_client client; 23 | yl_messenger messenger(&client, "u-apikey", "api.yeelink.net"); 24 | 25 | const int THERM_PIN = 0; 26 | 27 | float lm35_convertor(int analog_num) 28 | { 29 | return analog_num * (5.0 / 1024.0 * 100); 30 | } 31 | 32 | void setup() 33 | { 34 | Serial.begin(9600); //for output information 35 | byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xAA}; 36 | Ethernet.begin(mac); 37 | } 38 | 39 | void loop() 40 | { 41 | int v = analogRead(THERM_PIN); 42 | Serial.println(lm35_convertor(v)); 43 | yl_value_data_point dp(lm35_convertor(v)); 44 | therm.single_post(messenger, dp); 45 | therm1.single_post(messenger, dp); 46 | delay(1000 * 30); 47 | } -------------------------------------------------------------------------------- /examples/post_multi_data_point_2/post_multi_data_point_2.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | //this example reads data from a lm35dz sensor, convert value to degree Celsius 14 | //and then post it to yeelink.net 15 | 16 | //replace 2633 3539 with ur device id and sensor id 17 | yl_device ardu(2633); 18 | yl_sensor therm(3539, &ardu); 19 | yl_sensor therm1(4169, &ardu); 20 | //replace first param value with ur u-apikey 21 | yl_w5100_client client; 22 | yl_messenger messenger(&client, "u-apikey", "api.yeelink.net"); 23 | 24 | const int THERM_PIN = 0; 25 | 26 | float lm35_convertor(int analog_num) 27 | { 28 | return analog_num * (5.0 / 1024.0 * 100); 29 | } 30 | 31 | void setup() 32 | { 33 | Serial.begin(9600); //for output information 34 | byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xAA}; 35 | Ethernet.begin(mac); 36 | } 37 | 38 | void loop() 39 | { 40 | int v = analogRead(THERM_PIN); 41 | Serial.println(lm35_convertor(v)); 42 | yl_value_data_point dp(lm35_convertor(v)); 43 | messenger.connect_yl(); 44 | therm.post(messenger, dp, true); 45 | therm1.post(messenger, dp, false); 46 | messenger.flush_stop(); 47 | delay(1000 * 30); 48 | } -------------------------------------------------------------------------------- /examples/post_value_data_point/post_value_data_point.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | //this example reads data from a lm35dz sensor, convert value to degree Celsius 14 | //and then post it to yeelink.net 15 | 16 | //replace 2633 3539 with ur device id and sensor id 17 | yl_device ardu(2633); 18 | yl_sensor therm(3539, &ardu); 19 | //replace first param value with ur u-apikey 20 | yl_w5100_client client; 21 | yl_messenger messenger(&client, "u-apikey", "api.yeelink.net"); 22 | 23 | const int THERM_PIN = 0; 24 | 25 | float lm35_convertor(int analog_num) 26 | { 27 | return analog_num * (5.0 / 1024.0 * 100); 28 | } 29 | 30 | void setup() 31 | { 32 | Serial.begin(9600); //for output information 33 | byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xAA}; 34 | Ethernet.begin(mac); 35 | } 36 | 37 | void loop() 38 | { 39 | int v = analogRead(THERM_PIN); 40 | Serial.println(lm35_convertor(v)); 41 | yl_value_data_point dp(lm35_convertor(v)); 42 | therm.single_post(messenger, dp); 43 | delay(1000 * 30); 44 | } -------------------------------------------------------------------------------- /examples/wifi_post_value_data_point/wifi_post_value_data_point.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | //this example reads data from a lm35dz sensor, convert value to degree Celsius 14 | //and then post it to yeelink.net 15 | 16 | //replace 2633 3539 with ur device id and sensor id 17 | yl_device ardu(2633); 18 | yl_sensor therm(3539, &ardu); 19 | //replace first param value with ur u-apikey 20 | yl_wifi_client client; 21 | yl_messenger messenger(&client, "u-apikey", "api.yeelink.net"); 22 | 23 | const int THERM_PIN = 0; 24 | 25 | float lm35_convertor(int analog_num) 26 | { 27 | return analog_num * (5.0 / 1024.0 * 100); 28 | } 29 | 30 | void setup() 31 | { 32 | Serial.begin(9600); //for output information 33 | char mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xAA}; 34 | WiFi.begin(mac); 35 | } 36 | 37 | void loop() 38 | { 39 | int v = analogRead(THERM_PIN); 40 | Serial.println(lm35_convertor(v)); 41 | yl_value_data_point dp(lm35_convertor(v)); 42 | therm.single_post(messenger, dp); 43 | delay(1000 * 30); 44 | } -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For YeeLink 3 | ####################################### 4 | # Class 5 | ####################################### 6 | 7 | yl_device KEYWORD1 8 | yl_sensor KEYWORD1 9 | yl_data_point KEYWORD1 10 | yl_gps_data_point KEYWORD1 11 | yl_value_data_point KEYWORD1 12 | yl_messenger KEYWORD1 13 | yl_generic_data_point KEYWORD1 14 | yl_w5100_client KEYWORD1 15 | yl_wifi_client KEYWORD1 16 | 17 | ####################################### 18 | # Methods and Functions (KEYWORD2) 19 | ####################################### 20 | 21 | get_id KEYWORD2 22 | set_id KEYWORD2 23 | 24 | ####################################### 25 | # Constants (LITERAL1) 26 | ####################################### 27 | 28 | -------------------------------------------------------------------------------- /yl_data_point.cpp: -------------------------------------------------------------------------------- 1 | #include "yl_data_point.h" 2 | 3 | yl_data_point::yl_data_point(const String &key /* = String */) 4 | : key_(key) 5 | {} 6 | 7 | void yl_data_point::set_key(const String &key) 8 | { 9 | key_ = key; 10 | } 11 | 12 | const String& yl_data_point::get_key() const 13 | { 14 | return key_; 15 | } 16 | 17 | String yl_data_point::to_string() const 18 | { 19 | String result("{"); 20 | String key = key_to_string(); 21 | if (key.length()) 22 | { 23 | result += key; 24 | result += ","; 25 | } 26 | result += "\"value\":"; 27 | result += value_to_string(); 28 | result += "}"; 29 | 30 | return result; 31 | } 32 | 33 | String yl_data_point::key_to_string() const 34 | { 35 | if (key_.length()) 36 | { 37 | String key("\"timestamp\":\""); 38 | key += key_; 39 | key += "\""; 40 | return key; 41 | } 42 | return ""; 43 | } 44 | 45 | bool yl_data_point::from_string(const String &str) 46 | { 47 | if (!key_from_string(str) 48 | || !value_from_string(str)) 49 | { 50 | return false; 51 | } 52 | return true; 53 | } 54 | 55 | bool yl_data_point::key_from_string(const String &str) 56 | { 57 | if (str.length() == 0) 58 | { 59 | return true; 60 | } 61 | 62 | key_ = sub_string(str, 0, "\"timestamp\":\"", "\","); 63 | return key_.length(); 64 | } 65 | 66 | String yl_data_point::ftoa(float val, char resolution) const 67 | { 68 | if (val < 0.000001 && val > -0.000001) 69 | { 70 | return "0"; 71 | } 72 | String result; 73 | bool positive = val > 0.0f ? true : false; 74 | if (!positive) 75 | { 76 | result += "-"; 77 | val = -val; 78 | } 79 | int i_part = int(val); 80 | float f_part = val - i_part; 81 | result += i_part; 82 | 83 | String temp; 84 | resolution = (resolution <= 6 ? resolution : 6); 85 | float min_val = 0.000001; 86 | for (int i = 0; i < resolution && f_part >= min_val; ++i) 87 | { 88 | temp += int(f_part * 10); 89 | f_part = f_part * 10 - int(f_part * 10); 90 | min_val *= 10; 91 | } 92 | if (temp.length()) 93 | { 94 | result += "."; 95 | result += temp; 96 | } 97 | return result; 98 | } 99 | 100 | String yl_data_point::sub_string(const String &str, int from_index, const String &start_str, const String &end_str) 101 | { 102 | int start_index = str.indexOf(start_str, from_index); 103 | if (start_index < 0) 104 | { 105 | return ""; 106 | } 107 | start_index += start_str.length(); 108 | int end_index = str.indexOf(end_str, start_index); 109 | String value = str.substring(start_index, end_index); 110 | value.trim(); 111 | if (value.length() == 0) 112 | { 113 | return ""; 114 | } 115 | return value; 116 | } -------------------------------------------------------------------------------- /yl_data_point.h: -------------------------------------------------------------------------------- 1 | #ifndef ARDUINO_YEELINKLIB_DATAPOINT_H 2 | #define ARDUINO_YEELINKLIB_DATAPOINT_H 3 | 4 | #include 5 | 6 | class yl_data_point 7 | { 8 | public: 9 | yl_data_point(const String &key = String()); 10 | 11 | void set_key(const String &key); 12 | const String& get_key() const; 13 | virtual String to_string() const; 14 | virtual bool from_string(const String &str); 15 | 16 | protected: 17 | virtual String key_to_string() const; 18 | virtual String value_to_string() const = 0; 19 | virtual bool key_from_string(const String &str); 20 | virtual bool value_from_string(const String &str) = 0; 21 | String ftoa(float val, char resolution) const; 22 | String sub_string(const String &str, int from_index, const String &start_str, const String &end_str); 23 | 24 | protected: 25 | String key_; 26 | }; 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /yl_device.cpp: -------------------------------------------------------------------------------- 1 | #include "yl_device.h" 2 | 3 | yl_device::yl_device(int id /* = 0 */) 4 | : id_(id) 5 | {} 6 | 7 | void yl_device::set_id(int id) 8 | { 9 | id_ = id; 10 | } 11 | 12 | int yl_device::get_id() const 13 | { 14 | return id_; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /yl_device.h: -------------------------------------------------------------------------------- 1 | #ifndef ARDUINO_YEELINKLIB_DEVICE_H 2 | #define ARDUINO_YEELINKLIB_DEVICE_H 3 | 4 | class yl_device 5 | { 6 | public: 7 | yl_device(int id = 0); 8 | 9 | void set_id(int id); 10 | int get_id() const; 11 | 12 | private: 13 | int id_; 14 | }; 15 | 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /yl_generic_data_point.cpp: -------------------------------------------------------------------------------- 1 | #include "yl_generic_data_point.h" 2 | 3 | yl_generic_data_point::yl_generic_data_point(const String &key) 4 | : yl_data_point(key) 5 | {} 6 | 7 | String yl_generic_data_point::key_to_string() const 8 | { 9 | return "\"key\":\"" + key_ + "\""; 10 | } 11 | 12 | bool yl_generic_data_point::key_from_string(const String &str) 13 | { 14 | if (str.length() == 0) 15 | { 16 | return false; 17 | } 18 | 19 | key_ = sub_string(str, 0, "\"key\":\"", "\","); 20 | return key_.length(); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /yl_generic_data_point.h: -------------------------------------------------------------------------------- 1 | #ifndef ARDUINO_YEELINKLIB_GENERICDATAPOINT_H 2 | #define ARDUINO_YEELINKLIB_GENERICDATAPOINT_H 3 | 4 | #include "yl_data_point.h" 5 | 6 | class yl_generic_data_point : public yl_data_point 7 | { 8 | public: 9 | yl_generic_data_point(const String &key); 10 | 11 | protected: 12 | virtual String key_to_string() const; 13 | virtual bool key_from_string(const String &str); 14 | 15 | }; 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /yl_gps_data_point.cpp: -------------------------------------------------------------------------------- 1 | #include "yl_gps_data_point.h" 2 | 3 | yl_gps_data_point::yl_gps_data_point() 4 | : speed_(0) 5 | , offset_(true) 6 | { 7 | memset(&loc_, 0, sizeof(loc_)); 8 | } 9 | yl_gps_data_point::yl_gps_data_point(const location &loc, float speed, bool offset, const String &key) 10 | : yl_data_point(key) 11 | , speed_(speed) 12 | , offset_(offset) 13 | , loc_(loc) 14 | {} 15 | 16 | void yl_gps_data_point::set_location(const location &loc) 17 | { 18 | loc_ = loc; 19 | } 20 | 21 | const location& yl_gps_data_point::get_location() const 22 | { 23 | return loc_; 24 | } 25 | 26 | void yl_gps_data_point::set_speed(float speed) 27 | { 28 | speed_ = speed; 29 | } 30 | 31 | float yl_gps_data_point::get_speed() const 32 | { 33 | return speed_; 34 | } 35 | 36 | void yl_gps_data_point::set_offset(bool offset) 37 | { 38 | offset_ = offset; 39 | } 40 | 41 | bool yl_gps_data_point::get_offset() const 42 | { 43 | return offset_; 44 | } 45 | 46 | String yl_gps_data_point::value_to_string() const 47 | { 48 | String value("{\"lat\":"); 49 | value += ftoa(loc_.lat, 2); 50 | value += ",\"lng\":"; 51 | value += ftoa(loc_.lng, 2); 52 | value += ",\"speed\":"; 53 | value += ftoa(speed_, 2); 54 | value += ",\"offset\":"; 55 | value += (offset_ ? "\"yes\"}" : "\"no\"}"); 56 | return value; 57 | } 58 | 59 | bool yl_gps_data_point::value_from_string(const String &str) 60 | { 61 | if (str.length() == 0) 62 | { 63 | return false; 64 | } 65 | 66 | String value = sub_string(str, 0, "\"lat\":", ","); 67 | loc_.lat = atof(&value[0]); 68 | int start_index = str.indexOf(",") + 1; 69 | value = sub_string(str, start_index, "\"lng\":", ","); 70 | loc_.lng = atof(&value[0]); 71 | start_index = str.indexOf(",", start_index) + 1; 72 | value = sub_string(str, start_index, "\"speed\":", ","); 73 | if (value.length() == 0) 74 | { 75 | value = sub_string(str, start_index, "\"speed\":", "}"); 76 | speed_ = atof(&value[0]); 77 | return true; 78 | } 79 | speed_ = atof(&value[0]); 80 | start_index = str.indexOf(",", start_index) + 1; 81 | value = sub_string(str, start_index, "\"offset\":\"", "\"}"); 82 | offset_ = (value == "yes" ? true : false); 83 | return true; 84 | } 85 | 86 | -------------------------------------------------------------------------------- /yl_gps_data_point.h: -------------------------------------------------------------------------------- 1 | #ifndef ARDUINO_YEELINKLIB_GPSDATAPOINT_H 2 | #define ARDUINO_YEELINKLIB_GPSDATAPOINT_H 3 | 4 | #include "yl_data_point.h" 5 | 6 | struct location 7 | { 8 | float lat; 9 | float lng; 10 | }; 11 | 12 | class yl_gps_data_point : public yl_data_point 13 | { 14 | public: 15 | yl_gps_data_point(); 16 | yl_gps_data_point(const location &loc, float speed, bool offset, const String &key = String()); 17 | 18 | void set_location(const location &loc); 19 | const location& get_location() const; 20 | void set_speed(float speed); 21 | float get_speed() const; 22 | void set_offset(bool offset); 23 | bool get_offset() const; 24 | 25 | protected: 26 | virtual String value_to_string() const; 27 | virtual bool value_from_string(const String &str); 28 | 29 | private: 30 | location loc_; 31 | float speed_; 32 | bool offset_; 33 | }; 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /yl_messenger.cpp: -------------------------------------------------------------------------------- 1 | #include "yl_messenger.h" 2 | #include "yl_sensor.h" 3 | #include "yl_tcp_client.h" 4 | 5 | //#define YL_SERIAL_DEBUG 6 | 7 | #define YEELINK_VERSION ("v1.0") 8 | 9 | yl_messenger::yl_messenger() 10 | : version_(YEELINK_VERSION) 11 | , client_(NULL) 12 | {} 13 | 14 | yl_messenger::yl_messenger(yl_tcp_client *client) 15 | : version_(YEELINK_VERSION) 16 | , client_(client) 17 | {} 18 | 19 | yl_messenger::yl_messenger(const String &api_key, const String &host) 20 | : api_key_(api_key) 21 | , host_(host) 22 | , version_(YEELINK_VERSION) 23 | , client_(NULL) 24 | {} 25 | 26 | yl_messenger::yl_messenger(yl_tcp_client *client, const String &api_key, const String &host) 27 | : api_key_(api_key) 28 | , host_(host) 29 | , version_(YEELINK_VERSION) 30 | , client_(client) 31 | {} 32 | 33 | void yl_messenger::set_api_key(const String &api_key) 34 | { 35 | api_key_ = api_key; 36 | } 37 | 38 | const String& yl_messenger::get_api_key() const 39 | { 40 | return api_key_; 41 | } 42 | 43 | String& yl_messenger::get_api_key() 44 | { 45 | return api_key_; 46 | } 47 | 48 | void yl_messenger::set_host(const String &host) 49 | { 50 | host_ = host; 51 | } 52 | 53 | const String& yl_messenger::get_host() const 54 | { 55 | return host_; 56 | } 57 | 58 | String& yl_messenger::get_host() 59 | { 60 | return host_; 61 | } 62 | 63 | void yl_messenger::set_version(const String &version) 64 | { 65 | version_ = version; 66 | } 67 | 68 | const String& yl_messenger::get_version() const 69 | { 70 | return version_; 71 | } 72 | 73 | String& yl_messenger::get_version() 74 | { 75 | return version_; 76 | } 77 | 78 | void yl_messenger::set_tcp_client(yl_tcp_client *client) 79 | { 80 | client_ = client; 81 | } 82 | 83 | const yl_tcp_client* yl_messenger::get_tcp_client() const 84 | { 85 | return client_; 86 | } 87 | 88 | yl_tcp_client* yl_messenger::get_tcp_client() 89 | { 90 | return client_; 91 | } 92 | 93 | bool yl_messenger::connect_yl() 94 | { 95 | return client_->connect(&host_[0], 80); 96 | } 97 | 98 | bool yl_messenger::request_post(const yl_sensor &sensor, const yl_data_point &dp, bool keep_alive) 99 | { 100 | String data = dp.to_string(); 101 | if (data.length() == 0) 102 | { 103 | return false; 104 | } 105 | String connection = keep_alive ? "\r\n" : "Connection:close\r\n\r\n"; 106 | String temp("POST /"); 107 | temp += version_; 108 | temp += "/device/"; 109 | if (!client_->send(temp) 110 | || !client_->send(sensor.get_device()->get_id()) 111 | || !client_->send("/sensor/") 112 | || !client_->send(sensor.get_id()) 113 | || !client_->send("/datapoints HTTP/1.1\r\nHost:") 114 | || !client_->send(host_) 115 | || !client_->send("\r\nAccept:*/*\r\nU-ApiKey:") 116 | || !client_->send(api_key_) 117 | || !client_->send("\r\nContent-Length:") 118 | || !client_->send_ln(data.length()) 119 | || !client_->send("Content-Type:application/x-www-form-urlencoded\r\n") 120 | || !client_->send(connection) 121 | || !client_->send(data)) 122 | { 123 | #ifdef YL_SERIAL_DEBUG 124 | Serial.println("send error : 108"); 125 | #endif 126 | return false; 127 | } 128 | return true; 129 | } 130 | 131 | bool yl_messenger::get_request_result() 132 | { 133 | String temp; 134 | if (!recv_ln(temp)) 135 | { 136 | #ifdef YL_SERIAL_DEBUG 137 | Serial.println("recv error : 122"); 138 | #endif 139 | return false; 140 | } 141 | int start_index = temp.indexOf(' '); 142 | temp = temp.substring(start_index + 1); 143 | temp.trim(); 144 | int end_index = temp.indexOf(' '); 145 | temp = temp.substring(0, end_index); 146 | temp.trim(); 147 | return temp[0] == '2'; 148 | } 149 | 150 | bool yl_messenger::request_get(const yl_sensor &sensor, const String &key, bool keep_alive) 151 | { 152 | String temp("GET /"); 153 | temp += version_; 154 | temp += "/device/"; 155 | if (!client_->send(temp) 156 | || !client_->send(sensor.get_device()->get_id()) 157 | || !client_->send("/sensor/") 158 | || !client_->send(sensor.get_id()) 159 | || !client_->send("/datapoints/")) 160 | { 161 | #ifdef YL_SERIAL_DEBUG 162 | Serial.println("send error : 144"); 163 | #endif 164 | return false; 165 | } 166 | String connection = keep_alive ? "\r\n\r\n" : "\r\nConnection:close\r\n\r\n"; 167 | if ((key.length() && !client_->send(key)) 168 | || !client_->send(" HTTP/1.1\r\nHost:") 169 | || !client_->send(host_) 170 | || !client_->send("\r\nU-ApiKey:") 171 | || !client_->send(api_key_) 172 | || !client_->send(connection)) 173 | { 174 | #ifdef YL_SERIAL_DEBUG 175 | Serial.println("send error : 156"); 176 | #endif 177 | return false; 178 | } 179 | return true; 180 | } 181 | 182 | bool yl_messenger::recv_get_data(String &data) 183 | { 184 | if (!get_request_result()) 185 | { 186 | #ifdef YL_SERIAL_DEBUG 187 | Serial.println("send error : 168"); 188 | #endif 189 | return false; 190 | } 191 | if (!recv_ln_start_with("{", data)) 192 | { 193 | #ifdef YL_SERIAL_DEBUG 194 | Serial.println("recv error : 193"); 195 | #endif 196 | return false; 197 | } 198 | return true; 199 | } 200 | 201 | bool yl_messenger::recv_ln(String &data) 202 | { 203 | int result = 0; 204 | for (uint8_t step=0;step < 2;) 205 | { 206 | if ((result = client_->recv()) <= 0) 207 | { 208 | #ifdef YL_SERIAL_DEBUG 209 | Serial.println("read error : 190"); 210 | #endif 211 | return false; 212 | } 213 | if (step == 0 && result == '\r') 214 | { 215 | step = 1; 216 | continue; 217 | } 218 | if (step == 1) 219 | { 220 | if (result == '\n') 221 | { 222 | step = 2; 223 | } 224 | else 225 | { 226 | step = 0; 227 | data += '\r'; 228 | } 229 | continue; 230 | } 231 | data += char(result); 232 | } 233 | return true; 234 | } 235 | 236 | bool yl_messenger::recv_ln_start_with(const String start, String &data) 237 | { 238 | String start_temp(start); 239 | start_temp.toLowerCase(); 240 | for (;;) 241 | { 242 | String temp; 243 | if (!recv_ln(temp)) 244 | { 245 | #ifdef YL_SERIAL_DEBUG 246 | Serial.println("read error : 245"); 247 | #endif 248 | return false; 249 | } 250 | temp.toLowerCase(); 251 | if (temp == start_temp) 252 | { 253 | return recv_ln(data); 254 | } 255 | if (temp.substring(0, start_temp.length()) == start_temp) 256 | { 257 | data = temp.substring(start_temp.length()); 258 | return true; 259 | } 260 | } 261 | return false; 262 | } 263 | 264 | void yl_messenger::flush_stop() 265 | { 266 | client_->flush(); 267 | client_->stop(); 268 | } 269 | -------------------------------------------------------------------------------- /yl_messenger.h: -------------------------------------------------------------------------------- 1 | #ifndef ARDUINO_YEELINKLIB_MESSENGER_H 2 | #define ARDUINO_YEELINKLIB_MESSENGER_H 3 | 4 | #include "yl_data_point.h" 5 | #include 6 | 7 | class yl_sensor; 8 | class yl_tcp_client; 9 | class yl_messenger 10 | { 11 | public: 12 | yl_messenger(); 13 | yl_messenger(yl_tcp_client *client); 14 | yl_messenger(const String &api_key, const String &host); 15 | yl_messenger(yl_tcp_client *client, const String &api_key, const String &host); 16 | 17 | void set_api_key(const String &api_key); 18 | const String& get_api_key() const; 19 | String& get_api_key(); 20 | void set_host(const String &host); 21 | const String& get_host() const; 22 | String& get_host(); 23 | void set_version(const String &version); 24 | const String& get_version() const; 25 | String& get_version(); 26 | void set_tcp_client(yl_tcp_client *client); 27 | const yl_tcp_client* get_tcp_client() const; 28 | yl_tcp_client* get_tcp_client(); 29 | 30 | bool connect_yl(); 31 | bool request_post(const yl_sensor &sensor, const yl_data_point &dp, bool keep_alive); 32 | bool get_request_result(); 33 | bool request_get(const yl_sensor &sensor, const String &key, bool keep_alive); 34 | bool recv_get_data(String &data); 35 | void flush_stop(); 36 | 37 | protected: 38 | bool recv_ln(String &data); 39 | bool recv_ln_start_with(const String start, String &data); 40 | 41 | private: 42 | String api_key_; 43 | String host_; 44 | String version_; 45 | yl_tcp_client *client_; 46 | }; 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /yl_sensor.cpp: -------------------------------------------------------------------------------- 1 | #include "yl_sensor.h" 2 | #include "yl_messenger.h" 3 | #include 4 | 5 | yl_sensor::yl_sensor(void) 6 | : id_(0), device_(0) 7 | { 8 | } 9 | 10 | yl_sensor::yl_sensor(int id, yl_device *d) 11 | : id_(id), device_(d) 12 | {} 13 | 14 | void yl_sensor::set_id(int id) 15 | { 16 | id_ = id; 17 | } 18 | 19 | int yl_sensor::get_id() const 20 | { 21 | return id_; 22 | } 23 | 24 | void yl_sensor::set_device(yl_device *dev) 25 | { 26 | device_ = dev; 27 | } 28 | 29 | yl_device * yl_sensor::get_device() const 30 | { 31 | return device_; 32 | } 33 | 34 | bool yl_sensor::single_post(yl_messenger &sock, const yl_data_point &dp) 35 | { 36 | if (!sock.connect_yl()) 37 | { 38 | return false; 39 | } 40 | bool ret = post(sock, dp, false); 41 | sock.flush_stop(); 42 | return ret; 43 | } 44 | 45 | bool yl_sensor::single_get(yl_messenger &sock, yl_data_point &dp) 46 | { 47 | if (!sock.connect_yl()) 48 | { 49 | return false; 50 | } 51 | bool ret = get(sock, dp, false); 52 | sock.flush_stop(); 53 | return ret; 54 | } 55 | 56 | bool yl_sensor::post(yl_messenger &sock, const yl_data_point &dp, bool keep_alive) 57 | { 58 | if (!sock.request_post(*this, dp, keep_alive)) 59 | { 60 | return false; 61 | } 62 | //wait for pending data, the value depends on ur network environment 63 | delay(1000); 64 | return sock.get_request_result(); 65 | } 66 | 67 | bool yl_sensor::get(yl_messenger &sock, yl_data_point &dp, bool keep_alive) 68 | { 69 | if (!sock.request_get(*this, dp.get_key(), keep_alive)) 70 | { 71 | return false; 72 | } 73 | delay(1000); 74 | String data; 75 | if (!sock.recv_get_data(data)) 76 | { 77 | return false; 78 | } 79 | return dp.from_string(data); 80 | } 81 | -------------------------------------------------------------------------------- /yl_sensor.h: -------------------------------------------------------------------------------- 1 | #ifndef ARDUINO_YEELINKLIB_SENSOR_H 2 | #define ARDUINO_YEELINKLIB_SENSOR_H 3 | 4 | #include "yl_device.h" 5 | 6 | class yl_messenger; 7 | class yl_data_point; 8 | class yl_sensor 9 | { 10 | public: 11 | yl_sensor(void); 12 | yl_sensor(int id, yl_device *d); 13 | 14 | void set_id(int id); 15 | int get_id() const; 16 | void set_device(yl_device *dev); 17 | yl_device * get_device() const; 18 | 19 | virtual bool single_post(yl_messenger &sock, const yl_data_point &dp); 20 | virtual bool post(yl_messenger &sock, const yl_data_point &dp, bool keep_alive); 21 | virtual bool single_get(yl_messenger &sock, yl_data_point &dp); 22 | virtual bool get(yl_messenger &sock, yl_data_point &dp, bool keep_alive); 23 | 24 | protected: 25 | int id_; 26 | yl_device *device_; 27 | }; 28 | 29 | #endif 30 | 31 | -------------------------------------------------------------------------------- /yl_tcp_client.h: -------------------------------------------------------------------------------- 1 | #ifndef ARDUINO_YEELINKLIB_TCPCLLIENT_H 2 | #define ARDUINO_YEELINKLIB_TCPCLLIENT_H 3 | 4 | #include 5 | 6 | class yl_tcp_client 7 | { 8 | public: 9 | virtual int connect(const char *host, uint16_t port) = 0; 10 | virtual size_t send(int) = 0; 11 | virtual size_t send_ln(int) = 0; 12 | virtual size_t send(const String &) = 0; 13 | virtual int available() = 0; 14 | virtual int recv() = 0; 15 | virtual int recv(uint8_t *buf, size_t size) = 0; 16 | virtual void flush() = 0; 17 | virtual void stop() = 0; 18 | }; 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /yl_value_data_point.cpp: -------------------------------------------------------------------------------- 1 | #include "yl_value_data_point.h" 2 | 3 | yl_value_data_point::yl_value_data_point(float value /*= 0.0f*/, const String &key /* = String */) 4 | : yl_data_point(key) 5 | , value_(value) 6 | {} 7 | 8 | void yl_value_data_point::set_value(float value) 9 | { 10 | value_ = value; 11 | } 12 | 13 | float yl_value_data_point::get_value() const 14 | { 15 | return value_; 16 | } 17 | 18 | String yl_value_data_point::value_to_string() const 19 | { 20 | return ftoa(value_, 2); 21 | } 22 | 23 | bool yl_value_data_point::value_from_string(const String &str) 24 | { 25 | if (str.length() == 0) 26 | { 27 | return false; 28 | } 29 | 30 | String value = sub_string(str, 0, "\"value\":", "}"); 31 | value_ = atof(&value[0]); 32 | return true; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /yl_value_data_point.h: -------------------------------------------------------------------------------- 1 | #ifndef ARDUINO_YEELINKLIB_VALUEDATAPOINT_H 2 | #define ARDUINO_YEELINKLIB_VALUEDATAPOINT_H 3 | 4 | #include "yl_data_point.h" 5 | 6 | class yl_value_data_point : public yl_data_point 7 | { 8 | public: 9 | yl_value_data_point(float value = 0.0f, const String &key = String()); 10 | 11 | void set_value(float value); 12 | float get_value() const; 13 | 14 | protected: 15 | virtual String value_to_string() const; 16 | virtual bool value_from_string(const String &str); 17 | 18 | private: 19 | float value_; 20 | }; 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /yl_w5100_client.cpp: -------------------------------------------------------------------------------- 1 | #include "yl_w5100_client.h" 2 | 3 | yl_w5100_client::yl_w5100_client() 4 | {} 5 | 6 | yl_w5100_client::yl_w5100_client(uint8_t sock) 7 | : client_(sock) 8 | {} 9 | 10 | int yl_w5100_client::connect(const char *host, uint16_t port) 11 | { 12 | return client_.connect(host, port); 13 | } 14 | 15 | size_t yl_w5100_client::send(int n) 16 | { 17 | return client_.print(n); 18 | } 19 | 20 | size_t yl_w5100_client::send_ln(int n) 21 | { 22 | return client_.println(n); 23 | } 24 | 25 | size_t yl_w5100_client::send(const String &s) 26 | { 27 | if (s.length() == 0) 28 | { 29 | return 0; 30 | } 31 | String temp(s); 32 | char *c = &temp[0]; 33 | return client_.write((uint8_t*)c, temp.length()); 34 | } 35 | 36 | int yl_w5100_client::available() 37 | { 38 | return client_.available(); 39 | } 40 | 41 | int yl_w5100_client::recv() 42 | { 43 | return client_.read(); 44 | } 45 | 46 | int yl_w5100_client::recv(uint8_t *buf, size_t size) 47 | { 48 | return client_.read(buf, size); 49 | } 50 | 51 | void yl_w5100_client::flush() 52 | { 53 | client_.flush(); 54 | } 55 | 56 | void yl_w5100_client::stop() 57 | { 58 | client_.stop(); 59 | } 60 | -------------------------------------------------------------------------------- /yl_w5100_client.h: -------------------------------------------------------------------------------- 1 | #ifndef ARDUINO_YEELINKLIB_W5100CLLIENT_H 2 | #define ARDUINO_YEELINKLIB_W5100CLLIENT_H 3 | 4 | #include 5 | #include 6 | #include "yl_tcp_client.h" 7 | 8 | class yl_w5100_client : public yl_tcp_client 9 | { 10 | public: 11 | yl_w5100_client(); 12 | yl_w5100_client(uint8_t sock); 13 | 14 | virtual int connect(const char *host, uint16_t port); 15 | virtual size_t send(int); 16 | virtual size_t send_ln(int); 17 | virtual size_t send(const String &); 18 | virtual int available(); 19 | virtual int recv(); 20 | virtual int recv(uint8_t *buf, size_t size); 21 | virtual void flush(); 22 | virtual void stop(); 23 | 24 | private: 25 | EthernetClient client_; 26 | }; 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /yl_wifi_client.cpp: -------------------------------------------------------------------------------- 1 | #include "yl_wifi_client.h" 2 | 3 | yl_wifi_client::yl_wifi_client() 4 | {} 5 | 6 | yl_wifi_client::yl_wifi_client(uint8_t sock) 7 | : client_(sock) 8 | {} 9 | 10 | int yl_wifi_client::connect(const char *host, uint16_t port) 11 | { 12 | return client_.connect(host, port); 13 | } 14 | 15 | size_t yl_wifi_client::send(int n) 16 | { 17 | return client_.print(n); 18 | } 19 | 20 | size_t yl_wifi_client::send_ln(int n) 21 | { 22 | return client_.println(n); 23 | } 24 | 25 | size_t yl_wifi_client::send(const String &s) 26 | { 27 | if (s.length() == 0) 28 | { 29 | return 0; 30 | } 31 | String temp(s); 32 | char *c = &temp[0]; 33 | return client_.write((uint8_t*)c, temp.length()); 34 | } 35 | 36 | int yl_wifi_client::available() 37 | { 38 | return client_.available(); 39 | } 40 | 41 | int yl_wifi_client::recv() 42 | { 43 | return client_.read(); 44 | } 45 | 46 | int yl_wifi_client::recv(uint8_t *buf, size_t size) 47 | { 48 | return client_.read(buf, size); 49 | } 50 | 51 | void yl_wifi_client::flush() 52 | { 53 | client_.flush(); 54 | } 55 | 56 | void yl_wifi_client::stop() 57 | { 58 | client_.stop(); 59 | } -------------------------------------------------------------------------------- /yl_wifi_client.h: -------------------------------------------------------------------------------- 1 | #ifndef ARDUINO_YEELINKLIB_WIFICLLIENT_H 2 | #define ARDUINO_YEELINKLIB_WIFICLLIENT_H 3 | 4 | #include 5 | #include 6 | #include "yl_tcp_client.h" 7 | 8 | class yl_wifi_client : public yl_tcp_client 9 | { 10 | public: 11 | yl_wifi_client(); 12 | yl_wifi_client(uint8_t sock); 13 | 14 | virtual int connect(const char *host, uint16_t port); 15 | virtual size_t send(int); 16 | virtual size_t send_ln(int); 17 | virtual size_t send(const String &); 18 | virtual int available(); 19 | virtual int recv(); 20 | virtual int recv(uint8_t *buf, size_t size); 21 | virtual void flush(); 22 | virtual void stop(); 23 | 24 | private: 25 | WiFiClient client_; 26 | }; 27 | 28 | #endif --------------------------------------------------------------------------------