]> Zhao Yanbai Git Server - acecode.git/commitdiff
add RPi Sensor.py
authorroot <zhaoyanbai@126.com>
Thu, 30 Oct 2014 15:37:22 +0000 (15:37 +0000)
committerroot <zhaoyanbai@126.com>
Thu, 30 Oct 2014 15:37:22 +0000 (15:37 +0000)
tools/python/Sensors.py [new file with mode: 0755]

diff --git a/tools/python/Sensors.py b/tools/python/Sensors.py
new file mode 100755 (executable)
index 0000000..af8a4e5
--- /dev/null
@@ -0,0 +1,65 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# ------------------------------------------------------------------------
+#   File Name: Sensors.py
+#      Author: Zhao Yanbai
+#              Thu Oct 30 14:46:58 2014
+# Description: none
+# ------------------------------------------------------------------------
+
+import time
+import serial
+import os
+import commands
+import config
+from utils import * 
+
+log = CreateLogger(config.ACE_GLOBAL_LOG_PATH)
+
+s = serial.Serial('/dev/ttyACM0', 9600)
+
+ApiKey = 'c2ac9bff44b29c8f3edca022ed621aa9'
+class Sensor :
+    def __init__(self, Name, url) :
+        self.Name   = Name
+        self.Url    = url
+        self.LastTs = 0
+        self.Value  = 0
+
+Map = { }
+
+SensorList = [Sensor('Light', 'http://api.yeelink.net/v1.0/device/15328/sensor/26264/datapoints'),
+              Sensor('Temperature', 'http://api.yeelink.net/v1.0/device/15328/sensor/26256/datapoints')]
+
+for x in SensorList :
+    Map[x.Name] = x
+
+while True :
+    line = s.readline().strip()
+
+    if line[0] != '>' :
+        continue
+
+    log.info(line)
+
+    (name, value) = line[1:].strip().split(': ')
+    if Map.get(name, None) == None :
+        continue
+
+    if int(time.time()) - Map[name].LastTs < 10 :
+        continue
+
+    log.info('Prepare to Report')
+
+    Map[name].LastTs = int(time.time())
+    Map[name].Value  = float(value)
+
+    json = '{"value":%f}' % Map[name].Value
+    fd = open('datafile.txt', 'w+')
+    fd.write(json + '\n')
+    fd.close()
+
+    cmd = 'curl --request POST --data-binary @datafile.txt --header "U-ApiKey: '+ApiKey+'" ' + Map[name].Url
+    output = commands.getoutput(cmd)
+    log.info(output)
+