Skip to content

Instantly share code, notes, and snippets.

@adamrunner
Last active March 27, 2016 22:37
Show Gist options
  • Save adamrunner/05fd4af5da7728b9dfd7 to your computer and use it in GitHub Desktop.
Save adamrunner/05fd4af5da7728b9dfd7 to your computer and use it in GitHub Desktop.
A very simple data polling ruby script. It's intended to be ran on a Cron schedule where it will then poll the data from my ESP8266, and also the Forecast.IO API. It will then upload the data to data.sparkfun.com and also my very own ElasticSearch cluster.
require 'time'
require 'forecast_io'
require 'curb'
phant_public_key = '---'
phant_private_key = '---'
location = ['45.2912', '-122.4037']
#NOTE: This script is intended to be ran on a Cron schedule.
ForecastIO.configure do |configuration|
configuration.api_key = '---'
end
outside_temp = ForecastIO.forecast(*location)["currently"]["temperature"]
temperature_client = Curl::Easy.http_get('http://192.168.1.110/temp')
indoor_temp = temperature_client.body
payload = {outside_temp: outside_temp, indoor_temp: indoor_temp}.to_json
c = Curl::Easy.http_post("http://data.sparkfun.com/input/#{phant_public_key}", payload) do |curl|
curl.headers['Accept'] = 'application/json'
curl.headers['Content-Type'] = 'application/json'
curl.headers['Phant-Private-Key'] = phant_private_key
end
#NOTE: if using ElasticSearch, uncomment this.
# elastic_payload = {outside_temp: outside_temp, indoor_temp: indoor_temp, timestamp: DateTime.now.iso8601}.to_json
# elastic = Curl::Easy.http_post("http://es.adamrunner.com:9200/temperature/data", elastic_payload) do |curl|
# curl.headers['Accept'] = 'application/json'
# curl.headers['Content-Type'] = 'application/json'
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment