Skip to content

Instantly share code, notes, and snippets.

@burisu
Last active Oct 28, 2015
Embed
What would you like to do?
Sensor for Ekylibre
#!/usr/bin/env ruby
require 'colored'
require 'rest-client'
require 'yaml'
require 'pathname'
sensors = [
{
name: "SNS01",
token: "Y7OQ54T64m3DJtFG",
},
{
name: "SNS02",
token: "NDhohqCHJ6ffA7wg"
},
{
name: "SNS03",
token: "LP2HK8ZVtcMJfz2J"
}
]
last_data_file = Pathname.new("last-data.yml")
loop do
sensor = sensors.sample
puts sensor[:name]
name = sensor[:name]
last_data = last_data_file.exist? ? YAML.load_file(last_data_file) : {}
last_data = {} unless last_data.is_a?(Hash)
last_data[name] ||= {}
items = last_data[name]
items[:temperature] ||= 25
items[:temperature] += 2 * (rand - 0.5)
items[:net_mass] ||= 45
items[:net_mass] += 1 * (rand - 0.5)
items[:vlat] ||= 0
items[:vlat] += (0.1 * items[:vlat] + 0.0001) * (rand - 0.5)
items[:vlon] ||= 0
items[:vlon] += (0.1 * items[:vlon] + 0.0001) * (rand - 0.5)
items[:lat] ||= 44.79565 + 0.005 * (rand - 0.5)
items[:lat] += items[:vlat]
items[:lon] ||= -0.62289 + 0.005 * (rand - 0.5)
items[:lon] += items[:vlon]
body = {token: sensor[:token], items: { temperature: "#{items[:temperature]}celsius", net_mass: "#{items[:net_mass]}kg" }, latlon: [items[:lat], items[:lon]]}
puts body.to_json.green
RestClient.post 'http://test.ekylibre.lan:8080/iot/v1/a', body.to_json, content_type: :json
# Or more simply: RestClient.post 'http://test.ekylibre.lan:8080/iot/v1/a', body
File.write(last_data_file, last_data.to_yaml)
duration = (2.8 * rand).round(3)
puts "Sleep #{duration.to_s.yellow}s..."
sleep(duration)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment