Water Temp API



Basic Info

This water temperature api is Restful and returns the temperature of the water, temperature history and lots other fun data! The base url is http://water.connorcode.com/api/ and the current api endpoints are: /temp, /temp/:time, /stats, /history. The data is returned in JSON format and is cached for 2 minutes to keep pressure on the sensor low.


API Endpoints

GET /temp

This endpoint returns the current temperature in fahrenheit and if the data is cached. A response may look like the following { "temp": 79.475, "cached": false }.

GET /temp/:time

This endpoint returns the temperature at a point in time in fahrenheit and if the response was cached. The :time parameter is a unix timestamp in seconds EX: 1625959170. A response may look like the following { "temp": 79.475, "cached": false }. If there is no data for the time requested, the temp will be Null.

GET /stats

This endpoint returns some stats about the data. Again the data is in fahrenheit and is cached. The date in from this endpoint can all be calculated using the /history endpoint but this is more efficient. The response may look like the following: {"length":23532,"mean":73.9687449291441785,"first":1625959169,"last":1626273206,"rate":60,"min":75.3,"max": 80.1,"cached":false}

GET /history

This endpoint returns all of the saved temperature data. The data is logged every minute. The time is in Unix Epoch time and the data is in fahrenheit. The data is returned in a JSON object like the following: { "temp": {1625959169: 80.123, ...}, "cached": false }.


Examples

These are some examples of the api in action. There are in Python because it is easy to understand but this same logic will apply to any language. Run the examples on Repl.it here.

Get the current lake temperature

import requests

# Get data from the API
data = requests.get('https://water.connorcode.com/api/temp')

# Basic error handling
if data.status_code != 200:
    print(f'Error: {data.json()["error"]}')
    exit(1)

# Get the temperature from the data
temp = data.json()['temp']

# Print the data
print(f'Current Temp: {temp}°F')

Get temperature at a point in time

import requests
from datetime import datetime

# Define timestamp
time = 1625959170

# Get data from the API
data = requests.get(f'https://water.connorcode.com/api/temp/{time}')

# Basic error handling
if data.status_code != 200:
    print(f'Error: {data.json()["error"]}')
    exit(1)

# Get the temperature section of the data
temp = data.json()['temp']

# If there is no temperature data for the time, exit
if temp is None:
    print('Error: No temperature data available')
    exit(1)

# Print the data
print(f'Temperature at {datetime.fromtimestamp(time)} is {temp}°F')

Get data stats

import requests

# Get data from the API
data = requests.get('https://water.connorcode.com/api/stats')

# Basic error handling
if data.status_code != 200:
    print(f'Error: {data.json()["error"]}')
    exit(1)

# Get the temperature from the data
data = data.json()

# Print some information from the data
print(f'Data Points: {data["length"]}')
print(f'Average Temp: {data["mean"]}°F')
print(f'First Datapoint: {data["first"]}')
print(f'Last Datapoint: {data["last"]}')
print(f'Last Datapoint: {data["min"]}')
print(f'Last Datapoint: {data["max"]}')
print(f'Date Rate: {data["rate"]}/h')

Get the temperature history

import requests

# Get data from the API
data = requests.get('https://water.connorcode.com/api/history')

# Basic error handling
if data.status_code != 200:
    print(f'Error: {data.json()["error"]}')
    exit(1)

# Get the temperature section of the data
data = data.json()['temp']

# Loop over the data and sum the temperatures
temp = 0
for i in data:
    temp += data[i]

# Get the average temperature
avg = temp / len(data)

# Print the data
print(f'Average Temp: {avg}°F')

Putting it all together!

Some more advanced examples that do more interesting things!

Graph the temperature history

import matplotlib.pyplot as plt
from datetime import datetime
import requests

# Get data from the API
print('Getting data...')
data = requests.get('https://water.connorcode.com/api/history')

# Basic error handling
if data.status_code != 200:
    print(f'Error: {data.json()["error"]}')
    exit(1)

# Get the temperature section of the data
print('Parsing data...')
temp = data.json()['temp']

x = []
y = []

for i in temp:
    x.append(datetime.fromtimestamp(int(i)))
    y.append(float(temp[i]))

print('Plotting...')
plt.figure()
plt.fill_between(x, y)
plt.plot(x, y)
plt.title('Lake Temp')
    
plt.show()