Reading Temperature values from API

73 views
Skip to first unread message

Steve Midgley

unread,
Apr 24, 2023, 7:43:44 PM4/24/23
to Wireless Sensor Tags
Hi,

I got some help from Support (thanks!) on this, and I wanted to share the result. I was building a Tasker app to obtain temperature readings from an outdoor Wireless tag senor. This is now all working. I wrote the prototype code in Ruby to get everything working (b/c writing new code in Tasker is insanity). I thought I'd share this Ruby code in case anyone else is trying to figure out how to do low level API access. For me the tricky part is that you call one api endpoint to trigger a new reading, and then another endpoint to obtain the resulting reading (so you want to wait for the first call to return before calling the second).

(I'll paste it to the end of this message also to make search and preservation better).

I hope this helps someone else! 
Steve

Ruby code (should work "out of the box" - no gem installations required):

# First follow instructions here: https://wirelesstag.net/eth/oauth2_apps.html to obtain Oauth2 credentials/keys
# You should now have an access_token (and a refresh_token). Use the access_token value in var: wtag_api_key below
# The code below pulls temp from the first tag in the tag list. If you have more than one sensor, you may need to adjust this

require 'net/http'
require 'json'
require 'uri'

def request_immediate_postback(wtag_api_key, tag_id)
  url = "https://wirelesstag.net/ethClient.asmx/RequestImmediatePostback"
  uri = URI(url)
  headers = {
    "Content-Type" => "application/json; charset=utf-8",
    "Authorization" => "Bearer #{wtag_api_key}"
  }
  data = {
    "id" => tag_id
  }
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request = Net::HTTP::Post.new(uri, headers)
  request.body = data.to_json
  response = http.request(request)

  if response.code != "200"
    raise "Error: #{response.code} #{response.body}"
  end
end

def get_updated_temperature(wtag_api_key, tag_id)
  url = "https://wirelesstag.net/ethClient.asmx/GetTagList"
  uri = URI(url)
  headers = {
    "Content-Type" => "application/json; charset=utf-8",
    "Authorization" => "Bearer #{wtag_api_key}"
  }
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request = Net::HTTP::Post.new(uri, headers)
  response = http.request(request)

  if response.code == "200"
    tag_list = JSON.parse(response.body)
    temperature = tag_list['d'][0]['temperature']
    ambient_temperature = tag_list['d'][0]['cap']
    {temperature: temperature, ambient_temperature: ambient_temperature}
  else
    raise "Error: #{response.code} #{response.body}"
  end
end

def get_immediate_temperature(wtag_api_key, tag_id)
  request_immediate_postback(wtag_api_key, tag_id)
  sleep(5) # Wait for the postback to be processed
  retval = get_updated_temperature(wtag_api_key, tag_id)
  return retval
end

def get_tag_names_and_uuids(wtag_api_key)
  url = "https://wirelesstag.net/ethClient.asmx/GetTagList"
  uri = URI(url)
  headers = {
    "Content-Type" => "application/json; charset=utf-8",
    "Authorization" => "Bearer #{wtag_api_key}"
  }
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request = Net::HTTP::Post.new(uri, headers)
  response = http.request(request)

  if response.code == "200"
    tag_list = JSON.parse(response.body)
    puts response.body
    puts tag_list
    return ""
  else
    raise "Error: #{response.code} #{response.body}"
  end
end

wtag_api_key = "[your oauth access_token here]"
tag_id = "0" # hardcoded value for my (only) sensor - adjust as needed

retval = get_immediate_temperature(wtag_api_key, tag_id)
temperature = retval[:temperature]
ambient_temperature = retval[:ambient_temperature]
puts "Current temperature: #{temperature}"
puts "Current ambient temperature: #{ambient_temperature}"


Reply all
Reply to author
Forward
0 new messages