Created
April 24, 2023 23:37
-
-
Save science/6e9b55938e981fb3681d5b039decc183 to your computer and use it in GitHub Desktop.
For WirelessTag temperature sensors: Force new temp reading on device and then obtain those temp values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment