[Original] Linux upload datapoint data to Yeelink "Golang version"

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
Package main/* Create by sndnvaps<sndnvaps@gmail.com> * date:2015-04-05 * upload datapoint to yeelink.net * Orig In post by Sndnvaps * Hold on Https://github.com/sndnvaps/MyRasPi */import ("FMT" "Io/ioutil" "Net" "Time" "Os "StrConv") Func checkerror (err error) {if err! = Nil {fmt. fprintf (OS. Stderr, "Fatal Error:%s\n", err. Error ()) OS. Exit (1)}}func getcputemp () (t float32) {tf, err: = OS. Open ("/sys/class/thermal/thermal_zone0/temp") if err! = Nil {fmt. Printf ("Read Cpu file Error =%s\n", err.) Error ())} defer TF. Close () Data: = Make ([]byte,5) TF. Read (data) Data_temp: = string (data) Temp, _: = StrConv. Atoi (Data_temp) var tt float32 TT = float32 (temp) fmt.        Printf ("Cpu temp =%2.2f\n", tt/1000.00) return (tt/1000.00)}func main () {//connet the api.yeelink.net REMOTE_IP 42.96.164.52//port conn, err: = Net.   Dial ("TCP", "42.96.164.52:80")     Defer Conn. Close () checkerror (err) fmt. Printf ("Post Request \ n")//FMT. Printf ("Cpu temp =%2.2f\n", getcputemp ()) time. Sleep (time. Second) _, err = conn. Write ([]byte ("Post/v1.0/device/19374/sensor/33945/datapoints http/1.0\r\nhost:api.yeelink.net\r\naccept: */*\r\n ")///Here to modify your own device number and sensor if err! = Nil {fmt. Printf ("Sent requset ok\n")}/* checkerror (Err) result, err: = Ioutil. ReadAll (conn)//get the info checkerror (err) fmt. PRINTLN (string result) */FMT. Printf ("Send the api-keys\n") time. Sleep (time. Second)//Send the Api-key _, err = conn. Write ([]byte ("U-apikey:108968b03a7e9334b2aca7c45b199dee\r\ncontent-length:15\r\ncontent-type:application/json;   charset=utf-8\r\n "))//Please modify your own U-apikey
CheckError (Err) time. Sleep (time. Second)//send the ' \ r \ n ' _, err = conn. Write ([]byte ("\ r \ n")) CheckError (ERR)//send the value time. Sleep (time. Second) var value string value = Fmt. Sprintf ("{\" value\ ":%2.2f}\n\n", Getcputemp ())//_, err = conn. Write ([]byte ("{\" value\ ": 13.14}\r\n"))//var b []byte//b = str2byte (value)//FMT. Println ("b =", B) _, err = conn. Write ([]byte (value)) CheckError (Err) time. Sleep (time. Second) result, err: = Ioutil. ReadAll (conn) checkerror (err) fmt. PRINTLN (string result) os. Exit (1)}

You can use go run upload_cpu_temp.go directly

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.