[Original] Linux upload datapoint data to Yeelink "Golang version" Upload 2 data points simultaneously

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
/* Create by sndnvaps<sndnvaps@gmail.com>
* data:2015-04-12
* Upload 2 datapoint to yeelink.net at the same time
* Post by Sndnvaps
* Hold on Https://github.com/sndnvaps/MyRasPi
*/
Upload_client.go

Import ("FMT" "Io/ioutil" "Net" " Time" "OS" "StrConv") func checkerror (err error) {ifErr! =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") ifErr! =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)varTT float32 TT=float32 (temp) fmt. Printf ("Cpu temp =%2.2f\n", tt/1000.00) return(tt/1000.00)}varC Chanintfunc yeelink_upload (device_id, sensor_idstring, Val float32) { //connet the Api.yeelink.net//remote_ip 42.96.164.52//PORTConn, 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) Post:= Fmt. Sprintf ("post/v1.0/device/%s/sensor/%s/datapoints http/1.0\r\nhost:api.yeelink.net\r\naccept: */*\r\n", device_id,sensor_id); _, Err= Conn. Write ([]byte(POST)); ifErr! =Nil {fmt. Printf ("Sent requset ok\n") }

FMT. Printf ("Send the api-keys\n") time. Sleep (time. Second)//Send the Api-key_, Err = conn. Write ([]byte("u-apikey:108968b03a7e9334b2aca7c45bxxxxxx\r\ncontent-length:15\r\ncontent-type:application/json;charset= utf-8\r\n"))
//fill in your own api-key herecheckerror (Err) time. Sleep (time. Second)//send the ' \ r \ n '_, Err = conn. Write ([]byte("\ r \ n") ) checkerror (err)//Send the valueTime . Sleep (time. Second)varValuestringvalue= Fmt. Sprintf ("{\ "value\":%2.2f}\n\n", Val)//data for xx.xx//_, Err = conn. Write ([]byte ("{\" value\ ": 13.14}\r\n"))_, Err= Conn. Write ([]byte(value)) CheckError (Err) time. Sleep (time. Second) result, err:=Ioutil. ReadAll (conn) checkerror (err) fmt. Println (string(Result)) C<-1 //OS. Exit (1)}func Main () {device_id:="XXXXX" //Device IDSENSOR_ID: ="XXXXX" //Sensor IDSensor_id_2: ="XXXXX" //sensor Id_2Val: =getcputemp ()//Val Here is only for testing, if you have other parameters, you can modify it yourself, Val type is float32, length requires 5 bits, i.e. xx.xx, 33.33, with 2 decimal places C= Make (chanint) Go yeelink_upload (device_id,sensor_id,val)// //Time . Sleep (time. Second *)Go Yeelink_upload (Device_id,sensor_id_2, Val)//return<-C<-C}

Achieve simultaneous uploading of two datapoint data to Yeelink.net

The principle is to use Golang Chanel

Operating effects such as:

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.