Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- from influxdb import InfluxDBClient
- from datetime import datetime
- import calendar
- import samsara
- from samsara.apis import SamsaraClient
- access_token = "MX9e5tDJMnmNyadjuU1ueRIDZXG0xZ"
- group_id = 5423
- sensor_id = 212014918091278
- def get_sensors_history():
- # Create an instance of the SamsaraClient.
- client = SamsaraClient()
- # Get a sensor's temperature history at hour-intervals for the past two hours.
- now = datetime.utcnow()
- end_ms = calendar.timegm(now.utctimetuple()) * 1000
- #Use every 10 seconds as datapoint
- step_ms = 10000
- start_ms = end_ms - (3600000)
- series = [{"widgetId": sensor_id, "field": "currentLoop1Mapped"}, {"widgetId": sensor_id, "field": "currentLoop2Mapped"}]
- fill_missing = "withNull"
- params = samsara.HistoryParam(group_id, start_ms, end_ms, step_ms, series, fill_missing)
- history = client.get_sensors_history(access_token, params)
- for result in history.results:
- #[0]: Temperature
- #[1]: CO2
- influxWrite("temperature", result.series[0], result.time_ms)
- influxWrite("co2", result.series[1], result.time_ms)
- print result
- #def get_sensor_live(access_token, group_id, sensor_id):
- if __name__ == "__main__":
- get_sensors_history()
- #get_sensor_live()
- def influxWrite(measurement, value, time):
- """Instantiate a connection to the InfluxDB."""
- host='192.168.166.36'
- port=8086
- user = 'root'
- password = '!@#$QWERasdf'
- dbname = 'samsara'
- dbuser = 'bogie'
- dbuser_password = '!@#$QWERasdf'
- json_body = [
- {
- "measurement": measurement,
- "time": time,
- "fields": {
- "value": value
- }
- }
- ]
- client = InfluxDBClient(host, port, user, password, dbname)
- print("Write points: {0}".format(json_body))
- client.write_points(json_body)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement